2012-05-08 02:44:16 +00:00
|
|
|
/*
|
|
|
|
dags.c
|
|
|
|
|
|
|
|
DAG representation of basic blocks
|
|
|
|
|
|
|
|
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2012/05/08
|
|
|
|
|
|
|
|
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-10-30 03:28:22 +00:00
|
|
|
#include "QF/dstring.h"
|
2012-11-07 05:11:26 +00:00
|
|
|
#include "QF/mathlib.h"
|
2012-10-30 03:28:22 +00:00
|
|
|
|
2012-05-08 02:44:16 +00:00
|
|
|
#include "dags.h"
|
2012-05-08 23:43:48 +00:00
|
|
|
#include "diagnostic.h"
|
2012-11-05 10:00:57 +00:00
|
|
|
#include "flow.h"
|
2012-05-08 23:43:48 +00:00
|
|
|
#include "qfcc.h"
|
2012-11-05 10:00:57 +00:00
|
|
|
#include "set.h"
|
2012-05-08 02:44:16 +00:00
|
|
|
#include "statements.h"
|
2012-10-30 03:28:22 +00:00
|
|
|
#include "strpool.h"
|
2012-05-08 02:44:16 +00:00
|
|
|
#include "symtab.h"
|
|
|
|
|
2012-05-08 23:43:48 +00:00
|
|
|
static daglabel_t *free_labels;
|
|
|
|
static dagnode_t *free_nodes;
|
|
|
|
|
2012-07-19 02:38:02 +00:00
|
|
|
static daglabel_t *daglabel_chain;
|
|
|
|
|
|
|
|
static void
|
|
|
|
flush_daglabels (void)
|
|
|
|
{
|
|
|
|
while (daglabel_chain) {
|
|
|
|
operand_t *op;
|
|
|
|
|
|
|
|
if ((op = daglabel_chain->op)) {
|
|
|
|
while (op->op_type == op_alias)
|
|
|
|
op = op->o.alias;
|
|
|
|
if (op->op_type == op_symbol)
|
|
|
|
op->o.symbol->daglabel = 0;
|
|
|
|
else if (op->op_type == op_temp)
|
|
|
|
op->o.tempop.daglabel = 0;
|
|
|
|
else if (op->op_type == op_value || op->op_type == op_pointer)
|
|
|
|
op->o.value->daglabel = 0;
|
|
|
|
else
|
|
|
|
internal_error (0, "unexpected operand type");
|
|
|
|
}
|
|
|
|
daglabel_chain = daglabel_chain->daglabel_chain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-08 23:43:48 +00:00
|
|
|
static daglabel_t *
|
|
|
|
new_label (void)
|
|
|
|
{
|
|
|
|
daglabel_t *label;
|
|
|
|
ALLOC (256, daglabel_t, labels, label);
|
2012-07-19 02:38:02 +00:00
|
|
|
label->daglabel_chain = daglabel_chain;
|
|
|
|
daglabel_chain = label;
|
2012-05-08 23:43:48 +00:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dagnode_t *
|
|
|
|
new_node (void)
|
|
|
|
{
|
|
|
|
dagnode_t *node;
|
|
|
|
ALLOC (256, dagnode_t, nodes, node);
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-05-08 02:44:16 +00:00
|
|
|
const char *
|
|
|
|
daglabel_string (daglabel_t *label)
|
|
|
|
{
|
2012-10-30 03:28:22 +00:00
|
|
|
static dstring_t *str;
|
2012-07-15 12:10:51 +00:00
|
|
|
if ((label->opcode && label->op) || (!label->opcode && !label->op))
|
2012-05-08 02:44:16 +00:00
|
|
|
return "bad label";
|
|
|
|
if (label->opcode)
|
|
|
|
return label->opcode;
|
2012-10-30 03:28:22 +00:00
|
|
|
if (!str)
|
|
|
|
str = dstring_new ();
|
|
|
|
// operand_string might use quote_string, which returns a pointer to
|
|
|
|
// a static variable.
|
|
|
|
dstring_copystr (str, operand_string (label->op));
|
|
|
|
return quote_string (str->str);
|
2012-05-08 02:44:16 +00:00
|
|
|
}
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-07-15 12:10:51 +00:00
|
|
|
static daglabel_t *
|
|
|
|
opcode_label (const char *opcode)
|
2012-05-08 23:43:48 +00:00
|
|
|
{
|
|
|
|
daglabel_t *label;
|
2012-07-15 12:10:51 +00:00
|
|
|
|
|
|
|
label = new_label ();
|
|
|
|
label->opcode = opcode;
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2012-11-06 23:50:28 +00:00
|
|
|
static daglabel_t *
|
2012-07-15 12:10:51 +00:00
|
|
|
operand_label (operand_t *op)
|
|
|
|
{
|
2012-05-08 23:43:48 +00:00
|
|
|
symbol_t *sym = 0;
|
2012-07-19 01:42:05 +00:00
|
|
|
ex_value_t *val = 0;
|
2012-07-15 12:10:51 +00:00
|
|
|
daglabel_t *label;
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-07-15 12:10:51 +00:00
|
|
|
if (!op)
|
|
|
|
return 0;
|
2012-11-15 06:53:51 +00:00
|
|
|
while (op->op_type == op_alias)
|
|
|
|
op = op->o.alias;
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-11-15 06:53:51 +00:00
|
|
|
if (op->op_type == op_temp) {
|
|
|
|
if (op->o.tempop.daglabel)
|
|
|
|
return op->o.tempop.daglabel;
|
2012-07-16 08:37:13 +00:00
|
|
|
label = new_label ();
|
|
|
|
label->op = op;
|
2012-11-15 06:53:51 +00:00
|
|
|
op->o.tempop.daglabel = label;
|
|
|
|
} else if (op->op_type == op_symbol) {
|
|
|
|
sym = op->o.symbol;
|
2012-07-16 08:37:13 +00:00
|
|
|
if (sym->daglabel)
|
|
|
|
return sym->daglabel;
|
|
|
|
label = new_label ();
|
|
|
|
label->op = op;
|
2012-05-08 23:43:48 +00:00
|
|
|
sym->daglabel = label;
|
2012-11-15 06:53:51 +00:00
|
|
|
} else if (op->op_type == op_value || op->op_type == op_pointer) {
|
|
|
|
val = op->o.value;
|
2012-07-19 01:42:05 +00:00
|
|
|
if (val->daglabel)
|
|
|
|
return val->daglabel;
|
|
|
|
label = new_label ();
|
|
|
|
label->op = op;
|
|
|
|
val->daglabel = label;
|
|
|
|
} else {
|
2012-11-15 06:53:51 +00:00
|
|
|
internal_error (0, "unexpected operand type: %d", op->op_type);
|
2012-07-16 08:37:13 +00:00
|
|
|
}
|
2012-07-15 12:10:51 +00:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dagnode_t *
|
|
|
|
leaf_node (operand_t *op)
|
|
|
|
{
|
|
|
|
daglabel_t *label;
|
|
|
|
dagnode_t *node;
|
|
|
|
|
|
|
|
if (!op)
|
|
|
|
return 0;
|
2012-05-08 23:43:48 +00:00
|
|
|
node = new_node ();
|
2012-11-15 08:58:33 +00:00
|
|
|
node->tl = op->type;
|
|
|
|
label = operand_label (op);
|
2012-07-15 12:10:51 +00:00
|
|
|
label->dagnode = node;
|
2012-11-15 08:58:33 +00:00
|
|
|
node->label = label;
|
2012-05-08 23:43:48 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-07-15 12:10:51 +00:00
|
|
|
static dagnode_t *
|
|
|
|
node (operand_t *op)
|
|
|
|
{
|
|
|
|
symbol_t *sym;
|
|
|
|
|
|
|
|
if (!op)
|
|
|
|
return 0;
|
2012-11-15 06:53:51 +00:00
|
|
|
while (op->op_type == op_alias)
|
|
|
|
op = op->o.alias;
|
|
|
|
if (op->op_type == op_symbol) {
|
|
|
|
sym = op->o.symbol;
|
2012-07-16 08:37:13 +00:00
|
|
|
if (sym->sy_type == sy_const)
|
|
|
|
return 0;
|
|
|
|
if (sym->daglabel)
|
|
|
|
return sym->daglabel->dagnode;
|
2012-11-15 06:53:51 +00:00
|
|
|
} else if (op->op_type == op_temp) {
|
|
|
|
if (op->o.tempop.daglabel)
|
|
|
|
return op->o.tempop.daglabel->dagnode;
|
2012-07-16 08:37:13 +00:00
|
|
|
}
|
2012-07-15 12:10:51 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dagnode_match (const dagnode_t *n, const daglabel_t *op,
|
|
|
|
const dagnode_t *y, const dagnode_t *z, const dagnode_t *w)
|
|
|
|
{
|
|
|
|
if (n->label->opcode != op->opcode)
|
|
|
|
return 0;
|
|
|
|
if (n->a && y && n->a->label->op != y->label->op)
|
|
|
|
return 0;
|
|
|
|
if (n->b && z && n->b->label->op != z->label->op)
|
|
|
|
return 0;
|
|
|
|
if (n->c && w && n->c->label->op != w->label->op)
|
|
|
|
return 0;
|
|
|
|
if ((!n->a) ^ (!y))
|
|
|
|
return 0;
|
|
|
|
if ((!n->c) ^ (!z))
|
|
|
|
return 0;
|
|
|
|
if ((!n->b) ^ (!w))
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-19 00:59:17 +00:00
|
|
|
static int
|
|
|
|
op_is_identifer (operand_t *op)
|
|
|
|
{
|
|
|
|
while (op->op_type == op_alias)
|
|
|
|
op = op->o.alias;
|
|
|
|
if (op->op_type == op_pointer)
|
|
|
|
return 1;
|
|
|
|
if (op->op_type == op_temp)
|
|
|
|
return 1;
|
|
|
|
if (op->op_type != op_symbol)
|
|
|
|
return 0;
|
|
|
|
if (op->o.symbol->sy_type != sy_var)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-19 00:27:42 +00:00
|
|
|
static void
|
|
|
|
dagnode_attach_label (dagnode_t *n, daglabel_t *l)
|
|
|
|
{
|
2012-07-19 00:59:17 +00:00
|
|
|
if (!l->op)
|
|
|
|
internal_error (0, "attempt to attach operator label to dagnode "
|
|
|
|
"identifers");
|
|
|
|
if (!op_is_identifer (l->op))
|
|
|
|
internal_error (0, "attempt to attach non-identifer label to dagnode "
|
|
|
|
"identifers");
|
2012-07-19 00:27:42 +00:00
|
|
|
if (n->identifiers)
|
|
|
|
n->identifiers->prev = &l->next;
|
|
|
|
l->next = n->identifiers;
|
|
|
|
l->prev = &n->identifiers;
|
|
|
|
l->dagnode = n;
|
|
|
|
n->identifiers = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
daglabel_detatch (daglabel_t *l)
|
|
|
|
{
|
|
|
|
if (l->next)
|
|
|
|
l->next->prev = l->prev;
|
|
|
|
*l->prev = l->next;
|
|
|
|
l->dagnode = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-15 06:53:51 +00:00
|
|
|
static statement_t *
|
|
|
|
build_statement (const char *opcode, operand_t *a, operand_t *b, operand_t *c,
|
|
|
|
expr_t *expr)
|
|
|
|
{
|
|
|
|
if ((!a && (b || c)) || (a && !b && c))
|
|
|
|
internal_error (0, "invalid operand combo");
|
2012-11-16 07:16:06 +00:00
|
|
|
statement_t *st = new_statement (st_none, opcode, expr);//FIXME
|
2012-11-15 06:53:51 +00:00
|
|
|
st->opa = a;
|
|
|
|
st->opb = b;
|
|
|
|
st->opc = c;
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
|
2012-05-08 23:43:48 +00:00
|
|
|
dagnode_t *
|
2012-11-07 02:28:33 +00:00
|
|
|
dag_create (const flownode_t *flownode)
|
2012-05-08 23:43:48 +00:00
|
|
|
{
|
2012-11-07 02:28:33 +00:00
|
|
|
sblock_t *block = flownode->sblock;
|
2012-05-08 23:43:48 +00:00
|
|
|
statement_t *s;
|
2012-07-15 12:10:51 +00:00
|
|
|
dagnode_t *dagnodes = 0;
|
2012-11-06 23:34:55 +00:00
|
|
|
dagnode_t **dagtail = &dagnodes;
|
2012-07-19 02:38:02 +00:00
|
|
|
dagnode_t *d;
|
|
|
|
|
|
|
|
flush_daglabels ();
|
2012-05-08 23:43:48 +00:00
|
|
|
|
|
|
|
for (s = block->statements; s; s = s->next) {
|
2012-07-15 12:10:51 +00:00
|
|
|
operand_t *x = 0, *y = 0, *z = 0, *w = 0;
|
2012-07-19 00:27:42 +00:00
|
|
|
dagnode_t *n = 0, *ny, *nz, *nw;
|
2012-07-17 05:03:04 +00:00
|
|
|
daglabel_t *op, *lx;
|
2012-11-16 10:33:37 +00:00
|
|
|
int simp = 0;
|
2012-07-15 12:10:51 +00:00
|
|
|
|
2012-11-16 10:33:37 +00:00
|
|
|
//simp = find_operands (s, &x, &y, &z, &w);
|
2012-07-19 02:38:02 +00:00
|
|
|
if (!(ny = node (y))) {
|
2012-07-15 12:10:51 +00:00
|
|
|
ny = leaf_node (y);
|
2012-07-19 02:38:02 +00:00
|
|
|
if (simp) {
|
2012-11-06 23:34:55 +00:00
|
|
|
*dagtail = ny;
|
|
|
|
dagtail = &ny->next;
|
2012-07-19 02:38:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-15 12:10:51 +00:00
|
|
|
if (!(nz = node (z)))
|
|
|
|
nz = leaf_node (z);
|
|
|
|
if (!(nw = node (w)))
|
|
|
|
nw = leaf_node (w);
|
|
|
|
op = opcode_label (s->opcode);
|
|
|
|
if (simp) {
|
|
|
|
n = ny;
|
|
|
|
} else {
|
|
|
|
for (n = dagnodes; n; n = n->next)
|
|
|
|
if (dagnode_match (n, op, ny, nz, nw))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!n) {
|
|
|
|
n = new_node ();
|
2012-11-15 06:53:51 +00:00
|
|
|
n->statement = s;
|
2012-07-15 12:10:51 +00:00
|
|
|
n->label = op;
|
|
|
|
n->a = ny;
|
|
|
|
n->b = nz;
|
|
|
|
n->c = nw;
|
2012-11-15 06:53:51 +00:00
|
|
|
if (ny) {
|
2012-07-19 02:38:02 +00:00
|
|
|
ny->is_child = 1;
|
2012-11-15 06:53:51 +00:00
|
|
|
n->ta = y->type;
|
|
|
|
}
|
|
|
|
if (nz) {
|
2012-07-19 02:38:02 +00:00
|
|
|
nz->is_child = 1;
|
2012-11-15 06:53:51 +00:00
|
|
|
n->tb = z->type;
|
|
|
|
}
|
|
|
|
if (nw) {
|
2012-07-19 02:38:02 +00:00
|
|
|
nw->is_child = 1;
|
2012-11-15 06:53:51 +00:00
|
|
|
n->tc = w->type;
|
|
|
|
}
|
2012-11-06 23:34:55 +00:00
|
|
|
*dagtail = n;
|
|
|
|
dagtail = &n->next;
|
2012-07-15 12:10:51 +00:00
|
|
|
}
|
2012-07-17 05:03:04 +00:00
|
|
|
lx = operand_label (x);
|
|
|
|
if (lx) {
|
2012-07-19 00:27:42 +00:00
|
|
|
if (lx->prev)
|
|
|
|
daglabel_detatch (lx);
|
|
|
|
dagnode_attach_label (n, lx);
|
2012-07-15 12:10:51 +00:00
|
|
|
}
|
2012-05-08 23:43:48 +00:00
|
|
|
// c = a * b
|
|
|
|
// c = ~a
|
|
|
|
// c = a / b
|
|
|
|
// c = a + b
|
|
|
|
// c = a - b
|
|
|
|
// c = a {==,!=,<=,>=,<,>} b
|
|
|
|
// c = a.b
|
|
|
|
// c = &a.b
|
|
|
|
// c = a (convert)
|
|
|
|
// b = a
|
|
|
|
// b .= a
|
|
|
|
// b.c = a
|
|
|
|
// c = !a
|
|
|
|
// cond a goto b
|
|
|
|
// callN a
|
|
|
|
// rcallN a, [b, [c]]
|
|
|
|
// state a, b
|
|
|
|
// state a, b, c
|
|
|
|
// goto a
|
|
|
|
// jump a
|
|
|
|
// jumpb a, b
|
|
|
|
// c = a &&/|| b
|
|
|
|
// c = a <</>> b
|
|
|
|
// c = a & b
|
|
|
|
// c = a | b
|
|
|
|
// c = a % b
|
|
|
|
// c = a ^ b
|
|
|
|
// c = a (move) b (count)
|
|
|
|
}
|
2012-11-07 02:49:17 +00:00
|
|
|
for (d = dagnodes; d; d = d->next) {
|
|
|
|
daglabel_t **l = &d->identifiers;
|
|
|
|
|
|
|
|
while (*l) {
|
|
|
|
if ((*l)->op->op_type == op_temp
|
|
|
|
&& !set_is_member (flownode->live_vars.out,
|
|
|
|
flow_get_var ((*l)->op)->number))
|
|
|
|
daglabel_detatch (*l);
|
|
|
|
else
|
|
|
|
l = &(*l)->next;
|
|
|
|
}
|
|
|
|
}
|
2012-07-19 02:38:02 +00:00
|
|
|
while (dagnodes->is_child) {
|
|
|
|
dagnode_t *n = dagnodes->next;
|
|
|
|
dagnodes->next = 0;
|
|
|
|
dagnodes = n;
|
|
|
|
}
|
|
|
|
for (d = dagnodes; d && d->next; d = d->next) {
|
|
|
|
while (d->next && d->next->is_child) {
|
|
|
|
dagnode_t *n = d->next->next;
|
|
|
|
d->next->next = 0;
|
|
|
|
d->next = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dagnodes;
|
2012-05-08 23:43:48 +00:00
|
|
|
}
|
2012-11-07 05:11:26 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
dag_calc_node_costs (dagnode_t *dagnode)
|
|
|
|
{
|
|
|
|
if ((!dagnode->a && (dagnode->b || dagnode->c))
|
|
|
|
|| (dagnode->a && !dagnode->b && dagnode->c))
|
|
|
|
internal_error (0, "bad dag node");
|
|
|
|
|
|
|
|
if (dagnode->a)
|
|
|
|
dag_calc_node_costs (dagnode->a);
|
|
|
|
if (dagnode->b)
|
|
|
|
dag_calc_node_costs (dagnode->b);
|
|
|
|
if (dagnode->c)
|
|
|
|
dag_calc_node_costs (dagnode->c);
|
|
|
|
|
|
|
|
// if dagnode->a is null, then this is a leaf (as b and c are guaranted to
|
|
|
|
// be null)
|
|
|
|
if (!dagnode->a) {
|
|
|
|
// Because qc vm statements don't mix source and destination operands,
|
|
|
|
// leaves never need temporary variables.
|
|
|
|
dagnode->cost = 0;
|
|
|
|
} else {
|
|
|
|
int different = 0;
|
|
|
|
|
|
|
|
// a non-leaf is guaranteed to have a valid "a"
|
|
|
|
dagnode->cost = dagnode->a->cost;
|
|
|
|
if (dagnode->b && dagnode->b->cost != dagnode->cost) {
|
|
|
|
dagnode->cost = max (dagnode->cost, dagnode->b->cost);
|
|
|
|
different = 1;
|
|
|
|
}
|
|
|
|
if (dagnode->c && (different || dagnode->c->cost != dagnode->cost)) {
|
|
|
|
dagnode->cost = max (dagnode->cost, dagnode->c->cost);
|
|
|
|
different = 1;
|
|
|
|
}
|
|
|
|
if (!different)
|
|
|
|
dagnode->cost += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-15 08:15:34 +00:00
|
|
|
static operand_t *
|
|
|
|
fix_op_type (operand_t *op, etype_t type)
|
|
|
|
{
|
|
|
|
if (op && op->op_type != op_label && op->type != type)
|
|
|
|
op = alias_operand (op, type);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
2012-11-07 05:11:26 +00:00
|
|
|
static operand_t *
|
|
|
|
dag_gencode (sblock_t *block, const dagnode_t *dagnode)
|
|
|
|
{
|
2012-11-15 06:53:51 +00:00
|
|
|
if (!dagnode->a) {
|
|
|
|
statement_t *st;
|
|
|
|
daglabel_t *var = 0;
|
2012-11-15 08:58:33 +00:00
|
|
|
operand_t *op = fix_op_type (dagnode->label->op, dagnode->tl);
|
2012-11-15 06:53:51 +00:00
|
|
|
|
|
|
|
if (dagnode->identifiers) {
|
|
|
|
var = dagnode->identifiers;
|
|
|
|
}
|
|
|
|
for (var = dagnode->identifiers; var; var = var->next) {
|
2012-11-15 08:58:33 +00:00
|
|
|
operand_t *vop = fix_op_type (var->op, op->type);
|
|
|
|
st = build_statement ("=", op, vop, 0, 0);
|
2012-11-15 06:53:51 +00:00
|
|
|
sblock_add_statement (block, st);
|
|
|
|
}
|
|
|
|
return op;
|
|
|
|
} else {
|
|
|
|
statement_t *st;
|
|
|
|
daglabel_t *var = 0;
|
|
|
|
operand_t *op_a = 0;
|
|
|
|
operand_t *op_b = 0;
|
|
|
|
operand_t *op_c = 0;
|
|
|
|
|
|
|
|
if (flow_is_cond (dagnode->statement))
|
|
|
|
op_b = dagnode->statement->opb;
|
|
|
|
|
|
|
|
if (dagnode->b && dagnode->c) {
|
|
|
|
op_a = dag_gencode (block, dagnode->a);
|
|
|
|
op_b = dag_gencode (block, dagnode->b);
|
|
|
|
op_c = dag_gencode (block, dagnode->c);
|
2012-11-15 08:58:33 +00:00
|
|
|
op_c = fix_op_type (op_c, dagnode->tc);
|
2012-11-15 06:53:51 +00:00
|
|
|
} else if (dagnode->b) {
|
|
|
|
if (dagnode->a->cost < dagnode->b->cost) {
|
|
|
|
op_b = dag_gencode (block, dagnode->b);
|
|
|
|
op_a = dag_gencode (block, dagnode->a);
|
|
|
|
} else {
|
|
|
|
op_a = dag_gencode (block, dagnode->a);
|
|
|
|
op_b = dag_gencode (block, dagnode->b);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
op_a = dag_gencode (block, dagnode->a);
|
|
|
|
}
|
|
|
|
if (op_a && op_b && get_type (dagnode->statement->expr)) {
|
|
|
|
if (dagnode->identifiers) {
|
|
|
|
op_c = dagnode->identifiers->op;
|
|
|
|
var = dagnode->identifiers->next;
|
2012-11-15 08:58:33 +00:00
|
|
|
op_c = fix_op_type (op_c,
|
|
|
|
extract_type (dagnode->statement->expr));
|
2012-11-15 06:53:51 +00:00
|
|
|
} else {
|
|
|
|
op_c = temp_operand (get_type (dagnode->statement->expr));
|
|
|
|
}
|
|
|
|
}
|
2012-11-15 08:15:34 +00:00
|
|
|
op_a = fix_op_type (op_a, dagnode->ta);
|
|
|
|
op_b = fix_op_type (op_b, dagnode->tb);
|
2012-11-15 06:53:51 +00:00
|
|
|
st = build_statement (dagnode->label->opcode, op_a, op_b, op_c,
|
|
|
|
dagnode->statement->expr);
|
|
|
|
sblock_add_statement (block, st);
|
|
|
|
while (var) {
|
2012-11-15 08:58:33 +00:00
|
|
|
operand_t *vop = fix_op_type (var->op, op_c->type);
|
|
|
|
st = build_statement ("=", op_c, vop, 0, 0);
|
2012-11-15 06:53:51 +00:00
|
|
|
sblock_add_statement (block, st);
|
|
|
|
var = var->next;
|
|
|
|
}
|
|
|
|
return op_c;
|
|
|
|
}
|
2012-11-07 05:11:26 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dag_generate (sblock_t *block, const flownode_t *flownode)
|
|
|
|
{
|
|
|
|
const dagnode_t *dag;
|
|
|
|
|
|
|
|
dag_calc_node_costs (flownode->dag);
|
|
|
|
for (dag = flownode->dag; dag; dag = dag->next) {
|
2012-11-15 08:58:33 +00:00
|
|
|
//if (!dag->a || (strcmp (dag->label->opcode, ".=") && !dag->identifiers))
|
|
|
|
// continue;
|
2012-11-07 05:11:26 +00:00
|
|
|
dag_gencode (block, dag);
|
|
|
|
}
|
|
|
|
}
|