ir.c: importing ir_function

This commit is contained in:
Wolfgang (Blub) Bumiller 2012-04-25 14:47:20 +02:00
parent 86bed959d4
commit 9b6cfcb80f

104
ir.c
View file

@ -2,6 +2,10 @@
#include <string.h>
#include "ir.h"
/***********************************************************************
*IR Builder
*/
ir_builder* ir_builder_new(const char *modulename)
{
ir_builder* self;
@ -89,3 +93,103 @@ ir_value* ir_builder_create_global(ir_builder *self, const char *name, ir_type_t
ir_builder_globals_add(self, ve);
return ve;
}
/***********************************************************************
*IR Function
*/
void ir_function_naive_phi(ir_function*);
void ir_function_enumerate(ir_function*);
void ir_function_calculate_liferanges(ir_function*);
ir_function* ir_function_new(ir_builder* owner)
{
ir_function *self;
self = (ir_function*)malloc(sizeof(*self));
self->owner = owner;
self->context.file = "<@no context>";
self->context.line = 0;
self->retype = qc_void;
VEC_INIT(self, params);
VEC_INIT(self, blocks);
VEC_INIT(self, values);
VEC_INIT(self, locals);
ir_function_set_name(self, "<@unnamed>");
self->run_id = 0;
return self;
}
MAKE_VEC_ADD(ir_function, ir_value*, values)
MAKE_VEC_ADD(ir_function, ir_block*, blocks)
MAKE_VEC_ADD(ir_function, ir_value*, locals)
void ir_function_set_name(ir_function *self, const char *name)
{
if (self->_name)
free((void*)self->_name);
self->_name = strdup(name);
}
void ir_function_delete(ir_function *self)
{
size_t i;
free((void*)self->_name);
for (i = 0; i != self->blocks_count; ++i)
ir_block_delete(self->blocks[i]);
VEC_CLEAR(self, blocks);
VEC_CLEAR(self, params);
for (i = 0; i != self->values_count; ++i)
ir_value_delete(self->values[i]);
VEC_CLEAR(self, values);
for (i = 0; i != self->locals_count; ++i)
ir_value_delete(self->locals[i]);
VEC_CLEAR(self, locals);
free(self);
}
void ir_function_collect_value(ir_function *self, ir_value *v)
{
ir_function_values_add(self, v);
}
ir_block* ir_function_create_block(ir_function *self, const char *label)
{
ir_block* bn = ir_block_new(self, label);
memcpy(&bn->context, &self->context, sizeof(self->context));
ir_function_blocks_add(self, bn);
return bn;
}
void ir_function_finalize(ir_function *self)
{
ir_function_naive_phi(self);
ir_function_enumerate(self);
ir_function_calculate_liferanges(self);
}
ir_value* ir_function_get_local(ir_function *self, const char *name)
{
size_t i;
for (i = 0; i < self->locals_count; ++i) {
if (!strcmp(self->locals[i]->_name, name))
return self->locals[i];
}
return NULL;
}
ir_value* ir_function_create_local(ir_function *self, const char *name, ir_type_t vtype)
{
ir_value *ve = ir_function_get_local(self, name);
if (ve) {
return NULL;
}
ve = ir_value_var(name, qc_localvar, vtype);
ir_function_locals_add(self, ve);
return ve;
}