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>
|
|
|
|
|
2020-06-21 14:15:17 +00:00
|
|
|
#include "tools/qfcc/include/codespace.h"
|
|
|
|
#include "tools/qfcc/include/def.h"
|
|
|
|
#include "tools/qfcc/include/defspace.h"
|
|
|
|
#include "tools/qfcc/include/debug.h"
|
|
|
|
#include "tools/qfcc/include/diagnostic.h"
|
|
|
|
#include "tools/qfcc/include/emit.h"
|
|
|
|
#include "tools/qfcc/include/function.h"
|
|
|
|
#include "tools/qfcc/include/opcodes.h"
|
|
|
|
#include "tools/qfcc/include/options.h"
|
|
|
|
#include "tools/qfcc/include/qfcc.h"
|
|
|
|
#include "tools/qfcc/include/reloc.h"
|
|
|
|
#include "tools/qfcc/include/statements.h"
|
|
|
|
#include "tools/qfcc/include/symtab.h"
|
|
|
|
#include "tools/qfcc/include/type.h"
|
|
|
|
#include "tools/qfcc/include/value.h"
|
2001-07-26 05:15:34 +00:00
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
static def_t zero_def;
|
|
|
|
|
2020-03-16 03:15:55 +00:00
|
|
|
static def_t *get_operand_def (expr_t *expr, operand_t *op);
|
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
static def_t *
|
2020-03-17 06:28:15 +00:00
|
|
|
get_tempop_def (expr_t *expr, operand_t *tmpop, type_t *type)
|
2020-03-16 03:15:55 +00:00
|
|
|
{
|
2021-12-25 03:40:24 +00:00
|
|
|
tempop_t *tempop = &tmpop->tempop;
|
2020-03-16 03:15:55 +00:00
|
|
|
if (tempop->def) {
|
|
|
|
return tempop->def;
|
|
|
|
}
|
|
|
|
if (tempop->alias) {
|
|
|
|
def_t *tdef = get_operand_def (expr, tempop->alias);
|
|
|
|
int offset = tempop->offset;
|
|
|
|
tempop->def = alias_def (tdef, type, offset);
|
|
|
|
}
|
|
|
|
if (!tempop->def) {
|
|
|
|
tempop->def = temp_def (type);
|
|
|
|
}
|
|
|
|
return tempop->def;
|
|
|
|
}
|
|
|
|
|
|
|
|
static def_t *
|
|
|
|
get_value_def (expr_t *expr, ex_value_t *value, type_t *type)
|
2001-07-26 05:15:34 +00:00
|
|
|
{
|
2011-01-25 06:46:48 +00:00
|
|
|
def_t *def;
|
|
|
|
|
2020-03-16 03:15:55 +00:00
|
|
|
if (is_short (type)) {
|
2012-12-02 01:11:30 +00:00
|
|
|
def = new_def (0, &type_short, 0, sc_extern);
|
2011-02-09 13:13:43 +00:00
|
|
|
def->offset = value->v.short_val;
|
|
|
|
return def;
|
|
|
|
}
|
2022-01-19 08:48:46 +00:00
|
|
|
if (is_ptr (type) && value->v.pointer.tempop && !value->v.pointer.def) {
|
2020-03-16 03:15:55 +00:00
|
|
|
value->v.pointer.def = get_tempop_def (expr, value->v.pointer.tempop,
|
|
|
|
type->t.fldptr.type);
|
|
|
|
}
|
2011-02-13 05:13:32 +00:00
|
|
|
def = emit_value (value, 0);
|
2019-06-17 14:38:34 +00:00
|
|
|
if (type != def->type)
|
|
|
|
return alias_def (def, type, 0);
|
2011-02-13 05:13:32 +00:00
|
|
|
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-01-25 06:46:48 +00:00
|
|
|
if (!op)
|
|
|
|
return 0;
|
|
|
|
switch (op->op_type) {
|
2012-12-05 10:47:22 +00:00
|
|
|
case op_def:
|
2021-12-25 03:40:24 +00:00
|
|
|
return op->def;
|
2011-01-25 06:46:48 +00:00
|
|
|
case op_value:
|
2021-12-25 03:40:24 +00:00
|
|
|
return get_value_def (expr, op->value, op->type);
|
2011-01-25 06:46:48 +00:00
|
|
|
case op_label:
|
2019-06-17 14:38:34 +00:00
|
|
|
op->type = &type_short;
|
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:
|
2020-03-17 06:28:15 +00:00
|
|
|
return get_tempop_def (expr, op, op->type);
|
2012-12-11 06:52:37 +00:00
|
|
|
case op_alias:
|
2021-12-25 03:40:24 +00:00
|
|
|
return get_operand_def (expr, op->alias);
|
2020-03-14 08:47:23 +00:00
|
|
|
case op_nil:
|
|
|
|
internal_error (expr, "unexpected nil operand");
|
2021-12-25 03:42:15 +00:00
|
|
|
case op_pseudo:
|
|
|
|
internal_error (expr, "unexpected pseudo operand");
|
2011-01-25 06:46:48 +00:00
|
|
|
}
|
2020-03-16 11:31:21 +00:00
|
|
|
internal_error (expr, "unexpected operand");
|
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;
|
2012-05-03 04:27:30 +00:00
|
|
|
int alias_depth = 0;
|
|
|
|
expr_t alias_depth_expr;
|
2011-01-25 06:46:48 +00:00
|
|
|
|
2012-12-12 05:12:21 +00:00
|
|
|
alias_depth_expr.file = def->file;
|
|
|
|
alias_depth_expr.line = def->line;
|
2011-03-04 23:39:16 +00:00
|
|
|
while (def->alias) {
|
2012-05-03 04:27:30 +00:00
|
|
|
alias_depth++;
|
2011-03-05 07:31:32 +00:00
|
|
|
offset_reloc |= def->offset_reloc;
|
2011-03-04 23:39:16 +00:00
|
|
|
def = def->alias;
|
2011-01-25 06:46:48 +00:00
|
|
|
}
|
2012-05-03 04:27:30 +00:00
|
|
|
if (alias_depth > 1) {
|
2012-11-15 06:18:00 +00:00
|
|
|
internal_error (&alias_depth_expr, "alias chain detected: %d %s",
|
2012-05-03 04:27:30 +00:00
|
|
|
alias_depth, def->name);
|
|
|
|
}
|
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
|
|
|
|
2021-12-25 03:40:24 +00:00
|
|
|
reloc->next = op->label->dest->relocs;
|
|
|
|
op->label->dest->relocs = reloc;
|
2011-01-27 12:11:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-18 10:08:16 +00:00
|
|
|
static void
|
2012-11-18 11:29:40 +00:00
|
|
|
use_tempop (operand_t *op, expr_t *expr)
|
2012-11-18 10:08:16 +00:00
|
|
|
{
|
|
|
|
if (!op || op->op_type != op_temp)
|
|
|
|
return;
|
2021-12-25 03:40:24 +00:00
|
|
|
while (op->tempop.alias)
|
|
|
|
op = op->tempop.alias;
|
|
|
|
if (--op->tempop.users == 0)
|
|
|
|
free_temp_def (op->tempop.def);
|
|
|
|
if (op->tempop.users <= -1)
|
2012-11-18 11:29:40 +00:00
|
|
|
bug (expr, "temp users went negative: %s", operand_string (op));
|
2012-11-18 10:08:16 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 01:02:26 +00:00
|
|
|
static def_t *
|
|
|
|
cover_def_32 (def_t *def, int *adj)
|
|
|
|
{
|
|
|
|
int offset = def->offset;
|
|
|
|
def_t *cover = def;
|
|
|
|
|
|
|
|
if (def->alias) {
|
|
|
|
offset += def->alias->offset;
|
|
|
|
}
|
|
|
|
*adj = offset & 3;
|
|
|
|
if (offset & 3) {
|
|
|
|
if (def->alias) {
|
2023-08-26 14:08:55 +00:00
|
|
|
cover = cover_alias_def (def->alias, def->type,
|
|
|
|
def->offset - (offset & 3));
|
2022-05-01 01:02:26 +00:00
|
|
|
} else {
|
2023-08-26 14:08:55 +00:00
|
|
|
cover = cover_alias_def (def, def->type, -(offset & 3));
|
2022-05-01 01:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
static def_t *
|
|
|
|
cover_def_64 (def_t *def, int *adj)
|
|
|
|
{
|
|
|
|
int offset = def->offset;
|
|
|
|
def_t *cover = def;
|
|
|
|
|
|
|
|
if (def->alias) {
|
|
|
|
offset += def->alias->offset;
|
|
|
|
}
|
|
|
|
if (offset & 1) {
|
|
|
|
internal_error (0, "misaligned 64-bit swizzle source");
|
|
|
|
}
|
|
|
|
*adj = (offset & 6) >> 1;
|
|
|
|
if (offset & 6) {
|
|
|
|
if (def->alias) {
|
2023-08-26 14:08:55 +00:00
|
|
|
cover = cover_alias_def (def->alias, def->type,
|
|
|
|
def->offset - (offset & 6));
|
2022-05-01 01:02:26 +00:00
|
|
|
} else {
|
2023-08-26 14:08:55 +00:00
|
|
|
cover = cover_alias_def (def, def->type, -(offset & 6));
|
2022-05-01 01:02:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return cover;
|
|
|
|
}
|
|
|
|
|
|
|
|
static def_t *
|
|
|
|
cover_def (def_t *def, int *adj)
|
|
|
|
{
|
|
|
|
if (type_size (base_type (def->type)) == 1) {
|
|
|
|
return cover_def_32 (def, adj);
|
|
|
|
} else {
|
|
|
|
return cover_def_64 (def, adj);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
adjust_swizzle (def_t *def, int adj)
|
|
|
|
{
|
|
|
|
pr_ushort_t swiz = def->offset;
|
|
|
|
for (int i = 0; i < 8; i += 2) {
|
|
|
|
pr_ushort_t mask = 3 << i;
|
|
|
|
pr_ushort_t ind = swiz & mask;
|
|
|
|
swiz &= ~mask;
|
|
|
|
swiz |= (ind + (adj << i)) & mask;
|
|
|
|
}
|
|
|
|
def->offset = swiz;
|
|
|
|
}
|
|
|
|
|
2011-01-25 06:46:48 +00:00
|
|
|
static void
|
|
|
|
emit_statement (statement_t *statement)
|
|
|
|
{
|
|
|
|
const char *opcode = statement->opcode;
|
2022-01-20 04:05:08 +00:00
|
|
|
operand_t *op_a, *op_b, *op_c;
|
2012-11-18 10:08:16 +00:00
|
|
|
def_t *def_a, *def_b, *def_c;
|
2022-01-07 10:05:26 +00:00
|
|
|
instruction_t *inst;
|
2011-01-25 06:46:48 +00:00
|
|
|
dstatement_t *s;
|
|
|
|
|
2022-01-20 04:05:08 +00:00
|
|
|
if (options.code.progsversion < PROG_VERSION
|
|
|
|
&& (strcmp (statement->opcode, "store") == 0
|
|
|
|
|| strcmp (statement->opcode, "assign") == 0
|
|
|
|
|| statement_is_cond (statement))) {
|
|
|
|
// the operands for assign, store and branch instructions are rotated
|
|
|
|
// when comparing v6/v6p and ruamoko
|
|
|
|
op_a = statement->opc;
|
|
|
|
op_b = statement->opa;
|
|
|
|
op_c = statement->opb;
|
|
|
|
} else {
|
|
|
|
op_a = statement->opa;
|
|
|
|
op_b = statement->opb;
|
|
|
|
op_c = statement->opc;
|
|
|
|
}
|
2022-05-01 01:02:26 +00:00
|
|
|
|
2022-01-20 04:05:08 +00:00
|
|
|
def_a = get_operand_def (statement->expr, op_a);
|
2022-01-22 15:59:38 +00:00
|
|
|
use_tempop (op_a, statement->expr);
|
2022-01-20 04:05:08 +00:00
|
|
|
def_b = get_operand_def (statement->expr, op_b);
|
2022-01-22 15:59:38 +00:00
|
|
|
use_tempop (op_b, statement->expr);
|
2022-01-20 04:05:08 +00:00
|
|
|
def_c = get_operand_def (statement->expr, op_c);
|
2022-01-22 15:59:38 +00:00
|
|
|
use_tempop (op_c, statement->expr);
|
2022-05-01 01:02:26 +00:00
|
|
|
|
|
|
|
if (strcmp (opcode, "swizzle") == 0) {
|
|
|
|
op_c->type = float_type (op_c->type);
|
|
|
|
op_a->type = float_type (op_a->type);
|
|
|
|
if (!op_c->type || !op_a->type) {
|
|
|
|
internal_error (statement->expr, "invalid types in swizzle");
|
|
|
|
}
|
|
|
|
if (op_a->width < 4) {
|
|
|
|
int adj;
|
|
|
|
def_a = cover_def (def_a, &adj);
|
|
|
|
adjust_swizzle (def_b, adj);
|
|
|
|
op_a->width = 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 04:05:08 +00:00
|
|
|
inst = opcode_find (opcode, op_a, op_b, op_c);
|
2012-11-18 10:08:16 +00:00
|
|
|
|
2022-01-07 10:05:26 +00:00
|
|
|
if (!inst) {
|
2011-02-10 05:25:09 +00:00
|
|
|
print_expr (statement->expr);
|
2022-01-20 04:05:08 +00:00
|
|
|
printf ("%d ", pr.code->size);
|
2011-02-10 05:25:09 +00:00
|
|
|
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);
|
2022-01-21 11:34:43 +00:00
|
|
|
memset (s, 0, sizeof (*s));
|
2022-01-07 10:05:26 +00:00
|
|
|
s->op = opcode_get (inst);
|
2022-01-21 11:34:43 +00:00
|
|
|
if (def_a) {
|
|
|
|
s->a = def_a->offset;
|
|
|
|
s->op |= ((def_a->reg) << OP_A_SHIFT) & OP_A_BASE;
|
|
|
|
}
|
|
|
|
if (def_b) {
|
|
|
|
s->b = def_b->offset;
|
|
|
|
s->op |= ((def_b->reg) << OP_B_SHIFT) & OP_B_BASE;
|
|
|
|
}
|
|
|
|
if (def_c) {
|
|
|
|
s->c = def_c->offset;
|
|
|
|
s->op |= ((def_c->reg) << OP_C_SHIFT) & OP_C_BASE;
|
|
|
|
}
|
2011-01-25 06:46:48 +00:00
|
|
|
|
2022-01-20 03:55:56 +00:00
|
|
|
if (options.verbosity >= 2) {
|
|
|
|
opcode_print_statement (pr.code->size - 1, s);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-01-20 04:05:08 +00:00
|
|
|
add_statement_op_ref (op_a, s, 0);
|
|
|
|
add_statement_op_ref (op_b, s, 1);
|
|
|
|
add_statement_op_ref (op_c, 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
|
|
|
}
|