#include <stdlib.h>
#include <string.h>
+#include "gmqcc.h"
#include "ast.h"
#define ast_setfunc(me, fn, what) ( *(void**)&((me)->fn) = what )
ast_setfunc(&self->expression, codegen, codegen);
}
-ast_value* ast_value_new(lex_ctx_t ctx, const char *name, qc_type_t t)
+ast_value* ast_value_new(lex_ctx_t ctx, const char *name, int t)
{
ast_instantiate(ast_value, ctx, ast_value_delete);
ast_expression_init((ast_expression*)self,
self->vtype = t;
self->next = NULL;
MEM_VECTOR_INIT(self, params);
- self->has_constval = false;
- memset(&self->cvalue, 0, sizeof(self->cvalue));
+ self->isconst = false;
+ memset(&self->constval, 0, sizeof(self->constval));
self->ir_v = NULL;
MEM_VECTOR_CLEAR(self, params);
if (self->next)
ast_delete(self->next);
- if (self->has_constval) {
+ if (self->isconst) {
switch (self->vtype)
{
case qc_string:
- mem_d((void*)self->cvalue.vstring);
+ mem_d((void*)self->constval.vstring);
break;
/* NOTE: delete function? currently collected in
* the parser structure
self->name = util_strdup(name);
}
-ast_binary* ast_binary_new(lex_ctx_t ctx, qc_op_t op,
+ast_binary* ast_binary_new(lex_ctx_t ctx, int op,
ast_value* left, ast_value* right)
{
ast_instantiate(ast_binary, ctx, ast_binary_delete);
- ast_expression_init((ast_expression*)self, (ast_expression_codegen*)codegen);
+ ast_expression_init((ast_expression*)self, NULL); /* FIXME */
self->op = op;
self->left = left;
/* AST codegen aprt
*/
+#if 0
static qbool ast_value_gen_global(ir_builder *ir, ast_value *self, ir_value **out)
{
ir_value *v;
*out = NULL;
/* Generate functions */
- if (self->vtype == qc_function && self->has_constval)
+ if (self->vtype == qc_function && self->isconst)
{
- /* Without has_constval it would be invalid... function pointers actually have
+ /* Without isconst it would be invalid... function pointers actually have
* type qc_pointer and next with type qc_function
*/
- ast_function *func = self->cvalue.vfunc;
+ ast_function *func = self->constval.vfunc;
if (!ast_function_codegen(func, ir))
return false;
/* Here we do return NULL anyway */
return true;
}
- else if (self->vtype == qc_function && !self->has_constval) {
+ else if (self->vtype == qc_function && !self->isconst) {
fprintf(stderr,
- "!v->has_constval <- qc_function body missing - FIXME: remove when implementing prototypes\n");
- fprintf(stderr, "Value: %s\n", self->_name);
+ "!v->isconst <- qc_function body missing - FIXME: remove when implementing prototypes\n");
+ fprintf(stderr, "Value: %s\n", self->name);
abort();
}
- v = ir_builder_create_global(ir, self->_name, self->vtype);
+ v = ir_builder_create_global(ir, self->name, self->vtype);
self->ir_v = v;
*out = v;
qbool ast_value_codegen(ast_value *self, ast_function *func, ir_value **out)
{
if (!func)
- return ast_value_gen_global(parser.ir, self, out);
+ return ast_value_gen_global(ir, self, out);
return false;
}
{
return false;
}
+#endif
#include <stdlib.h>
#include <string.h>
+#include "gmqcc.h"
#include "ir.h"
/***********************************************************************
}
MEM_VEC_FUNCTIONS(ir_builder, ir_value*, globals)
-MEM_VECTOR_FUNCTIONS(ir_builder, ir_function*, functions)
+MEM_VEC_FUNCTIONS(ir_builder, ir_function*, functions)
void ir_builder_delete(ir_builder* self)
{
self->run_id = 0;
return self;
}
-MEM_VECTOR_FUNCTIONS(ir_function, ir_value*, values)
-MEM_VECTOR_FUNCTIONS(ir_function, ir_block*, blocks)
-MEM_VECTOR_FUNCTIONS(ir_function, ir_value*, locals)
+MEM_VEC_FUNCTIONS(ir_function, ir_value*, values)
+MEM_VEC_FUNCTIONS(ir_function, ir_block*, blocks)
+MEM_VEC_FUNCTIONS(ir_function, ir_value*, locals)
void ir_function_set_name(ir_function *self, const char *name)
{
MEM_VECTOR_INIT(self, living);
return self;
}
-MEM_VECTOR_FUNCTIONS(ir_block, ir_instr*, instr)
-MEM_VECTOR_FUNCTIONS_ALL(ir_block, ir_block*, entries)
-MEM_VECTOR_FUNCTIONS_ALL(ir_block, ir_block*, exits)
-MEM_VECTOR_FUNCTIONS_ALL(ir_block, ir_value*, living)
+MEM_VEC_FUNCTIONS(ir_block, ir_instr*, instr)
+MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, entries)
+MEM_VEC_FUNCTIONS_ALL(ir_block, ir_block*, exits)
+MEM_VEC_FUNCTIONS_ALL(ir_block, ir_value*, living)
void ir_block_delete(ir_block* self)
{
self->eid = 0;
return self;
}
-MEM_VECTOR_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
+MEM_VEC_FUNCTIONS(ir_instr, ir_phi_entry_t, phi)
void ir_instr_delete(ir_instr *self)
{
self->store = storetype;
MEM_VECTOR_INIT(self, reads);
MEM_VECTOR_INIT(self, writes);
- self->has_constval = false;
+ self->isconst = false;
self->context.file = "<@no context>";
self->context.line = 0;
self->name = NULL;
MEM_VECTOR_INIT(self, life);
return self;
}
-MEM_VECTOR_FUNCTIONS(ir_value, ir_life_entry_t, life)
-MEM_VECTOR_FUNCTIONS(ir_value, ir_instr*, reads)
-MEM_VECTOR_FUNCTIONS(ir_value, ir_instr*, writes)
+MEM_VEC_FUNCTIONS(ir_value, ir_life_entry_t, life)
+MEM_VEC_FUNCTIONS(ir_value, ir_instr*, reads)
+MEM_VEC_FUNCTIONS(ir_value, ir_instr*, writes)
ir_value* ir_value_out(ir_function *owner, const char *name, int storetype, int vtype)
{
void ir_value_delete(ir_value* self)
{
mem_d((void*)self->name);
- if (self->has_constval)
+ if (self->isconst)
{
if (self->vtype == qc_string)
- mem_d((void*)self->cvalue.vstring);
+ mem_d((void*)self->constval.vstring);
}
MEM_VECTOR_CLEAR(self, reads);
MEM_VECTOR_CLEAR(self, writes);
{
if (self->vtype != qc_float)
return false;
- self->cvalue.vfloat = f;
- self->has_constval = true;
+ self->constval.vfloat = f;
+ self->isconst = true;
return true;
}
-qbool ir_value_set_vector(ir_value *self, qc_vec_t v)
+qbool ir_value_set_vector(ir_value *self, vector_t v)
{
if (self->vtype != qc_vector)
return false;
- self->cvalue.vvec = v;
- self->has_constval = true;
+ self->constval.vvec = v;
+ self->isconst = true;
return true;
}
{
if (self->vtype != qc_string)
return false;
- self->cvalue.vstring = util_strdup(str);
- self->has_constval = true;
+ self->constval.vstring = util_strdup(str);
+ self->isconst = true;
return true;
}
{
if (self->vtype != qc_int)
return false;
- self->cvalue.vint = i;
- self->has_constval = true;
+ self->constval.vint = i;
+ self->isconst = true;
return true;
}
switch (vtype) {
case qc_float:
+#if 0
if (what->vtype == qc_int)
op = INSTR_CONV_ITOF;
else
+#endif
op = INSTR_STORE_F;
break;
case qc_vector:
case qc_string:
op = INSTR_STORE_S;
break;
+#if 0
case qc_int:
if (what->vtype == qc_int)
op = INSTR_CONV_FTOI;
else
op = INSTR_STORE_I;
break;
+#endif
case qc_pointer:
+#if 0
op = INSTR_STORE_I;
+#else
+ op = INSTR_STORE_ENT;
+#endif
break;
}
return ir_block_create_store_op(self, op, target, what);
{
ir_instr *in;
if (self->final) {
- fprintf(stderr, "block already ended (%s)\n", self->_label);
+ fprintf(stderr, "block already ended (%s)\n", self->label);
return;
}
self->final = true;
{
ir_instr *in;
if (self->final) {
- fprintf(stderr, "block already ended (%s)\n", self->_label);
+ fprintf(stderr, "block already ended (%s)\n", self->label);
return;
}
self->final = true;
{
ir_instr *in;
if (self->final) {
- fprintf(stderr, "block already ended (%s)\n", self->_label);
+ fprintf(stderr, "block already ended (%s)\n", self->label);
return;
}
self->final = true;
{
ir_instr *in;
if (self->final) {
- fprintf(stderr, "block already ended (%s)\n", self->_label);
+ fprintf(stderr, "block already ended (%s)\n", self->label);
return;
}
self->final = true;
case INSTR_MUL_V:
case INSTR_AND:
case INSTR_OR:
+#if 0
case INSTR_AND_I:
case INSTR_AND_IF:
case INSTR_AND_FI:
case INSTR_OR_I:
case INSTR_OR_IF:
case INSTR_OR_FI:
+#endif
case INSTR_BITAND:
case INSTR_BITOR:
+#if 0
case INSTR_SUB_S: /* -- offset of string as float */
case INSTR_MUL_IF:
case INSTR_MUL_FI:
case INSTR_BITAND_IF:
case INSTR_EQ_I:
case INSTR_NE_I:
+#endif
ot = qc_float;
break;
+#if 0
case INSTR_ADD_I:
case INSTR_ADD_IF:
case INSTR_ADD_FI:
case INSTR_LSHIFT_I:
ot = qc_int;
break;
+#endif
case INSTR_ADD_V:
case INSTR_SUB_V:
case INSTR_MUL_VF:
case INSTR_MUL_FV:
+#if 0
case INSTR_DIV_VF:
case INSTR_MUL_IV:
case INSTR_MUL_VI:
+#endif
ot = qc_vector;
break;
+#if 0
case INSTR_ADD_SF:
ot = qc_pointer;
break;
+#endif
default:
// ranges:
/* boolean operations result in floats */
ot = qc_float;
else if (opcode >= INSTR_LE && opcode <= INSTR_GT)
ot = qc_float;
+#if 0
else if (opcode >= INSTR_LE_I && opcode <= INSTR_EQ_FI)
ot = qc_float;
+#endif
break;
};
if (ot == qc_void) {
- fprintf(stderr, "binop %i (%s)\n", opcode, qc_opname(opcode));
+ /* The AST or parser were supposed to check this! */
abort();
return NULL;
}
case qc_float:
op = INSTR_ADD_F;
break;
+#if 0
case qc_int:
op = INSTR_ADD_I;
break;
+#endif
case qc_vector:
op = INSTR_ADD_V;
break;
}
} else {
+#if 0
if ( (l == qc_float && r == qc_int) )
op = INSTR_ADD_FI;
else if ( (l == qc_int && r == qc_float) )
op = INSTR_ADD_IF;
else
+#endif
return NULL;
}
return ir_block_create_binop(self, label, op, left, right);
case qc_float:
op = INSTR_SUB_F;
break;
+#if 0
case qc_int:
op = INSTR_SUB_I;
break;
+#endif
case qc_vector:
op = INSTR_SUB_V;
break;
}
} else {
+#if 0
if ( (l == qc_float && r == qc_int) )
op = INSTR_SUB_FI;
else if ( (l == qc_int && r == qc_float) )
op = INSTR_SUB_IF;
else
+#endif
return NULL;
}
return ir_block_create_binop(self, label, op, left, right);
case qc_float:
op = INSTR_MUL_F;
break;
+#if 0
case qc_int:
op = INSTR_MUL_I;
break;
+#endif
case qc_vector:
op = INSTR_MUL_V;
break;
}
} else {
- if ( (l == qc_float && r == qc_int) )
- op = INSTR_MUL_FI;
- else if ( (l == qc_int && r == qc_float) )
- op = INSTR_MUL_IF;
- else if ( (l == qc_vector && r == qc_float) )
+ if ( (l == qc_vector && r == qc_float) )
op = INSTR_MUL_VF;
else if ( (l == qc_float && r == qc_vector) )
op = INSTR_MUL_FV;
+#if 0
else if ( (l == qc_vector && r == qc_int) )
op = INSTR_MUL_VI;
else if ( (l == qc_int && r == qc_vector) )
op = INSTR_MUL_IV;
+ else if ( (l == qc_float && r == qc_int) )
+ op = INSTR_MUL_FI;
+ else if ( (l == qc_int && r == qc_float) )
+ op = INSTR_MUL_IF;
+#endif
else
return NULL;
}
case qc_float:
op = INSTR_DIV_F;
break;
+#if 0
case qc_int:
op = INSTR_DIV_I;
break;
+#endif
}
} else {
- if ( (l == qc_float && r == qc_int) )
+#if 0
+ if ( (l == qc_vector && r == qc_float) )
+ op = INSTR_DIV_VF;
+ else if ( (l == qc_float && r == qc_int) )
op = INSTR_DIV_FI;
else if ( (l == qc_int && r == qc_float) )
op = INSTR_DIV_IF;
- else if ( (l == qc_vector && r == qc_float) )
- op = INSTR_DIV_VF;
else
+#endif
return NULL;
}
return ir_block_create_binop(self, label, op, left, right);
size_t v_count;
size_t v_alloc;
} new_reads_t;
-MEM_VECTOR_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
+MEM_VEC_FUNCTIONS_ALL(new_reads_t, ir_value*, v)
/* Enumerate instructions used by value's life-ranges
*/
break;
case INSTR_IF:
case INSTR_IFNOT:
+#if 0
case INSTR_IF_S:
case INSTR_IFNOT_S:
+#endif
case INSTR_RETURN:
case VINSTR_COND:
*write = 0;
continue;
ir_block_living_add(self, prev->living[i]);
/*
- printf("%s got from prev: %s\n", self->_label, prev->living[i]->_name);
+ printf("%s got from prev: %s\n", self->label, prev->living[i]->_name);
*/
}
}
char dbg_ind[16] = { '#', '0' };
(void)dbg_ind;
- VEC_INIT(&new_reads, v);
+ MEM_VECTOR_INIT(&new_reads, v);
if (prev)
ir_block_life_prop_previous(self, prev, changed);
* since this function is run multiple times.
*/
/* For now: debug info: */
- fprintf(stderr, "Value only written %s\n", value->_name);
+ fprintf(stderr, "Value only written %s\n", value->name);
tempbool = ir_value_life_merge(value, instr->eid);
*changed = *changed || tempbool;
/*
tempbool = ir_value_life_merge(value, instr->eid);
/*
if (tempbool)
- fprintf(stderr, "value added id %s %i\n", value->_name, (int)instr->eid);
+ fprintf(stderr, "value added id %s %i\n", value->name, (int)instr->eid);
*/
*changed = *changed || tempbool;
/* Then remove */
struct ir_function_s;
typedef struct ir_value_s {
char *name;
- int vtype;
- int store;
- filecontext_t context;
+ int vtype;
+ int store;
+ lex_ctx_t context;
MEM_VECTOR_MAKE(struct ir_instr_s*, reads);
MEM_VECTOR_MAKE(struct ir_instr_s*, writes);
/* constantvalues */
+ qbool isconst;
union {
float vfloat;
int vint;
vector_t vvec;
char *vstring;
struct ir_value_s *vpointer;
- } cvalue;
- qbool has_constval;
+ } constval;
/* For the temp allocator */
MEM_VECTOR_MAKE(ir_life_entry_t, life);
qbool ir_value_set_float(ir_value*, float f);
qbool ir_value_set_int(ir_value*, int i);
qbool ir_value_set_string(ir_value*, const char *s);
-qbool ir_value_set_vector(ir_value*, qc_vec_t v);
+qbool ir_value_set_vector(ir_value*, vector_t v);
/*qbool ir_value_set_pointer_v(ir_value*, ir_value* p); */
/*qbool ir_value_set_pointer_i(ir_value*, int i); */
/* instruction */
typedef struct ir_instr_s
{
- ir_op_t opcode;
- filecontext_t context;
- ir_value* (_ops[3]);
+ int opcode;
+ lex_ctx_t context;
+ ir_value* (_ops[3]);
struct ir_block_s* (bops[2]);
MEM_VECTOR_MAKE(ir_phi_entry_t, phi);
struct ir_block_s *owner;
} ir_instr;
-ir_instr* ir_instr_new(struct ir_block_s *owner, ir_op_t opcode);
+ir_instr* ir_instr_new(struct ir_block_s *owner, int opcode);
void ir_instr_delete(ir_instr*);
MEM_VECTOR_PROTO(ir_value, ir_phi_entry_t, phi)
MEM_VECTOR_PROTO_ALL(ir_block, ir_block*, exits)
MEM_VECTOR_PROTO_ALL(ir_block, ir_block*, entries)
-ir_value* ir_block_create_binop(ir_block*, const char *label, ir_op_t op,
+ir_value* ir_block_create_binop(ir_block*, const char *label, int op,
ir_value *left, ir_value *right);
-qbool ir_block_create_store_op(ir_block*, ir_op_t op, ir_value *target, ir_value *what);
+qbool ir_block_create_store_op(ir_block*, int op, ir_value *target, ir_value *what);
qbool ir_block_create_store(ir_block*, ir_value *target, ir_value *what);
ir_value* ir_block_create_add(ir_block*, const char *label, ir_value *l, ir_value *r);