2001-09-28 07:09:38 +00:00
|
|
|
/*
|
2002-10-22 14:53:18 +00:00
|
|
|
emit.c
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
statement emittion
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
|
2001-09-28 07:09:38 +00:00
|
|
|
|
2002-10-22 14:53:18 +00:00
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2001/07/26
|
2001-09-28 07:09:38 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2003-01-15 15:31:36 +00:00
|
|
|
|
2002-06-01 04:41:25 +00:00
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
2001-07-26 05:15:34 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <QF/mathlib.h>
|
|
|
|
#include <QF/va.h>
|
|
|
|
|
2011-01-09 10:41:24 +00:00
|
|
|
#include "codespace.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "def.h"
|
2011-01-25 06:46:48 +00:00
|
|
|
#include "defspace.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "debug.h"
|
2011-01-25 06:46:48 +00:00
|
|
|
#include "diagnostic.h"
|
2002-07-08 18:53:07 +00:00
|
|
|
#include "emit.h"
|
2008-08-01 13:54:24 +00:00
|
|
|
#include "function.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "opcodes.h"
|
2002-06-04 18:44:03 +00:00
|
|
|
#include "options.h"
|
2002-06-04 21:23:39 +00:00
|
|
|
#include "qfcc.h"
|
2002-06-07 17:29:30 +00:00
|
|
|
#include "reloc.h"
|
2011-01-25 06:46:48 +00:00
|
|
|
#include "statements.h"
|
2011-01-13 05:54:24 +00:00
|
|
|
#include "symtab.h"
|
2002-05-09 17:11:14 +00:00
|
|
|
#include "type.h"
|
2011-03-22 03:24:39 +00:00
|
|
|
#include "value.h"
|
2001-07-26 05:15:34 +00:00
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
static def_t zero_def;
|
|
|
|
|
|
|
|
static def_t *
|
2011-02-06 11:08:54 +00:00
|
|
|
get_value_def (ex_value_t *value, etype_t type)
|
2001-07-26 05:15:34 +00:00
|
|
|
{
|
2011-01-25 06:46:48 +00:00
|
|
|
def_t *def;
|
|
|
|
|
2011-02-09 13:13:43 +00:00
|
|
|
if (type == ev_short) {
|
|
|
|
def = new_def (0, &type_short, 0, st_extern);
|
|
|
|
def->offset = value->v.short_val;
|
|
|
|
return def;
|
|
|
|
}
|
2011-02-13 05:13:32 +00:00
|
|
|
def = emit_value (value, 0);
|
|
|
|
if (type != def->type->type)
|
|
|
|
return alias_def (def, ev_types[type]);
|
|
|
|
return def;
|
2011-02-06 11:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static def_t *
|
2011-02-08 09:18:34 +00:00
|
|
|
get_operand_def (expr_t *expr, operand_t *op)
|
2011-02-06 11:08:54 +00:00
|
|
|
{
|
2011-02-08 05:45:48 +00:00
|
|
|
def_t *def;
|
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
if (!op)
|
|
|
|
return 0;
|
|
|
|
switch (op->op_type) {
|
|
|
|
case op_symbol:
|
2011-01-26 05:48:22 +00:00
|
|
|
switch (op->o.symbol->sy_type) {
|
|
|
|
case sy_var:
|
|
|
|
if (op->type != op->o.symbol->type->type)
|
|
|
|
return alias_def (op->o.symbol->s.def,
|
|
|
|
ev_types[op->type]);
|
|
|
|
return op->o.symbol->s.def;
|
|
|
|
case sy_func:
|
|
|
|
return op->o.symbol->s.func->def;
|
|
|
|
case sy_const:
|
2011-02-06 11:08:54 +00:00
|
|
|
return get_value_def (&op->o.symbol->s.value, op->type);
|
2011-01-26 05:48:22 +00:00
|
|
|
case sy_type:
|
|
|
|
case sy_expr:
|
2011-02-13 07:05:09 +00:00
|
|
|
case sy_class:
|
2011-02-08 09:18:34 +00:00
|
|
|
internal_error (expr, "invalid operand type");
|
2011-01-26 05:48:22 +00:00
|
|
|
}
|
|
|
|
break;
|
2011-01-25 06:46:48 +00:00
|
|
|
case op_value:
|
2011-02-06 11:08:54 +00:00
|
|
|
return get_value_def (op->o.value, op->type);
|
2011-01-25 06:46:48 +00:00
|
|
|
case op_label:
|
2011-01-26 13:43:59 +00:00
|
|
|
zero_def.type = &type_short;
|
2011-01-25 06:46:48 +00:00
|
|
|
return &zero_def; //FIXME
|
|
|
|
case op_temp:
|
2011-03-23 12:32:14 +00:00
|
|
|
if (!op->o.def)
|
|
|
|
op->o.def = temp_def (op->type, op->size);
|
2011-01-25 06:46:48 +00:00
|
|
|
return op->o.def;
|
2011-02-08 05:45:48 +00:00
|
|
|
case op_pointer:
|
|
|
|
def = op->o.pointer->def;
|
|
|
|
if (op->o.pointer->val || op->type != def->type->type) {
|
|
|
|
def = alias_def (def, ev_types[op->type]);
|
|
|
|
def->offset = op->o.pointer->val;
|
2011-02-15 13:43:07 +00:00
|
|
|
def->offset_reloc = 1;
|
2011-02-08 05:45:48 +00:00
|
|
|
}
|
|
|
|
return def;
|
2011-03-03 04:46:07 +00:00
|
|
|
case op_alias:
|
|
|
|
def = alias_def (get_operand_def (expr, op->o.alias),
|
|
|
|
ev_types[op->type]);
|
|
|
|
return def;
|
2011-01-25 06:46:48 +00:00
|
|
|
}
|
2011-01-17 13:33:33 +00:00
|
|
|
return 0;
|
2003-10-22 08:05:17 +00:00
|
|
|
}
|
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
static void
|
2011-01-27 12:10:37 +00:00
|
|
|
add_statement_def_ref (def_t *def, dstatement_t *st, int field)
|
2001-07-26 05:15:34 +00:00
|
|
|
{
|
2011-01-25 06:46:48 +00:00
|
|
|
if (def) {
|
|
|
|
int st_ofs = st - pr.code->code;
|
2011-03-05 07:31:32 +00:00
|
|
|
int offset_reloc = 0;
|
2011-01-25 06:46:48 +00:00
|
|
|
|
2011-03-04 23:39:16 +00:00
|
|
|
while (def->alias) {
|
|
|
|
//FIXME it seems there is a bug somewhere creating chains
|
|
|
|
//of aliases
|
|
|
|
def_t *a = def;
|
2011-03-05 07:31:32 +00:00
|
|
|
offset_reloc |= def->offset_reloc;
|
2011-03-04 23:39:16 +00:00
|
|
|
def = def->alias;
|
|
|
|
free_def (a);
|
2011-01-25 06:46:48 +00:00
|
|
|
}
|
2011-03-05 07:31:32 +00:00
|
|
|
if (offset_reloc)
|
|
|
|
reloc_op_def_ofs (def, st_ofs, field);
|
|
|
|
else
|
|
|
|
reloc_op_def (def, st_ofs, field);
|
2011-01-25 06:46:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-27 12:11:32 +00:00
|
|
|
static void
|
|
|
|
add_statement_op_ref (operand_t *op, dstatement_t *st, int field)
|
|
|
|
{
|
|
|
|
if (op && op->op_type == op_label) {
|
|
|
|
int st_ofs = st - pr.code->code;
|
2011-02-12 13:34:38 +00:00
|
|
|
reloc_t *reloc = new_reloc (0, st_ofs, rel_op_a_op + field);
|
2011-01-27 12:11:32 +00:00
|
|
|
|
|
|
|
reloc->next = op->o.label->dest->relocs;
|
|
|
|
op->o.label->dest->relocs = reloc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
static void
|
|
|
|
emit_statement (statement_t *statement)
|
|
|
|
{
|
|
|
|
const char *opcode = statement->opcode;
|
2011-02-08 09:18:34 +00:00
|
|
|
def_t *def_a = get_operand_def (statement->expr, statement->opa);
|
|
|
|
def_t *def_b = get_operand_def (statement->expr, statement->opb);
|
|
|
|
def_t *def_c = get_operand_def (statement->expr, statement->opc);
|
2011-01-25 06:46:48 +00:00
|
|
|
opcode_t *op = opcode_find (opcode, def_a, def_b, def_c);
|
|
|
|
dstatement_t *s;
|
|
|
|
|
2011-02-10 05:25:09 +00:00
|
|
|
if (!op) {
|
|
|
|
print_expr (statement->expr);
|
|
|
|
print_statement (statement);
|
2011-02-08 09:18:34 +00:00
|
|
|
internal_error (statement->expr, "ice ice baby");
|
2011-02-10 05:25:09 +00:00
|
|
|
}
|
2011-03-07 04:33:02 +00:00
|
|
|
if (options.code.debug) {
|
2011-02-09 00:59:43 +00:00
|
|
|
expr_t *e = statement->expr;
|
|
|
|
pr_uint_t line = (e ? e->line : pr.source_line) - lineno_base;
|
|
|
|
|
|
|
|
if (line != pr.linenos[pr.num_linenos - 1].line) {
|
|
|
|
pr_lineno_t *lineno = new_lineno ();
|
|
|
|
|
|
|
|
lineno->line = line;
|
|
|
|
lineno->fa.addr = pr.code->size;
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 06:46:48 +00:00
|
|
|
s = codespace_newstatement (pr.code);
|
|
|
|
s->op = op->opcode;
|
|
|
|
s->a = def_a ? def_a->offset : 0;
|
|
|
|
s->b = def_b ? def_b->offset : 0;
|
|
|
|
s->c = def_c ? def_c->offset : 0;
|
|
|
|
|
2011-01-27 12:10:37 +00:00
|
|
|
add_statement_def_ref (def_a, s, 0);
|
|
|
|
add_statement_def_ref (def_b, s, 1);
|
|
|
|
add_statement_def_ref (def_c, s, 2);
|
2011-01-27 12:11:32 +00:00
|
|
|
|
|
|
|
add_statement_op_ref (statement->opa, s, 0);
|
|
|
|
add_statement_op_ref (statement->opb, s, 1);
|
|
|
|
add_statement_op_ref (statement->opc, s, 2);
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-01-25 06:46:48 +00:00
|
|
|
emit_statements (sblock_t *first_sblock)
|
2001-07-26 05:15:34 +00:00
|
|
|
{
|
2011-01-25 06:46:48 +00:00
|
|
|
sblock_t *sblock;
|
|
|
|
statement_t *s;
|
|
|
|
|
|
|
|
for (sblock = first_sblock; sblock; sblock = sblock->next) {
|
|
|
|
sblock->offset = pr.code->size;
|
|
|
|
for (s = sblock->statements; s; s = s->next)
|
|
|
|
emit_statement (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (sblock = first_sblock; sblock; sblock = sblock->next)
|
|
|
|
relocate_refs (sblock->relocs, sblock->offset);
|
2001-07-26 05:15:34 +00:00
|
|
|
}
|