2001-09-28 07:09:38 +00:00
|
|
|
/*
|
|
|
|
#FILENAME#
|
|
|
|
|
|
|
|
#DESCRIPTION#
|
|
|
|
|
|
|
|
Copyright (C) 2001 #AUTHOR#
|
|
|
|
|
|
|
|
Author: #AUTHOR#
|
|
|
|
Date: #DATE#
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
2001-12-06 19:49:40 +00:00
|
|
|
static const char rcsid[] =
|
2001-09-28 07:09:38 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <QF/mathlib.h>
|
|
|
|
#include <QF/va.h>
|
|
|
|
|
|
|
|
#include "qfcc.h"
|
|
|
|
#include "qc-parse.h"
|
|
|
|
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *emit_sub_expr (expr_t *e, def_t *dest);
|
2001-08-22 21:55:01 +00:00
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
def_t *
|
2001-12-06 19:49:40 +00:00
|
|
|
emit_statement (int sline, opcode_t *op, def_t *var_a, def_t *var_b,
|
|
|
|
def_t *var_c)
|
2001-07-26 05:15:34 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
dstatement_t *statement;
|
|
|
|
def_t *ret;
|
2001-07-26 05:15:34 +00:00
|
|
|
|
|
|
|
if (!op) {
|
2001-12-06 19:49:40 +00:00
|
|
|
expr_t e;
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
e.line = sline;
|
|
|
|
e.file = s_file;
|
|
|
|
error (&e, "ice ice baby\n");
|
|
|
|
abort ();
|
|
|
|
}
|
2001-10-26 06:43:56 +00:00
|
|
|
if (options.code.debug) {
|
2001-12-06 19:49:40 +00:00
|
|
|
int line = sline - lineno_base;
|
2001-07-26 05:15:34 +00:00
|
|
|
|
|
|
|
if (line != linenos[num_linenos - 1].line) {
|
2001-12-06 19:49:40 +00:00
|
|
|
pr_lineno_t *lineno = new_lineno ();
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
lineno->line = line;
|
|
|
|
lineno->fa.addr = numstatements;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
statement = &statements[numstatements];
|
|
|
|
numstatements++;
|
|
|
|
statement_linenums[statement - statements] = pr_source_line;
|
|
|
|
statement->op = op->opcode;
|
|
|
|
statement->a = var_a ? var_a->ofs : 0;
|
|
|
|
statement->b = var_b ? var_b->ofs : 0;
|
|
|
|
if (op->type_c == ev_void || op->right_associative) {
|
|
|
|
// ifs, gotos, and assignments don't need vars allocated
|
2001-11-15 04:32:50 +00:00
|
|
|
if (op->type_c == ev_void) {
|
|
|
|
var_c = NULL;
|
|
|
|
statement->c = 0;
|
2001-12-08 08:19:48 +00:00
|
|
|
} else {
|
|
|
|
statement->c = var_c->ofs;
|
2001-11-15 04:32:50 +00:00
|
|
|
}
|
2001-07-26 05:15:34 +00:00
|
|
|
ret = var_a;
|
2001-12-06 19:49:40 +00:00
|
|
|
} else { // allocate result space
|
2001-08-22 21:55:01 +00:00
|
|
|
if (!var_c) {
|
2001-07-26 05:15:34 +00:00
|
|
|
var_c = PR_GetTempDef (types[op->type_c], pr_scope);
|
2001-08-22 21:55:01 +00:00
|
|
|
var_c->users += 2;
|
|
|
|
}
|
2001-07-26 05:15:34 +00:00
|
|
|
statement->c = var_c->ofs;
|
|
|
|
ret = var_c;
|
|
|
|
}
|
2001-12-12 08:39:47 +00:00
|
|
|
#if 0
|
|
|
|
printf ("%s %s(%d) %s(%d) %s(%d)\n", op->opname,
|
|
|
|
var_a ? var_a->name : "", statement->a,
|
|
|
|
var_b ? var_b->name : "", statement->b,
|
|
|
|
var_c ? var_c->name : "", statement->c);
|
|
|
|
#endif
|
2001-10-31 18:40:02 +00:00
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
PR_AddStatementRef (var_a, statement, 0);
|
|
|
|
PR_AddStatementRef (var_b, statement, 1);
|
|
|
|
PR_AddStatementRef (var_c, statement, 2);
|
|
|
|
|
|
|
|
if (op->right_associative)
|
|
|
|
return var_a;
|
|
|
|
return var_c;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
emit_branch (int line, opcode_t *op, expr_t *e, expr_t *l)
|
|
|
|
{
|
|
|
|
dstatement_t *st;
|
2001-12-06 19:49:40 +00:00
|
|
|
statref_t *ref;
|
|
|
|
def_t *def = 0;
|
2001-07-26 05:15:34 +00:00
|
|
|
|
|
|
|
if (e)
|
|
|
|
def = emit_sub_expr (e, 0);
|
|
|
|
st = &statements[numstatements];
|
|
|
|
emit_statement (line, op, def, 0, 0);
|
|
|
|
if (l->e.label.statement) {
|
|
|
|
if (op == op_goto)
|
|
|
|
st->a = l->e.label.statement - st;
|
|
|
|
else
|
|
|
|
st->b = l->e.label.statement - st;
|
|
|
|
} else {
|
|
|
|
ref = PR_NewStatref (st, op != op_goto);
|
|
|
|
ref->next = l->e.label.refs;
|
|
|
|
l->e.label.refs = ref;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
def_t *
|
|
|
|
emit_function_call (expr_t *e, def_t *dest)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *func = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
def_t parm;
|
|
|
|
def_t *arg;
|
|
|
|
expr_t *earg;
|
|
|
|
opcode_t *op;
|
|
|
|
int count = 0, ind;
|
2001-07-26 05:15:34 +00:00
|
|
|
|
|
|
|
for (earg = e->e.expr.e2; earg; earg = earg->next)
|
|
|
|
count++;
|
|
|
|
ind = count;
|
|
|
|
for (earg = e->e.expr.e2; earg; earg = earg->next) {
|
|
|
|
ind--;
|
2001-08-20 04:56:00 +00:00
|
|
|
parm = def_parms[ind];
|
2001-10-02 18:55:52 +00:00
|
|
|
parm.type = types[extract_type (earg)];
|
2001-08-20 04:56:00 +00:00
|
|
|
arg = emit_sub_expr (earg, &parm);
|
2001-08-09 17:05:30 +00:00
|
|
|
if (earg->type != ex_expr && earg->type != ex_uexpr) {
|
2001-10-25 17:48:35 +00:00
|
|
|
op = PR_Opcode_Find ("=", arg, &parm, &def_void);
|
2001-08-20 04:56:00 +00:00
|
|
|
emit_statement (e->line, op, arg, &parm, 0);
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
2001-12-06 18:14:44 +00:00
|
|
|
op = PR_Opcode_Find (va ("<CALL%d>", count), &def_function, &def_void,
|
|
|
|
&def_void);
|
2001-07-26 05:15:34 +00:00
|
|
|
emit_statement (e->line, op, func, 0, 0);
|
|
|
|
|
|
|
|
def_ret.type = func->type->aux_type;
|
2001-08-20 04:56:00 +00:00
|
|
|
if (dest) {
|
2001-10-25 17:48:35 +00:00
|
|
|
op = PR_Opcode_Find ("=", dest, &def_ret, &def_void);
|
2001-07-26 05:15:34 +00:00
|
|
|
emit_statement (e->line, op, &def_ret, dest, 0);
|
|
|
|
return dest;
|
2001-08-20 04:56:00 +00:00
|
|
|
} else {
|
2001-07-26 05:15:34 +00:00
|
|
|
return &def_ret;
|
2001-08-20 04:56:00 +00:00
|
|
|
}
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
def_t *
|
2001-12-12 20:35:58 +00:00
|
|
|
emit_assign_expr (int oper, expr_t *e)
|
2001-07-26 05:15:34 +00:00
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def_a, *def_b, *def_c;
|
|
|
|
opcode_t *op;
|
|
|
|
expr_t *e1 = e->e.expr.e1;
|
|
|
|
expr_t *e2 = e->e.expr.e2;
|
2001-12-12 20:35:58 +00:00
|
|
|
const char *operator = get_op_string (oper);
|
2001-07-26 05:15:34 +00:00
|
|
|
|
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 (e1->type == ex_temp && e1->e.temp.users < 2) {
|
|
|
|
e1->e.temp.users--;
|
|
|
|
return 0;
|
|
|
|
}
|
2001-12-12 20:35:58 +00:00
|
|
|
if (oper == '=') {
|
2001-11-15 04:32:50 +00:00
|
|
|
def_a = emit_sub_expr (e1, 0);
|
2001-12-12 20:35:58 +00:00
|
|
|
if (def_a->constant) {
|
|
|
|
if (options.code.cow) {
|
|
|
|
int size = pr_type_size[def_a->type->type];
|
|
|
|
int ofs = PR_NewLocation (def_a->type);
|
|
|
|
|
|
|
|
memcpy (pr_globals + ofs, pr_globals + def_a->ofs, size);
|
|
|
|
def_a->ofs = ofs;
|
|
|
|
def_a->constant = 0;
|
|
|
|
if (options.warnings.cow)
|
|
|
|
warning (e1, "assignment to constant %s (Moooooooo!)",
|
|
|
|
def_a->name);
|
2001-12-12 08:39:47 +00:00
|
|
|
} else {
|
2002-02-18 06:23:59 +00:00
|
|
|
if (options.traditional)
|
|
|
|
warning (e1, "assignment to constant %s", def_a->name);
|
|
|
|
else
|
|
|
|
error (e1, "assignment to constant %s", def_a->name);
|
2001-12-12 08:39:47 +00:00
|
|
|
}
|
2001-12-12 20:35:58 +00:00
|
|
|
}
|
|
|
|
def_b = emit_sub_expr (e2, def_a);
|
|
|
|
if (def_b != def_a) {
|
|
|
|
op = PR_Opcode_Find (operator, def_b, def_a, &def_void);
|
|
|
|
emit_statement (e->line, op, def_b, def_a, 0);
|
|
|
|
}
|
2001-12-12 20:55:55 +00:00
|
|
|
return def_a;
|
2001-12-12 20:35:58 +00:00
|
|
|
} else {
|
|
|
|
def_b = emit_sub_expr (e2, 0);
|
2002-02-21 20:34:04 +00:00
|
|
|
if (e->rvalue && def_b->managed)
|
|
|
|
def_b->users++;
|
2001-12-12 20:35:58 +00:00
|
|
|
if (e1->type == ex_expr && extract_type (e1->e.expr.e1) == ev_pointer) {
|
|
|
|
def_a = emit_sub_expr (e1->e.expr.e1, 0);
|
|
|
|
def_c = emit_sub_expr (e1->e.expr.e2, 0);
|
|
|
|
op = PR_Opcode_Find (operator, def_b, def_a, def_c);
|
2001-11-15 04:32:50 +00:00
|
|
|
} else {
|
2001-12-12 20:35:58 +00:00
|
|
|
def_a = emit_sub_expr (e1, 0);
|
|
|
|
def_c = 0;
|
|
|
|
op = PR_Opcode_Find (operator, def_b, def_a, &def_void);
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
2001-12-12 20:35:58 +00:00
|
|
|
emit_statement (e->line, op, def_b, def_a, def_c);
|
2001-12-12 20:55:55 +00:00
|
|
|
return def_b;
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-11-13 18:11:19 +00:00
|
|
|
def_t *
|
|
|
|
emit_bind_expr (expr_t *e1, expr_t *e2)
|
|
|
|
{
|
|
|
|
type_t *t1 = get_type (e1);
|
|
|
|
type_t *t2 = get_type (e2);
|
|
|
|
def_t *def;
|
|
|
|
|
|
|
|
if (!e2 || e2->type != ex_temp) {
|
|
|
|
error (e1, "internal error");
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
def = emit_sub_expr (e1, e2->e.temp.def);
|
|
|
|
if (t1 != t2) {
|
|
|
|
def_t *tmp = PR_NewDef (t2, 0, def->scope);
|
2001-12-06 19:49:40 +00:00
|
|
|
|
2001-11-13 18:11:19 +00:00
|
|
|
tmp->ofs = def->ofs;
|
|
|
|
tmp->users = e2->e.temp.users;
|
2001-12-06 19:49:40 +00:00
|
|
|
tmp->freed = 1; // don't free this offset when freeing def
|
2001-11-13 18:11:19 +00:00
|
|
|
def = tmp;
|
|
|
|
}
|
|
|
|
e2->e.temp.def = def;
|
|
|
|
return e2->e.temp.def;
|
|
|
|
}
|
|
|
|
|
2001-07-26 05:15:34 +00:00
|
|
|
def_t *
|
|
|
|
emit_sub_expr (expr_t *e, def_t *dest)
|
|
|
|
{
|
2001-08-22 21:55:01 +00:00
|
|
|
opcode_t *op;
|
2001-10-25 17:48:35 +00:00
|
|
|
const char *operator;
|
2001-08-22 21:55:01 +00:00
|
|
|
def_t *def_a, *def_b, *d = 0;
|
2002-01-17 19:32:04 +00:00
|
|
|
static expr_t zero;
|
|
|
|
def_t *tmp = 0;
|
2001-07-26 05:15:34 +00:00
|
|
|
|
|
|
|
switch (e->type) {
|
|
|
|
case ex_block:
|
2001-08-11 21:15:24 +00:00
|
|
|
if (e->e.block.result) {
|
2001-08-22 21:55:01 +00:00
|
|
|
d = emit_sub_expr (e->e.block.result, dest);
|
2001-08-11 21:15:24 +00:00
|
|
|
for (e = e->e.block.head; e; e = e->next)
|
|
|
|
emit_expr (e);
|
2001-08-22 21:55:01 +00:00
|
|
|
break;
|
2001-08-11 21:15:24 +00:00
|
|
|
}
|
2001-12-08 08:19:48 +00:00
|
|
|
case ex_name:
|
2001-10-17 18:36:19 +00:00
|
|
|
case ex_nil:
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_label:
|
2001-07-26 05:15:34 +00:00
|
|
|
error (e, "internal error");
|
|
|
|
abort ();
|
|
|
|
case ex_expr:
|
2001-08-22 21:55:01 +00:00
|
|
|
if (e->e.expr.op == 'c') {
|
|
|
|
d = emit_function_call (e, dest);
|
|
|
|
break;
|
|
|
|
}
|
2001-12-12 20:35:58 +00:00
|
|
|
if (e->e.expr.op == '=' || e->e.expr.op == PAS) {
|
|
|
|
d = emit_assign_expr (e->e.expr.op, e);
|
2001-12-12 21:29:04 +00:00
|
|
|
if (!d->managed)
|
|
|
|
d->users++;
|
2001-08-22 21:55:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-08-24 21:47:52 +00:00
|
|
|
if (e->e.expr.e1->type == ex_block
|
|
|
|
&& e->e.expr.e1->e.block.is_call) {
|
|
|
|
def_b = emit_sub_expr (e->e.expr.e2, 0);
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
} else {
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
def_b = emit_sub_expr (e->e.expr.e2, 0);
|
|
|
|
}
|
2001-10-25 17:48:35 +00:00
|
|
|
operator = get_op_string (e->e.expr.op);
|
2001-08-22 21:55:01 +00:00
|
|
|
if (!dest) {
|
2001-07-26 05:15:34 +00:00
|
|
|
dest = PR_GetTempDef (e->e.expr.type, pr_scope);
|
2001-08-22 21:55:01 +00:00
|
|
|
dest->users += 2;
|
|
|
|
}
|
2001-10-25 17:48:35 +00:00
|
|
|
op = PR_Opcode_Find (operator, def_a, def_b, dest);
|
2001-08-22 21:55:01 +00:00
|
|
|
d = emit_statement (e->line, op, def_a, def_b, dest);
|
|
|
|
break;
|
2001-07-26 05:15:34 +00:00
|
|
|
case ex_uexpr:
|
2002-01-17 19:32:04 +00:00
|
|
|
switch (e->e.expr.op) {
|
|
|
|
case '!':
|
|
|
|
operator = "!";
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, 0);
|
2001-12-12 08:39:47 +00:00
|
|
|
def_b = &def_void;
|
2002-01-17 19:32:04 +00:00
|
|
|
break;
|
|
|
|
case '~':
|
|
|
|
operator = "~";
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
def_b = &def_void;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
zero.type = expr_types[extract_type (e->e.expr.e1)];
|
|
|
|
|
|
|
|
operator = "-";
|
|
|
|
def_a = PR_ReuseConstant (&zero, 0);
|
|
|
|
def_b = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
if (!dest) {
|
|
|
|
dest = PR_GetTempDef (e->e.expr.type, pr_scope);
|
|
|
|
dest->users += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '&':
|
|
|
|
zero.type = ex_short;
|
|
|
|
|
|
|
|
operator = "&";
|
|
|
|
if (e->e.expr.e1->type == ex_expr
|
|
|
|
&& e->e.expr.e1->e.expr.op == '.') {
|
|
|
|
tmp = PR_GetTempDef (e->e.expr.type, pr_scope);
|
|
|
|
tmp->users += 2;
|
|
|
|
def_b = emit_sub_expr (&zero, 0);
|
|
|
|
} else {
|
|
|
|
def_b = &def_void;
|
|
|
|
}
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, tmp);
|
|
|
|
if (!dest) {
|
|
|
|
dest = PR_GetTempDef (e->e.expr.type, pr_scope);
|
|
|
|
dest->users += 2;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
if (!dest
|
|
|
|
&& (e->e.expr.e1->type != ex_pointer
|
|
|
|
|| !(e->e.expr.e1->e.pointer.val > 0
|
|
|
|
&& e->e.expr.e1->e.pointer.val < 65536))) {
|
|
|
|
dest = PR_GetTempDef (e->e.expr.type, pr_scope);
|
|
|
|
dest->users += 2;
|
|
|
|
}
|
|
|
|
if (e->e.expr.e1->type == ex_expr
|
|
|
|
&& e->e.expr.e1->e.expr.op == '&')
|
|
|
|
e->e.expr.e1->e.expr.op = '.';
|
|
|
|
d = emit_sub_expr (e->e.expr.e1, dest);
|
|
|
|
if (!d->name)
|
|
|
|
d->type = e->e.expr.type;
|
|
|
|
return d;
|
|
|
|
case 'C':
|
|
|
|
if (!dest) {
|
|
|
|
dest = PR_GetTempDef (e->e.expr.type, pr_scope);
|
|
|
|
dest->users = 2;
|
|
|
|
}
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
def_b = &def_void;
|
|
|
|
operator = "=";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
abort ();
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
2001-10-25 17:48:35 +00:00
|
|
|
op = PR_Opcode_Find (operator, def_a, def_b, dest);
|
2001-08-22 21:55:01 +00:00
|
|
|
d = emit_statement (e->line, op, def_a, def_b, dest);
|
|
|
|
break;
|
2001-07-26 05:15:34 +00:00
|
|
|
case ex_def:
|
2001-08-22 21:55:01 +00:00
|
|
|
d = e->e.def;
|
|
|
|
break;
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_temp:
|
2001-08-12 02:38:12 +00:00
|
|
|
if (!e->e.temp.def) {
|
|
|
|
if (dest)
|
|
|
|
e->e.temp.def = dest;
|
|
|
|
else
|
|
|
|
e->e.temp.def = PR_GetTempDef (e->e.temp.type, pr_scope);
|
2001-08-22 21:55:01 +00:00
|
|
|
e->e.temp.def->users = e->e.temp.users;
|
|
|
|
e->e.temp.def->expr = e;
|
2001-12-12 21:29:04 +00:00
|
|
|
e->e.temp.def->managed = 1;
|
2001-08-12 02:38:12 +00:00
|
|
|
}
|
2001-08-22 21:55:01 +00:00
|
|
|
d = e->e.temp.def;
|
|
|
|
break;
|
2001-12-12 08:39:47 +00:00
|
|
|
case ex_pointer:
|
|
|
|
if (e->e.pointer.val > 0 && e->e.pointer.val < 65536
|
|
|
|
&& e->e.pointer.type->type != ev_struct) {
|
|
|
|
d = PR_NewDef (e->e.pointer.type, 0, pr_scope);
|
|
|
|
d->ofs = e->e.short_val;
|
|
|
|
d->absolute = e->e.pointer.abs;
|
|
|
|
d->users = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// fall through
|
2001-07-26 05:15:34 +00:00
|
|
|
case ex_string:
|
|
|
|
case ex_float:
|
|
|
|
case ex_vector:
|
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_quaternion:
|
|
|
|
case ex_integer:
|
2001-11-13 08:58:54 +00:00
|
|
|
case ex_uinteger:
|
2001-08-22 21:55:01 +00:00
|
|
|
d = PR_ReuseConstant (e, 0);
|
|
|
|
break;
|
2001-12-07 20:10:30 +00:00
|
|
|
case ex_short:
|
|
|
|
d = PR_NewDef (&type_short, 0, pr_scope);
|
|
|
|
d->ofs = e->e.short_val;
|
|
|
|
d->absolute = 1;
|
2001-12-08 08:19:48 +00:00
|
|
|
d->users = 1;
|
2001-12-07 20:10:30 +00:00
|
|
|
break;
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
2001-08-22 21:55:01 +00:00
|
|
|
PR_FreeTempDefs ();
|
|
|
|
return d;
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
emit_expr (expr_t *e)
|
|
|
|
{
|
2001-12-06 19:49:40 +00:00
|
|
|
def_t *def;
|
|
|
|
def_t *def_a;
|
|
|
|
def_t *def_b;
|
|
|
|
statref_t *ref;
|
|
|
|
elabel_t *label;
|
2001-07-26 05:15:34 +00:00
|
|
|
|
2002-02-21 20:34:04 +00:00
|
|
|
//printf ("%d ", e->line);
|
|
|
|
//print_expr (e);
|
|
|
|
//puts ("");
|
2001-07-26 05:15:34 +00:00
|
|
|
switch (e->type) {
|
|
|
|
case ex_label:
|
|
|
|
label = &e->e.label;
|
|
|
|
label->statement = &statements[numstatements];
|
|
|
|
for (ref = label->refs; ref; ref = ref->next) {
|
|
|
|
switch (ref->field) {
|
|
|
|
case 0:
|
|
|
|
ref->statement->a = label->statement - ref->statement;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
ref->statement->b = label->statement - ref->statement;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ref->statement->c = label->statement - ref->statement;
|
|
|
|
break;
|
2001-11-13 08:58:54 +00:00
|
|
|
case 3:
|
2001-12-06 19:49:40 +00:00
|
|
|
*(int *) ref->statement = label->statement - statements;
|
2001-11-13 08:58:54 +00:00
|
|
|
break;
|
2001-07-26 05:15:34 +00:00
|
|
|
default:
|
2001-12-06 19:49:40 +00:00
|
|
|
abort ();
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ex_block:
|
|
|
|
for (e = e->e.block.head; e; e = e->next)
|
|
|
|
emit_expr (e);
|
|
|
|
break;
|
|
|
|
case ex_expr:
|
|
|
|
switch (e->e.expr.op) {
|
2001-12-12 08:39:47 +00:00
|
|
|
case PAS:
|
2001-07-26 05:15:34 +00:00
|
|
|
case '=':
|
2001-12-12 20:35:58 +00:00
|
|
|
emit_assign_expr (e->e.expr.op, e);
|
2001-07-26 05:15:34 +00:00
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
emit_branch (e->line, op_ifnot, e->e.expr.e1, e->e.expr.e2);
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
emit_branch (e->line, op_if, e->e.expr.e1, e->e.expr.e2);
|
|
|
|
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 IFBE:
|
|
|
|
emit_branch (e->line, op_ifbe, e->e.expr.e1, e->e.expr.e2);
|
|
|
|
break;
|
|
|
|
case IFB:
|
|
|
|
emit_branch (e->line, op_ifb, e->e.expr.e1, e->e.expr.e2);
|
|
|
|
break;
|
|
|
|
case IFAE:
|
|
|
|
emit_branch (e->line, op_ifae, e->e.expr.e1, e->e.expr.e2);
|
|
|
|
break;
|
|
|
|
case IFA:
|
|
|
|
emit_branch (e->line, op_ifa, e->e.expr.e1, e->e.expr.e2);
|
|
|
|
break;
|
2001-07-26 05:15:34 +00:00
|
|
|
case 'c':
|
|
|
|
emit_function_call (e, 0);
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
def_b = emit_sub_expr (e->e.expr.e2, 0);
|
|
|
|
emit_statement (e->line, op_state, def_a, def_b, 0);
|
|
|
|
break;
|
2001-08-24 21:14:04 +00:00
|
|
|
case 'b':
|
2001-11-13 18:11:19 +00:00
|
|
|
emit_bind_expr (e->e.expr.e1, e->e.expr.e2);
|
2001-08-24 21:14:04 +00:00
|
|
|
break;
|
2001-11-13 08:58:54 +00:00
|
|
|
case 'g':
|
|
|
|
def_a = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
def_b = emit_sub_expr (e->e.expr.e2, 0);
|
|
|
|
emit_statement (e->line, op_jumpb, def_a, def_b, 0);
|
|
|
|
break;
|
2001-07-26 05:15:34 +00:00
|
|
|
default:
|
|
|
|
warning (e, "Ignoring useless expression");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ex_uexpr:
|
|
|
|
switch (e->e.expr.op) {
|
|
|
|
case 'r':
|
|
|
|
def = 0;
|
|
|
|
if (e->e.expr.e1)
|
|
|
|
def = emit_sub_expr (e->e.expr.e1, 0);
|
|
|
|
emit_statement (e->line, op_return, def, 0, 0);
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
emit_branch (e->line, op_goto, 0, e->e.expr.e1);
|
|
|
|
break;
|
|
|
|
default:
|
2001-08-20 04:56:00 +00:00
|
|
|
warning (e, "useless expression");
|
2001-07-26 05:15:34 +00:00
|
|
|
emit_expr (e->e.expr.e1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ex_def:
|
2001-08-11 21:15:24 +00:00
|
|
|
case ex_temp:
|
2001-07-26 05:15:34 +00:00
|
|
|
case ex_string:
|
|
|
|
case ex_float:
|
|
|
|
case ex_vector:
|
|
|
|
case ex_entity:
|
|
|
|
case ex_field:
|
|
|
|
case ex_func:
|
|
|
|
case ex_pointer:
|
|
|
|
case ex_quaternion:
|
|
|
|
case ex_integer:
|
2001-11-13 08:58:54 +00:00
|
|
|
case ex_uinteger:
|
2001-12-07 20:10:30 +00:00
|
|
|
case ex_short:
|
2002-02-21 20:34:04 +00:00
|
|
|
case ex_name:
|
|
|
|
case ex_nil:
|
2001-07-26 05:15:34 +00:00
|
|
|
warning (e, "Ignoring useless expression");
|
|
|
|
break;
|
|
|
|
}
|
2001-08-22 21:55:01 +00:00
|
|
|
PR_FreeTempDefs ();
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|