2001-09-28 07:09:38 +00:00
|
|
|
/*
|
2002-10-22 14:53:18 +00:00
|
|
|
expr.c
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
expression construction and manipulations
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2001/06/15
|
2001-09-28 07:09:38 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
|
|
|
static __attribute__ ((unused)) const char rcsid[] =
|
|
|
|
"$Id$";
|
|
|
|
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2001-06-15 19:38:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2002-05-09 20:12:28 +00:00
|
|
|
#include <QF/dstring.h>
|
2001-06-20 21:18:04 +00:00
|
|
|
#include <QF/mathlib.h>
|
2001-10-24 22:50:06 +00:00
|
|
|
#include <QF/sys.h>
|
2001-06-26 07:21:20 +00:00
|
|
|
#include <QF/va.h>
|
2001-06-20 21:18:04 +00:00
|
|
|
|
2001-06-15 19:38:43 +00:00
|
|
|
#include "qfcc.h"
|
2002-05-10 00:00:23 +00:00
|
|
|
#include "class.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "def.h"
|
2002-07-17 18:49:58 +00:00
|
|
|
#include "emit.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "expr.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "function.h"
|
|
|
|
#include "idstuff.h"
|
|
|
|
#include "immediate.h"
|
2002-05-08 23:12:49 +00:00
|
|
|
#include "method.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "options.h"
|
2002-07-17 18:49:58 +00:00
|
|
|
#include "reloc.h"
|
2002-07-05 20:02:10 +00:00
|
|
|
#include "strpool.h"
|
2001-12-08 08:19:48 +00:00
|
|
|
#include "struct.h"
|
2001-12-12 08:39:47 +00:00
|
|
|
#include "type.h"
|
2001-06-20 21:18:04 +00:00
|
|
|
#include "qc-parse.h"
|
|
|
|
|
2002-06-05 21:13:29 +00:00
|
|
|
static expr_t *free_exprs;
|
2001-12-06 19:49:40 +00:00
|
|
|
int lineno_base;
|
|
|
|
|
|
|
|
etype_t qc_types[] = {
|
2002-05-01 22:08:59 +00:00
|
|
|
ev_void, // ex_error
|
2001-12-06 19:49:40 +00:00
|
|
|
ev_void, // ex_label
|
2003-10-22 08:05:17 +00:00
|
|
|
ev_void, // ex_bool
|
2001-12-06 19:49:40 +00:00
|
|
|
ev_void, // ex_block
|
|
|
|
ev_void, // ex_expr
|
|
|
|
ev_void, // ex_uexpr
|
|
|
|
ev_void, // ex_def
|
|
|
|
ev_void, // ex_temp
|
|
|
|
ev_void, // ex_nil
|
2001-12-08 08:19:48 +00:00
|
|
|
ev_void, // ex_name
|
2001-12-06 19:49:40 +00:00
|
|
|
|
|
|
|
ev_string, // ex_string
|
|
|
|
ev_float, // ex_float
|
|
|
|
ev_vector, // ex_vector
|
|
|
|
ev_entity, // ex_entity
|
|
|
|
ev_field, // ex_field
|
|
|
|
ev_func, // ex_func
|
|
|
|
ev_pointer, // ex_pointer
|
|
|
|
ev_quaternion, // ex_quaternion
|
|
|
|
ev_integer, // ex_integer
|
|
|
|
ev_uinteger, // ex_uinteger
|
2001-12-07 20:10:30 +00:00
|
|
|
ev_short, // ex_short
|
2001-06-20 07:02:36 +00:00
|
|
|
};
|
|
|
|
|
2003-08-22 19:48:14 +00:00
|
|
|
type_t *ev_types[] = {
|
2001-06-20 07:02:36 +00:00
|
|
|
&type_void,
|
|
|
|
&type_string,
|
|
|
|
&type_float,
|
|
|
|
&type_vector,
|
|
|
|
&type_entity,
|
|
|
|
&type_field,
|
|
|
|
&type_function,
|
|
|
|
&type_pointer,
|
2001-07-18 06:37:14 +00:00
|
|
|
&type_quaternion,
|
2001-07-22 06:59:12 +00:00
|
|
|
&type_integer,
|
2001-11-13 08:58:54 +00:00
|
|
|
&type_uinteger,
|
2001-12-07 20:10:30 +00:00
|
|
|
&type_short,
|
2002-05-21 21:28:40 +00:00
|
|
|
&type_void, // FIXME what type?
|
|
|
|
&type_void, // FIXME what type?
|
|
|
|
&type_void, // FIXME what type?
|
|
|
|
&type_SEL,
|
2002-06-12 22:37:18 +00:00
|
|
|
&type_void, // FIXME what type?
|
2001-06-20 07:02:36 +00:00
|
|
|
};
|
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_type expr_types[] = {
|
|
|
|
ex_nil, // ev_void
|
|
|
|
ex_string, // ev_string
|
|
|
|
ex_float, // ev_float
|
|
|
|
ex_vector, // ev_vector
|
|
|
|
ex_entity, // ev_entity
|
|
|
|
ex_field, // ev_field
|
|
|
|
ex_func, // ev_func
|
|
|
|
ex_pointer, // ev_pointer
|
|
|
|
ex_quaternion, // ev_quaternion
|
|
|
|
ex_integer, // ev_integer
|
|
|
|
ex_uinteger, // ev_uinteger
|
2001-12-07 20:10:30 +00:00
|
|
|
ex_short, // ev_short
|
2002-05-17 17:37:44 +00:00
|
|
|
ex_nil, // ev_struct
|
|
|
|
ex_nil, // ev_object
|
|
|
|
ex_nil, // ev_class
|
|
|
|
ex_nil, // ev_sel
|
2002-06-12 22:37:18 +00:00
|
|
|
ex_nil, // ev_array
|
2001-06-26 07:21:20 +00:00
|
|
|
};
|
|
|
|
|
2001-12-08 08:19:48 +00:00
|
|
|
void
|
|
|
|
convert_name (expr_t *e)
|
|
|
|
{
|
|
|
|
if (e->type == ex_name) {
|
2002-01-21 19:03:29 +00:00
|
|
|
const char *name = e->e.string_val;
|
2002-05-10 00:00:23 +00:00
|
|
|
def_t *d;
|
2002-05-17 19:35:26 +00:00
|
|
|
expr_t *new;
|
2002-05-10 00:00:23 +00:00
|
|
|
class_t *class;
|
2002-01-21 19:03:29 +00:00
|
|
|
|
2002-05-10 00:00:23 +00:00
|
|
|
class = get_class (name, 0);
|
|
|
|
if (class) {
|
|
|
|
e->type = ex_def;
|
2002-07-03 20:16:32 +00:00
|
|
|
e->e.def = class_pointer_def (class);
|
2002-05-10 00:00:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2002-06-28 17:59:32 +00:00
|
|
|
d = get_def (NULL, name, current_scope, st_none);
|
2002-01-21 19:03:29 +00:00
|
|
|
if (d) {
|
2002-06-10 20:54:22 +00:00
|
|
|
if (d->global) {
|
2002-05-17 19:35:26 +00:00
|
|
|
new = class_ivar_expr (current_class, name);
|
|
|
|
if (new)
|
|
|
|
goto convert;
|
|
|
|
}
|
2002-01-21 19:03:29 +00:00
|
|
|
e->type = ex_def;
|
|
|
|
e->e.def = d;
|
|
|
|
return;
|
|
|
|
}
|
2002-05-17 19:35:26 +00:00
|
|
|
new = class_ivar_expr (current_class, name);
|
|
|
|
if (new)
|
|
|
|
goto convert;
|
|
|
|
new = get_enum (name);
|
|
|
|
if (new)
|
|
|
|
goto convert;
|
2002-01-21 19:03:29 +00:00
|
|
|
error (e, "Undeclared variable \"%s\".", name);
|
2002-05-17 19:35:26 +00:00
|
|
|
return;
|
|
|
|
convert:
|
|
|
|
e->type = new->type;
|
|
|
|
e->e = new->e;
|
2001-12-08 08:19:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-10-02 18:55:52 +00:00
|
|
|
type_t *
|
2001-06-20 07:02:36 +00:00
|
|
|
get_type (expr_t *e)
|
|
|
|
{
|
2001-12-08 08:19:48 +00:00
|
|
|
convert_name (e);
|
2001-06-20 07:02:36 +00:00
|
|
|
switch (e->type) {
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-12-08 08:19:48 +00:00
|
|
|
case ex_name:
|
2002-05-01 22:08:59 +00:00
|
|
|
case ex_error:
|
2001-12-06 19:49:40 +00:00
|
|
|
return 0; // something went very wrong
|
2003-10-22 08:05:17 +00:00
|
|
|
case ex_bool:
|
|
|
|
if (options.code.progsversion == PROG_ID_VERSION)
|
|
|
|
return &type_float;
|
|
|
|
return &type_integer;
|
2001-10-17 07:45:37 +00:00
|
|
|
case ex_nil:
|
|
|
|
return &type_void;
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_block:
|
|
|
|
if (e->e.block.result)
|
|
|
|
return get_type (e->e.block.result);
|
2001-10-02 18:55:52 +00:00
|
|
|
return &type_void;
|
2001-06-20 07:02:36 +00:00
|
|
|
case ex_expr:
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_uexpr:
|
2001-10-02 18:55:52 +00:00
|
|
|
return e->e.expr.type;
|
2001-06-20 07:02:36 +00:00
|
|
|
case ex_def:
|
2001-10-02 18:55:52 +00:00
|
|
|
return e->e.def->type;
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_temp:
|
2001-10-02 18:55:52 +00:00
|
|
|
return e->e.temp.type;
|
2001-12-12 08:39:47 +00:00
|
|
|
case ex_pointer:
|
2002-05-09 20:12:28 +00:00
|
|
|
return pointer_type (e->e.pointer.type);
|
2001-08-07 16:50:22 +00:00
|
|
|
case ex_integer:
|
2001-10-26 06:43:56 +00:00
|
|
|
if (options.code.progsversion == PROG_ID_VERSION) {
|
2001-08-07 16:50:22 +00:00
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.integer_val;
|
|
|
|
}
|
|
|
|
// fall through
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_string:
|
|
|
|
case ex_float:
|
|
|
|
case ex_vector:
|
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_quaternion:
|
2001-11-13 08:58:54 +00:00
|
|
|
case ex_uinteger:
|
2001-12-07 20:10:30 +00:00
|
|
|
case ex_short:
|
2003-08-22 19:48:14 +00:00
|
|
|
return ev_types[qc_types[e->type]];
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
2001-10-02 18:55:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
etype_t
|
|
|
|
extract_type (expr_t *e)
|
|
|
|
{
|
|
|
|
type_t *type = get_type (e);
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2001-10-02 18:55:52 +00:00
|
|
|
if (type)
|
|
|
|
return type->type;
|
2001-07-24 19:51:44 +00:00
|
|
|
return ev_type_count;
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
|
|
|
|
2001-07-23 01:31:22 +00:00
|
|
|
const char *
|
|
|
|
get_op_string (int op)
|
2001-07-09 18:17:04 +00:00
|
|
|
{
|
|
|
|
switch (op) {
|
2001-12-12 08:39:47 +00:00
|
|
|
case PAS: return ".=";
|
2001-07-26 05:08:27 +00:00
|
|
|
case OR: return "||";
|
|
|
|
case AND: return "&&";
|
|
|
|
case EQ: return "==";
|
|
|
|
case NE: return "!=";
|
|
|
|
case LE: return "<=";
|
|
|
|
case GE: return ">=";
|
|
|
|
case LT: return "<";
|
|
|
|
case GT: return ">";
|
|
|
|
case '=': return "=";
|
|
|
|
case '+': return "+";
|
|
|
|
case '-': return "-";
|
|
|
|
case '*': return "*";
|
|
|
|
case '/': return "/";
|
2001-10-25 17:48:35 +00:00
|
|
|
case '%': return "%";
|
2001-07-26 05:08:27 +00:00
|
|
|
case '&': return "&";
|
|
|
|
case '|': return "|";
|
2001-08-09 16:34:46 +00:00
|
|
|
case '^': return "^";
|
|
|
|
case '~': return "~";
|
2001-07-26 05:08:27 +00:00
|
|
|
case '!': return "!";
|
2001-08-10 16:17:00 +00:00
|
|
|
case SHL: return "<<";
|
|
|
|
case SHR: return ">>";
|
2001-07-26 05:08:27 +00:00
|
|
|
case '.': return ".";
|
|
|
|
case 'i': return "<if>";
|
|
|
|
case 'n': return "<ifnot>";
|
pr_comp.h:
o add ev_uniteger to the types enum
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
progs.h:
o add uinteger accessors
pr_exec.c:
o implement ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
pr_opcode.c:
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
expr.h:
o prototype inc_users
qfcc.h:
o add externs for op_ifbe, op_ifb, op_ifae and op_ifa
emit.c:
o don't bother emiting an assignment to a temp def that's only used once
(ie, it's never read, only written to)
o support the new if* instructions
expr.c:
o support the new if* insructions
o dectect expression loops in append_expr
o support unsigned integers
o re-work temp def usage counting
pr_def.c
o debugging for temp def usage counts
pr_opcode.c:
o support the new if* instructions
qc-parse.y:
o provide defines for IFBE IFB IFAE IFA
switch.c:
o do binary searches for strings, floats and ints if there are more than
8 cases in a switch. Strings need more testing.
2001-11-09 00:58:16 +00:00
|
|
|
case IFBE: return "<ifbe>";
|
|
|
|
case IFB: return "<ifb>";
|
|
|
|
case IFAE: return "<ifae>";
|
|
|
|
case IFA: return "<ifa>";
|
2001-07-26 05:08:27 +00:00
|
|
|
case 'g': return "<goto>";
|
|
|
|
case 'r': return "<return>";
|
2001-11-05 19:11:03 +00:00
|
|
|
case 'b': return "<bind>";
|
|
|
|
case 's': return "<state>";
|
|
|
|
case 'c': return "<call>";
|
2002-08-20 06:11:10 +00:00
|
|
|
case 'C': return "<cast>";
|
2002-10-20 02:35:12 +00:00
|
|
|
case 'M': return "<move>";
|
2001-12-06 19:49:40 +00:00
|
|
|
default:
|
|
|
|
return "unknown";
|
2001-07-09 18:17:04 +00:00
|
|
|
}
|
2001-07-23 01:31:22 +00:00
|
|
|
}
|
|
|
|
|
2004-01-25 08:55:03 +00:00
|
|
|
expr_t *
|
2001-07-23 01:31:22 +00:00
|
|
|
type_mismatch (expr_t *e1, expr_t *e2, int op)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
etype_t t1, t2;
|
2001-07-23 01:31:22 +00:00
|
|
|
|
2001-10-02 18:55:52 +00:00
|
|
|
t1 = extract_type (e1);
|
|
|
|
t2 = extract_type (e2);
|
2001-07-23 01:31:22 +00:00
|
|
|
|
2001-07-09 18:17:04 +00:00
|
|
|
return error (e1, "type mismatch: %s %s %s",
|
2001-12-12 17:15:09 +00:00
|
|
|
pr_type_name[t1], get_op_string (op), pr_type_name[t2]);
|
2001-07-09 18:17:04 +00:00
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2001-10-19 05:16:24 +00:00
|
|
|
check_initialized (expr_t *e)
|
|
|
|
{
|
2001-10-26 06:43:56 +00:00
|
|
|
if (options.warnings.uninited_variable) {
|
2001-10-19 05:16:24 +00:00
|
|
|
if (e->type == ex_def
|
2002-06-09 05:19:13 +00:00
|
|
|
&& !(e->e.def->type->type == ev_func
|
2002-06-10 20:54:22 +00:00
|
|
|
&& e->e.def->global)
|
2003-08-20 19:58:41 +00:00
|
|
|
&& !(e->e.def->type->type == ev_struct)
|
2003-08-18 06:11:41 +00:00
|
|
|
&& !e->e.def->external
|
2001-10-19 05:16:24 +00:00
|
|
|
&& !e->e.def->initialized) {
|
|
|
|
warning (e, "%s may be used uninitialized", e->e.def->name);
|
2001-12-06 19:49:40 +00:00
|
|
|
e->e.def->initialized = 1; // only warn once
|
2001-10-19 05:16:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-08-22 21:55:01 +00:00
|
|
|
void
|
|
|
|
inc_users (expr_t *e)
|
|
|
|
{
|
|
|
|
if (e && e->type == ex_temp)
|
|
|
|
e->e.temp.users++;
|
|
|
|
else if (e && e->type == ex_block)
|
|
|
|
inc_users (e->e.block.result);
|
|
|
|
}
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
expr_t *
|
2001-06-25 20:52:04 +00:00
|
|
|
new_expr (void)
|
2001-06-20 21:18:04 +00:00
|
|
|
{
|
2002-06-05 21:13:29 +00:00
|
|
|
expr_t *e;
|
|
|
|
|
|
|
|
ALLOC (16384, expr_t, exprs, e);
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2002-07-03 21:32:03 +00:00
|
|
|
e->line = pr.source_line;
|
|
|
|
e->file = pr.source_file;
|
2001-06-26 16:23:21 +00:00
|
|
|
return e;
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
2001-11-13 08:58:54 +00:00
|
|
|
const char *
|
|
|
|
new_label_name (void)
|
2001-06-25 20:52:04 +00:00
|
|
|
{
|
2001-08-22 21:55:01 +00:00
|
|
|
static int label = 0;
|
|
|
|
int lnum = ++label;
|
2001-06-25 22:11:20 +00:00
|
|
|
const char *fname = current_func->def->name;
|
2002-06-01 05:06:19 +00:00
|
|
|
char *lname;
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2002-06-13 16:39:33 +00:00
|
|
|
lname = nva ("$%s_%d", fname, lnum);
|
2002-05-14 06:37:28 +00:00
|
|
|
SYS_CHECKMEM (lname);
|
2001-11-13 08:58:54 +00:00
|
|
|
return lname;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static expr_t *
|
2002-06-28 16:38:05 +00:00
|
|
|
new_error_expr (void)
|
2002-05-16 20:20:23 +00:00
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_error;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2001-11-13 08:58:54 +00:00
|
|
|
expr_t *
|
|
|
|
new_label_expr (void)
|
|
|
|
{
|
2001-06-25 20:52:04 +00:00
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *l = new_expr ();
|
|
|
|
|
2001-06-25 22:11:20 +00:00
|
|
|
l->type = ex_label;
|
2001-11-13 08:58:54 +00:00
|
|
|
l->e.label.name = new_label_name ();
|
2002-06-07 17:29:30 +00:00
|
|
|
l->e.label.next = pr.labels;
|
|
|
|
pr.labels = &l->e.label;
|
2001-06-25 20:52:04 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
expr_t *
|
|
|
|
new_bool_expr (ex_list_t *true_list, ex_list_t *false_list, expr_t *e)
|
|
|
|
{
|
|
|
|
expr_t *b = new_expr ();
|
|
|
|
|
|
|
|
b->type = ex_bool;
|
|
|
|
b->e.bool.true_list = true_list;
|
|
|
|
b->e.bool.false_list = false_list;
|
|
|
|
b->e.bool.e = e;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2001-06-26 03:33:01 +00:00
|
|
|
expr_t *
|
|
|
|
new_block_expr (void)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *b = new_expr ();
|
2001-06-26 03:33:01 +00:00
|
|
|
|
|
|
|
b->type = ex_block;
|
|
|
|
b->e.block.head = 0;
|
|
|
|
b->e.block.tail = &b->e.block.head;
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2001-06-26 02:46:02 +00:00
|
|
|
expr_t *
|
|
|
|
new_binary_expr (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *e = new_expr ();
|
2001-06-26 02:46:02 +00:00
|
|
|
|
2002-06-28 16:38:05 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
if (e2 && e2->type == ex_error)
|
|
|
|
return e2;
|
|
|
|
|
2001-08-22 21:55:01 +00:00
|
|
|
inc_users (e1);
|
|
|
|
inc_users (e2);
|
|
|
|
|
2001-06-26 02:46:02 +00:00
|
|
|
e->type = ex_expr;
|
|
|
|
e->e.expr.op = op;
|
|
|
|
e->e.expr.e1 = e1;
|
|
|
|
e->e.expr.e2 = e2;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_unary_expr (int op, expr_t *e1)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *e = new_expr ();
|
2001-06-26 02:46:02 +00:00
|
|
|
|
2002-06-28 16:38:05 +00:00
|
|
|
if (e1 && e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
|
2001-08-22 21:55:01 +00:00
|
|
|
inc_users (e1);
|
|
|
|
|
2001-06-26 02:46:02 +00:00
|
|
|
e->type = ex_uexpr;
|
|
|
|
e->e.expr.op = op;
|
|
|
|
e->e.expr.e1 = e1;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2002-09-11 16:21:26 +00:00
|
|
|
expr_t *
|
|
|
|
new_def_expr (def_t *def)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_def;
|
|
|
|
e->e.def = def;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2001-08-11 21:15:24 +00:00
|
|
|
expr_t *
|
|
|
|
new_temp_def_expr (type_t *type)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *e = new_expr ();
|
2001-08-11 21:15:24 +00:00
|
|
|
|
|
|
|
e->type = ex_temp;
|
|
|
|
e->e.temp.type = type;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2001-11-13 18:49:27 +00:00
|
|
|
expr_t *
|
2002-09-11 16:21:26 +00:00
|
|
|
new_nil_expr (void)
|
2001-11-13 18:49:27 +00:00
|
|
|
{
|
2002-09-11 16:21:26 +00:00
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_nil;
|
2001-11-13 18:49:27 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2002-01-21 19:03:29 +00:00
|
|
|
expr_t *
|
|
|
|
new_name_expr (const char *name)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_name;
|
|
|
|
e->e.string_val = name;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2002-05-09 20:12:28 +00:00
|
|
|
expr_t *
|
2002-09-11 16:21:26 +00:00
|
|
|
new_string_expr (const char *string_val)
|
2002-05-09 20:12:28 +00:00
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
2002-09-11 16:21:26 +00:00
|
|
|
e->type = ex_string;
|
|
|
|
e->e.string_val = string_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_float_expr (float float_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = float_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_vector_expr (float *vector_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_vector;
|
|
|
|
memcpy (e->e.vector_val, vector_val, sizeof (e->e.vector_val));
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_entity_expr (int entity_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_entity;
|
|
|
|
e->e.entity_val = entity_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_field_expr (int field_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_field;
|
|
|
|
e->e.field_val = field_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_func_expr (int func_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_func;
|
|
|
|
e->e.func_val = func_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
//expr_t *new_pointer_expr ();
|
|
|
|
expr_t *
|
|
|
|
new_quaternion_expr (float *quaternion_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_quaternion;
|
|
|
|
memcpy (e->e.quaternion_val, quaternion_val, sizeof (e->e.quaternion_val));
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_integer_expr (int integer_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_integer;
|
|
|
|
e->e.integer_val = integer_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_uinteger_expr (unsigned int uinteger_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_uinteger;
|
|
|
|
e->e.uinteger_val = uinteger_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_short_expr (short short_val)
|
|
|
|
{
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
e->type = ex_short;
|
|
|
|
e->e.short_val = short_val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2004-01-25 08:55:03 +00:00
|
|
|
int
|
|
|
|
is_constant (expr_t *e)
|
|
|
|
{
|
|
|
|
if (e->type >= ex_string
|
|
|
|
|| (e->type == ex_def && e->e.def->constant))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-02-28 04:54:07 +00:00
|
|
|
expr_t *
|
|
|
|
constant_expr (expr_t *var)
|
|
|
|
{
|
2004-01-25 08:55:03 +00:00
|
|
|
if (var->type != ex_def || !var->e.def->constant)
|
|
|
|
return var;
|
2003-02-28 04:54:07 +00:00
|
|
|
switch (var->e.def->type->type) {
|
|
|
|
case ev_string:
|
|
|
|
return new_string_expr (G_GETSTR (var->e.def->ofs));
|
|
|
|
case ev_float:
|
|
|
|
return new_float_expr (G_FLOAT (var->e.def->ofs));
|
|
|
|
case ev_vector:
|
|
|
|
return new_vector_expr (G_VECTOR (var->e.def->ofs));
|
|
|
|
case ev_field:
|
|
|
|
return new_field_expr (G_var (integer, var->e.def->ofs));
|
|
|
|
case ev_integer:
|
|
|
|
return new_integer_expr (G_INT (var->e.def->ofs));
|
|
|
|
case ev_uinteger:
|
|
|
|
return new_uinteger_expr (G_INT (var->e.def->ofs));
|
|
|
|
default:
|
2004-01-25 08:55:03 +00:00
|
|
|
return var;
|
2003-02-28 04:54:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-11 16:21:26 +00:00
|
|
|
expr_t *
|
|
|
|
new_bind_expr (expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
expr_t *e;
|
|
|
|
|
|
|
|
if (!e2 || e2->type != ex_temp) {
|
|
|
|
error (e1, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
e = new_expr ();
|
|
|
|
e->type = ex_expr;
|
|
|
|
e->e.expr.op = 'b';
|
|
|
|
e->e.expr.e1 = e1;
|
|
|
|
e->e.expr.e2 = e2;
|
2002-10-08 18:20:09 +00:00
|
|
|
e->e.expr.type = get_type (e2);
|
2002-05-09 20:12:28 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2002-05-18 00:30:14 +00:00
|
|
|
expr_t *
|
|
|
|
new_self_expr (void)
|
|
|
|
{
|
2002-07-14 05:40:38 +00:00
|
|
|
def_t *def = get_def (&type_entity, ".self", pr.scope, st_extern);
|
2002-05-18 00:30:14 +00:00
|
|
|
|
2002-06-09 04:30:02 +00:00
|
|
|
def_initialized (def);
|
2002-05-18 00:30:14 +00:00
|
|
|
return new_def_expr (def);
|
|
|
|
}
|
|
|
|
|
2002-05-31 16:58:42 +00:00
|
|
|
expr_t *
|
|
|
|
new_this_expr (void)
|
|
|
|
{
|
|
|
|
type_t *type = field_type (&type_id);
|
2002-07-14 05:40:38 +00:00
|
|
|
def_t *def = get_def (type, ".this", pr.scope, st_extern);
|
2002-05-31 16:58:42 +00:00
|
|
|
|
2002-06-09 04:30:02 +00:00
|
|
|
def_initialized (def);
|
2003-04-25 17:00:22 +00:00
|
|
|
def->nosave = 1;
|
2002-05-31 16:58:42 +00:00
|
|
|
return new_def_expr (def);
|
|
|
|
}
|
|
|
|
|
2003-04-22 15:29:32 +00:00
|
|
|
static expr_t *
|
|
|
|
param_expr (const char *name, type_t *type)
|
|
|
|
{
|
2003-04-23 06:17:36 +00:00
|
|
|
def_t *def = get_def (&type_param, name, pr.scope, st_extern);
|
2003-04-22 15:29:32 +00:00
|
|
|
expr_t *def_expr;
|
|
|
|
|
|
|
|
def_initialized (def);
|
2003-04-25 17:00:22 +00:00
|
|
|
def->nosave = 1;
|
2003-04-22 15:29:32 +00:00
|
|
|
def_expr = new_def_expr (def);
|
|
|
|
return unary_expr ('.', address_expr (def_expr, 0, type));
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_ret_expr (type_t *type)
|
|
|
|
{
|
|
|
|
return param_expr (".return", type);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
new_param_expr (type_t *type, int num)
|
|
|
|
{
|
|
|
|
return param_expr (va (".param_%d", num), type);
|
|
|
|
}
|
|
|
|
|
2002-10-20 02:35:12 +00:00
|
|
|
expr_t *
|
|
|
|
new_move_expr (expr_t *e1, expr_t *e2, type_t *type)
|
|
|
|
{
|
|
|
|
expr_t *e = new_binary_expr ('M', e1, e2);
|
|
|
|
e->e.expr.type = type;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2001-06-26 03:33:01 +00:00
|
|
|
expr_t *
|
|
|
|
append_expr (expr_t *block, expr_t *e)
|
|
|
|
{
|
|
|
|
if (block->type != ex_block)
|
|
|
|
abort ();
|
|
|
|
|
2002-06-28 16:38:05 +00:00
|
|
|
if (!e || e->type == ex_error)
|
2001-06-26 03:33:01 +00:00
|
|
|
return block;
|
|
|
|
|
pr_comp.h:
o add ev_uniteger to the types enum
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
progs.h:
o add uinteger accessors
pr_exec.c:
o implement ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
pr_opcode.c:
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
expr.h:
o prototype inc_users
qfcc.h:
o add externs for op_ifbe, op_ifb, op_ifae and op_ifa
emit.c:
o don't bother emiting an assignment to a temp def that's only used once
(ie, it's never read, only written to)
o support the new if* instructions
expr.c:
o support the new if* insructions
o dectect expression loops in append_expr
o support unsigned integers
o re-work temp def usage counting
pr_def.c
o debugging for temp def usage counts
pr_opcode.c:
o support the new if* instructions
qc-parse.y:
o provide defines for IFBE IFB IFAE IFA
switch.c:
o do binary searches for strings, floats and ints if there are more than
8 cases in a switch. Strings need more testing.
2001-11-09 00:58:16 +00:00
|
|
|
if (e->next) {
|
|
|
|
error (e, "append_expr: expr loop detected");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2001-06-26 03:33:01 +00:00
|
|
|
*block->e.block.tail = e;
|
|
|
|
block->e.block.tail = &e->next;
|
|
|
|
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
|
2001-06-25 17:15:56 +00:00
|
|
|
void
|
|
|
|
print_expr (expr_t *e)
|
|
|
|
{
|
|
|
|
printf (" ");
|
2001-06-25 20:52:04 +00:00
|
|
|
if (!e) {
|
|
|
|
printf ("(nil)");
|
|
|
|
return;
|
|
|
|
}
|
2001-06-25 17:15:56 +00:00
|
|
|
switch (e->type) {
|
2002-05-01 22:08:59 +00:00
|
|
|
case ex_error:
|
|
|
|
printf ("(error)");
|
|
|
|
break;
|
2003-10-22 08:05:17 +00:00
|
|
|
case ex_bool:
|
|
|
|
printf ("bool"); //FIXME
|
|
|
|
break;
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-06-26 03:33:01 +00:00
|
|
|
printf ("%s", e->e.label.name);
|
|
|
|
break;
|
|
|
|
case ex_block:
|
2001-08-11 21:15:24 +00:00
|
|
|
if (e->e.block.result) {
|
|
|
|
print_expr (e->e.block.result);
|
|
|
|
printf ("=");
|
|
|
|
}
|
2001-06-26 03:33:01 +00:00
|
|
|
printf ("{\n");
|
|
|
|
for (e = e->e.block.head; e; e = e->next) {
|
|
|
|
print_expr (e);
|
2001-12-06 19:49:40 +00:00
|
|
|
puts ("");
|
2001-06-26 03:33:01 +00:00
|
|
|
}
|
|
|
|
printf ("}");
|
2001-06-25 22:11:20 +00:00
|
|
|
break;
|
2001-06-25 17:15:56 +00:00
|
|
|
case ex_expr:
|
|
|
|
print_expr (e->e.expr.e1);
|
|
|
|
if (e->e.expr.op == 'c') {
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *p = e->e.expr.e2;
|
|
|
|
|
2001-06-25 17:15:56 +00:00
|
|
|
printf ("(");
|
|
|
|
while (p) {
|
|
|
|
print_expr (p);
|
|
|
|
if (p->next)
|
|
|
|
printf (",");
|
|
|
|
p = p->next;
|
|
|
|
}
|
|
|
|
printf (")");
|
2001-08-24 21:14:04 +00:00
|
|
|
} else if (e->e.expr.op == 'b') {
|
2002-10-09 19:30:43 +00:00
|
|
|
printf (" <-->");
|
2001-08-24 21:14:04 +00:00
|
|
|
print_expr (e->e.expr.e2);
|
2001-06-25 17:15:56 +00:00
|
|
|
} else {
|
|
|
|
print_expr (e->e.expr.e2);
|
2001-07-23 06:15:33 +00:00
|
|
|
printf (" %s", get_op_string (e->e.expr.op));
|
2001-06-25 17:15:56 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ex_uexpr:
|
|
|
|
print_expr (e->e.expr.e1);
|
2001-07-23 06:15:33 +00:00
|
|
|
printf (" u%s", get_op_string (e->e.expr.op));
|
2001-06-25 17:15:56 +00:00
|
|
|
break;
|
|
|
|
case ex_def:
|
2001-08-22 21:55:01 +00:00
|
|
|
if (e->e.def->name)
|
|
|
|
printf ("%s", e->e.def->name);
|
2002-06-10 20:54:22 +00:00
|
|
|
if (!e->e.def->global) {
|
2001-08-22 21:55:01 +00:00
|
|
|
printf ("<%d>", e->e.def->ofs);
|
|
|
|
} else {
|
|
|
|
printf ("[%d]", e->e.def->ofs);
|
|
|
|
}
|
2001-06-25 17:15:56 +00:00
|
|
|
break;
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_temp:
|
2001-08-22 21:55:01 +00:00
|
|
|
printf ("(");
|
2001-08-11 21:15:24 +00:00
|
|
|
print_expr (e->e.temp.expr);
|
2001-08-22 21:55:01 +00:00
|
|
|
printf (":");
|
|
|
|
if (e->e.temp.def) {
|
|
|
|
if (e->e.temp.def->name) {
|
2001-12-06 19:49:40 +00:00
|
|
|
printf ("%s", e->e.temp.def->name);
|
2001-08-22 21:55:01 +00:00
|
|
|
} else {
|
2001-12-06 19:49:40 +00:00
|
|
|
printf ("<%d>", e->e.temp.def->ofs);
|
2001-08-22 21:55:01 +00:00
|
|
|
}
|
|
|
|
} else {
|
2001-12-06 19:49:40 +00:00
|
|
|
printf ("<>");
|
2001-08-22 21:55:01 +00:00
|
|
|
}
|
2001-12-12 17:15:09 +00:00
|
|
|
printf (":%s:%d)@", pr_type_name[e->e.temp.type->type],
|
2001-08-22 23:21:47 +00:00
|
|
|
e->e.temp.users);
|
2001-08-11 21:15:24 +00:00
|
|
|
break;
|
2001-10-17 07:45:37 +00:00
|
|
|
case ex_nil:
|
|
|
|
printf ("NULL");
|
|
|
|
break;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_string:
|
2001-12-08 08:19:48 +00:00
|
|
|
case ex_name:
|
2001-07-18 06:37:14 +00:00
|
|
|
printf ("\"%s\"", e->e.string_val);
|
2001-06-25 17:15:56 +00:00
|
|
|
break;
|
|
|
|
case ex_float:
|
|
|
|
printf ("%g", e->e.float_val);
|
|
|
|
break;
|
|
|
|
case ex_vector:
|
|
|
|
printf ("'%g", e->e.vector_val[0]);
|
2001-07-26 05:08:27 +00:00
|
|
|
printf (" %g", e->e.vector_val[1]);
|
|
|
|
printf (" %g'", e->e.vector_val[2]);
|
2001-06-25 17:15:56 +00:00
|
|
|
break;
|
|
|
|
case ex_quaternion:
|
|
|
|
printf ("'%g", e->e.quaternion_val[0]);
|
|
|
|
printf (" %g", e->e.quaternion_val[1]);
|
|
|
|
printf (" %g", e->e.quaternion_val[2]);
|
|
|
|
printf (" %g'", e->e.quaternion_val[3]);
|
|
|
|
break;
|
2001-12-12 08:39:47 +00:00
|
|
|
case ex_pointer:
|
2001-12-12 17:15:09 +00:00
|
|
|
printf ("(%s)[%d]", pr_type_name[e->e.pointer.type->type],
|
2001-12-12 08:39:47 +00:00
|
|
|
e->e.pointer.val);
|
|
|
|
break;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
2001-07-22 06:59:12 +00:00
|
|
|
case ex_integer:
|
2001-07-23 01:31:22 +00:00
|
|
|
printf ("%d", e->e.integer_val);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
2001-11-13 16:08:50 +00:00
|
|
|
case ex_uinteger:
|
|
|
|
printf ("%d", e->e.uinteger_val);
|
|
|
|
break;
|
2001-12-07 20:10:30 +00:00
|
|
|
case ex_short:
|
|
|
|
printf ("%d", e->e.short_val);
|
|
|
|
break;
|
2001-06-25 17:15:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
static expr_t *
|
|
|
|
field_expr (expr_t *e1, expr_t *e2)
|
|
|
|
{
|
2001-12-12 08:39:47 +00:00
|
|
|
type_t *t1, *t2;
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *e;
|
2002-10-09 19:30:43 +00:00
|
|
|
def_t *d;
|
|
|
|
int i;
|
2001-12-08 08:19:48 +00:00
|
|
|
struct_field_t *field;
|
2002-05-17 19:35:26 +00:00
|
|
|
class_t *class;
|
2003-07-30 04:11:45 +00:00
|
|
|
struct_t *strct;
|
2001-06-20 21:18:04 +00:00
|
|
|
|
2002-06-28 16:38:05 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
2001-12-12 08:39:47 +00:00
|
|
|
t1 = get_type (e1);
|
|
|
|
switch (t1->type) {
|
|
|
|
case ev_struct:
|
2003-05-15 05:58:31 +00:00
|
|
|
case ev_class:
|
2003-03-12 21:58:24 +00:00
|
|
|
check_initialized (e1);
|
2001-12-12 08:39:47 +00:00
|
|
|
if (e2->type != ex_name)
|
|
|
|
return error (e2, "structure field name expected");
|
2003-07-30 04:11:45 +00:00
|
|
|
if (t1->type == ev_struct)
|
|
|
|
strct = t1->s.strct;
|
|
|
|
else
|
|
|
|
strct = t1->s.class->ivars;
|
|
|
|
field = struct_find_field (strct, e2->e.string_val);
|
2001-12-08 17:36:58 +00:00
|
|
|
if (!field)
|
|
|
|
return error (e2, "structure has no field %s",
|
2001-12-12 08:39:47 +00:00
|
|
|
e2->e.string_val);
|
2001-12-08 17:36:58 +00:00
|
|
|
e2->type = ex_short;
|
|
|
|
e2->e.short_val = field->offset;
|
2001-12-12 08:39:47 +00:00
|
|
|
e = unary_expr ('.', address_expr (e1, e2, field->type));
|
2001-12-08 17:36:58 +00:00
|
|
|
return e;
|
2001-12-12 08:39:47 +00:00
|
|
|
case ev_pointer:
|
2003-03-12 21:58:24 +00:00
|
|
|
check_initialized (e1);
|
2002-05-17 06:20:27 +00:00
|
|
|
switch (t1->aux_type->type) {
|
|
|
|
case ev_struct:
|
|
|
|
if (e2->type == ex_name) {
|
2003-07-30 04:11:45 +00:00
|
|
|
field = struct_find_field (t1->aux_type->s.strct,
|
2002-05-17 06:20:27 +00:00
|
|
|
e2->e.string_val);
|
|
|
|
if (!field)
|
|
|
|
return error (e2, "structure has no field %s",
|
|
|
|
e2->e.string_val);
|
|
|
|
e2->type = ex_short;
|
|
|
|
e2->e.short_val = field->offset;
|
|
|
|
t1 = pointer_type (field->type);
|
|
|
|
}
|
|
|
|
break;
|
2002-05-17 17:37:44 +00:00
|
|
|
case ev_object:
|
2002-05-17 06:20:27 +00:00
|
|
|
case ev_class:
|
|
|
|
if (e2->type == ex_name) {
|
2002-11-14 18:17:43 +00:00
|
|
|
int protected;
|
|
|
|
|
2003-07-30 04:11:45 +00:00
|
|
|
class = t1->aux_type->s.class;
|
2002-11-14 18:17:43 +00:00
|
|
|
protected = class_access (current_class, class);
|
|
|
|
field = class_find_ivar (class, protected,
|
2002-05-17 06:20:27 +00:00
|
|
|
e2->e.string_val);
|
|
|
|
if (!field)
|
|
|
|
return new_error_expr ();
|
|
|
|
e2->type = ex_short;
|
|
|
|
e2->e.short_val = field->offset;
|
|
|
|
t1 = pointer_type (field->type);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
if (e1->type == ex_pointer) {
|
|
|
|
if (e2->type == ex_short) {
|
|
|
|
e1->e.pointer.val += e2->e.short_val;
|
|
|
|
} else if (e2->type == ex_integer) {
|
|
|
|
e1->e.pointer.val += e2->e.integer_val;
|
|
|
|
} else if (e2->type == ex_uinteger) {
|
|
|
|
e1->e.pointer.val += e2->e.uinteger_val;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
e1->e.pointer.type = t1->aux_type;
|
|
|
|
return unary_expr ('.', e1);
|
|
|
|
} else {
|
|
|
|
e = new_binary_expr ('&', e1, e2);
|
|
|
|
e->e.expr.type = t1;
|
|
|
|
return unary_expr ('.', e);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ev_entity:
|
2003-03-12 21:58:24 +00:00
|
|
|
check_initialized (e1);
|
2003-07-23 18:28:31 +00:00
|
|
|
if (e2->type == ex_name) {
|
|
|
|
def_t *d = field_def (e2->e.string_val);
|
|
|
|
|
|
|
|
if (!d) {
|
|
|
|
t2 = get_type (e2);
|
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
e2 = new_def_expr (d);
|
|
|
|
t2 = get_type (e2);
|
2001-12-12 08:39:47 +00:00
|
|
|
e = new_binary_expr ('.', e1, e2);
|
|
|
|
e->e.expr.type = t2->aux_type;
|
|
|
|
return e;
|
2003-07-23 18:28:31 +00:00
|
|
|
} else {
|
|
|
|
t2 = get_type (e2);
|
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
|
|
|
if (t2->type == ev_field) {
|
|
|
|
e = new_binary_expr ('.', e1, e2);
|
|
|
|
e->e.expr.type = t2->aux_type;
|
|
|
|
return e;
|
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
break;
|
2002-10-09 19:30:43 +00:00
|
|
|
case ev_vector:
|
|
|
|
if (!options.traditional && e2->type == ex_name) {
|
|
|
|
field = struct_find_field (vector_struct, e2->e.string_val);
|
|
|
|
if (!field)
|
|
|
|
return error (e2, "vector has no field %s",
|
|
|
|
e2->e.string_val);
|
|
|
|
switch (e1->type) {
|
|
|
|
case ex_expr:
|
|
|
|
if (e1->e.expr.op == '.'
|
|
|
|
&& extract_type (e1->e.expr.e1) == ev_entity
|
|
|
|
&& e1->e.expr.e2->type == ex_def) {
|
|
|
|
d = e1->e.expr.e2->e.def->def_next;
|
|
|
|
for (i = field->offset; i; i--)
|
|
|
|
d = d->def_next;
|
|
|
|
e = new_def_expr (d);
|
|
|
|
e = new_binary_expr ('.', e1->e.expr.e1, e);
|
|
|
|
e->e.expr.type = field->type;
|
|
|
|
return e;
|
|
|
|
}
|
2002-10-09 20:35:25 +00:00
|
|
|
break;
|
2002-10-09 19:30:43 +00:00
|
|
|
case ex_uexpr:
|
2002-10-09 20:35:25 +00:00
|
|
|
if (e1->e.expr.op == '.') {
|
|
|
|
if (e1->e.expr.e1->type == ex_pointer) {
|
|
|
|
e = new_expr ();
|
|
|
|
e->type = ex_pointer;
|
|
|
|
e1 = e1->e.expr.e1;
|
2003-08-21 02:21:30 +00:00
|
|
|
i = e1->e.pointer.val;
|
2002-10-09 20:35:25 +00:00
|
|
|
e->e.pointer.val = i + field->offset;
|
|
|
|
e->e.pointer.type = field->type;
|
|
|
|
return unary_expr ('.', e);
|
2003-03-12 21:58:24 +00:00
|
|
|
} else if (extract_type (e1->e.expr.e1)
|
|
|
|
== ev_pointer) {
|
|
|
|
e = new_integer_expr (field->offset);
|
|
|
|
e = address_expr (e1, e, field->type);
|
|
|
|
return unary_expr ('.', e);
|
2002-10-09 20:35:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ex_block:
|
|
|
|
print_expr (e1); puts ("");
|
2002-10-09 19:30:43 +00:00
|
|
|
#if 0
|
|
|
|
e1 = new_bind_expr (e1, new_temp_def_expr (t1));
|
|
|
|
e2 = new_short_expr (field->offset);
|
|
|
|
e = address_expr (e1, e2, field->type);
|
|
|
|
e = unary_expr ('.', e);
|
|
|
|
return e;
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
case ex_def:
|
|
|
|
d = e1->e.def->def_next;
|
|
|
|
for (i = field->offset; i; i--)
|
|
|
|
d = d->def_next;
|
|
|
|
e = new_def_expr (d);
|
|
|
|
return e;
|
|
|
|
case ex_vector:
|
|
|
|
e = new_float_expr (e1->e.vector_val[field->offset]);
|
|
|
|
return e;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2001-12-12 08:39:47 +00:00
|
|
|
default:
|
|
|
|
break;
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
return type_mismatch (e1, e2, '.');
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
|
2001-07-24 22:30:31 +00:00
|
|
|
expr_t *
|
2001-07-18 06:37:14 +00:00
|
|
|
test_expr (expr_t *e, int test)
|
|
|
|
{
|
2002-09-11 16:21:26 +00:00
|
|
|
static float zero[4] = {0, 0, 0, 0};
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *new = 0;
|
2002-06-12 22:37:18 +00:00
|
|
|
etype_t type;
|
2001-07-18 06:37:14 +00:00
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
|
|
|
|
2001-10-19 05:16:24 +00:00
|
|
|
check_initialized (e);
|
|
|
|
|
2001-07-18 06:37:14 +00:00
|
|
|
if (!test)
|
|
|
|
return unary_expr ('!', e);
|
|
|
|
|
2002-06-28 16:38:05 +00:00
|
|
|
type = extract_type (e);
|
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
|
|
|
switch (type) {
|
2001-07-18 06:37:14 +00:00
|
|
|
case ev_type_count:
|
|
|
|
error (e, "internal error");
|
|
|
|
abort ();
|
2001-07-24 19:51:44 +00:00
|
|
|
case ev_void:
|
2002-12-13 23:16:42 +00:00
|
|
|
if (options.traditional) {
|
|
|
|
warning (e, "void has no value");
|
|
|
|
return e;
|
|
|
|
}
|
2001-12-08 08:19:48 +00:00
|
|
|
return error (e, "void has no value");
|
2001-07-18 06:37:14 +00:00
|
|
|
case ev_string:
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_string_expr (0);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
pr_comp.h:
o add ev_uniteger to the types enum
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
progs.h:
o add uinteger accessors
pr_exec.c:
o implement ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
pr_opcode.c:
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
expr.h:
o prototype inc_users
qfcc.h:
o add externs for op_ifbe, op_ifb, op_ifae and op_ifa
emit.c:
o don't bother emiting an assignment to a temp def that's only used once
(ie, it's never read, only written to)
o support the new if* instructions
expr.c:
o support the new if* insructions
o dectect expression loops in append_expr
o support unsigned integers
o re-work temp def usage counting
pr_def.c
o debugging for temp def usage counts
pr_opcode.c:
o support the new if* instructions
qc-parse.y:
o provide defines for IFBE IFB IFAE IFA
switch.c:
o do binary searches for strings, floats and ints if there are more than
8 cases in a switch. Strings need more testing.
2001-11-09 00:58:16 +00:00
|
|
|
case ev_uinteger:
|
2001-07-23 01:31:22 +00:00
|
|
|
case ev_integer:
|
2001-12-07 20:10:30 +00:00
|
|
|
case ev_short:
|
2001-07-18 06:37:14 +00:00
|
|
|
return e;
|
2001-07-23 05:31:49 +00:00
|
|
|
case ev_float:
|
2001-10-26 06:43:56 +00:00
|
|
|
if (options.code.progsversion == PROG_ID_VERSION)
|
2001-10-06 04:48:52 +00:00
|
|
|
return e;
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_float_expr (0);
|
2001-07-23 05:31:49 +00:00
|
|
|
break;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ev_vector:
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_vector_expr (zero);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
|
|
|
case ev_entity:
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_entity_expr (0);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
|
|
|
case ev_field:
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_field_expr (0);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
|
|
|
case ev_func:
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_func_expr (0);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
|
|
|
case ev_pointer:
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_nil_expr ();
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
|
|
|
case ev_quaternion:
|
2002-09-11 16:21:26 +00:00
|
|
|
new = new_quaternion_expr (zero);
|
2001-07-18 06:37:14 +00:00
|
|
|
break;
|
2001-12-08 08:19:48 +00:00
|
|
|
case ev_struct:
|
2002-05-10 23:19:57 +00:00
|
|
|
case ev_object:
|
|
|
|
case ev_class:
|
|
|
|
case ev_sel:
|
2002-06-12 22:37:18 +00:00
|
|
|
case ev_array:
|
|
|
|
return error (e, "%s cannot be tested", pr_type_name[type]);
|
2001-07-18 06:37:14 +00:00
|
|
|
}
|
2001-07-24 22:30:31 +00:00
|
|
|
new->line = e->line;
|
|
|
|
new->file = e->file;
|
|
|
|
new = binary_expr (NE, e, new);
|
|
|
|
new->line = e->line;
|
|
|
|
new->file = e->file;
|
|
|
|
return new;
|
2001-07-18 06:37:14 +00:00
|
|
|
}
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
void
|
|
|
|
backpatch (ex_list_t *list, expr_t *label)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
expr_t *e;
|
|
|
|
|
2003-10-22 09:14:53 +00:00
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
for (i = 0; i < list->size; i++) {
|
|
|
|
e = list->e[i];
|
|
|
|
if (e->type == ex_uexpr && e->e.expr.op == 'g')
|
|
|
|
e->e.expr.e1 = label;
|
|
|
|
else if (e->type == ex_expr && (e->e.expr.op == 'i'
|
|
|
|
|| e->e.expr.op == 'n'))
|
|
|
|
e->e.expr.e2 = label;
|
|
|
|
else {
|
|
|
|
error (e, "internal compiler error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static ex_list_t *
|
|
|
|
merge (ex_list_t *l1, ex_list_t *l2)
|
|
|
|
{
|
|
|
|
ex_list_t *m;
|
|
|
|
|
2004-02-09 02:23:37 +00:00
|
|
|
if (!l1 && !l2) {
|
|
|
|
error (0, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (!l2)
|
|
|
|
return l1;
|
|
|
|
if (!l1)
|
|
|
|
return l2;
|
2003-10-22 08:05:17 +00:00
|
|
|
m = malloc ((size_t)&((ex_list_t *)0)->e[l1->size + l2->size]);
|
|
|
|
m->size = l1->size + l2->size;
|
|
|
|
memcpy (m->e, l1->e, l1->size * sizeof (expr_t *));
|
|
|
|
memcpy (m->e + l1->size, l2->e, l2->size * sizeof (expr_t *));
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
static ex_list_t *
|
|
|
|
make_list (expr_t *e)
|
|
|
|
{
|
|
|
|
ex_list_t *m;
|
|
|
|
|
|
|
|
m = malloc ((size_t)&((ex_list_t *) 0)->e[1]);
|
|
|
|
m->size = 1;
|
|
|
|
m->e[0] = e;
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
convert_bool (expr_t *e, int block)
|
|
|
|
{
|
|
|
|
expr_t *b;
|
|
|
|
|
2004-01-25 21:45:07 +00:00
|
|
|
if (e->type == ex_expr && (e->e.expr.op == '=' || e->e.expr.op == PAS)
|
|
|
|
&& !e->paren)
|
|
|
|
warning (e,
|
|
|
|
"suggest parentheses around assignment used as truth value");
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
if (e->type == ex_uexpr && e->e.expr.op == '!') {
|
|
|
|
e = convert_bool (e->e.expr.e1, 0);
|
2003-10-24 22:23:56 +00:00
|
|
|
if (e->type == ex_error)
|
2003-11-16 07:17:18 +00:00
|
|
|
return e;
|
2003-10-22 08:05:17 +00:00
|
|
|
e = unary_expr ('!', e);
|
|
|
|
}
|
|
|
|
if (e->type != ex_bool) {
|
|
|
|
e = test_expr (e, 1);
|
2003-10-24 22:23:56 +00:00
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
2003-10-22 08:05:17 +00:00
|
|
|
if (e->type == ex_integer) {
|
2003-11-16 07:17:18 +00:00
|
|
|
b = new_unary_expr ('g', 0);
|
2003-10-22 08:05:17 +00:00
|
|
|
if (e->e.integer_val)
|
2003-11-16 07:17:18 +00:00
|
|
|
e = new_bool_expr (make_list (b), 0, b);
|
2003-10-22 08:05:17 +00:00
|
|
|
else
|
2003-11-16 07:17:18 +00:00
|
|
|
e = new_bool_expr (0, make_list (b), b);
|
2003-10-22 08:05:17 +00:00
|
|
|
} else {
|
|
|
|
b = new_block_expr ();
|
|
|
|
append_expr (b, new_binary_expr ('i', e, 0));
|
|
|
|
append_expr (b, new_unary_expr ('g', 0));
|
|
|
|
e = new_bool_expr (make_list (b->e.block.head),
|
|
|
|
make_list (b->e.block.head->next), b);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (block && e->e.bool.e->type != ex_block) {
|
|
|
|
expr_t *block = new_block_expr ();
|
|
|
|
append_expr (block, e->e.bool.e);
|
|
|
|
e->e.bool.e = block;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2003-10-22 09:14:53 +00:00
|
|
|
static expr_t *
|
|
|
|
convert_from_bool (expr_t *e, type_t *type)
|
|
|
|
{
|
2003-10-22 16:49:57 +00:00
|
|
|
expr_t *zero;
|
|
|
|
expr_t *one;
|
|
|
|
expr_t *cond;
|
|
|
|
|
|
|
|
if (type == &type_float) {
|
|
|
|
one = new_float_expr (1);
|
|
|
|
zero = new_float_expr (0);
|
|
|
|
} else if (type == &type_integer) {
|
|
|
|
one = new_integer_expr (1);
|
|
|
|
zero = new_integer_expr (0);
|
|
|
|
} else if (type == &type_uinteger) {
|
|
|
|
one = new_uinteger_expr (1);
|
|
|
|
zero = new_uinteger_expr (0);
|
|
|
|
} else {
|
|
|
|
return error (e, "can't convert from bool value");
|
|
|
|
}
|
|
|
|
cond = new_expr ();
|
|
|
|
*cond = *e;
|
|
|
|
cond->next = 0;
|
|
|
|
|
|
|
|
cond = conditional_expr (cond, one, zero);
|
|
|
|
e->type = cond->type;
|
|
|
|
e->e = cond->e;
|
2003-10-22 09:14:53 +00:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
expr_t *
|
|
|
|
bool_expr (int op, expr_t *label, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
expr_t *block;
|
|
|
|
|
2003-10-22 08:27:38 +00:00
|
|
|
if (!options.code.short_circuit)
|
|
|
|
return binary_expr (op, e1, e2);
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
e1 = convert_bool (e1, 0);
|
2003-10-24 22:23:56 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
e2 = convert_bool (e2, 0);
|
2003-10-24 22:23:56 +00:00
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
2003-10-22 08:05:17 +00:00
|
|
|
|
|
|
|
block = new_block_expr ();
|
|
|
|
append_expr (block, e1);
|
|
|
|
append_expr (block, label);
|
|
|
|
append_expr (block, e2);
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case OR:
|
|
|
|
backpatch (e1->e.bool.false_list, label);
|
|
|
|
return new_bool_expr (merge (e1->e.bool.true_list,
|
|
|
|
e2->e.bool.true_list),
|
|
|
|
e2->e.bool.false_list, block);
|
|
|
|
break;
|
|
|
|
case AND:
|
|
|
|
backpatch (e1->e.bool.true_list, label);
|
|
|
|
return new_bool_expr (e2->e.bool.true_list,
|
|
|
|
merge (e1->e.bool.false_list,
|
|
|
|
e2->e.bool.false_list), block);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
error (e1, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2001-07-23 05:31:49 +00:00
|
|
|
void
|
|
|
|
convert_int (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.integer_val;
|
|
|
|
}
|
|
|
|
|
2003-08-01 21:20:04 +00:00
|
|
|
void
|
|
|
|
convert_uint (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.uinteger_val;
|
|
|
|
}
|
|
|
|
|
2004-01-25 08:55:03 +00:00
|
|
|
void
|
|
|
|
convert_short (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.short_val;
|
|
|
|
}
|
|
|
|
|
2003-08-01 21:20:04 +00:00
|
|
|
void
|
|
|
|
convert_uint_int (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_integer;
|
|
|
|
e->e.integer_val = e->e.uinteger_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
convert_int_uint (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_uinteger;
|
|
|
|
e->e.uinteger_val = e->e.integer_val;
|
|
|
|
}
|
|
|
|
|
2003-09-02 17:41:16 +00:00
|
|
|
void
|
|
|
|
convert_short_int (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_integer;
|
|
|
|
e->e.integer_val = e->e.short_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
convert_short_uint (expr_t *e)
|
|
|
|
{
|
|
|
|
e->type = ex_uinteger;
|
|
|
|
e->e.uinteger_val = e->e.short_val;
|
|
|
|
}
|
|
|
|
|
2003-01-06 18:28:13 +00:00
|
|
|
static void
|
2002-01-30 22:17:55 +00:00
|
|
|
convert_nil (expr_t *e, type_t *t)
|
|
|
|
{
|
|
|
|
e->type = expr_types[t->type];
|
|
|
|
if (e->type == ex_pointer)
|
|
|
|
e->e.pointer.type = &type_void;
|
|
|
|
}
|
|
|
|
|
2004-01-25 08:55:03 +00:00
|
|
|
int
|
2003-03-25 17:18:58 +00:00
|
|
|
is_compare (int op)
|
|
|
|
{
|
2003-08-05 17:48:16 +00:00
|
|
|
if (op == EQ || op == NE || op == LE || op == GE || op == LT || op == GT
|
|
|
|
|| op == '>' || op == '<')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-25 08:55:03 +00:00
|
|
|
int
|
2003-08-05 17:48:16 +00:00
|
|
|
is_logic (int op)
|
|
|
|
{
|
|
|
|
if (op == OR || op == AND)
|
2003-03-25 17:18:58 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-22 22:15:31 +00:00
|
|
|
static expr_t *
|
|
|
|
check_precedence (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
if (e1->type == ex_uexpr && e1->e.expr.op == '!' && !e1->paren) {
|
|
|
|
if (options.traditional) {
|
|
|
|
if (op != AND && op != OR && op != '=') {
|
|
|
|
notice (e1, "precedence of `!' and `%s' inverted for "
|
|
|
|
"traditional code", get_op_string (op));
|
|
|
|
e1->e.expr.e1->paren = 1;
|
|
|
|
return unary_expr ('!', binary_expr (op, e1->e.expr.e1, e2));
|
|
|
|
}
|
|
|
|
} else if (op == '&' || op == '|') {
|
|
|
|
warning (e1, "ambiguous logic. Suggest explicit parentheses with "
|
|
|
|
"expressions involving ! and %s", get_op_string (op));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.traditional) {
|
|
|
|
if (e2->type == ex_expr && !e2->paren) {
|
|
|
|
if (((op == '&' || op == '|')
|
|
|
|
&& (e2->e.expr.op == '*' || e2->e.expr.op == '/'
|
|
|
|
|| e2->e.expr.op == '+' || e2->e.expr.op == '-'
|
|
|
|
|| is_compare (e2->e.expr.op)))
|
|
|
|
|| (op == '='
|
|
|
|
&& (e2->e.expr.op == OR || e2->e.expr.op == AND))) {
|
|
|
|
notice (e1, "precedence of `%s' and `%s' inverted for "
|
|
|
|
"traditional code", get_op_string (op),
|
|
|
|
get_op_string (e2->e.expr.op));
|
|
|
|
e1 = binary_expr (op, e1, e2->e.expr.e1);
|
|
|
|
e1->paren = 1;
|
|
|
|
return binary_expr (e2->e.expr.op, e1, e2->e.expr.e2);
|
|
|
|
}
|
|
|
|
if (((op == EQ || op == NE) && is_compare (e2->e.expr.op))
|
|
|
|
|| (op == OR && e2->e.expr.op == AND)
|
|
|
|
|| (op == '|' && e2->e.expr.op == '&')) {
|
|
|
|
notice (e1, "precedence of `%s' raised to `%s' for "
|
|
|
|
"traditional code", get_op_string (op),
|
|
|
|
get_op_string (e2->e.expr.op));
|
|
|
|
e1 = binary_expr (op, e1, e2->e.expr.e1);
|
|
|
|
e1->paren = 1;
|
|
|
|
return binary_expr (e2->e.expr.op, e1, e2->e.expr.e2);
|
|
|
|
}
|
|
|
|
}
|
2004-01-22 01:04:48 +00:00
|
|
|
} else {
|
|
|
|
if (e2->type == ex_expr && !e2->paren) {
|
|
|
|
if ((op == '&' || op == '|' || op == '^')
|
|
|
|
&& is_compare (e2->e.expr.op)) {
|
|
|
|
warning (e2, "suggest parentheses around comparison in "
|
|
|
|
"operand of %c", op);
|
|
|
|
}
|
|
|
|
}
|
2003-09-22 22:15:31 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-01-31 04:11:45 +00:00
|
|
|
static int
|
|
|
|
has_function_call (expr_t *e)
|
|
|
|
{
|
|
|
|
switch (e->type) {
|
|
|
|
case ex_bool:
|
|
|
|
return has_function_call (e->e.bool.e);
|
|
|
|
case ex_block:
|
|
|
|
if (e->e.block.is_call)
|
|
|
|
return 1;
|
|
|
|
for (e = e->e.block.head; e; e = e->next)
|
|
|
|
if (has_function_call (e))
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
case ex_expr:
|
|
|
|
if (e->e.expr.op == 'c')
|
|
|
|
return 1;
|
|
|
|
return (has_function_call (e->e.expr.e1)
|
|
|
|
|| has_function_call (e->e.expr.e2));
|
|
|
|
case ex_uexpr:
|
|
|
|
if (e->e.expr.op != 'g')
|
|
|
|
return has_function_call (e->e.expr.e1);
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-20 07:02:36 +00:00
|
|
|
expr_t *
|
|
|
|
binary_expr (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
2001-10-17 07:45:37 +00:00
|
|
|
type_t *t1, *t2;
|
|
|
|
type_t *type = 0;
|
|
|
|
expr_t *e;
|
2001-06-20 07:02:36 +00:00
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
|
|
|
|
2001-12-08 08:19:48 +00:00
|
|
|
convert_name (e1);
|
2001-10-18 20:05:26 +00:00
|
|
|
|
2001-08-24 21:47:52 +00:00
|
|
|
if (e1->type == ex_block && e1->e.block.is_call
|
2004-01-31 04:11:45 +00:00
|
|
|
&& has_function_call (e2) && e1->e.block.result) {
|
2003-04-22 15:29:32 +00:00
|
|
|
e = new_temp_def_expr (get_type (e1->e.block.result));
|
2001-12-06 19:49:40 +00:00
|
|
|
inc_users (e); // for the block itself
|
2001-12-12 08:39:47 +00:00
|
|
|
e1 = assign_expr (e, e1);
|
2001-08-24 21:47:52 +00:00
|
|
|
}
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
if (op == '.')
|
|
|
|
return field_expr (e1, e2);
|
2003-03-12 21:58:24 +00:00
|
|
|
check_initialized (e1);
|
2001-06-20 21:18:04 +00:00
|
|
|
|
2001-12-08 08:19:48 +00:00
|
|
|
convert_name (e2);
|
|
|
|
check_initialized (e2);
|
|
|
|
|
2001-07-18 06:37:14 +00:00
|
|
|
if (op == OR || op == AND) {
|
|
|
|
e1 = test_expr (e1, true);
|
|
|
|
e2 = test_expr (e2, true);
|
|
|
|
}
|
|
|
|
|
2002-06-28 16:38:05 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
2004-01-25 08:55:03 +00:00
|
|
|
e1 = constant_expr (e1);
|
|
|
|
e2 = constant_expr (e2);
|
2001-10-17 07:45:37 +00:00
|
|
|
t1 = get_type (e1);
|
|
|
|
t2 = get_type (e2);
|
|
|
|
if (!t1 || !t2) {
|
2001-07-23 05:31:49 +00:00
|
|
|
error (e1, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
2002-01-30 22:17:55 +00:00
|
|
|
if (op == EQ || op == NE) {
|
|
|
|
if (e1->type == ex_nil) {
|
|
|
|
t1 = t2;
|
|
|
|
convert_nil (e1, t1);
|
|
|
|
} else if (e2->type == ex_nil) {
|
|
|
|
t2 = t1;
|
|
|
|
convert_nil (e2, t2);
|
|
|
|
}
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
if (e1->type == ex_bool)
|
2003-10-22 09:14:53 +00:00
|
|
|
e1 = convert_from_bool (e1, t2);
|
2003-10-22 08:05:17 +00:00
|
|
|
|
|
|
|
if (e2->type == ex_bool)
|
2003-10-22 09:14:53 +00:00
|
|
|
e2 = convert_from_bool (e2, t1);
|
2003-10-22 08:05:17 +00:00
|
|
|
|
2003-09-02 17:41:16 +00:00
|
|
|
if (e1->type == ex_short) {
|
|
|
|
if (t2 == &type_integer) {
|
|
|
|
convert_short_int (e1);
|
|
|
|
t1 = &type_integer;
|
|
|
|
} else if (t2 == &type_uinteger) {
|
|
|
|
convert_short_uint (e1);
|
|
|
|
t1 = &type_uinteger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e2->type == ex_short) {
|
|
|
|
if (t1 == &type_integer) {
|
|
|
|
convert_short_int (e2);
|
|
|
|
t2 = &type_integer;
|
|
|
|
} else if (t1 == &type_uinteger) {
|
|
|
|
convert_short_uint (e2);
|
|
|
|
t2 = &type_uinteger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-01 21:20:04 +00:00
|
|
|
if (e1->type == ex_integer) {
|
|
|
|
if (t2 == &type_float
|
|
|
|
|| t2 == &type_vector
|
|
|
|
|| t2 == &type_quaternion) {
|
|
|
|
convert_int (e1);
|
|
|
|
t1 = &type_float;
|
|
|
|
} else if (t2 == &type_uinteger) {
|
|
|
|
convert_int_uint (e1);
|
|
|
|
t1 = &type_uinteger;
|
|
|
|
}
|
|
|
|
} else if (e1->type == ex_uinteger) {
|
|
|
|
if (t2 == &type_float
|
2001-10-17 07:45:37 +00:00
|
|
|
|| t2 == &type_vector
|
2003-08-01 21:20:04 +00:00
|
|
|
|| t2 == &type_quaternion) {
|
|
|
|
convert_uint (e1);
|
|
|
|
t1 = &type_float;
|
|
|
|
} else if (t2 == &type_integer) {
|
|
|
|
convert_uint_int (e1);
|
|
|
|
t1 = &type_integer;
|
|
|
|
}
|
|
|
|
} else if (e2->type == ex_integer) {
|
|
|
|
if (t1 == &type_float
|
|
|
|
|| t1 == &type_vector
|
|
|
|
|| t1 == &type_quaternion) {
|
|
|
|
convert_int (e2);
|
|
|
|
t2 = &type_float;
|
|
|
|
} else if (t1 == &type_uinteger) {
|
|
|
|
convert_int_uint (e2);
|
|
|
|
t2 = &type_uinteger;
|
|
|
|
}
|
|
|
|
} else if (e2->type == ex_uinteger) {
|
|
|
|
if (t1 == &type_float
|
|
|
|
|| t1 == &type_vector
|
|
|
|
|| t1 == &type_quaternion) {
|
|
|
|
convert_uint (e2);
|
|
|
|
t2 = &type_float;
|
|
|
|
} else if (t1 == &type_integer) {
|
|
|
|
convert_uint_int (e2);
|
|
|
|
t2 = &type_integer;
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
}
|
|
|
|
|
2003-09-22 22:15:31 +00:00
|
|
|
if ((e = check_precedence (op, e1, e2)))
|
|
|
|
return e;
|
2001-06-28 15:20:31 +00:00
|
|
|
|
2001-07-23 05:31:49 +00:00
|
|
|
if (t1 != t2) {
|
2001-10-17 07:45:37 +00:00
|
|
|
switch (t1->type) {
|
2001-06-20 21:18:04 +00:00
|
|
|
case ev_float:
|
2004-01-25 08:55:03 +00:00
|
|
|
if (t2 == &type_vector || t2 == &type_quaternion) {
|
2001-07-23 05:31:49 +00:00
|
|
|
type = &type_vector;
|
2001-06-20 21:18:04 +00:00
|
|
|
} else {
|
2004-01-25 08:55:03 +00:00
|
|
|
type = &type_float;
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
break;
|
2001-06-20 21:18:04 +00:00
|
|
|
case ev_vector:
|
2004-01-25 08:55:03 +00:00
|
|
|
if (t2 == &type_quaternion) {
|
|
|
|
type = &type_quaternion;
|
2001-06-20 21:18:04 +00:00
|
|
|
} else {
|
2004-01-25 08:55:03 +00:00
|
|
|
type = &type_vector;
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
break;
|
2001-06-20 21:18:04 +00:00
|
|
|
case ev_field:
|
2001-10-17 07:45:37 +00:00
|
|
|
if (t1->aux_type == t2) {
|
|
|
|
type = t1->aux_type;
|
|
|
|
} else {
|
|
|
|
goto type_mismatch;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ev_func:
|
|
|
|
if (e1->type == ex_func && !e1->e.func_val) {
|
|
|
|
type = t2;
|
|
|
|
} else if (e2->type == ex_func && !e2->e.func_val) {
|
|
|
|
type = t1;
|
2001-06-20 21:18:04 +00:00
|
|
|
} else {
|
|
|
|
goto type_mismatch;
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
break;
|
2002-07-18 05:16:12 +00:00
|
|
|
case ev_pointer:
|
|
|
|
if (!type_assignable (t1, t2) && !type_assignable (t2, t1))
|
|
|
|
goto type_mismatch;
|
|
|
|
type = t1;
|
|
|
|
break;
|
2001-06-20 21:18:04 +00:00
|
|
|
default:
|
2001-12-06 19:49:40 +00:00
|
|
|
type_mismatch:
|
2004-01-25 08:55:03 +00:00
|
|
|
type = t1;
|
|
|
|
break;
|
|
|
|
//return type_mismatch (e1, e2, op);
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
} else {
|
2001-10-17 07:45:37 +00:00
|
|
|
type = t1;
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
2003-08-05 17:48:16 +00:00
|
|
|
if (is_compare (op) || is_logic (op)) {
|
2001-10-26 06:43:56 +00:00
|
|
|
if (options.code.progsversion > PROG_ID_VERSION)
|
2001-08-03 07:47:15 +00:00
|
|
|
type = &type_integer;
|
|
|
|
else
|
|
|
|
type = &type_float;
|
2001-10-17 07:45:37 +00:00
|
|
|
} else if (op == '*' && t1 == &type_vector && t2 == &type_vector) {
|
2001-07-23 05:31:49 +00:00
|
|
|
type = &type_float;
|
2001-08-03 07:47:15 +00:00
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
if (!type)
|
|
|
|
error (e1, "internal error");
|
|
|
|
|
2002-09-13 17:12:26 +00:00
|
|
|
if (options.code.progsversion == PROG_ID_VERSION) {
|
|
|
|
switch (op) {
|
|
|
|
case '%':
|
|
|
|
{
|
|
|
|
expr_t *tmp1, *tmp2;
|
|
|
|
e = new_block_expr ();
|
2002-09-13 17:38:19 +00:00
|
|
|
if (e2->type < ex_string)
|
|
|
|
tmp1 = new_temp_def_expr (&type_float);
|
|
|
|
else
|
|
|
|
tmp1 = e2;
|
2002-09-13 17:12:26 +00:00
|
|
|
tmp2 = new_temp_def_expr (&type_float);
|
|
|
|
e2 = binary_expr ('&', e2, new_float_expr (-1.0));
|
|
|
|
e1 = binary_expr ('&', e1, new_float_expr (-1.0));
|
2002-09-13 17:38:19 +00:00
|
|
|
if (tmp1 != e2)
|
|
|
|
append_expr (e, new_bind_expr (e2, tmp1));
|
|
|
|
append_expr (e, new_bind_expr (binary_expr ('/', e1, tmp1),
|
|
|
|
tmp2));
|
2002-09-13 17:12:26 +00:00
|
|
|
e2 = binary_expr ('&', tmp2, new_float_expr (-1.0));
|
|
|
|
e->e.block.result = binary_expr ('-', tmp2, e2);
|
|
|
|
e2 = e;
|
|
|
|
e1 = tmp1;
|
|
|
|
op = '*';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-07-23 05:31:49 +00:00
|
|
|
e = new_binary_expr (op, e1, e2);
|
|
|
|
e->e.expr.type = type;
|
|
|
|
return e;
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
|
|
|
|
2001-08-10 16:17:00 +00:00
|
|
|
expr_t *
|
|
|
|
asx_expr (int op, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
2002-05-01 22:08:59 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
else if (e2->type == ex_error)
|
|
|
|
return e2;
|
|
|
|
else {
|
|
|
|
expr_t *e = new_expr ();
|
|
|
|
|
|
|
|
*e = *e1;
|
2004-02-08 05:28:30 +00:00
|
|
|
e2->paren = 1;
|
2002-05-01 22:08:59 +00:00
|
|
|
return assign_expr (e, binary_expr (op, e1, e2));
|
|
|
|
}
|
2001-08-10 16:17:00 +00:00
|
|
|
}
|
|
|
|
|
2001-06-20 07:02:36 +00:00
|
|
|
expr_t *
|
|
|
|
unary_expr (int op, expr_t *e)
|
|
|
|
{
|
2001-12-08 08:19:48 +00:00
|
|
|
convert_name (e);
|
2001-10-19 05:16:24 +00:00
|
|
|
check_initialized (e);
|
2002-06-28 16:38:05 +00:00
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
2001-06-20 21:18:04 +00:00
|
|
|
switch (op) {
|
|
|
|
case '-':
|
|
|
|
switch (e->type) {
|
2002-05-01 22:08:59 +00:00
|
|
|
case ex_error:
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-12-08 08:19:48 +00:00
|
|
|
case ex_name:
|
2001-07-26 05:08:27 +00:00
|
|
|
error (e, "internal error");
|
2001-06-25 22:11:20 +00:00
|
|
|
abort ();
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_uexpr:
|
|
|
|
if (e->e.expr.op == '-')
|
|
|
|
return e->e.expr.e1;
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_block:
|
|
|
|
if (!e->e.block.result)
|
|
|
|
return error (e, "invalid type for unary -");
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_expr:
|
2003-10-22 08:05:17 +00:00
|
|
|
case ex_bool:
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_def:
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_temp:
|
2001-06-20 21:18:04 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *n = new_unary_expr (op, e);
|
|
|
|
|
2001-06-20 21:18:04 +00:00
|
|
|
n->e.expr.type = (e->type == ex_def)
|
2001-12-06 19:49:40 +00:00
|
|
|
? e->e.def->type : e->e.expr.type;
|
2001-06-20 21:18:04 +00:00
|
|
|
return n;
|
|
|
|
}
|
2001-12-07 20:10:30 +00:00
|
|
|
case ex_short:
|
|
|
|
e->e.short_val *= -1;
|
|
|
|
return e;
|
2001-07-22 06:59:12 +00:00
|
|
|
case ex_integer:
|
2001-11-13 16:08:50 +00:00
|
|
|
case ex_uinteger:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val *= -1;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_float:
|
|
|
|
e->e.float_val *= -1;
|
|
|
|
return e;
|
2001-10-17 07:45:37 +00:00
|
|
|
case ex_nil:
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_string:
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
2001-06-28 21:26:40 +00:00
|
|
|
return error (e, "invalid type for unary -");
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_vector:
|
|
|
|
e->e.vector_val[0] *= -1;
|
|
|
|
e->e.vector_val[1] *= -1;
|
|
|
|
e->e.vector_val[2] *= -1;
|
|
|
|
return e;
|
|
|
|
case ex_quaternion:
|
|
|
|
e->e.quaternion_val[0] *= -1;
|
|
|
|
e->e.quaternion_val[1] *= -1;
|
|
|
|
e->e.quaternion_val[2] *= -1;
|
|
|
|
e->e.quaternion_val[3] *= -1;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
switch (e->type) {
|
2002-05-01 22:08:59 +00:00
|
|
|
case ex_error:
|
2001-06-25 22:11:20 +00:00
|
|
|
case ex_label:
|
2001-12-08 08:19:48 +00:00
|
|
|
case ex_name:
|
|
|
|
error (e, "internal error");
|
2001-06-25 22:11:20 +00:00
|
|
|
abort ();
|
2003-10-22 08:05:17 +00:00
|
|
|
case ex_bool:
|
|
|
|
return new_bool_expr (e->e.bool.false_list,
|
|
|
|
e->e.bool.true_list, e);
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_block:
|
|
|
|
if (!e->e.block.result)
|
2001-10-06 04:48:52 +00:00
|
|
|
return error (e, "invalid type for unary !");
|
2001-06-20 21:18:04 +00:00
|
|
|
case ex_uexpr:
|
|
|
|
case ex_expr:
|
|
|
|
case ex_def:
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_temp:
|
2001-06-20 21:18:04 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *n = new_unary_expr (op, e);
|
|
|
|
|
2001-10-26 06:43:56 +00:00
|
|
|
if (options.code.progsversion > PROG_ID_VERSION)
|
2001-08-03 07:47:15 +00:00
|
|
|
n->e.expr.type = &type_integer;
|
|
|
|
else
|
|
|
|
n->e.expr.type = &type_float;
|
2001-06-20 21:18:04 +00:00
|
|
|
return n;
|
|
|
|
}
|
2001-10-17 07:45:37 +00:00
|
|
|
case ex_nil:
|
|
|
|
return error (e, "invalid type for unary !");
|
2001-12-07 20:10:30 +00:00
|
|
|
case ex_short:
|
|
|
|
e->e.short_val = !e->e.short_val;
|
|
|
|
return e;
|
2001-07-22 06:59:12 +00:00
|
|
|
case ex_integer:
|
2001-11-13 16:08:50 +00:00
|
|
|
case ex_uinteger:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.integer_val;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_float:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.float_val;
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_string:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.string_val || !e->e.string_val[0];
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_vector:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.vector_val[0]
|
2001-12-06 19:49:40 +00:00
|
|
|
&& !e->e.vector_val[1]
|
|
|
|
&& !e->e.vector_val[2];
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
|
|
|
case ex_quaternion:
|
2001-07-23 01:31:22 +00:00
|
|
|
e->e.integer_val = !e->e.quaternion_val[0]
|
2001-12-06 19:49:40 +00:00
|
|
|
&& !e->e.quaternion_val[1]
|
|
|
|
&& !e->e.quaternion_val[2]
|
|
|
|
&& !e->e.quaternion_val[3];
|
2001-07-22 06:59:12 +00:00
|
|
|
e->type = ex_integer;
|
2001-06-20 21:18:04 +00:00
|
|
|
return e;
|
2001-07-18 06:37:14 +00:00
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
|
|
|
error (e, "internal error");
|
|
|
|
abort ();
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
|
|
|
break;
|
2001-08-09 16:34:46 +00:00
|
|
|
case '~':
|
|
|
|
switch (e->type) {
|
2002-05-01 22:08:59 +00:00
|
|
|
case ex_error:
|
2001-08-09 16:34:46 +00:00
|
|
|
case ex_label:
|
2001-12-08 08:19:48 +00:00
|
|
|
case ex_name:
|
|
|
|
error (e, "internal error");
|
2001-08-09 16:34:46 +00:00
|
|
|
abort ();
|
|
|
|
case ex_uexpr:
|
|
|
|
if (e->e.expr.op == '~')
|
|
|
|
return e->e.expr.e1;
|
2002-09-04 16:18:52 +00:00
|
|
|
goto bitnot_expr;
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_block:
|
|
|
|
if (!e->e.block.result)
|
2001-12-08 08:19:48 +00:00
|
|
|
return error (e, "invalid type for unary ~");
|
2002-09-04 16:18:52 +00:00
|
|
|
goto bitnot_expr;
|
2001-08-09 16:34:46 +00:00
|
|
|
case ex_expr:
|
2003-10-22 08:05:17 +00:00
|
|
|
case ex_bool:
|
2001-08-09 16:34:46 +00:00
|
|
|
case ex_def:
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_temp:
|
2002-09-04 16:18:52 +00:00
|
|
|
bitnot_expr:
|
|
|
|
if (options.code.progsversion == PROG_ID_VERSION) {
|
2002-09-11 16:21:26 +00:00
|
|
|
expr_t *n1 = new_integer_expr (-1);
|
2002-09-04 16:18:52 +00:00
|
|
|
return binary_expr ('-', n1, e);
|
|
|
|
} else {
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *n = new_unary_expr (op, e);
|
|
|
|
type_t *t = get_type (e);
|
|
|
|
|
2001-08-09 16:34:46 +00:00
|
|
|
if (t != &type_integer && t != &type_float)
|
|
|
|
return error (e, "invalid type for unary ~");
|
|
|
|
n->e.expr.type = t;
|
|
|
|
return n;
|
|
|
|
}
|
2001-12-07 20:10:30 +00:00
|
|
|
case ex_short:
|
|
|
|
e->e.short_val = ~e->e.short_val;
|
|
|
|
return e;
|
2001-08-09 16:34:46 +00:00
|
|
|
case ex_integer:
|
2001-11-13 16:08:50 +00:00
|
|
|
case ex_uinteger:
|
2001-08-09 16:34:46 +00:00
|
|
|
e->e.integer_val = ~e->e.integer_val;
|
|
|
|
return e;
|
|
|
|
case ex_float:
|
2001-12-06 19:49:40 +00:00
|
|
|
e->e.float_val = ~(int) e->e.float_val;
|
2001-08-09 16:34:46 +00:00
|
|
|
return e;
|
2001-10-17 07:45:37 +00:00
|
|
|
case ex_nil:
|
2001-08-09 16:34:46 +00:00
|
|
|
case ex_string:
|
|
|
|
case ex_vector:
|
|
|
|
case ex_quaternion:
|
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
|
|
|
return error (e, "invalid type for unary ~");
|
|
|
|
}
|
|
|
|
break;
|
2001-12-12 08:39:47 +00:00
|
|
|
case '.':
|
|
|
|
if (extract_type (e) != ev_pointer)
|
|
|
|
return error (e, "invalid type for unary .");
|
2001-12-12 21:52:07 +00:00
|
|
|
e = new_unary_expr ('.', e);
|
|
|
|
e->e.expr.type = get_type (e->e.expr.e1)->aux_type;
|
2001-12-12 08:39:47 +00:00
|
|
|
return e;
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-06-28 21:26:40 +00:00
|
|
|
error (e, "internal error");
|
2001-06-20 21:18:04 +00:00
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
2004-02-04 05:49:54 +00:00
|
|
|
static expr_t *
|
|
|
|
build_function_call (expr_t *fexpr, type_t *ftype, expr_t *params)
|
2001-06-20 21:18:04 +00:00
|
|
|
{
|
2001-08-20 04:56:00 +00:00
|
|
|
expr_t *e;
|
2002-05-08 17:33:28 +00:00
|
|
|
int arg_count = 0, parm_count = 0;
|
2001-08-20 04:56:00 +00:00
|
|
|
int i;
|
|
|
|
expr_t *args = 0, **a = &args;
|
|
|
|
type_t *arg_types[MAX_PARMS];
|
2001-08-24 23:22:02 +00:00
|
|
|
expr_t *arg_exprs[MAX_PARMS][2];
|
|
|
|
int arg_expr_count = 0;
|
2001-08-20 04:56:00 +00:00
|
|
|
expr_t *call;
|
2001-08-20 18:23:47 +00:00
|
|
|
expr_t *err = 0;
|
2001-06-20 21:18:04 +00:00
|
|
|
|
2004-02-04 05:49:54 +00:00
|
|
|
for (e = params; e; e = e->next) {
|
2002-05-02 05:03:57 +00:00
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
2002-05-08 17:33:28 +00:00
|
|
|
arg_count++;
|
2002-05-02 05:03:57 +00:00
|
|
|
}
|
2002-05-01 22:08:59 +00:00
|
|
|
|
2002-05-08 17:33:28 +00:00
|
|
|
if (arg_count > MAX_PARMS) {
|
2004-02-04 05:49:54 +00:00
|
|
|
return error (fexpr, "more than %d parameters", MAX_PARMS);
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2002-05-08 17:33:28 +00:00
|
|
|
if (ftype->num_parms < -1) {
|
2002-05-09 20:12:28 +00:00
|
|
|
if (-arg_count > ftype->num_parms + 1) {
|
2002-05-08 17:33:28 +00:00
|
|
|
if (!options.traditional)
|
2004-02-04 05:49:54 +00:00
|
|
|
return error (fexpr, "too few arguments");
|
|
|
|
warning (fexpr, "too few arguments");
|
2002-05-08 17:33:28 +00:00
|
|
|
}
|
|
|
|
parm_count = -ftype->num_parms - 1;
|
|
|
|
} else if (ftype->num_parms >= 0) {
|
|
|
|
if (arg_count > ftype->num_parms) {
|
2004-02-04 05:49:54 +00:00
|
|
|
return error (fexpr, "too many arguments");
|
2002-05-08 17:33:28 +00:00
|
|
|
} else if (arg_count < ftype->num_parms) {
|
2002-02-18 06:23:59 +00:00
|
|
|
if (!options.traditional)
|
2004-02-04 05:49:54 +00:00
|
|
|
return error (fexpr, "too few arguments");
|
|
|
|
warning (fexpr, "too few arguments");
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2002-05-08 17:33:28 +00:00
|
|
|
parm_count = ftype->num_parms;
|
2001-08-20 18:23:47 +00:00
|
|
|
}
|
2004-02-04 05:49:54 +00:00
|
|
|
for (i = arg_count - 1, e = params; i >= 0; i--, e = e->next) {
|
2001-12-06 19:49:40 +00:00
|
|
|
type_t *t = get_type (e);
|
2002-08-29 03:06:00 +00:00
|
|
|
|
|
|
|
check_initialized (e);
|
2004-02-04 04:49:46 +00:00
|
|
|
if (ftype->parm_types[i] == &type_float && e->type == ex_integer) {
|
|
|
|
convert_int (e);
|
|
|
|
t = &type_float;
|
|
|
|
}
|
2002-05-08 17:33:28 +00:00
|
|
|
if (i < parm_count) {
|
2003-08-20 20:28:28 +00:00
|
|
|
if (e->type == ex_nil)
|
|
|
|
convert_nil (e, t = ftype->parm_types[i]);
|
2003-10-22 16:49:57 +00:00
|
|
|
if (e->type == ex_bool)
|
|
|
|
convert_from_bool (e, ftype->parm_types[i]);
|
|
|
|
if (e->type == ex_error)
|
2004-02-04 05:49:54 +00:00
|
|
|
return e;
|
2002-05-16 20:20:23 +00:00
|
|
|
if (!type_assignable (ftype->parm_types[i], t)) {
|
2003-03-04 21:13:10 +00:00
|
|
|
//print_type (ftype->parm_types[i]); puts ("");
|
|
|
|
//print_type (t); puts ("");
|
2001-07-23 01:56:49 +00:00
|
|
|
err = error (e, "type mismatch for parameter %d of %s",
|
2004-02-04 05:49:54 +00:00
|
|
|
i + 1, fexpr->e.def->name);
|
2002-01-17 08:19:53 +00:00
|
|
|
}
|
2004-02-04 05:49:54 +00:00
|
|
|
t = ftype->parm_types[i];
|
2001-08-20 18:23:47 +00:00
|
|
|
} else {
|
2003-08-22 06:04:31 +00:00
|
|
|
if (e->type == ex_nil)
|
|
|
|
convert_nil (e, t = &type_vector); //XXX largest param size
|
2003-10-22 16:49:57 +00:00
|
|
|
if (e->type == ex_bool)
|
|
|
|
convert_from_bool (e, get_type (e));
|
2004-02-04 05:49:54 +00:00
|
|
|
if (e->type == ex_integer
|
|
|
|
&& options.code.progsversion == PROG_ID_VERSION)
|
|
|
|
convert_int (e);
|
2001-12-06 19:49:40 +00:00
|
|
|
if (e->type == ex_integer && options.warnings.vararg_integer)
|
|
|
|
warning (e, "passing integer consant into ... function");
|
2001-07-23 01:56:49 +00:00
|
|
|
}
|
2002-05-08 17:33:28 +00:00
|
|
|
arg_types[arg_count - 1 - i] = t;
|
2001-06-20 21:18:04 +00:00
|
|
|
}
|
2001-08-20 18:23:47 +00:00
|
|
|
if (err)
|
|
|
|
return err;
|
2001-08-20 04:56:00 +00:00
|
|
|
|
|
|
|
call = new_block_expr ();
|
2001-08-24 21:47:52 +00:00
|
|
|
call->e.block.is_call = 1;
|
2004-02-04 05:49:54 +00:00
|
|
|
for (e = params, i = 0; e; e = e->next, i++) {
|
2004-02-04 04:49:46 +00:00
|
|
|
if (has_function_call (e)) {
|
2001-08-24 05:40:48 +00:00
|
|
|
*a = new_temp_def_expr (arg_types[i]);
|
2004-02-04 05:49:54 +00:00
|
|
|
arg_exprs[arg_expr_count][0] = cast_expr (arg_types[i], e);
|
2001-08-24 23:22:02 +00:00
|
|
|
arg_exprs[arg_expr_count][1] = *a;
|
|
|
|
arg_expr_count++;
|
2001-08-24 05:40:48 +00:00
|
|
|
} else {
|
2004-02-04 05:49:54 +00:00
|
|
|
*a = cast_expr (arg_types[i], e);
|
2001-08-24 21:57:07 +00:00
|
|
|
}
|
2002-08-20 17:21:32 +00:00
|
|
|
// new_binary_expr calls inc_users for both args, but in_users doesn't
|
|
|
|
// walk expression chains so only the first arg expression in the chain
|
|
|
|
// (the last arg in the call) gets its user count incremented, thus
|
|
|
|
// ensure all other arg expressions get their user counts incremented.
|
|
|
|
if (a != &args)
|
|
|
|
inc_users (*a);
|
2001-08-24 21:57:07 +00:00
|
|
|
a = &(*a)->next;
|
|
|
|
}
|
2001-08-24 23:22:02 +00:00
|
|
|
for (i = 0; i < arg_expr_count - 1; i++) {
|
2001-12-12 08:39:47 +00:00
|
|
|
append_expr (call, assign_expr (arg_exprs[i][1], arg_exprs[i][0]));
|
2001-08-24 23:22:02 +00:00
|
|
|
}
|
|
|
|
if (arg_expr_count) {
|
2001-11-13 18:49:27 +00:00
|
|
|
e = new_bind_expr (arg_exprs[arg_expr_count - 1][0],
|
|
|
|
arg_exprs[arg_expr_count - 1][1]);
|
|
|
|
inc_users (arg_exprs[arg_expr_count - 1][0]);
|
|
|
|
inc_users (arg_exprs[arg_expr_count - 1][1]);
|
2001-08-24 23:22:02 +00:00
|
|
|
append_expr (call, e);
|
|
|
|
}
|
2004-02-04 05:49:54 +00:00
|
|
|
e = new_binary_expr ('c', fexpr, args);
|
2001-06-20 21:18:04 +00:00
|
|
|
e->e.expr.type = ftype->aux_type;
|
2001-08-20 04:56:00 +00:00
|
|
|
append_expr (call, e);
|
|
|
|
if (ftype->aux_type != &type_void) {
|
2003-04-22 15:29:32 +00:00
|
|
|
call->e.block.result = new_ret_expr (ftype->aux_type);
|
2003-09-23 16:57:53 +00:00
|
|
|
} else if (options.traditional) {
|
|
|
|
call->e.block.result = new_ret_expr (&type_float);
|
2001-08-20 04:56:00 +00:00
|
|
|
}
|
|
|
|
return call;
|
2001-06-20 07:02:36 +00:00
|
|
|
}
|
2001-06-26 07:21:20 +00:00
|
|
|
|
2004-02-04 05:49:54 +00:00
|
|
|
expr_t *
|
|
|
|
function_expr (expr_t *fexpr, expr_t *params)
|
|
|
|
{
|
|
|
|
type_t *ftype;
|
|
|
|
|
|
|
|
ftype = get_type (fexpr);
|
|
|
|
|
|
|
|
if (fexpr->type == ex_error)
|
|
|
|
return fexpr;
|
|
|
|
if (ftype->type != ev_func) {
|
|
|
|
if (fexpr->type == ex_def)
|
|
|
|
return error (fexpr, "Called object \"%s\" is not a function",
|
|
|
|
fexpr->e.def->name);
|
|
|
|
else
|
|
|
|
return error (fexpr, "Called object is not a function");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fexpr->type == ex_def && params && params->type == ex_string) {
|
|
|
|
// FIXME eww, I hate this, but it's needed :(
|
|
|
|
// FIXME make a qc hook? :)
|
|
|
|
def_t *func = fexpr->e.def;
|
|
|
|
def_t *e = ReuseConstant (params, 0);
|
|
|
|
|
|
|
|
if (strncmp (func->name, "precache_sound", 14) == 0)
|
|
|
|
PrecacheSound (e, func->name[14]);
|
|
|
|
else if (strncmp (func->name, "precache_model", 14) == 0)
|
|
|
|
PrecacheModel (e, func->name[14]);
|
|
|
|
else if (strncmp (func->name, "precache_file", 13) == 0)
|
|
|
|
PrecacheFile (e, func->name[13]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return build_function_call (fexpr, ftype, params);
|
|
|
|
}
|
|
|
|
|
2001-07-23 02:27:46 +00:00
|
|
|
expr_t *
|
|
|
|
return_expr (function_t *f, expr_t *e)
|
|
|
|
{
|
2003-10-22 08:30:54 +00:00
|
|
|
type_t *t;
|
2003-10-22 08:05:17 +00:00
|
|
|
|
2001-07-23 02:27:46 +00:00
|
|
|
if (!e) {
|
2002-02-18 06:23:59 +00:00
|
|
|
if (f->def->type->aux_type != &type_void) {
|
|
|
|
if (options.traditional) {
|
|
|
|
warning (e, "return from non-void function without a value");
|
2002-09-11 16:21:26 +00:00
|
|
|
e = new_nil_expr ();
|
2002-02-18 06:23:59 +00:00
|
|
|
} else {
|
|
|
|
e = error (e, "return from non-void function without a value");
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
2003-10-22 08:05:17 +00:00
|
|
|
return new_unary_expr ('r', 0);
|
2002-02-18 06:23:59 +00:00
|
|
|
}
|
2001-10-02 18:55:52 +00:00
|
|
|
|
2003-10-22 08:30:54 +00:00
|
|
|
t = get_type (e);
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
|
|
|
if (f->def->type->aux_type == &type_void) {
|
|
|
|
if (!options.traditional)
|
|
|
|
return error (e, "returning a value for a void function");
|
|
|
|
warning (e, "returning a value for a void function");
|
|
|
|
}
|
2003-10-22 09:14:53 +00:00
|
|
|
if (e->type == ex_bool)
|
|
|
|
e = convert_from_bool (e, f->def->type->aux_type);
|
2003-10-22 08:05:17 +00:00
|
|
|
if (f->def->type->aux_type == &type_float && e->type == ex_integer) {
|
|
|
|
e->type = ex_float;
|
|
|
|
e->e.float_val = e->e.integer_val;
|
|
|
|
t = &type_float;
|
|
|
|
}
|
|
|
|
if (t == &type_void) {
|
2004-02-04 02:30:18 +00:00
|
|
|
if (e->type == ex_nil) {
|
|
|
|
t = f->def->type->aux_type;
|
|
|
|
e->type = expr_types[t->type];
|
|
|
|
} else {
|
|
|
|
if (!options.traditional)
|
|
|
|
return error (e, "void value not ignored as it ought to be");
|
|
|
|
warning (e, "void value not ignored as it ought to be");
|
|
|
|
//FIXME does anything need to be done here?
|
|
|
|
}
|
2003-10-22 08:05:17 +00:00
|
|
|
}
|
|
|
|
if (!type_assignable (f->def->type->aux_type, t)) {
|
|
|
|
if (!options.traditional)
|
|
|
|
return error (e, "type mismatch for return value of %s",
|
|
|
|
f->def->name);
|
|
|
|
warning (e, "type mismatch for return value of %s",
|
|
|
|
f->def->name);
|
2004-02-06 08:40:28 +00:00
|
|
|
} else {
|
|
|
|
if (f->def->type->aux_type != t)
|
|
|
|
e = cast_expr (f->def->type->aux_type, e);
|
2001-07-23 02:27:46 +00:00
|
|
|
}
|
|
|
|
return new_unary_expr ('r', e);
|
|
|
|
}
|
2001-08-11 21:15:24 +00:00
|
|
|
|
|
|
|
expr_t *
|
|
|
|
conditional_expr (expr_t *cond, expr_t *e1, expr_t *e2)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *block = new_block_expr ();
|
|
|
|
type_t *type1 = get_type (e1);
|
|
|
|
type_t *type2 = get_type (e2);
|
|
|
|
expr_t *tlabel = new_label_expr ();
|
2003-10-22 08:05:17 +00:00
|
|
|
expr_t *flabel = new_label_expr ();
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t *elabel = new_label_expr ();
|
2001-08-11 21:15:24 +00:00
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (cond->type == ex_error)
|
|
|
|
return cond;
|
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
|
|
|
|
2003-10-22 08:05:17 +00:00
|
|
|
cond = convert_bool (cond, 1);
|
2003-10-24 22:23:56 +00:00
|
|
|
if (cond->type == ex_error)
|
|
|
|
return cond;
|
2003-10-22 08:05:17 +00:00
|
|
|
|
|
|
|
backpatch (cond->e.bool.true_list, tlabel);
|
|
|
|
backpatch (cond->e.bool.false_list, flabel);
|
|
|
|
|
2001-08-11 21:15:24 +00:00
|
|
|
block->e.block.result = (type1 == type2) ? new_temp_def_expr (type1) : 0;
|
2003-10-22 08:05:17 +00:00
|
|
|
append_expr (block, cond);
|
|
|
|
append_expr (cond->e.bool.e, flabel);
|
2001-08-11 21:15:24 +00:00
|
|
|
if (block->e.block.result)
|
2001-12-12 08:39:47 +00:00
|
|
|
append_expr (block, assign_expr (block->e.block.result, e2));
|
2001-08-11 21:15:24 +00:00
|
|
|
else
|
|
|
|
append_expr (block, e2);
|
|
|
|
append_expr (block, new_unary_expr ('g', elabel));
|
|
|
|
append_expr (block, tlabel);
|
|
|
|
if (block->e.block.result)
|
2001-12-12 08:39:47 +00:00
|
|
|
append_expr (block, assign_expr (block->e.block.result, e1));
|
2001-08-11 21:15:24 +00:00
|
|
|
else
|
|
|
|
append_expr (block, e1);
|
|
|
|
append_expr (block, elabel);
|
|
|
|
return block;
|
|
|
|
}
|
2001-08-20 06:22:28 +00:00
|
|
|
|
|
|
|
expr_t *
|
|
|
|
incop_expr (int op, expr_t *e, int postop)
|
|
|
|
{
|
2002-09-11 16:21:26 +00:00
|
|
|
expr_t *one;
|
2001-08-20 06:22:28 +00:00
|
|
|
expr_t *incop;
|
2001-11-14 06:45:31 +00:00
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
|
|
|
|
2002-09-11 16:21:26 +00:00
|
|
|
one = new_integer_expr (1); // integer constants get auto-cast to float
|
2001-08-20 06:22:28 +00:00
|
|
|
incop = asx_expr (op, e, one);
|
|
|
|
if (postop) {
|
|
|
|
expr_t *temp;
|
pr_comp.h:
o add ev_uniteger to the types enum
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
progs.h:
o add uinteger accessors
pr_exec.c:
o implement ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
pr_opcode.c:
o add opcodes for ifbe, ifb, ifae, ifa, jump, lt.ui, gt.ui, le.ui, ge.ui
expr.h:
o prototype inc_users
qfcc.h:
o add externs for op_ifbe, op_ifb, op_ifae and op_ifa
emit.c:
o don't bother emiting an assignment to a temp def that's only used once
(ie, it's never read, only written to)
o support the new if* instructions
expr.c:
o support the new if* insructions
o dectect expression loops in append_expr
o support unsigned integers
o re-work temp def usage counting
pr_def.c
o debugging for temp def usage counts
pr_opcode.c:
o support the new if* instructions
qc-parse.y:
o provide defines for IFBE IFB IFAE IFA
switch.c:
o do binary searches for strings, floats and ints if there are more than
8 cases in a switch. Strings need more testing.
2001-11-09 00:58:16 +00:00
|
|
|
type_t *type = get_type (e);
|
2001-08-20 06:22:28 +00:00
|
|
|
expr_t *block = new_block_expr ();
|
|
|
|
|
|
|
|
temp = new_temp_def_expr (type);
|
2001-12-12 08:39:47 +00:00
|
|
|
append_expr (block, assign_expr (temp, e));
|
2001-08-20 06:22:28 +00:00
|
|
|
append_expr (block, incop);
|
|
|
|
block->e.block.result = temp;
|
|
|
|
return block;
|
|
|
|
}
|
|
|
|
return incop;
|
|
|
|
}
|
2001-11-15 00:46:36 +00:00
|
|
|
|
|
|
|
expr_t *
|
|
|
|
array_expr (expr_t *array, expr_t *index)
|
|
|
|
{
|
|
|
|
type_t *array_type = get_type (array);
|
|
|
|
type_t *index_type = get_type (index);
|
|
|
|
expr_t *scale;
|
2001-12-12 08:39:47 +00:00
|
|
|
expr_t *e;
|
2001-11-15 00:46:36 +00:00
|
|
|
int size;
|
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (array->type == ex_error)
|
|
|
|
return array;
|
|
|
|
if (index->type == ex_error)
|
|
|
|
return index;
|
|
|
|
|
2002-06-12 22:37:18 +00:00
|
|
|
if (array_type->type != ev_pointer && array_type->type != ev_array)
|
2001-11-15 00:46:36 +00:00
|
|
|
return error (array, "not an array");
|
|
|
|
if (index_type != &type_integer && index_type != &type_uinteger)
|
|
|
|
return error (index, "invalid array index type");
|
2002-02-24 01:11:34 +00:00
|
|
|
if (array_type->num_parms
|
|
|
|
&& index->type >= ex_integer
|
2003-04-17 00:01:48 +00:00
|
|
|
&& index->e.uinteger_val >= (unsigned int) array_type->num_parms)
|
2001-12-06 19:49:40 +00:00
|
|
|
return error (index, "array index out of bounds");
|
2002-05-15 23:24:19 +00:00
|
|
|
size = type_size (array_type->aux_type);
|
2001-11-15 00:46:36 +00:00
|
|
|
if (size > 1) {
|
|
|
|
scale = new_expr ();
|
2001-12-06 19:49:40 +00:00
|
|
|
scale->type = expr_types[index_type->type];
|
2001-11-15 00:46:36 +00:00
|
|
|
scale->e.integer_val = size;
|
|
|
|
index = binary_expr ('*', index, scale);
|
|
|
|
}
|
2001-12-07 20:10:30 +00:00
|
|
|
if ((index->type == ex_integer
|
|
|
|
&& index->e.integer_val < 32768 && index->e.integer_val >= -32768)
|
|
|
|
|| (index->type == ex_uinteger
|
|
|
|
&& index->e.uinteger_val < 32768)) {
|
|
|
|
index->type = ex_short;
|
|
|
|
}
|
2002-06-12 22:37:18 +00:00
|
|
|
if (array_type->type == ev_array) {
|
|
|
|
e = address_expr (array, index, array_type->aux_type);
|
|
|
|
} else {
|
2003-05-13 19:49:23 +00:00
|
|
|
if (index->type != ex_short || index->e.integer_val) {
|
2004-01-25 08:55:03 +00:00
|
|
|
e = new_binary_expr ('&', array, index);
|
|
|
|
//e->e.expr.type = array_type->aux_type;
|
|
|
|
e->e.expr.type = array_type;
|
2003-05-13 19:49:23 +00:00
|
|
|
} else {
|
|
|
|
e = array;
|
|
|
|
}
|
2002-06-12 22:37:18 +00:00
|
|
|
}
|
|
|
|
e = unary_expr ('.', e);
|
|
|
|
return e;
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
address_expr (expr_t *e1, expr_t *e2, type_t *t)
|
|
|
|
{
|
|
|
|
expr_t *e;
|
2003-07-30 04:11:45 +00:00
|
|
|
type_t *type;
|
2001-12-12 08:39:47 +00:00
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
|
2001-12-12 08:39:47 +00:00
|
|
|
if (!t)
|
|
|
|
t = get_type (e1);
|
|
|
|
|
|
|
|
switch (e1->type) {
|
|
|
|
case ex_def:
|
2002-07-14 03:41:13 +00:00
|
|
|
{
|
2002-06-13 07:20:15 +00:00
|
|
|
def_t *def = e1->e.def;
|
2003-05-15 05:58:31 +00:00
|
|
|
def->used = 1;
|
2002-07-14 03:41:13 +00:00
|
|
|
type = def->type;
|
2003-05-15 05:58:31 +00:00
|
|
|
if (type->type == ev_struct || type->type == ev_class) {
|
|
|
|
e = new_expr ();
|
|
|
|
e->line = e1->line;
|
|
|
|
e->file = e1->file;
|
2002-07-14 03:41:13 +00:00
|
|
|
e->type = ex_pointer;
|
2003-05-15 05:58:31 +00:00
|
|
|
e->e.pointer.val = 0;
|
2002-07-14 03:41:13 +00:00
|
|
|
e->e.pointer.type = t;
|
2002-10-16 02:04:06 +00:00
|
|
|
e->e.pointer.def = def;
|
2003-07-30 21:55:32 +00:00
|
|
|
} else if (type->type == ev_array) {
|
2002-07-14 03:41:13 +00:00
|
|
|
e = e1;
|
|
|
|
e->type = ex_pointer;
|
2003-05-15 05:58:31 +00:00
|
|
|
e->e.pointer.val = 0;
|
2002-07-14 03:41:13 +00:00
|
|
|
e->e.pointer.type = t;
|
2002-10-16 02:04:06 +00:00
|
|
|
e->e.pointer.def = def;
|
2002-07-14 03:41:13 +00:00
|
|
|
} else {
|
|
|
|
e = new_unary_expr ('&', e1);
|
2003-07-30 04:11:45 +00:00
|
|
|
e->e.expr.type = pointer_type (t);
|
2002-07-14 03:41:13 +00:00
|
|
|
}
|
|
|
|
break;
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
case ex_expr:
|
|
|
|
if (e1->e.expr.op == '.') {
|
2001-12-12 18:34:41 +00:00
|
|
|
e = e1;
|
|
|
|
e->e.expr.op = '&';
|
|
|
|
e->e.expr.type = pointer_type (e->e.expr.type);
|
|
|
|
break;
|
2002-10-09 19:30:43 +00:00
|
|
|
} else if (e1->e.expr.op == 'b') {
|
|
|
|
e = new_unary_expr ('&', e1);
|
|
|
|
e->e.expr.type = pointer_type (e1->e.expr.type);
|
|
|
|
break;
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
2001-12-12 18:34:41 +00:00
|
|
|
return error (e1, "invalid type for unary &");
|
2001-12-12 08:39:47 +00:00
|
|
|
case ex_uexpr:
|
|
|
|
if (e1->e.expr.op == '.') {
|
|
|
|
e = e1->e.expr.e1;
|
|
|
|
if (e->type == ex_expr && e->e.expr.op == '.') {
|
2003-05-13 19:42:14 +00:00
|
|
|
e->e.expr.type = e->e.expr.type;
|
2001-12-12 08:39:47 +00:00
|
|
|
e->e.expr.op = '&';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2001-12-13 16:40:46 +00:00
|
|
|
return error (e1, "invalid type for unary &");
|
2001-12-12 08:39:47 +00:00
|
|
|
default:
|
2001-12-13 16:40:46 +00:00
|
|
|
return error (e1, "invalid type for unary &");
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
if (e2) {
|
2002-05-02 05:03:57 +00:00
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
2001-12-12 08:39:47 +00:00
|
|
|
if (e->type == ex_pointer && e2->type == ex_short) {
|
|
|
|
e->e.pointer.val += e2->e.short_val;
|
|
|
|
} else {
|
|
|
|
if (e2->type != ex_short || e2->e.short_val) {
|
2003-09-02 17:41:16 +00:00
|
|
|
if (e->type == ex_expr && e->e.expr.op == '&') {
|
|
|
|
e = new_binary_expr ('&', e->e.expr.e1,
|
|
|
|
binary_expr ('+', e->e.expr.e2, e2));
|
|
|
|
} else {
|
|
|
|
e = new_binary_expr ('&', e, e2);
|
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
if (e->type == ex_expr || e->type == ex_uexpr)
|
|
|
|
e->e.expr.type = pointer_type (t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
is_indirect (expr_t *e)
|
|
|
|
{
|
|
|
|
if (e->type == ex_expr && e->e.expr.op == '.')
|
|
|
|
return 1;
|
|
|
|
if (!(e->type == ex_uexpr && e->e.expr.op == '.'))
|
|
|
|
return 0;
|
|
|
|
e = e->e.expr.e1;
|
|
|
|
if (e->type != ex_pointer
|
2003-05-15 05:58:31 +00:00
|
|
|
|| !(POINTER_VAL (e->e.pointer) >= 0
|
|
|
|
&& POINTER_VAL (e->e.pointer) < 65536)) {
|
2001-12-12 08:39:47 +00:00
|
|
|
return 1;
|
2003-05-15 05:58:31 +00:00
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-09-23 16:42:03 +00:00
|
|
|
static inline int
|
|
|
|
is_lvalue (expr_t *e)
|
|
|
|
{
|
|
|
|
if (e->type == ex_def || e->type == ex_temp)
|
|
|
|
return 1;
|
|
|
|
if (e->type == ex_expr && e->e.expr.op == '.')
|
|
|
|
return 1;
|
|
|
|
if (e->type == ex_uexpr && e->e.expr.op == '.')
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-12-12 08:39:47 +00:00
|
|
|
expr_t *
|
|
|
|
assign_expr (expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
int op = '=';
|
|
|
|
type_t *t1, *t2, *type;
|
|
|
|
expr_t *e;
|
|
|
|
|
2002-05-17 06:20:27 +00:00
|
|
|
convert_name (e1);
|
|
|
|
convert_name (e2);
|
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (e1->type == ex_error)
|
|
|
|
return e1;
|
|
|
|
if (e2->type == ex_error)
|
|
|
|
return e2;
|
|
|
|
|
2003-09-23 16:42:03 +00:00
|
|
|
if (options.traditional) {
|
|
|
|
if (e2->type == ex_expr && !e2->paren
|
|
|
|
&& (e2->e.expr.op == AND || e2->e.expr.op == OR)) {
|
|
|
|
notice (e2, "precedence of `%s' and `%s' inverted for "
|
|
|
|
"traditional code", get_op_string (op),
|
|
|
|
get_op_string (e2->e.expr.op));
|
|
|
|
e1 = assign_expr (e1, e2->e.expr.e1);
|
|
|
|
e1->paren = 1;
|
|
|
|
return binary_expr (e2->e.expr.op, e1, e2->e.expr.e2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-12-12 08:39:47 +00:00
|
|
|
if (e1->type == ex_def)
|
2002-06-09 04:30:02 +00:00
|
|
|
def_initialized (e1->e.def);
|
2003-09-23 16:42:03 +00:00
|
|
|
|
|
|
|
if (!is_lvalue (e1)) {
|
|
|
|
if (options.traditional)
|
|
|
|
warning (e1, "invalid lvalue in assignment");
|
|
|
|
else
|
|
|
|
return error (e1, "invalid lvalue in assignment");
|
|
|
|
}
|
2001-12-12 21:52:07 +00:00
|
|
|
//XXX func = func ???
|
2001-12-12 08:39:47 +00:00
|
|
|
check_initialized (e2);
|
|
|
|
t1 = get_type (e1);
|
|
|
|
t2 = get_type (e2);
|
|
|
|
if (!t1 || !t2) {
|
|
|
|
error (e1, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
2003-10-22 09:14:53 +00:00
|
|
|
if (e2->type == ex_bool)
|
|
|
|
e2 = convert_from_bool (e2, t1);
|
|
|
|
|
2001-12-12 08:39:47 +00:00
|
|
|
if (t1->type != ev_void && e2->type == ex_nil) {
|
|
|
|
t2 = t1;
|
2002-01-30 22:17:55 +00:00
|
|
|
convert_nil (e2, t2);
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
|
2002-02-21 20:34:04 +00:00
|
|
|
e2->rvalue = 1;
|
|
|
|
|
2002-05-16 20:20:23 +00:00
|
|
|
if (!type_assignable (t1, t2)) {
|
2002-02-18 06:23:59 +00:00
|
|
|
if (!options.traditional || t1->type != ev_func || t2->type != ev_func)
|
|
|
|
return type_mismatch (e1, e2, op);
|
|
|
|
warning (e1, "assignment between disparate function types");
|
|
|
|
}
|
2002-05-16 20:20:23 +00:00
|
|
|
type = t1;
|
2001-12-12 08:39:47 +00:00
|
|
|
if (is_indirect (e1) && is_indirect (e2)) {
|
2002-10-20 01:25:15 +00:00
|
|
|
if (extract_type (e2) == ev_struct) {
|
|
|
|
e1 = address_expr (e1, 0, 0);
|
|
|
|
e2 = address_expr (e2, 0, 0);
|
2002-10-20 02:35:12 +00:00
|
|
|
e = new_move_expr (e1, e2, get_type (e2));
|
2002-10-20 01:25:15 +00:00
|
|
|
} else {
|
2002-10-16 17:07:01 +00:00
|
|
|
expr_t *temp = new_temp_def_expr (t1);
|
2001-12-12 08:39:47 +00:00
|
|
|
|
2002-10-16 17:07:01 +00:00
|
|
|
e = new_block_expr ();
|
|
|
|
append_expr (e, assign_expr (temp, e2));
|
|
|
|
append_expr (e, assign_expr (e1, temp));
|
|
|
|
e->e.block.result = temp;
|
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
return e;
|
|
|
|
} else if (is_indirect (e1)) {
|
2002-10-16 17:07:01 +00:00
|
|
|
if (extract_type (e1) == ev_struct) {
|
|
|
|
e1 = address_expr (e1, 0, 0);
|
2002-10-20 02:35:12 +00:00
|
|
|
return new_move_expr (e1, e2, get_type (e2));
|
2002-10-16 17:07:01 +00:00
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
if (e1->type == ex_expr) {
|
|
|
|
if (get_type (e1->e.expr.e1) == &type_entity) {
|
|
|
|
type = e1->e.expr.type;
|
|
|
|
e1->e.expr.type = pointer_type (type);
|
|
|
|
e1->e.expr.op = '&';
|
|
|
|
}
|
|
|
|
op = PAS;
|
|
|
|
} else {
|
|
|
|
e = e1->e.expr.e1;
|
|
|
|
if (e->type != ex_pointer
|
2003-05-15 05:58:31 +00:00
|
|
|
|| !(POINTER_VAL (e->e.pointer) > 0
|
|
|
|
&& POINTER_VAL (e->e.pointer) < 65536)) {
|
2001-12-12 08:39:47 +00:00
|
|
|
e1 = e;
|
|
|
|
op = PAS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (is_indirect (e2)) {
|
2002-10-16 17:07:01 +00:00
|
|
|
if (extract_type (e1) == ev_struct) {
|
|
|
|
e2 = address_expr (e2, 0, 0);
|
2004-01-25 08:55:03 +00:00
|
|
|
e2->rvalue = 1;
|
2002-10-20 02:35:12 +00:00
|
|
|
return new_move_expr (e1, e2, get_type (e2));
|
2002-10-16 17:07:01 +00:00
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
if (e2->type == ex_uexpr) {
|
|
|
|
e = e2->e.expr.e1;
|
|
|
|
if (e->type != ex_pointer
|
2003-05-15 05:58:31 +00:00
|
|
|
|| !(POINTER_VAL (e->e.pointer) > 0
|
|
|
|
&& POINTER_VAL (e->e.pointer) < 65536)) {
|
2002-06-12 22:37:18 +00:00
|
|
|
if (e->type == ex_expr && e->e.expr.op == '&'
|
|
|
|
&& e->e.expr.type->type == ev_pointer
|
|
|
|
&& e->e.expr.e1->type < ex_string) {
|
|
|
|
e2 = e;
|
2001-12-12 20:35:58 +00:00
|
|
|
e2->e.expr.op = '.';
|
2004-01-25 08:55:03 +00:00
|
|
|
e2->e.expr.type = t2;
|
|
|
|
e2->rvalue = 1;
|
2002-06-12 22:37:18 +00:00
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-10-16 17:07:01 +00:00
|
|
|
if (extract_type (e1) == ev_struct) {
|
2002-10-20 02:35:12 +00:00
|
|
|
return new_move_expr (e1, e2, get_type (e2));
|
2002-10-16 17:07:01 +00:00
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
if (!type)
|
|
|
|
error (e1, "internal error");
|
|
|
|
|
|
|
|
e = new_binary_expr (op, e1, e2);
|
|
|
|
e->e.expr.type = type;
|
|
|
|
return e;
|
2001-11-15 00:46:36 +00:00
|
|
|
}
|
2002-01-17 19:32:04 +00:00
|
|
|
|
|
|
|
expr_t *
|
2002-05-22 05:03:36 +00:00
|
|
|
cast_expr (type_t *type, expr_t *e)
|
2002-01-17 19:32:04 +00:00
|
|
|
{
|
|
|
|
expr_t *c;
|
2002-05-22 05:03:36 +00:00
|
|
|
type_t *e_type;
|
|
|
|
|
|
|
|
convert_name (e);
|
2002-01-17 19:32:04 +00:00
|
|
|
|
2002-05-01 22:08:59 +00:00
|
|
|
if (e->type == ex_error)
|
|
|
|
return e;
|
|
|
|
|
2004-01-25 08:55:03 +00:00
|
|
|
check_initialized (e);
|
|
|
|
|
2002-05-22 05:03:36 +00:00
|
|
|
e_type = get_type (e);
|
|
|
|
|
2003-08-01 21:20:04 +00:00
|
|
|
if (type == e_type)
|
|
|
|
return e;
|
|
|
|
|
2003-08-24 08:03:25 +00:00
|
|
|
if (!(type->type == ev_pointer
|
|
|
|
&& (e_type->type == ev_pointer
|
|
|
|
|| e_type == &type_integer || e_type == &type_uinteger))
|
2003-03-10 21:23:05 +00:00
|
|
|
&& !(type->type == ev_func && e_type->type == ev_func)
|
2002-08-20 06:11:10 +00:00
|
|
|
&& !(((type == &type_integer || type == &type_uinteger)
|
2003-08-01 21:20:04 +00:00
|
|
|
&& (e_type == &type_float || e_type == &type_integer
|
2003-08-24 08:03:25 +00:00
|
|
|
|| e_type == &type_uinteger || e_type->type == ev_pointer))
|
2002-08-20 06:11:10 +00:00
|
|
|
|| (type == &type_float
|
|
|
|
&& (e_type == &type_integer || e_type == &type_uinteger)))) {
|
2004-01-25 08:55:03 +00:00
|
|
|
return error (e, "can not cast from %s to %s",
|
|
|
|
pr_type_name[extract_type (e)],
|
|
|
|
pr_type_name[type->type]);
|
2002-08-20 06:11:10 +00:00
|
|
|
}
|
|
|
|
if (e->type == ex_uexpr && e->e.expr.op == '.') {
|
|
|
|
e->e.expr.type = type;
|
|
|
|
c = e;
|
|
|
|
} else {
|
|
|
|
c = new_unary_expr ('C', e);
|
|
|
|
c->e.expr.type = type;
|
2002-01-17 19:32:04 +00:00
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
2002-01-18 08:26:37 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
init_elements (def_t *def, expr_t *eles)
|
|
|
|
{
|
2004-01-25 08:55:03 +00:00
|
|
|
expr_t *e, *c;
|
2003-04-07 17:44:18 +00:00
|
|
|
int count, i, num_params;
|
|
|
|
pr_type_t *g;
|
|
|
|
def_t *elements;
|
|
|
|
|
|
|
|
if (def->type->type == ev_array) {
|
|
|
|
elements = calloc (def->type->num_parms, sizeof (def_t));
|
|
|
|
for (i = 0; i < def->type->num_parms; i++) {
|
|
|
|
elements[i].type = def->type->aux_type;
|
|
|
|
elements[i].ofs = def->ofs + i * type_size (def->type->aux_type);
|
|
|
|
}
|
|
|
|
num_params = i;
|
|
|
|
} else if (def->type->type == ev_struct) {
|
|
|
|
struct_field_t *field;
|
|
|
|
|
2003-07-30 04:11:45 +00:00
|
|
|
for (i = 0, field = def->type->s.strct->struct_head; field;
|
2003-04-07 17:44:18 +00:00
|
|
|
i++, field = field->next)
|
|
|
|
;
|
|
|
|
elements = calloc (i, sizeof (def_t));
|
2003-07-30 04:11:45 +00:00
|
|
|
for (i = 0, field = def->type->s.strct->struct_head; field;
|
2003-04-07 17:44:18 +00:00
|
|
|
i++, field = field->next) {
|
|
|
|
elements[i].type = field->type;
|
|
|
|
elements[i].ofs = def->ofs + field->offset;
|
|
|
|
}
|
|
|
|
num_params = i;
|
|
|
|
} else {
|
|
|
|
error (eles, "invalid initializer");
|
|
|
|
return;
|
|
|
|
}
|
2002-01-18 08:26:37 +00:00
|
|
|
for (count = 0, e = eles->e.block.head; e; count++, e = e->next)
|
2003-04-07 18:30:47 +00:00
|
|
|
if (e->type == ex_error) {
|
|
|
|
free (elements);
|
2002-05-01 22:08:59 +00:00
|
|
|
return;
|
2003-04-07 18:30:47 +00:00
|
|
|
}
|
2003-07-30 04:11:45 +00:00
|
|
|
if (count > num_params) {
|
2002-01-18 08:26:37 +00:00
|
|
|
warning (eles, "excessive elements in initializer");
|
2003-07-30 04:11:45 +00:00
|
|
|
count = num_params;
|
2002-01-18 08:26:37 +00:00
|
|
|
}
|
|
|
|
for (i = 0, e = eles->e.block.head; i < count; i++, e = e->next) {
|
2003-04-07 17:44:18 +00:00
|
|
|
g = G_POINTER (pr_type_t, elements[i].ofs);
|
2004-01-25 21:36:44 +00:00
|
|
|
c = constant_expr (e);
|
2004-01-25 08:55:03 +00:00
|
|
|
if (c->type == ex_block) {
|
2003-04-07 17:44:18 +00:00
|
|
|
if (elements[i].type->type != ev_array
|
|
|
|
&& elements[i].type->type != ev_struct) {
|
|
|
|
error (e, "type mismatch in initializer");
|
|
|
|
continue;
|
|
|
|
}
|
2004-01-25 08:55:03 +00:00
|
|
|
init_elements (&elements[i], c);
|
|
|
|
} else if (c->type >= ex_string) {
|
|
|
|
if (c->type == ex_integer
|
2003-04-07 17:44:18 +00:00
|
|
|
&& elements[i].type->type == ev_float)
|
2004-01-25 08:55:03 +00:00
|
|
|
convert_int (c);
|
|
|
|
else if (c->type == ex_integer
|
2003-08-01 21:20:04 +00:00
|
|
|
&& elements[i].type->type == ev_uinteger)
|
2004-01-25 08:55:03 +00:00
|
|
|
convert_int_uint (c);
|
|
|
|
else if (c->type == ex_uinteger
|
2003-08-01 21:20:04 +00:00
|
|
|
&& elements[i].type->type == ev_float)
|
2004-01-25 08:55:03 +00:00
|
|
|
convert_uint (c);
|
|
|
|
else if (c->type == ex_uinteger
|
2003-08-01 21:20:04 +00:00
|
|
|
&& elements[i].type->type == ev_integer)
|
2004-01-25 08:55:03 +00:00
|
|
|
convert_uint_int (c);
|
|
|
|
if (get_type (c) != elements[i].type) {
|
2002-01-18 08:26:37 +00:00
|
|
|
error (e, "type mismatch in initializer");
|
2003-04-07 17:44:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-01-25 08:55:03 +00:00
|
|
|
if (c->type == ex_string) {
|
|
|
|
EMIT_STRING (g->string_var, c->e.string_val);
|
2002-01-18 08:26:37 +00:00
|
|
|
} else {
|
2004-01-25 08:55:03 +00:00
|
|
|
memcpy (g, &c->e, type_size (get_type (c)) * 4);
|
2002-01-18 08:26:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error (e, "non-constant initializer");
|
|
|
|
}
|
|
|
|
}
|
2003-04-07 18:30:47 +00:00
|
|
|
free (elements);
|
2002-01-18 08:26:37 +00:00
|
|
|
}
|
2002-05-08 23:12:49 +00:00
|
|
|
|
|
|
|
expr_t *
|
|
|
|
selector_expr (keywordarg_t *selector)
|
|
|
|
{
|
2002-05-09 20:12:28 +00:00
|
|
|
dstring_t *sel_id = dstring_newstr ();
|
|
|
|
dstring_t *sel_types = dstring_newstr ();
|
|
|
|
expr_t *sel;
|
2003-08-23 06:15:19 +00:00
|
|
|
def_t *sel_def;
|
|
|
|
int index;
|
2002-08-20 02:09:34 +00:00
|
|
|
|
|
|
|
selector = copy_keywordargs (selector);
|
2002-08-16 19:50:16 +00:00
|
|
|
selector = (keywordarg_t *) reverse_params ((param_t *) selector);
|
2002-05-09 20:12:28 +00:00
|
|
|
selector_name (sel_id, selector);
|
|
|
|
selector_types (sel_types, selector);
|
|
|
|
//printf ("'%s' '%s'\n", sel_id->str, sel_types->str);
|
2003-08-23 06:15:19 +00:00
|
|
|
index = selector_index (sel_id->str, sel_types->str);
|
|
|
|
index *= type_size (type_SEL.aux_type);
|
|
|
|
sel_def = get_def (type_SEL.aux_type, "_OBJ_SELECTOR_TABLE", pr.scope,
|
|
|
|
st_extern);
|
|
|
|
sel = new_def_expr (sel_def);
|
2002-05-09 20:12:28 +00:00
|
|
|
dstring_delete (sel_id);
|
|
|
|
dstring_delete (sel_types);
|
2003-08-23 06:15:19 +00:00
|
|
|
return address_expr (sel, new_short_expr (index), 0);
|
2002-05-08 23:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
protocol_expr (const char *protocol)
|
|
|
|
{
|
|
|
|
return error (0, "not implemented");
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
encode_expr (type_t *type)
|
|
|
|
{
|
2002-05-10 23:19:57 +00:00
|
|
|
dstring_t *encoding = dstring_newstr ();
|
2002-09-11 16:21:26 +00:00
|
|
|
expr_t *e;
|
2002-05-10 23:19:57 +00:00
|
|
|
|
|
|
|
encode_type (encoding, type);
|
2002-09-11 16:21:26 +00:00
|
|
|
e = new_string_expr (encoding->str);
|
2002-05-10 23:19:57 +00:00
|
|
|
free (encoding);
|
|
|
|
return e;
|
2002-05-08 23:12:49 +00:00
|
|
|
}
|
|
|
|
|
2003-05-15 05:58:31 +00:00
|
|
|
expr_t *
|
2003-08-18 06:11:41 +00:00
|
|
|
super_expr (class_type_t *class_type)
|
2003-05-15 05:58:31 +00:00
|
|
|
{
|
|
|
|
def_t *super_d;
|
|
|
|
expr_t *super;
|
|
|
|
expr_t *e;
|
|
|
|
expr_t *super_block;
|
2003-08-18 06:11:41 +00:00
|
|
|
class_t *class;
|
|
|
|
class_type_t _class_type;
|
2003-05-15 05:58:31 +00:00
|
|
|
|
2003-08-18 06:11:41 +00:00
|
|
|
if (!class_type)
|
2003-08-20 22:36:02 +00:00
|
|
|
return error (new_expr (),
|
|
|
|
"`super' used outside of class implementation");
|
2003-05-15 05:58:31 +00:00
|
|
|
|
2003-08-18 06:11:41 +00:00
|
|
|
if (class_type->is_class)
|
|
|
|
class = class_type->c.class;
|
|
|
|
else
|
|
|
|
class = class_type->c.category->class;
|
|
|
|
|
|
|
|
if (!class->super_class)
|
2003-08-20 22:36:02 +00:00
|
|
|
return error (new_expr (), "%s has no super class", class->name);
|
2003-05-15 05:58:31 +00:00
|
|
|
|
|
|
|
super_d = get_def (type_Super.aux_type, ".super", current_func->scope,
|
|
|
|
st_local);
|
|
|
|
def_initialized (super_d);
|
|
|
|
super = new_def_expr (super_d);
|
|
|
|
super_block = new_block_expr ();
|
|
|
|
|
|
|
|
e = assign_expr (binary_expr ('.', super, new_name_expr ("self")),
|
|
|
|
new_name_expr ("self"));
|
|
|
|
append_expr (super_block, e);
|
|
|
|
|
2003-08-18 06:11:41 +00:00
|
|
|
_class_type.is_class = 1;
|
|
|
|
_class_type.c.class = class;
|
|
|
|
e = new_def_expr (class_def (&_class_type, 1));
|
2003-05-15 05:58:31 +00:00
|
|
|
e = assign_expr (binary_expr ('.', super, new_name_expr ("class")),
|
|
|
|
binary_expr ('.', e, new_name_expr ("super_class")));
|
|
|
|
append_expr (super_block, e);
|
|
|
|
|
2004-02-04 05:49:54 +00:00
|
|
|
e = address_expr (super, 0, &type_void);
|
2003-05-15 05:58:31 +00:00
|
|
|
super_block->e.block.result = e;
|
|
|
|
return super_block;
|
|
|
|
}
|
|
|
|
|
2002-05-08 23:12:49 +00:00
|
|
|
expr_t *
|
|
|
|
message_expr (expr_t *receiver, keywordarg_t *message)
|
|
|
|
{
|
|
|
|
expr_t *args = 0, **a = &args;
|
|
|
|
expr_t *selector = selector_expr (message);
|
2002-05-16 20:20:23 +00:00
|
|
|
expr_t *call;
|
2002-05-08 23:12:49 +00:00
|
|
|
keywordarg_t *m;
|
2003-07-27 19:47:14 +00:00
|
|
|
int super = 0, class_msg = 0;
|
2002-05-16 20:20:23 +00:00
|
|
|
type_t *rec_type;
|
|
|
|
class_t *class;
|
|
|
|
method_t *method;
|
|
|
|
|
|
|
|
if (receiver->type == ex_name
|
|
|
|
&& strcmp (receiver->e.string_val, "super") == 0) {
|
|
|
|
super = 1;
|
2002-05-17 17:37:44 +00:00
|
|
|
|
2003-05-15 05:58:31 +00:00
|
|
|
receiver = super_expr (current_class);
|
2002-05-17 17:37:44 +00:00
|
|
|
|
2003-05-15 05:58:31 +00:00
|
|
|
if (receiver->type == ex_error)
|
|
|
|
return receiver;
|
|
|
|
if (current_class->is_class)
|
|
|
|
class = current_class->c.class;
|
|
|
|
else
|
|
|
|
class = current_class->c.category->class;
|
|
|
|
rec_type = class->type;
|
|
|
|
} else {
|
2003-07-27 19:47:14 +00:00
|
|
|
if (receiver->type == ex_name && get_class (receiver->e.string_val, 0))
|
|
|
|
class_msg = 1;
|
2003-05-15 05:58:31 +00:00
|
|
|
rec_type = get_type (receiver);
|
|
|
|
|
|
|
|
if (receiver->type == ex_error)
|
|
|
|
return receiver;
|
|
|
|
|
|
|
|
if (rec_type->type != ev_pointer
|
|
|
|
|| (rec_type->aux_type->type != ev_object
|
|
|
|
&& rec_type->aux_type->type != ev_class))
|
|
|
|
return error (receiver, "not a class/object");
|
2003-07-30 04:11:45 +00:00
|
|
|
class = rec_type->aux_type->s.class;
|
2003-05-15 05:58:31 +00:00
|
|
|
}
|
2003-07-29 17:38:29 +00:00
|
|
|
|
|
|
|
method = class_message_response (class, class_msg, selector);
|
|
|
|
if (method)
|
|
|
|
rec_type = method->type->aux_type;
|
|
|
|
|
2002-05-08 23:12:49 +00:00
|
|
|
for (m = message; m; m = m->next) {
|
|
|
|
*a = m->expr;
|
2002-05-09 20:12:28 +00:00
|
|
|
while ((*a))
|
2002-05-08 23:12:49 +00:00
|
|
|
a = &(*a)->next;
|
|
|
|
}
|
|
|
|
*a = selector;
|
|
|
|
a = &(*a)->next;
|
|
|
|
*a = receiver;
|
2003-08-22 04:08:33 +00:00
|
|
|
|
|
|
|
if (method) {
|
2003-08-22 05:26:47 +00:00
|
|
|
expr_t *err;
|
|
|
|
if ((err = method_check_params (method, args)))
|
2003-08-22 04:08:33 +00:00
|
|
|
return err;
|
2004-02-04 06:31:12 +00:00
|
|
|
call = build_function_call (send_message (super), method->type, args);
|
|
|
|
} else {
|
|
|
|
call = build_function_call (send_message (super), &type_IMP, args);
|
2003-08-22 04:08:33 +00:00
|
|
|
}
|
2002-05-17 17:37:44 +00:00
|
|
|
|
|
|
|
if (call->type == ex_error)
|
|
|
|
return receiver;
|
|
|
|
|
2003-04-22 15:29:32 +00:00
|
|
|
call->e.block.result = new_ret_expr (rec_type);
|
2002-05-16 20:20:23 +00:00
|
|
|
return call;
|
2002-05-08 23:12:49 +00:00
|
|
|
}
|
2002-08-18 04:08:02 +00:00
|
|
|
|
|
|
|
expr_t *
|
|
|
|
sizeof_expr (expr_t *expr, struct type_s *type)
|
|
|
|
{
|
2002-10-31 22:58:59 +00:00
|
|
|
if (!((!expr) ^ (!type))) {
|
2002-08-18 04:08:02 +00:00
|
|
|
error (0, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
if (!type)
|
|
|
|
type = get_type (expr);
|
2002-09-11 16:21:26 +00:00
|
|
|
expr = new_integer_expr (type_size (type));
|
2002-08-18 04:08:02 +00:00
|
|
|
return expr;
|
|
|
|
}
|
2003-05-07 02:45:17 +00:00
|
|
|
|
2003-10-25 04:35:47 +00:00
|
|
|
static void
|
|
|
|
report_function (expr_t *e)
|
|
|
|
{
|
|
|
|
static function_t *last_func = (function_t *)-1L;
|
|
|
|
string_t file = pr.source_file;
|
|
|
|
|
|
|
|
if (e)
|
|
|
|
file = e->file;
|
|
|
|
|
|
|
|
if (current_func != last_func) {
|
|
|
|
if (current_func) {
|
|
|
|
fprintf (stderr, "%s: In function `%s':\n", G_GETSTR (file),
|
|
|
|
current_func->def->name);
|
|
|
|
} else {
|
|
|
|
fprintf (stderr, "%s: At top level:\n", G_GETSTR (file));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_func = current_func;
|
|
|
|
}
|
|
|
|
|
2003-05-07 02:45:17 +00:00
|
|
|
static void
|
|
|
|
_warning (expr_t *e, const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
string_t file = pr.source_file;
|
|
|
|
int line = pr.source_line;
|
|
|
|
|
2003-10-25 04:35:47 +00:00
|
|
|
report_function (e);
|
2003-05-07 02:45:17 +00:00
|
|
|
if (options.warnings.promote) {
|
|
|
|
options.warnings.promote = 0; // only want to do this once
|
|
|
|
fprintf (stderr, "%s: warnings treated as errors\n", "qfcc");
|
|
|
|
pr.error_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e) {
|
|
|
|
file = e->file;
|
|
|
|
line = e->line;
|
|
|
|
}
|
|
|
|
fprintf (stderr, "%s:%d: warning: ", G_GETSTR (file), line);
|
|
|
|
vfprintf (stderr, fmt, args);
|
|
|
|
fputs ("\n", stderr);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
notice (expr_t *e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
if (options.notices.silent)
|
|
|
|
return;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
if (options.notices.promote) {
|
|
|
|
_warning (e, fmt, args);
|
|
|
|
} else {
|
|
|
|
string_t file = pr.source_file;
|
|
|
|
int line = pr.source_line;
|
|
|
|
|
2003-10-25 04:35:47 +00:00
|
|
|
report_function (e);
|
2003-05-07 02:45:17 +00:00
|
|
|
if (e) {
|
|
|
|
file = e->file;
|
|
|
|
line = e->line;
|
|
|
|
}
|
|
|
|
fprintf (stderr, "%s:%d: notice: ", G_GETSTR (file), line);
|
|
|
|
vfprintf (stderr, fmt, args);
|
|
|
|
fputs ("\n", stderr);
|
|
|
|
}
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
warning (expr_t *e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start (args, fmt);
|
|
|
|
_warning (e, fmt, args);
|
|
|
|
va_end (args);
|
|
|
|
}
|
|
|
|
|
|
|
|
expr_t *
|
|
|
|
error (expr_t *e, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
string_t file = pr.source_file;
|
|
|
|
int line = pr.source_line;
|
|
|
|
|
2003-10-25 04:35:47 +00:00
|
|
|
report_function (e);
|
2003-05-07 02:45:17 +00:00
|
|
|
va_start (args, fmt);
|
|
|
|
if (e) {
|
|
|
|
file = e->file;
|
|
|
|
line = e->line;
|
|
|
|
}
|
|
|
|
fprintf (stderr, "%s:%d: ", G_GETSTR (file), line);
|
|
|
|
vfprintf (stderr, fmt, args);
|
|
|
|
fputs ("\n", stderr);
|
|
|
|
va_end (args);
|
|
|
|
pr.error_count++;
|
|
|
|
|
|
|
|
if (e) {
|
|
|
|
e->type = ex_error;
|
|
|
|
}
|
|
|
|
return e;
|
|
|
|
}
|