2011-01-19 06:47:45 +00:00
|
|
|
/*
|
|
|
|
statements.c
|
|
|
|
|
|
|
|
Internal statements
|
|
|
|
|
|
|
|
Copyright (C) 2011 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2011/06/18
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-12-06 11:52:53 +00:00
|
|
|
#include "QF/alloc.h"
|
2011-01-19 06:47:45 +00:00
|
|
|
#include "QF/va.h"
|
|
|
|
|
2012-07-15 12:10:51 +00:00
|
|
|
#include "dags.h"
|
2011-01-24 12:54:57 +00:00
|
|
|
#include "diagnostic.h"
|
2012-11-07 02:00:08 +00:00
|
|
|
#include "dot.h"
|
2011-01-19 06:47:45 +00:00
|
|
|
#include "expr.h"
|
2011-01-28 02:45:19 +00:00
|
|
|
#include "function.h"
|
2011-01-19 06:47:45 +00:00
|
|
|
#include "options.h"
|
|
|
|
#include "qfcc.h"
|
2011-01-25 06:45:31 +00:00
|
|
|
#include "reloc.h"
|
2011-01-19 06:47:45 +00:00
|
|
|
#include "statements.h"
|
2011-01-24 12:31:32 +00:00
|
|
|
#include "strpool.h"
|
2011-01-19 06:47:45 +00:00
|
|
|
#include "symtab.h"
|
|
|
|
#include "type.h"
|
2012-12-13 03:49:00 +00:00
|
|
|
#include "value.h"
|
2011-02-01 12:15:51 +00:00
|
|
|
#include "qc-parse.h"
|
2011-01-19 06:47:45 +00:00
|
|
|
|
2012-12-05 10:47:22 +00:00
|
|
|
static const char *op_type_names[] = {
|
|
|
|
"op_def",
|
|
|
|
"op_value",
|
|
|
|
"op_label",
|
|
|
|
"op_temp",
|
|
|
|
};
|
|
|
|
|
2012-05-08 02:43:29 +00:00
|
|
|
const char *
|
2012-12-05 10:47:22 +00:00
|
|
|
optype_str (op_type_e type)
|
2012-05-08 02:43:29 +00:00
|
|
|
{
|
2012-12-05 10:47:22 +00:00
|
|
|
if (type < 0 || type > op_temp)
|
|
|
|
return "<invalid op_type>";
|
|
|
|
return op_type_names[type];
|
|
|
|
}
|
2012-05-08 02:43:29 +00:00
|
|
|
|
2012-12-05 10:47:22 +00:00
|
|
|
const char *
|
|
|
|
operand_string (operand_t *op)
|
|
|
|
{
|
2012-05-08 02:43:29 +00:00
|
|
|
if (!op)
|
|
|
|
return "";
|
|
|
|
switch (op->op_type) {
|
2012-12-05 10:47:22 +00:00
|
|
|
case op_def:
|
|
|
|
return op->o.def->name;
|
2012-05-08 02:43:29 +00:00
|
|
|
case op_value:
|
|
|
|
switch (op->o.value->type) {
|
|
|
|
case ev_string:
|
2012-10-30 03:25:53 +00:00
|
|
|
return va ("\"%s\"",
|
|
|
|
quote_string (op->o.value->v.string_val));
|
2012-05-08 02:43:29 +00:00
|
|
|
case ev_float:
|
|
|
|
return va ("%g", op->o.value->v.float_val);
|
|
|
|
case ev_vector:
|
|
|
|
return va ("'%g %g %g'",
|
|
|
|
op->o.value->v.vector_val[0],
|
|
|
|
op->o.value->v.vector_val[1],
|
|
|
|
op->o.value->v.vector_val[2]);
|
|
|
|
case ev_quat:
|
|
|
|
return va ("'%g %g %g %g'",
|
|
|
|
op->o.value->v.quaternion_val[0],
|
|
|
|
op->o.value->v.quaternion_val[1],
|
|
|
|
op->o.value->v.quaternion_val[2],
|
|
|
|
op->o.value->v.quaternion_val[3]);
|
|
|
|
case ev_pointer:
|
|
|
|
return va ("ptr %d", op->o.value->v.pointer.val);
|
|
|
|
case ev_field:
|
|
|
|
return va ("field %d", op->o.value->v.pointer.val);
|
|
|
|
case ev_entity:
|
|
|
|
return va ("ent %d", op->o.value->v.integer_val);
|
|
|
|
case ev_func:
|
|
|
|
return va ("func %d", op->o.value->v.integer_val);
|
|
|
|
case ev_integer:
|
|
|
|
return va ("int %d", op->o.value->v.integer_val);
|
|
|
|
case ev_uinteger:
|
|
|
|
return va ("uint %u", op->o.value->v.uinteger_val);
|
|
|
|
case ev_short:
|
|
|
|
return va ("short %d", op->o.value->v.short_val);
|
|
|
|
case ev_void:
|
|
|
|
return "(void)";
|
|
|
|
case ev_invalid:
|
|
|
|
return "(invalid)";
|
|
|
|
case ev_type_count:
|
|
|
|
return "(type_count)";
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case op_label:
|
|
|
|
return op->o.label->name;
|
|
|
|
case op_temp:
|
2012-12-11 03:26:48 +00:00
|
|
|
if (op->o.tempop.alias)
|
|
|
|
return va ("<tmp %p:%d:%p:%d>", op, op->o.tempop.users,
|
|
|
|
op->o.tempop.alias,
|
|
|
|
op->o.tempop.alias->o.tempop.users);
|
2012-11-18 10:08:16 +00:00
|
|
|
return va ("<tmp %p:%d>", op, op->o.tempop.users);
|
2012-12-11 06:52:37 +00:00
|
|
|
case op_alias:
|
|
|
|
{
|
|
|
|
const char *alias = operand_string (op->o.alias);
|
|
|
|
char *buf = alloca (strlen (alias) + 1);
|
|
|
|
strcpy (buf, alias);
|
|
|
|
return va ("alias(%s,%s)", pr_type_name[op->type], buf);
|
|
|
|
}
|
2012-05-08 02:43:29 +00:00
|
|
|
}
|
|
|
|
return ("??");
|
|
|
|
}
|
|
|
|
|
2011-01-20 09:07:24 +00:00
|
|
|
static void
|
|
|
|
print_operand (operand_t *op)
|
|
|
|
{
|
2011-01-21 01:54:40 +00:00
|
|
|
switch (op->op_type) {
|
2012-12-05 10:47:22 +00:00
|
|
|
case op_def:
|
2011-04-10 00:08:46 +00:00
|
|
|
printf ("(%s) ", pr_type_name[op->type]);
|
2012-12-05 10:47:22 +00:00
|
|
|
printf ("%s", op->o.def->name);
|
2011-01-20 09:07:24 +00:00
|
|
|
break;
|
|
|
|
case op_value:
|
2011-04-10 00:08:46 +00:00
|
|
|
printf ("(%s) ", pr_type_name[op->type]);
|
2011-01-20 09:07:24 +00:00
|
|
|
switch (op->o.value->type) {
|
|
|
|
case ev_string:
|
|
|
|
printf ("\"%s\"", op->o.value->v.string_val);
|
|
|
|
break;
|
|
|
|
case ev_float:
|
|
|
|
printf ("%g", op->o.value->v.float_val);
|
|
|
|
break;
|
|
|
|
case ev_vector:
|
|
|
|
printf ("'%g", op->o.value->v.vector_val[0]);
|
|
|
|
printf (" %g", op->o.value->v.vector_val[1]);
|
|
|
|
printf (" %g'", op->o.value->v.vector_val[2]);
|
|
|
|
break;
|
|
|
|
case ev_quat:
|
|
|
|
printf ("'%g", op->o.value->v.quaternion_val[0]);
|
|
|
|
printf (" %g", op->o.value->v.quaternion_val[1]);
|
|
|
|
printf (" %g", op->o.value->v.quaternion_val[2]);
|
|
|
|
printf (" %g'", op->o.value->v.quaternion_val[3]);
|
|
|
|
break;
|
|
|
|
case ev_pointer:
|
|
|
|
printf ("(%s)[%d]",
|
|
|
|
pr_type_name[op->o.value->v.pointer.type->type],
|
|
|
|
op->o.value->v.pointer.val);
|
|
|
|
break;
|
|
|
|
case ev_field:
|
|
|
|
printf ("%d", op->o.value->v.pointer.val);
|
|
|
|
break;
|
|
|
|
case ev_entity:
|
|
|
|
case ev_func:
|
|
|
|
case ev_integer:
|
|
|
|
printf ("%d", op->o.value->v.integer_val);
|
|
|
|
break;
|
2011-04-09 01:07:47 +00:00
|
|
|
case ev_uinteger:
|
|
|
|
printf ("%u", op->o.value->v.uinteger_val);
|
|
|
|
break;
|
2011-01-20 09:07:24 +00:00
|
|
|
case ev_short:
|
|
|
|
printf ("%d", op->o.value->v.short_val);
|
|
|
|
break;
|
|
|
|
case ev_void:
|
|
|
|
case ev_invalid:
|
|
|
|
case ev_type_count:
|
|
|
|
internal_error (0, "weird value type");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case op_label:
|
2011-01-21 06:53:31 +00:00
|
|
|
printf ("block %p", op->o.label->dest);
|
2011-01-20 09:07:24 +00:00
|
|
|
break;
|
|
|
|
case op_temp:
|
2011-04-10 00:08:46 +00:00
|
|
|
printf ("tmp (%s) %p", pr_type_name[op->type], op);
|
2012-11-19 02:02:31 +00:00
|
|
|
if (op->o.tempop.def)
|
|
|
|
printf (" %s", op->o.tempop.def->name);
|
2011-01-20 09:07:24 +00:00
|
|
|
break;
|
2012-12-11 06:52:37 +00:00
|
|
|
case op_alias:
|
|
|
|
printf ("alias(%s,", pr_type_name[op->type]);
|
|
|
|
print_operand (op->o.alias);
|
|
|
|
printf (")");
|
2011-01-20 09:07:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
print_statement (statement_t *s)
|
|
|
|
{
|
|
|
|
printf ("(%s, ", s->opcode);
|
|
|
|
if (s->opa)
|
|
|
|
print_operand (s->opa);
|
|
|
|
printf (", ");
|
|
|
|
if (s->opb)
|
|
|
|
print_operand (s->opb);
|
|
|
|
printf (", ");
|
|
|
|
if (s->opc)
|
|
|
|
print_operand (s->opc);
|
|
|
|
printf (")\n");
|
|
|
|
}
|
|
|
|
|
2011-01-19 06:47:45 +00:00
|
|
|
static sblock_t *free_sblocks;
|
|
|
|
static statement_t *free_statements;
|
|
|
|
static operand_t *free_operands;
|
|
|
|
|
2012-11-07 05:09:01 +00:00
|
|
|
sblock_t *
|
2011-01-19 06:47:45 +00:00
|
|
|
new_sblock (void)
|
|
|
|
{
|
|
|
|
sblock_t *sblock;
|
|
|
|
ALLOC (256, sblock_t, sblocks, sblock);
|
|
|
|
sblock->tail = &sblock->statements;
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2012-11-07 05:09:01 +00:00
|
|
|
void
|
2011-01-19 06:47:45 +00:00
|
|
|
sblock_add_statement (sblock_t *sblock, statement_t *statement)
|
|
|
|
{
|
|
|
|
// this should normally be null, but might be inserting
|
|
|
|
statement->next = *sblock->tail;
|
|
|
|
*sblock->tail = statement;
|
|
|
|
sblock->tail = &statement->next;
|
|
|
|
}
|
|
|
|
|
2012-11-07 06:52:37 +00:00
|
|
|
statement_t *
|
2012-11-16 07:16:06 +00:00
|
|
|
new_statement (st_type_t type, const char *opcode, expr_t *expr)
|
2011-01-19 06:47:45 +00:00
|
|
|
{
|
|
|
|
statement_t *statement;
|
|
|
|
ALLOC (256, statement_t, statements, statement);
|
2012-11-16 07:16:06 +00:00
|
|
|
statement->type = type;
|
2011-01-19 06:47:45 +00:00
|
|
|
statement->opcode = save_string (opcode);
|
2011-02-08 09:18:34 +00:00
|
|
|
statement->expr = expr;
|
2011-01-19 06:47:45 +00:00
|
|
|
return statement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static operand_t *
|
|
|
|
new_operand (op_type_e op)
|
|
|
|
{
|
|
|
|
operand_t *operand;
|
|
|
|
ALLOC (256, operand_t, operands, operand);
|
2011-01-21 01:54:40 +00:00
|
|
|
operand->op_type = op;
|
2011-01-19 06:47:45 +00:00
|
|
|
return operand;
|
|
|
|
}
|
|
|
|
|
2012-12-13 03:49:00 +00:00
|
|
|
void
|
2011-01-28 04:23:20 +00:00
|
|
|
free_operand (operand_t *op)
|
|
|
|
{
|
2012-12-04 04:23:31 +00:00
|
|
|
FREE (operands, op);
|
2011-01-28 04:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
free_statement (statement_t *s)
|
|
|
|
{
|
2012-12-04 04:23:31 +00:00
|
|
|
// if (s->opa)
|
|
|
|
// free_operand (s->opa);
|
|
|
|
// if (s->opb)
|
|
|
|
// free_operand (s->opb);
|
|
|
|
// if (s->opc)
|
|
|
|
// free_operand (s->opc);
|
|
|
|
FREE (statements, s);
|
2011-01-28 04:23:20 +00:00
|
|
|
}
|
2012-12-19 10:45:05 +00:00
|
|
|
|
2011-01-28 04:23:20 +00:00
|
|
|
static void
|
|
|
|
free_sblock (sblock_t *sblock)
|
|
|
|
{
|
|
|
|
while (sblock->statements) {
|
|
|
|
statement_t *s = sblock->statements;
|
|
|
|
sblock->statements = s->next;
|
|
|
|
free_statement (s);
|
|
|
|
}
|
2012-12-04 04:23:31 +00:00
|
|
|
FREE (sblocks, sblock);
|
2011-01-28 04:23:20 +00:00
|
|
|
}
|
2012-12-19 10:45:05 +00:00
|
|
|
|
2012-12-13 03:49:00 +00:00
|
|
|
operand_t *
|
|
|
|
def_operand (def_t *def, type_t *type)
|
|
|
|
{
|
|
|
|
operand_t *op;
|
|
|
|
|
|
|
|
if (!type)
|
|
|
|
type = def->type;
|
|
|
|
op = new_operand (op_def);
|
|
|
|
op->type = low_level_type (type);
|
|
|
|
op->o.def = def;
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
operand_t *
|
|
|
|
value_operand (ex_value_t *value)
|
|
|
|
{
|
|
|
|
operand_t *op;
|
|
|
|
op = new_operand (op_value);
|
|
|
|
op->type = value->type;
|
|
|
|
op->o.value = value;
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2012-11-07 05:09:01 +00:00
|
|
|
operand_t *
|
2011-03-23 12:32:14 +00:00
|
|
|
temp_operand (type_t *type)
|
|
|
|
{
|
|
|
|
operand_t *op = new_operand (op_temp);
|
|
|
|
|
2012-12-01 02:10:47 +00:00
|
|
|
op->o.tempop.type = type;
|
2011-03-23 12:32:14 +00:00
|
|
|
op->type = low_level_type (type);
|
|
|
|
op->size = type_size (type);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2012-12-11 06:52:37 +00:00
|
|
|
operand_t *
|
|
|
|
alias_operand (etype_t type, operand_t *op)
|
|
|
|
{
|
|
|
|
operand_t *aop;
|
|
|
|
|
|
|
|
if (pr_type_size[type] != pr_type_size[op->type])
|
|
|
|
internal_error (0, "aliasing operand with type of diffent size");
|
|
|
|
aop = new_operand (op_alias);
|
|
|
|
aop->o.alias = op;
|
|
|
|
aop->type = type;
|
|
|
|
aop->size = pr_type_size[type];
|
|
|
|
return aop;
|
|
|
|
}
|
|
|
|
|
2011-03-10 10:29:23 +00:00
|
|
|
static operand_t *
|
|
|
|
short_operand (short short_val)
|
|
|
|
{
|
2012-12-13 03:49:00 +00:00
|
|
|
ex_value_t *val = new_short_val (short_val);
|
|
|
|
return value_operand (val);
|
2011-03-10 10:29:23 +00:00
|
|
|
}
|
|
|
|
|
2011-01-19 06:47:45 +00:00
|
|
|
static const char *
|
|
|
|
convert_op (int op)
|
|
|
|
{
|
|
|
|
switch (op) {
|
|
|
|
case PAS: return ".=";
|
|
|
|
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 "/";
|
|
|
|
case '%': return "%";
|
|
|
|
case '&': return "&";
|
|
|
|
case '|': return "|";
|
|
|
|
case '^': return "^";
|
|
|
|
case '~': return "~";
|
|
|
|
case '!': return "!";
|
|
|
|
case SHL: return "<<";
|
|
|
|
case SHR: return ">>";
|
|
|
|
case '.': return ".";
|
|
|
|
case 'i': return "<IF>";
|
|
|
|
case 'n': return "<IFNOT>";
|
|
|
|
case IFBE: return "<IFBE>";
|
|
|
|
case IFB: return "<IFB>";
|
|
|
|
case IFAE: return "<IFAE>";
|
|
|
|
case IFA: return "<IFA>";
|
2011-03-09 01:30:57 +00:00
|
|
|
case 'm': return "<MOVE>";
|
|
|
|
case 'M': return "<MOVEP>";
|
2011-01-19 06:47:45 +00:00
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-01-20 14:26:13 +00:00
|
|
|
|
2012-11-30 05:06:52 +00:00
|
|
|
int
|
|
|
|
statement_is_cond (statement_t *s)
|
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
return !strncmp (s->opcode, "<IF", 3);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
statement_is_goto (statement_t *s)
|
2012-05-03 13:13:50 +00:00
|
|
|
{
|
2012-11-30 05:06:52 +00:00
|
|
|
if (!s)
|
|
|
|
return 0;
|
2012-05-03 13:13:50 +00:00
|
|
|
return !strcmp (s->opcode, "<GOTO>");
|
|
|
|
}
|
|
|
|
|
2012-11-30 05:06:52 +00:00
|
|
|
int
|
|
|
|
statement_is_jumpb (statement_t *s)
|
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
return !strcmp (s->opcode, "<JUMPB>");
|
|
|
|
}
|
|
|
|
|
2012-11-30 08:04:18 +00:00
|
|
|
int
|
|
|
|
statement_is_call (statement_t *s)
|
|
|
|
{
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
if (!strncmp (s->opcode, "<CALL", 5))
|
|
|
|
return 1;
|
|
|
|
if (!strncmp (s->opcode, "<RCALL", 6))
|
|
|
|
return 2;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-30 05:06:52 +00:00
|
|
|
int
|
|
|
|
statement_is_return (statement_t *s)
|
2012-05-03 13:13:50 +00:00
|
|
|
{
|
2012-11-30 05:06:52 +00:00
|
|
|
if (!s)
|
|
|
|
return 0;
|
2012-05-03 13:13:50 +00:00
|
|
|
return !strncmp (s->opcode, "<RETURN", 7);
|
|
|
|
}
|
|
|
|
|
2012-11-30 05:06:52 +00:00
|
|
|
sblock_t *
|
|
|
|
statement_get_target (statement_t *s)
|
2012-05-03 13:13:50 +00:00
|
|
|
{
|
2012-11-30 05:06:52 +00:00
|
|
|
if (statement_is_cond (s))
|
|
|
|
return s->opb->o.label->dest;
|
|
|
|
if (statement_is_goto (s))
|
|
|
|
return s->opa->o.label->dest;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sblock_t **
|
|
|
|
statement_get_targetlist (statement_t *s)
|
|
|
|
{
|
|
|
|
sblock_t **target_list;
|
|
|
|
int count = 0, i;
|
|
|
|
def_t *table = 0;
|
|
|
|
expr_t *e;
|
|
|
|
|
|
|
|
if (statement_is_cond (s)) {
|
|
|
|
count = 1;
|
|
|
|
} else if (statement_is_goto (s)) {
|
|
|
|
count = 1;
|
|
|
|
} else if (statement_is_jumpb (s)) {
|
2012-12-05 10:47:22 +00:00
|
|
|
table = s->opa->o.def;
|
2012-11-30 05:06:52 +00:00
|
|
|
count = table->type->t.array.size;
|
|
|
|
}
|
|
|
|
target_list = malloc ((count + 1) * sizeof (sblock_t *));
|
|
|
|
target_list[count] = 0;
|
|
|
|
if (statement_is_cond (s)) {
|
|
|
|
target_list[0] = statement_get_target (s);
|
|
|
|
} else if (statement_is_goto (s)) {
|
|
|
|
target_list[0] = statement_get_target (s);
|
|
|
|
} else if (statement_is_jumpb (s)) {
|
2012-12-05 13:20:55 +00:00
|
|
|
if (table->alias)
|
|
|
|
internal_error (0, "aliased jump table");
|
2012-11-30 05:06:52 +00:00
|
|
|
e = table->initializer->e.block.head; //FIXME check!!!
|
|
|
|
for (i = 0; i < count; e = e->next, i++)
|
|
|
|
target_list[i] = e->e.labelref.label->dest;
|
|
|
|
}
|
|
|
|
return target_list;
|
2012-05-03 13:13:50 +00:00
|
|
|
}
|
2012-12-19 06:28:39 +00:00
|
|
|
#if 0
|
2012-05-03 13:21:32 +00:00
|
|
|
static void
|
|
|
|
invert_conditional (statement_t *s)
|
|
|
|
{
|
|
|
|
if (!strcmp (s->opcode, "<IF>"))
|
|
|
|
s->opcode = "<IFNOT>";
|
|
|
|
else if (!strcmp (s->opcode, "<IFNOT>"))
|
|
|
|
s->opcode = "<IF>";
|
|
|
|
else if (!strcmp (s->opcode, "<IFBE>"))
|
|
|
|
s->opcode = "<IFA>";
|
|
|
|
else if (!strcmp (s->opcode, "<IFB>"))
|
|
|
|
s->opcode = "<IFAE>";
|
|
|
|
else if (!strcmp (s->opcode, "<IFAE>"))
|
|
|
|
s->opcode = "<IFB>";
|
|
|
|
else if (!strcmp (s->opcode, "<IFA>"))
|
|
|
|
s->opcode = "<IFBE>";
|
|
|
|
}
|
2012-12-19 06:28:39 +00:00
|
|
|
#endif
|
2011-01-20 14:26:13 +00:00
|
|
|
typedef sblock_t *(*statement_f) (sblock_t *, expr_t *);
|
|
|
|
typedef sblock_t *(*expr_f) (sblock_t *, expr_t *, operand_t **);
|
|
|
|
|
2011-01-19 06:47:45 +00:00
|
|
|
static sblock_t *statement_subexpr (sblock_t *sblock, expr_t *e,
|
|
|
|
operand_t **op);
|
2011-01-20 14:26:13 +00:00
|
|
|
static sblock_t *statement_slist (sblock_t *sblock, expr_t *e);
|
2011-01-19 06:47:45 +00:00
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_branch (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
statement_t *s = 0;
|
2011-01-20 14:26:13 +00:00
|
|
|
const char *opcode;
|
2011-01-19 06:47:45 +00:00
|
|
|
|
|
|
|
if (e->type == ex_uexpr && e->e.expr.op == 'g') {
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_flow, "<GOTO>", e);
|
2011-01-19 06:47:45 +00:00
|
|
|
s->opa = new_operand (op_label);
|
2011-01-19 13:25:43 +00:00
|
|
|
s->opa->o.label = &e->e.expr.e1->e.label;
|
2011-01-20 14:26:13 +00:00
|
|
|
} else {
|
2011-03-03 10:08:26 +00:00
|
|
|
if (e->e.expr.op == 'g') {
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_flow, "<JUMPB>", e);
|
2011-03-03 10:08:26 +00:00
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &s->opa);
|
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e2, &s->opb);
|
|
|
|
} else {
|
|
|
|
opcode = convert_op (e->e.expr.op);
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_flow, opcode, e);
|
2011-03-03 10:08:26 +00:00
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &s->opa);
|
|
|
|
s->opb = new_operand (op_label);
|
|
|
|
s->opb->o.label = &e->e.expr.e2->e.label;
|
|
|
|
}
|
2011-01-19 06:47:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
sblock->next = new_sblock ();
|
|
|
|
return sblock->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
2011-02-07 05:43:07 +00:00
|
|
|
expr_assign (sblock_t *sblock, expr_t *e, operand_t **op)
|
2011-01-19 06:47:45 +00:00
|
|
|
{
|
|
|
|
statement_t *s;
|
2011-01-21 01:59:16 +00:00
|
|
|
expr_t *src_expr = e->e.expr.e2;
|
|
|
|
expr_t *dst_expr = e->e.expr.e1;
|
|
|
|
operand_t *src = 0;
|
|
|
|
operand_t *dst = 0;
|
|
|
|
operand_t *ofs = 0;
|
2011-01-20 14:26:13 +00:00
|
|
|
const char *opcode = convert_op (e->e.expr.op);
|
2012-11-16 07:16:06 +00:00
|
|
|
st_type_t type;
|
2011-01-20 14:26:13 +00:00
|
|
|
|
|
|
|
if (e->e.expr.op == '=') {
|
2011-01-21 01:59:16 +00:00
|
|
|
sblock = statement_subexpr (sblock, dst_expr, &dst);
|
2011-02-11 05:01:18 +00:00
|
|
|
src = dst;
|
2011-01-21 01:59:16 +00:00
|
|
|
sblock = statement_subexpr (sblock, src_expr, &src);
|
|
|
|
ofs = 0;
|
2011-02-07 05:43:07 +00:00
|
|
|
if (op)
|
|
|
|
*op = dst;
|
2011-02-11 05:01:18 +00:00
|
|
|
if (src == dst)
|
|
|
|
return sblock;
|
2012-11-16 07:16:06 +00:00
|
|
|
type = st_assign;
|
2011-01-20 14:26:13 +00:00
|
|
|
} else {
|
2011-01-27 05:19:04 +00:00
|
|
|
//FIXME this sucks. find a better way to handle both pointer
|
|
|
|
//dereferences and pointer assignements
|
2011-01-21 01:59:16 +00:00
|
|
|
sblock = statement_subexpr (sblock, src_expr, &src);
|
|
|
|
if (dst_expr->type == ex_expr
|
|
|
|
&& extract_type (dst_expr->e.expr.e1) == ev_pointer
|
|
|
|
&& !is_constant (dst_expr->e.expr.e1)) {
|
|
|
|
sblock = statement_subexpr (sblock, dst_expr->e.expr.e1, &dst);
|
|
|
|
sblock = statement_subexpr (sblock, dst_expr->e.expr.e2, &ofs);
|
|
|
|
} else {
|
2011-01-27 05:19:04 +00:00
|
|
|
if (dst_expr->type == ex_uexpr
|
|
|
|
&& dst_expr->e.expr.op == '&') {
|
|
|
|
opcode = "=";
|
|
|
|
dst_expr = unary_expr ('.', dst_expr);
|
|
|
|
}
|
2011-01-21 01:59:16 +00:00
|
|
|
sblock = statement_subexpr (sblock, dst_expr, &dst);
|
|
|
|
ofs = 0;
|
|
|
|
}
|
2011-02-07 05:43:07 +00:00
|
|
|
if (op)
|
|
|
|
*op = src;
|
2012-11-16 07:16:06 +00:00
|
|
|
type = st_ptrassign;
|
2011-01-21 01:59:16 +00:00
|
|
|
}
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (type, opcode, e);
|
2011-01-21 01:59:16 +00:00
|
|
|
s->opa = src;
|
|
|
|
s->opb = dst;
|
|
|
|
s->opc = ofs;
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-02-11 06:02:51 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_move (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
|
|
|
statement_t *s;
|
|
|
|
type_t *type = e->e.expr.type;
|
|
|
|
expr_t *dst_expr = e->e.expr.e1;
|
|
|
|
expr_t *src_expr = e->e.expr.e2;
|
|
|
|
expr_t *size_expr;
|
|
|
|
operand_t *dst = 0;
|
|
|
|
operand_t *src = 0;
|
|
|
|
operand_t *size = 0;
|
|
|
|
|
|
|
|
if (!op)
|
|
|
|
op = &dst;
|
|
|
|
size_expr = new_short_expr (type_size (type));
|
|
|
|
sblock = statement_subexpr (sblock, dst_expr, op);
|
|
|
|
dst = *op;
|
|
|
|
sblock = statement_subexpr (sblock, src_expr, &src);
|
|
|
|
sblock = statement_subexpr (sblock, size_expr, &size);
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_move, convert_op (e->e.expr.op), e);
|
2011-02-11 06:02:51 +00:00
|
|
|
s->opa = src;
|
|
|
|
s->opb = size;
|
|
|
|
s->opc = dst;
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-01-21 10:40:46 +00:00
|
|
|
static sblock_t *
|
|
|
|
vector_call (sblock_t *sblock, expr_t *earg, expr_t *param, int ind,
|
|
|
|
operand_t **op)
|
|
|
|
{
|
2011-02-11 01:20:01 +00:00
|
|
|
//FIXME this should be done in the expression tree
|
2011-01-21 10:40:46 +00:00
|
|
|
expr_t *a, *v, *n;
|
|
|
|
int i;
|
|
|
|
static const char *names[] = {"x", "y", "z"};
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
n = new_name_expr (names[i]);
|
2012-07-18 14:01:09 +00:00
|
|
|
v = new_float_expr (earg->e.value->v.vector_val[i]);
|
2011-01-21 10:40:46 +00:00
|
|
|
a = assign_expr (binary_expr ('.', param, n), v);
|
|
|
|
param = new_param_expr (get_type (earg), ind);
|
|
|
|
a->line = earg->line;
|
|
|
|
a->file = earg->file;
|
|
|
|
sblock = statement_slist (sblock, a);
|
|
|
|
}
|
|
|
|
sblock = statement_subexpr (sblock, param, op);
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-01-21 06:53:31 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_call (sblock_t *sblock, expr_t *call, operand_t **op)
|
|
|
|
{
|
|
|
|
expr_t *func = call->e.expr.e1;
|
|
|
|
expr_t *args = call->e.expr.e2;
|
|
|
|
expr_t *a;
|
|
|
|
expr_t *param;
|
|
|
|
operand_t *arguments[2] = {0, 0};
|
|
|
|
int count = 0;
|
|
|
|
int ind;
|
|
|
|
const char *opcode;
|
|
|
|
const char *pref = "";
|
|
|
|
statement_t *s;
|
|
|
|
|
|
|
|
for (a = args; a; a = a->next)
|
|
|
|
count++;
|
|
|
|
ind = count;
|
|
|
|
for (a = args; a; a = a->next) {
|
|
|
|
ind--;
|
|
|
|
param = new_param_expr (get_type (a), ind);
|
|
|
|
if (count && options.code.progsversion != PROG_ID_VERSION && ind < 2) {
|
|
|
|
pref = "R";
|
|
|
|
sblock = statement_subexpr (sblock, param, &arguments[ind]);
|
2011-01-21 10:40:46 +00:00
|
|
|
if (options.code.vector_calls && a->type == ex_value
|
2012-07-18 14:01:09 +00:00
|
|
|
&& a->e.value->type == ev_vector)
|
2011-01-21 10:40:46 +00:00
|
|
|
sblock = vector_call (sblock, a, param, ind, &arguments[ind]);
|
|
|
|
else
|
2011-01-21 06:53:31 +00:00
|
|
|
sblock = statement_subexpr (sblock, a, &arguments[ind]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_struct (get_type (param))) {
|
2011-02-11 01:20:01 +00:00
|
|
|
//FIXME this should be done in the expression tree
|
2011-01-21 06:53:31 +00:00
|
|
|
expr_t *mov = assign_expr (param, a);
|
|
|
|
mov->line = a->line;
|
|
|
|
mov->file = a->file;
|
|
|
|
sblock = statement_slist (sblock, mov);
|
|
|
|
} else {
|
2012-05-04 09:00:05 +00:00
|
|
|
if (options.code.vector_calls && a->type == ex_value
|
2012-07-18 14:01:09 +00:00
|
|
|
&& a->e.value->type == ev_vector) {
|
2011-01-21 10:40:46 +00:00
|
|
|
sblock = vector_call (sblock, a, param, ind, 0);
|
|
|
|
} else {
|
2011-02-08 04:27:37 +00:00
|
|
|
operand_t *p = 0;
|
2011-01-21 10:40:46 +00:00
|
|
|
operand_t *arg;
|
|
|
|
sblock = statement_subexpr (sblock, param, &p);
|
|
|
|
arg = p;
|
|
|
|
sblock = statement_subexpr (sblock, a, &arg);
|
|
|
|
if (arg != p) {
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_assign, "=", a);
|
2011-01-21 10:40:46 +00:00
|
|
|
s->opa = arg;
|
|
|
|
s->opb = p;
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
}
|
2011-01-21 06:53:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opcode = va ("<%sCALL%d>", pref, count);
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_func, opcode, call);
|
2011-01-21 06:53:31 +00:00
|
|
|
sblock = statement_subexpr (sblock, func, &s->opa);
|
|
|
|
s->opb = arguments[0];
|
|
|
|
s->opc = arguments[1];
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
sblock->next = new_sblock ();
|
|
|
|
return sblock->next;
|
|
|
|
}
|
|
|
|
|
2011-01-21 01:59:16 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_address (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
|
|
|
if (e->type == ex_uexpr) {
|
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, op);
|
2011-01-27 05:17:55 +00:00
|
|
|
(*op)->type = ev_pointer;
|
2011-01-21 01:59:16 +00:00
|
|
|
}
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-03-10 10:29:23 +00:00
|
|
|
static statement_t *
|
|
|
|
lea_statement (operand_t *pointer, operand_t *offset, expr_t *e)
|
|
|
|
{
|
2012-11-16 07:16:06 +00:00
|
|
|
statement_t *s = new_statement (st_expr, "&", e);
|
2011-03-10 10:29:23 +00:00
|
|
|
s->opa = pointer;
|
|
|
|
s->opb = offset;
|
2011-03-23 12:32:14 +00:00
|
|
|
s->opc = temp_operand (&type_pointer);
|
2011-03-10 10:29:23 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static statement_t *
|
|
|
|
address_statement (operand_t *value, expr_t *e)
|
|
|
|
{
|
2012-11-16 07:16:06 +00:00
|
|
|
statement_t *s = new_statement (st_expr, "&", e);
|
2011-03-10 10:29:23 +00:00
|
|
|
s->opa = value;
|
2011-03-23 12:32:14 +00:00
|
|
|
s->opc = temp_operand (&type_pointer);
|
2011-03-10 10:29:23 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2011-01-21 01:59:16 +00:00
|
|
|
static sblock_t *
|
2011-02-14 06:13:58 +00:00
|
|
|
expr_deref (sblock_t *sblock, expr_t *deref, operand_t **op)
|
2011-01-21 01:59:16 +00:00
|
|
|
{
|
2011-02-14 06:13:58 +00:00
|
|
|
type_t *type = deref->e.expr.type;
|
|
|
|
expr_t *e;
|
2011-01-21 01:59:16 +00:00
|
|
|
|
2011-02-14 06:13:58 +00:00
|
|
|
e = deref->e.expr.e1;
|
2011-01-21 01:59:16 +00:00
|
|
|
if (e->type == ex_uexpr && e->e.expr.op == '&'
|
|
|
|
&& e->e.expr.e1->type == ex_symbol) {
|
2012-12-05 10:47:22 +00:00
|
|
|
if (e->e.expr.e1->e.symbol->sy_type != sy_var)
|
|
|
|
internal_error (e, "address of non-var");
|
2012-12-13 03:49:00 +00:00
|
|
|
*op = def_operand (e->e.expr.e1->e.symbol->s.def, type);
|
2011-02-07 05:56:29 +00:00
|
|
|
} else if (e->type == ex_expr && e->e.expr.op == '&') {
|
2011-02-14 06:13:58 +00:00
|
|
|
statement_t *s;
|
|
|
|
operand_t *ptr = 0;
|
|
|
|
operand_t *offs = 0;
|
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &ptr);
|
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e2, &offs);
|
2011-03-23 12:32:14 +00:00
|
|
|
if (!*op)
|
|
|
|
*op = temp_operand (type);
|
2011-02-14 06:13:58 +00:00
|
|
|
if (low_level_type (type) == ev_void) {
|
2011-03-10 10:29:23 +00:00
|
|
|
operand_t *src_addr;
|
|
|
|
operand_t *dst_addr;
|
|
|
|
|
|
|
|
s = lea_statement (ptr, offs, e);
|
|
|
|
src_addr = s->opc;
|
2011-02-14 06:13:58 +00:00
|
|
|
sblock_add_statement (sblock, s);
|
2011-03-10 10:29:23 +00:00
|
|
|
|
|
|
|
//FIXME an address immediate would be nice.
|
|
|
|
s = address_statement (*op, e);
|
|
|
|
dst_addr = s->opc;
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_move, "<MOVEP>", deref);
|
2011-03-10 10:29:23 +00:00
|
|
|
s->opa = src_addr;
|
|
|
|
s->opb = short_operand (type_size (type));
|
|
|
|
s->opc = dst_addr;
|
2011-02-14 06:13:58 +00:00
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
} else {
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_expr, ".", deref);
|
2011-02-14 06:13:58 +00:00
|
|
|
s->opa = ptr;
|
|
|
|
s->opb = offs;
|
|
|
|
s->opc = *op;
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
}
|
2012-07-18 14:01:09 +00:00
|
|
|
} else if (e->type == ex_value && e->e.value->type == ev_pointer) {
|
2012-12-05 10:47:22 +00:00
|
|
|
ex_pointer_t *ptr = &e->e.value->v.pointer;
|
2012-12-13 03:49:00 +00:00
|
|
|
*op = def_operand (alias_def (ptr->def, ptr->type, ptr->val),
|
|
|
|
ptr->type);
|
2012-11-03 11:10:20 +00:00
|
|
|
} else {
|
|
|
|
statement_t *s;
|
|
|
|
operand_t *ptr = 0;
|
|
|
|
|
|
|
|
sblock = statement_subexpr (sblock, e, &ptr);
|
|
|
|
if (!*op)
|
|
|
|
*op = temp_operand (type);
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_expr, ".", deref);
|
2012-11-03 11:10:20 +00:00
|
|
|
s->opa = ptr;
|
|
|
|
s->opb = short_operand (0);
|
|
|
|
s->opc = *op;
|
|
|
|
sblock_add_statement (sblock, s);
|
2011-01-20 14:26:13 +00:00
|
|
|
}
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-01-21 01:57:47 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_block (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
|
|
|
if (!e->e.block.result)
|
|
|
|
internal_error (e, "block sub-expression without result");
|
|
|
|
sblock = statement_slist (sblock, e->e.block.head);
|
|
|
|
sblock = statement_subexpr (sblock, e->e.block.result, op);
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-01-20 14:26:13 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_expr (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
2011-01-19 06:47:45 +00:00
|
|
|
const char *opcode;
|
2011-01-20 14:26:13 +00:00
|
|
|
statement_t *s;
|
|
|
|
|
|
|
|
switch (e->e.expr.op) {
|
|
|
|
case 'c':
|
2011-01-21 06:53:31 +00:00
|
|
|
sblock = expr_call (sblock, e, op);
|
2011-01-20 14:26:13 +00:00
|
|
|
break;
|
2011-02-07 05:43:07 +00:00
|
|
|
case '=':
|
|
|
|
case PAS:
|
|
|
|
sblock = expr_assign (sblock, e, op);
|
|
|
|
break;
|
2011-03-09 01:30:57 +00:00
|
|
|
case 'm':
|
2011-02-11 06:02:51 +00:00
|
|
|
case 'M':
|
|
|
|
sblock = expr_move (sblock, e, op);
|
|
|
|
break;
|
2011-01-20 14:26:13 +00:00
|
|
|
default:
|
|
|
|
opcode = convert_op (e->e.expr.op);
|
|
|
|
if (!opcode)
|
|
|
|
internal_error (e, "ice ice baby");
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_expr, opcode, e);
|
2011-01-20 14:26:13 +00:00
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &s->opa);
|
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e2, &s->opb);
|
2011-03-23 12:32:14 +00:00
|
|
|
if (!*op)
|
|
|
|
*op = temp_operand (e->e.expr.type);
|
2011-01-21 06:53:31 +00:00
|
|
|
s->opc = *op;
|
2011-01-20 14:26:13 +00:00
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-03-03 02:06:10 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_alias (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
2012-11-26 12:30:57 +00:00
|
|
|
operand_t *aop = 0;
|
2012-12-06 00:40:16 +00:00
|
|
|
operand_t *top;
|
|
|
|
etype_t type;
|
2012-12-05 10:47:22 +00:00
|
|
|
def_t *def;
|
2012-12-06 00:40:16 +00:00
|
|
|
|
2012-12-11 11:44:40 +00:00
|
|
|
type = low_level_type (e->e.expr.type);
|
2012-11-26 12:30:57 +00:00
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &aop);
|
2012-12-11 11:44:40 +00:00
|
|
|
if (aop->type == type) {
|
|
|
|
*op = aop;
|
|
|
|
return sblock;
|
|
|
|
}
|
2012-12-05 10:47:22 +00:00
|
|
|
if (aop->op_type == op_temp) {
|
|
|
|
while (aop->o.tempop.alias) {
|
|
|
|
aop = aop->o.tempop.alias;
|
|
|
|
if (aop->op_type != op_temp)
|
|
|
|
internal_error (e, "temp alias of non-temp var");
|
|
|
|
}
|
2012-12-11 11:44:40 +00:00
|
|
|
for (top = aop->o.tempop.alias_ops; top; top = top->next)
|
|
|
|
if (top->type == type)
|
|
|
|
break;
|
2012-12-06 00:40:16 +00:00
|
|
|
if (!top) {
|
|
|
|
top = new_operand (op_temp);
|
|
|
|
top->type = type;
|
|
|
|
top->o.tempop.alias = aop;
|
2012-12-11 03:57:37 +00:00
|
|
|
top->next = aop->o.tempop.alias_ops;
|
|
|
|
aop->o.tempop.alias_ops = top;
|
2012-12-06 00:40:16 +00:00
|
|
|
}
|
|
|
|
*op = top;
|
2012-12-05 10:47:22 +00:00
|
|
|
} else if (aop->op_type == op_def) {
|
|
|
|
def = aop->o.def;
|
|
|
|
while (def->alias)
|
|
|
|
def = def->alias;
|
2012-12-13 03:49:00 +00:00
|
|
|
*op = def_operand (alias_def (def, ev_types[type], 0), 0);
|
2012-12-05 10:47:22 +00:00
|
|
|
} else {
|
|
|
|
internal_error (e, "invalid alias target: %s: %s",
|
|
|
|
optype_str (aop->op_type), operand_string (aop));
|
|
|
|
}
|
2011-03-03 02:06:10 +00:00
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-01-23 02:53:08 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_cast (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
2011-04-08 04:55:26 +00:00
|
|
|
type_t *src_type;
|
2011-02-11 05:00:36 +00:00
|
|
|
type_t *type = e->e.expr.type;
|
|
|
|
statement_t *s;
|
|
|
|
|
2011-04-08 04:55:26 +00:00
|
|
|
src_type = get_type (e->e.expr.e1);
|
|
|
|
if ((src_type->type == ev_integer && type->type == ev_float)
|
|
|
|
|| (src_type->type == ev_float && type->type == ev_integer)) {
|
|
|
|
operand_t *src = 0;
|
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &src);
|
|
|
|
*op = temp_operand (e->e.expr.type);
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_expr, "<CONV>", e);
|
2011-02-11 05:00:36 +00:00
|
|
|
s->opa = src;
|
|
|
|
s->opc = *op;
|
2011-03-10 11:43:53 +00:00
|
|
|
sblock_add_statement (sblock, s);
|
2011-02-11 05:00:36 +00:00
|
|
|
} else {
|
2011-04-08 04:55:26 +00:00
|
|
|
sblock = expr_alias (sblock, e, op);
|
2011-01-23 02:53:08 +00:00
|
|
|
}
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-03-03 09:13:30 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_negate (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
|
|
|
expr_t *neg;
|
|
|
|
expr_t *zero;
|
|
|
|
|
|
|
|
zero = new_nil_expr ();
|
|
|
|
zero->file = e->file;
|
|
|
|
zero->line = e->line;
|
|
|
|
convert_nil (zero, e->e.expr.type);
|
|
|
|
neg = binary_expr ('-', zero, e->e.expr.e1);
|
|
|
|
neg->file = e->file;
|
|
|
|
neg->line = e->line;
|
|
|
|
return statement_subexpr (sblock, neg, op);
|
|
|
|
}
|
|
|
|
|
2011-01-20 14:26:13 +00:00
|
|
|
static sblock_t *
|
|
|
|
expr_uexpr (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
2011-03-03 04:10:07 +00:00
|
|
|
const char *opcode;
|
|
|
|
statement_t *s;
|
|
|
|
|
2011-01-21 01:59:16 +00:00
|
|
|
switch (e->e.expr.op) {
|
|
|
|
case '&':
|
|
|
|
sblock = expr_address (sblock, e, op);
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
sblock = expr_deref (sblock, e, op);
|
|
|
|
break;
|
2011-03-03 02:06:10 +00:00
|
|
|
case 'A':
|
|
|
|
sblock = expr_alias (sblock, e, op);
|
|
|
|
break;
|
2011-01-21 06:53:31 +00:00
|
|
|
case 'C':
|
2011-01-23 02:53:08 +00:00
|
|
|
sblock = expr_cast (sblock, e, op);
|
2011-01-21 06:53:31 +00:00
|
|
|
break;
|
2011-03-03 09:13:30 +00:00
|
|
|
case '-':
|
|
|
|
// progs has no neg instruction!?!
|
|
|
|
sblock = expr_negate (sblock, e, op);
|
|
|
|
break;
|
2011-01-21 01:59:16 +00:00
|
|
|
default:
|
2011-03-03 04:10:07 +00:00
|
|
|
opcode = convert_op (e->e.expr.op);
|
|
|
|
if (!opcode)
|
|
|
|
internal_error (e, "ice ice baby");
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_expr, opcode, e);
|
2011-03-03 04:10:07 +00:00
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &s->opa);
|
2011-03-23 12:32:14 +00:00
|
|
|
if (!*op)
|
|
|
|
*op = temp_operand (e->e.expr.type);
|
2011-03-03 04:10:07 +00:00
|
|
|
s->opc = *op;
|
|
|
|
sblock_add_statement (sblock, s);
|
2011-01-21 01:59:16 +00:00
|
|
|
}
|
2011-01-20 14:26:13 +00:00
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
expr_symbol (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
2012-12-05 10:47:22 +00:00
|
|
|
symbol_t *sym = e->e.symbol;
|
|
|
|
|
|
|
|
if (sym->sy_type == sy_var) {
|
2012-12-13 03:49:00 +00:00
|
|
|
*op = def_operand (sym->s.def, sym->type);
|
2012-12-05 10:47:22 +00:00
|
|
|
} else if (sym->sy_type == sy_const) {
|
2012-12-13 03:49:00 +00:00
|
|
|
*op = value_operand (sym->s.value);
|
2012-12-05 10:47:22 +00:00
|
|
|
} else if (sym->sy_type == sy_func) {
|
2012-12-13 03:49:00 +00:00
|
|
|
*op = def_operand (sym->s.func->def, 0);
|
2012-12-05 10:47:22 +00:00
|
|
|
} else {
|
|
|
|
internal_error (e, "unexpected symbol type: %s",
|
|
|
|
symtype_str(sym->sy_type));
|
|
|
|
}
|
2011-01-20 14:26:13 +00:00
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
expr_temp (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
2011-03-23 12:32:14 +00:00
|
|
|
if (!e->e.temp.op)
|
|
|
|
e->e.temp.op = temp_operand (e->e.temp.type);
|
2011-01-21 02:26:43 +00:00
|
|
|
*op = e->e.temp.op;
|
2011-01-20 14:26:13 +00:00
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
expr_value (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
2012-12-13 03:49:00 +00:00
|
|
|
*op = value_operand (e->e.value);
|
2011-01-20 14:26:13 +00:00
|
|
|
return sblock;
|
|
|
|
}
|
2011-01-19 06:47:45 +00:00
|
|
|
|
2011-01-20 14:26:13 +00:00
|
|
|
static sblock_t *
|
|
|
|
statement_subexpr (sblock_t *sblock, expr_t *e, operand_t **op)
|
|
|
|
{
|
|
|
|
static expr_f sfuncs[] = {
|
|
|
|
0, // ex_error
|
|
|
|
0, // ex_state
|
|
|
|
0, // ex_bool
|
|
|
|
0, // ex_label
|
2012-11-03 09:08:32 +00:00
|
|
|
0, // ex_labelref
|
2011-01-21 01:57:47 +00:00
|
|
|
expr_block, // ex_block
|
2011-01-20 14:26:13 +00:00
|
|
|
expr_expr,
|
|
|
|
expr_uexpr,
|
|
|
|
expr_symbol,
|
|
|
|
expr_temp,
|
|
|
|
0, // ex_nil
|
|
|
|
expr_value,
|
|
|
|
};
|
2011-01-19 06:47:45 +00:00
|
|
|
if (!e) {
|
|
|
|
*op = 0;
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
2011-01-20 14:26:13 +00:00
|
|
|
if (e->type < 0 || e->type > ex_value)
|
|
|
|
internal_error (e, "bad expression type");
|
|
|
|
if (!sfuncs[e->type])
|
|
|
|
internal_error (e, "unexpected expression type");
|
|
|
|
|
|
|
|
sblock = sfuncs[e->type] (sblock, e, op);
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_ignore (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_state (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
statement_t *s;
|
|
|
|
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_state, "<STATE>", e);
|
2011-01-20 14:26:13 +00:00
|
|
|
sblock = statement_subexpr (sblock, e->e.state.frame, &s->opa);
|
|
|
|
sblock = statement_subexpr (sblock, e->e.state.think, &s->opb);
|
|
|
|
sblock = statement_subexpr (sblock, e->e.state.step, &s->opc);
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
build_bool_block (expr_t *block, expr_t *e)
|
|
|
|
{
|
2011-01-19 06:47:45 +00:00
|
|
|
switch (e->type) {
|
|
|
|
case ex_bool:
|
2011-01-20 14:26:13 +00:00
|
|
|
build_bool_block (block, e->e.bool.e);
|
|
|
|
return;
|
2011-01-19 06:47:45 +00:00
|
|
|
case ex_label:
|
2011-01-20 14:26:13 +00:00
|
|
|
e->next = 0;
|
|
|
|
append_expr (block, e);
|
|
|
|
return;
|
2011-01-19 06:47:45 +00:00
|
|
|
case ex_expr:
|
2011-01-20 14:26:13 +00:00
|
|
|
if (e->e.expr.op == OR || e->e.expr.op == AND) {
|
|
|
|
build_bool_block (block, e->e.expr.e1);
|
|
|
|
build_bool_block (block, e->e.expr.e2);
|
|
|
|
} else if (e->e.expr.op == 'i') {
|
|
|
|
e->next = 0;
|
|
|
|
append_expr (block, e);
|
|
|
|
} else if (e->e.expr.op == 'n') {
|
|
|
|
e->next = 0;
|
|
|
|
append_expr (block, e);
|
2011-01-19 06:47:45 +00:00
|
|
|
}
|
2011-01-20 14:26:13 +00:00
|
|
|
return;
|
2011-01-19 06:47:45 +00:00
|
|
|
case ex_uexpr:
|
2011-01-20 14:26:13 +00:00
|
|
|
if (e->e.expr.op == 'g') {
|
|
|
|
e->next = 0;
|
|
|
|
append_expr (block, e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ex_block:
|
|
|
|
if (!e->e.block.result) {
|
|
|
|
expr_t *t;
|
|
|
|
for (e = e->e.block.head; e; e = t) {
|
|
|
|
t = e->next;
|
|
|
|
build_bool_block (block, e);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-01-19 06:47:45 +00:00
|
|
|
break;
|
2011-01-20 14:26:13 +00:00
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
internal_error (e, "bad boolean");
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-05-03 13:11:52 +00:00
|
|
|
is_goto_expr (expr_t *e)
|
2011-01-20 14:26:13 +00:00
|
|
|
{
|
|
|
|
return e && e->type == ex_uexpr && e->e.expr.op == 'g';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-05-03 13:11:52 +00:00
|
|
|
is_if_expr (expr_t *e)
|
2011-01-20 14:26:13 +00:00
|
|
|
{
|
|
|
|
return e && e->type == ex_expr && e->e.expr.op == 'i';
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-05-03 13:11:52 +00:00
|
|
|
is_ifnot_expr (expr_t *e)
|
2011-01-20 14:26:13 +00:00
|
|
|
{
|
|
|
|
return e && e->type == ex_expr && e->e.expr.op == 'n';
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_bool (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
expr_t **s;
|
|
|
|
expr_t *l;
|
|
|
|
expr_t *block = new_block_expr ();
|
|
|
|
|
|
|
|
build_bool_block (block, e);
|
|
|
|
|
|
|
|
s = &block->e.block.head;
|
|
|
|
while (*s) {
|
2012-05-03 13:11:52 +00:00
|
|
|
if (is_if_expr (*s) && is_goto_expr ((*s)->next)) {
|
2011-01-20 14:26:13 +00:00
|
|
|
l = (*s)->e.expr.e2;
|
|
|
|
for (e = (*s)->next->next; e && e->type == ex_label; e = e->next) {
|
|
|
|
if (e == l) {
|
2012-05-03 13:20:00 +00:00
|
|
|
l->e.label.used--;
|
2011-01-20 14:26:13 +00:00
|
|
|
e = *s;
|
|
|
|
e->e.expr.op = 'n';
|
|
|
|
e->e.expr.e2 = e->next->e.expr.e1;
|
|
|
|
e->next = e->next->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = &(*s)->next;
|
2012-05-03 13:11:52 +00:00
|
|
|
} else if (is_ifnot_expr (*s) && is_goto_expr ((*s)->next)) {
|
2011-01-20 14:26:13 +00:00
|
|
|
l = (*s)->e.expr.e2;
|
|
|
|
for (e = (*s)->next->next; e && e->type == ex_label; e = e->next) {
|
|
|
|
if (e == l) {
|
2012-05-03 13:20:00 +00:00
|
|
|
l->e.label.used--;
|
2011-01-20 14:26:13 +00:00
|
|
|
e = *s;
|
|
|
|
e->e.expr.op = 'i';
|
|
|
|
e->e.expr.e2 = e->next->e.expr.e1;
|
|
|
|
e->next = e->next->next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
s = &(*s)->next;
|
2012-05-03 13:11:52 +00:00
|
|
|
} else if (is_goto_expr (*s)) {
|
2011-01-20 14:26:13 +00:00
|
|
|
l = (*s)->e.expr.e1;
|
|
|
|
for (e = (*s)->next; e && e->type == ex_label; e = e->next) {
|
|
|
|
if (e == l) {
|
2012-11-26 07:13:59 +00:00
|
|
|
l->e.label.used--;
|
2011-01-20 14:26:13 +00:00
|
|
|
*s = (*s)->next;
|
|
|
|
l = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (l)
|
|
|
|
s = &(*s)->next;
|
|
|
|
} else {
|
|
|
|
s = &(*s)->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sblock = statement_slist (sblock, block->e.block.head);
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_label (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
if (sblock->statements) {
|
|
|
|
sblock->next = new_sblock ();
|
|
|
|
sblock = sblock->next;
|
|
|
|
}
|
2011-03-03 08:35:06 +00:00
|
|
|
if (e->e.label.used) {
|
|
|
|
e->e.label.dest = sblock;
|
|
|
|
e->e.label.next = sblock->labels;
|
|
|
|
sblock->labels = &e->e.label;
|
|
|
|
} else {
|
2012-05-04 10:48:32 +00:00
|
|
|
debug (e, "dropping unused label %s", e->e.label.name);
|
2011-03-03 08:35:06 +00:00
|
|
|
}
|
2011-01-20 14:26:13 +00:00
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_block (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
if (sblock->statements) {
|
|
|
|
sblock->next = new_sblock ();
|
|
|
|
sblock = sblock->next;
|
|
|
|
}
|
|
|
|
sblock = statement_slist (sblock, e->e.block.head);
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_expr (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
switch (e->e.expr.op) {
|
|
|
|
case 'c':
|
2011-01-21 06:53:31 +00:00
|
|
|
sblock = expr_call (sblock, e, 0);
|
2011-01-19 06:47:45 +00:00
|
|
|
break;
|
2011-03-03 10:08:26 +00:00
|
|
|
case 'g':
|
2011-01-20 14:26:13 +00:00
|
|
|
case 'i':
|
|
|
|
case 'n':
|
|
|
|
case IFBE:
|
|
|
|
case IFB:
|
|
|
|
case IFAE:
|
|
|
|
case IFA:
|
|
|
|
sblock = statement_branch (sblock, e);
|
2011-01-19 06:47:45 +00:00
|
|
|
break;
|
2011-01-20 14:26:13 +00:00
|
|
|
case '=':
|
|
|
|
case PAS:
|
2011-02-07 05:43:07 +00:00
|
|
|
sblock = expr_assign (sblock, e, 0);
|
2011-01-19 06:47:45 +00:00
|
|
|
break;
|
2011-03-09 01:30:57 +00:00
|
|
|
case 'm':
|
2011-02-11 06:02:51 +00:00
|
|
|
case 'M':
|
|
|
|
sblock = expr_move (sblock, e, 0);
|
|
|
|
break;
|
2011-01-20 14:26:13 +00:00
|
|
|
default:
|
|
|
|
if (e->e.expr.op < 256)
|
2011-01-28 04:28:45 +00:00
|
|
|
debug (e, "e %c", e->e.expr.op);
|
2011-01-20 14:26:13 +00:00
|
|
|
else
|
2011-01-28 04:28:45 +00:00
|
|
|
debug (e, "e %d", e->e.expr.op);
|
2011-01-20 14:26:13 +00:00
|
|
|
if (options.warnings.executable)
|
|
|
|
warning (e, "Non-executable statement;"
|
|
|
|
" executing programmer instead.");
|
2011-01-19 06:47:45 +00:00
|
|
|
}
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
2011-01-20 14:26:13 +00:00
|
|
|
statement_uexpr (sblock_t *sblock, expr_t *e)
|
2011-01-19 06:47:45 +00:00
|
|
|
{
|
|
|
|
const char *opcode;
|
2011-01-20 14:26:13 +00:00
|
|
|
statement_t *s;
|
2011-01-19 06:47:45 +00:00
|
|
|
|
2011-01-20 14:26:13 +00:00
|
|
|
switch (e->e.expr.op) {
|
|
|
|
case 'r':
|
2011-01-28 04:28:45 +00:00
|
|
|
debug (e, "RETURN");
|
2011-01-20 14:26:13 +00:00
|
|
|
opcode = "<RETURN>";
|
2011-03-07 04:52:28 +00:00
|
|
|
if (!e->e.expr.e1) {
|
|
|
|
if (options.code.progsversion != PROG_ID_VERSION) {
|
|
|
|
opcode = "<RETURN_V>";
|
|
|
|
} else {
|
|
|
|
e->e.expr.e1 = new_float_expr (0);
|
|
|
|
}
|
|
|
|
}
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_func, opcode, e);
|
2011-02-07 01:21:54 +00:00
|
|
|
if (e->e.expr.e1)
|
2011-01-30 12:43:56 +00:00
|
|
|
sblock = statement_subexpr (sblock, e->e.expr.e1, &s->opa);
|
2011-01-20 14:26:13 +00:00
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
sblock->next = new_sblock ();
|
|
|
|
sblock = sblock->next;
|
2011-01-22 06:52:01 +00:00
|
|
|
break;
|
2011-01-20 14:26:13 +00:00
|
|
|
case 'g':
|
|
|
|
sblock = statement_branch (sblock, e);
|
|
|
|
break;
|
|
|
|
default:
|
2011-01-28 04:28:45 +00:00
|
|
|
debug (e, "e ue %d", e->e.expr.op);
|
2011-01-20 14:26:13 +00:00
|
|
|
if (options.warnings.executable)
|
2011-01-19 06:47:45 +00:00
|
|
|
warning (e, "Non-executable statement;"
|
|
|
|
" executing programmer instead.");
|
|
|
|
}
|
2011-01-20 14:26:13 +00:00
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_nonexec (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
if (options.warnings.executable)
|
|
|
|
warning (e, "Non-executable statement; executing programmer instead.");
|
|
|
|
return sblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
statement_slist (sblock_t *sblock, expr_t *e)
|
|
|
|
{
|
|
|
|
static statement_f sfuncs[] = {
|
|
|
|
statement_ignore, // ex_error
|
|
|
|
statement_state,
|
|
|
|
statement_bool,
|
|
|
|
statement_label,
|
2012-11-03 09:08:32 +00:00
|
|
|
0, // ex_labelref
|
2011-01-20 14:26:13 +00:00
|
|
|
statement_block,
|
|
|
|
statement_expr,
|
|
|
|
statement_uexpr,
|
|
|
|
statement_nonexec, // ex_symbol
|
|
|
|
statement_nonexec, // ex_temp
|
|
|
|
statement_nonexec, // ex_nil
|
|
|
|
statement_nonexec, // ex_value
|
|
|
|
};
|
|
|
|
|
|
|
|
for (/**/; e; e = e->next) {
|
|
|
|
if (e->type < 0 || e->type > ex_value)
|
|
|
|
internal_error (e, "bad expression type");
|
|
|
|
sblock = sfuncs[e->type] (sblock, e);
|
|
|
|
}
|
|
|
|
return sblock;
|
2011-01-19 06:47:45 +00:00
|
|
|
}
|
|
|
|
|
2012-12-19 10:45:05 +00:00
|
|
|
static void
|
|
|
|
move_labels (sblock_t *dst, sblock_t *src)
|
|
|
|
{
|
|
|
|
ex_label_t *src_labels = src->labels;
|
|
|
|
|
|
|
|
if (!src_labels)
|
|
|
|
return;
|
|
|
|
while (src_labels->next)
|
|
|
|
src_labels = src_labels->next;
|
|
|
|
src_labels->next = dst->labels;
|
|
|
|
dst->labels = src->labels;
|
|
|
|
src->labels = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
move_code (sblock_t *dst, sblock_t *src)
|
|
|
|
{
|
|
|
|
if (!src->statements)
|
|
|
|
return;
|
|
|
|
*dst->tail = src->statements;
|
|
|
|
dst->tail = src->tail;
|
|
|
|
src->statements = 0;
|
|
|
|
src->tail = &src->statements;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sblock_t *
|
|
|
|
merge_blocks (sblock_t *blocks)
|
|
|
|
{
|
|
|
|
sblock_t **sb;
|
|
|
|
sblock_t *sblock;
|
|
|
|
statement_t *s;
|
|
|
|
|
|
|
|
if (!blocks)
|
|
|
|
return blocks;
|
|
|
|
// merge any blocks that can be merged
|
|
|
|
for (sblock = blocks; sblock; sblock = sblock->next) {
|
|
|
|
if (sblock->statements && sblock->next) {
|
|
|
|
s = (statement_t *) sblock->tail;
|
|
|
|
// func and flow statements end blocks
|
|
|
|
if (s->type >= st_func)
|
|
|
|
continue;
|
|
|
|
// labels begin blocks
|
|
|
|
if (sblock->next->labels)
|
|
|
|
continue;
|
|
|
|
// blocks can be merged
|
|
|
|
move_code (sblock, sblock->next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (sb = &blocks; (*sb)->next;) {
|
|
|
|
if (!(*sb)->statements) {
|
|
|
|
// empty non-final block
|
|
|
|
// move labels from empty block to next block
|
|
|
|
if ((*sb)->labels)
|
|
|
|
move_labels ((*sb)->next, (*sb));
|
|
|
|
sblock = *sb;
|
|
|
|
*sb = (*sb)->next;
|
|
|
|
free_sblock (sblock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
sb = &(*sb)->next;
|
|
|
|
}
|
|
|
|
// so long as blocks doesn't become null, remove an empty final block
|
|
|
|
if (sb != &blocks) {
|
|
|
|
if (!(*sb)->statements && !(*sb)->labels) {
|
|
|
|
// empty final block with no labels
|
|
|
|
sblock = *sb;
|
|
|
|
*sb = (*sb)->next;
|
|
|
|
free_sblock (sblock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return blocks;
|
|
|
|
}
|
|
|
|
|
2011-01-28 02:44:19 +00:00
|
|
|
static void
|
|
|
|
remove_label_from_dest (ex_label_t *label)
|
|
|
|
{
|
|
|
|
sblock_t *sblock;
|
|
|
|
ex_label_t **l;
|
|
|
|
|
|
|
|
if (!label)
|
|
|
|
return;
|
|
|
|
|
2012-05-03 10:32:44 +00:00
|
|
|
debug (0, "dropping deceased label %s", label->name);
|
2011-01-28 02:44:19 +00:00
|
|
|
sblock = label->dest;
|
2012-05-04 13:35:20 +00:00
|
|
|
label->dest = 0;
|
2011-01-28 02:44:19 +00:00
|
|
|
for (l = &sblock->labels; *l; l = &(*l)->next) {
|
|
|
|
if (*l == label) {
|
|
|
|
*l = label->next;
|
|
|
|
label->next = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-04 05:01:39 +00:00
|
|
|
static void
|
|
|
|
unuse_label (ex_label_t *label)
|
|
|
|
{
|
|
|
|
if (label && !--label->used)
|
|
|
|
remove_label_from_dest (label);
|
|
|
|
}
|
|
|
|
|
2012-05-03 08:42:58 +00:00
|
|
|
static void
|
|
|
|
thread_jumps (sblock_t *blocks)
|
|
|
|
{
|
|
|
|
sblock_t *sblock;
|
|
|
|
|
|
|
|
if (!blocks)
|
|
|
|
return;
|
2012-05-04 05:10:30 +00:00
|
|
|
for (sblock = blocks; sblock; sblock = sblock->next) {
|
2012-05-03 10:32:44 +00:00
|
|
|
statement_t *s;
|
|
|
|
ex_label_t **label, *l;
|
|
|
|
|
2012-11-19 07:11:07 +00:00
|
|
|
if (!sblock->statements)
|
|
|
|
continue;
|
2012-05-03 08:42:58 +00:00
|
|
|
s = (statement_t *) sblock->tail;
|
2012-11-30 05:06:52 +00:00
|
|
|
if (statement_is_goto (s))
|
2012-05-03 10:32:44 +00:00
|
|
|
label = &s->opa->o.label;
|
2012-11-30 05:06:52 +00:00
|
|
|
else if (statement_is_cond (s))
|
2012-05-03 10:32:44 +00:00
|
|
|
label = &s->opb->o.label;
|
2012-05-03 08:42:58 +00:00
|
|
|
else
|
|
|
|
continue;
|
2012-05-03 10:32:44 +00:00
|
|
|
for (l = *label;
|
2012-11-30 05:06:52 +00:00
|
|
|
l->dest->statements && statement_is_goto (l->dest->statements);
|
2012-05-03 08:42:58 +00:00
|
|
|
l = l->dest->statements->opa->o.label) {
|
|
|
|
}
|
2012-05-03 10:32:44 +00:00
|
|
|
if (l != *label) {
|
2012-05-04 05:01:39 +00:00
|
|
|
unuse_label (*label);
|
2012-05-03 10:32:44 +00:00
|
|
|
l->used++;
|
|
|
|
*label = l;
|
2012-05-03 08:42:58 +00:00
|
|
|
}
|
2012-11-30 05:06:52 +00:00
|
|
|
if (statement_is_goto (s) && (*label)->dest == sblock->next) {
|
2012-11-24 06:08:48 +00:00
|
|
|
statement_t **p;
|
|
|
|
unuse_label (*label);
|
|
|
|
for (p = &sblock->statements; *p != s; p = &(*p)->next)
|
|
|
|
;
|
|
|
|
free_statement (s);
|
|
|
|
*p = 0;
|
|
|
|
sblock->tail = p;
|
|
|
|
}
|
2012-05-03 08:42:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-28 02:45:19 +00:00
|
|
|
static void
|
|
|
|
check_final_block (sblock_t *sblock)
|
|
|
|
{
|
|
|
|
statement_t *s;
|
|
|
|
symbol_t *return_symbol = 0;
|
2012-12-05 10:47:22 +00:00
|
|
|
def_t *return_def = 0;
|
2011-01-28 02:45:19 +00:00
|
|
|
operand_t *return_operand = 0;
|
|
|
|
const char *return_opcode = "<RETURN_V>";
|
|
|
|
|
|
|
|
if (!sblock)
|
|
|
|
return;
|
|
|
|
while (sblock->next)
|
|
|
|
sblock = sblock->next;
|
2012-11-19 07:11:07 +00:00
|
|
|
if (sblock->statements) {
|
|
|
|
s = (statement_t *) sblock->tail;
|
2012-11-30 05:06:52 +00:00
|
|
|
if (statement_is_goto (s))
|
2012-11-19 07:11:07 +00:00
|
|
|
return; // the end of function is the end of a loop
|
2012-11-30 05:06:52 +00:00
|
|
|
if (statement_is_return (s))
|
2012-11-19 07:11:07 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-01-28 02:45:19 +00:00
|
|
|
if (current_func->sym->type->t.func.type != &type_void)
|
|
|
|
warning (0, "control reaches end of non-void function");
|
|
|
|
if (options.traditional || options.code.progsversion == PROG_ID_VERSION) {
|
2012-11-27 11:32:52 +00:00
|
|
|
return_symbol = make_symbol (".return", &type_param, pr.symtab->space,
|
2012-12-02 01:11:30 +00:00
|
|
|
sc_extern);
|
2012-12-05 10:47:22 +00:00
|
|
|
return_def = return_symbol->s.def;
|
2011-01-28 02:45:19 +00:00
|
|
|
return_opcode = "<RETURN>";
|
|
|
|
}
|
2012-12-13 03:49:00 +00:00
|
|
|
if (return_symbol)
|
|
|
|
return_operand = def_operand (return_def, &type_void);
|
2012-11-16 07:16:06 +00:00
|
|
|
s = new_statement (st_func, return_opcode, 0);
|
2011-01-28 02:45:19 +00:00
|
|
|
s->opa = return_operand;
|
|
|
|
sblock_add_statement (sblock, s);
|
|
|
|
}
|
|
|
|
|
2012-11-07 05:07:21 +00:00
|
|
|
void
|
2012-11-07 02:00:08 +00:00
|
|
|
dump_dot_sblock (void *data, const char *fname)
|
|
|
|
{
|
|
|
|
print_sblock ((sblock_t *) data, fname);
|
|
|
|
}
|
|
|
|
|
2011-01-19 06:47:45 +00:00
|
|
|
sblock_t *
|
|
|
|
make_statements (expr_t *e)
|
|
|
|
{
|
2011-01-20 14:26:13 +00:00
|
|
|
sblock_t *sblock = new_sblock ();
|
2012-11-26 12:00:28 +00:00
|
|
|
if (options.block_dot.expr)
|
|
|
|
dump_dot ("expr", e, dump_dot_expr);
|
2011-01-20 14:26:13 +00:00
|
|
|
statement_slist (sblock, e);
|
2012-12-19 10:45:05 +00:00
|
|
|
sblock = merge_blocks (sblock);
|
2012-05-04 00:45:51 +00:00
|
|
|
if (options.block_dot.initial)
|
2012-11-07 02:00:08 +00:00
|
|
|
dump_dot ("initial", sblock, dump_dot_sblock);
|
2012-05-03 08:42:58 +00:00
|
|
|
thread_jumps (sblock);
|
2012-05-04 00:45:51 +00:00
|
|
|
if (options.block_dot.thread)
|
2012-11-07 02:00:08 +00:00
|
|
|
dump_dot ("thread", sblock, dump_dot_sblock);
|
2011-01-28 02:45:19 +00:00
|
|
|
check_final_block (sblock);
|
2012-05-04 00:45:51 +00:00
|
|
|
if (options.block_dot.final)
|
2012-11-07 02:00:08 +00:00
|
|
|
dump_dot ("final", sblock, dump_dot_sblock);
|
2012-07-15 12:10:51 +00:00
|
|
|
|
2011-01-20 14:26:13 +00:00
|
|
|
return sblock;
|
2011-01-19 06:47:45 +00:00
|
|
|
}
|
2012-12-01 02:13:45 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
count_temp (operand_t *op)
|
|
|
|
{
|
|
|
|
if (!op)
|
|
|
|
return;
|
2012-12-05 10:47:22 +00:00
|
|
|
if (op->op_type == op_temp) {
|
|
|
|
while (op->o.tempop.alias)
|
|
|
|
op = op->o.tempop.alias;
|
2012-12-01 02:13:45 +00:00
|
|
|
op->o.tempop.users++;
|
2012-12-05 10:47:22 +00:00
|
|
|
}
|
2012-12-01 02:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
statements_count_temps (sblock_t *sblock)
|
|
|
|
{
|
|
|
|
statement_t *st;
|
|
|
|
|
|
|
|
while (sblock) {
|
|
|
|
for (st = sblock->statements; st; st = st->next) {
|
|
|
|
count_temp (st->opa);
|
|
|
|
count_temp (st->opb);
|
|
|
|
count_temp (st->opc);
|
|
|
|
}
|
|
|
|
sblock = sblock->next;
|
|
|
|
}
|
|
|
|
}
|