mirror of
https://github.com/Ed94/Odin.git
synced 2026-06-17 19:32:23 -07:00
Restart LLVM IR SSA generation
This is the third go and I'm going for it!
This commit is contained in:
@@ -0,0 +1,318 @@
|
||||
#include "ssa.cpp"
|
||||
#include "print.cpp"
|
||||
|
||||
struct ssaGen {
|
||||
ssaModule module;
|
||||
gbFile output_file;
|
||||
};
|
||||
|
||||
b32 ssa_gen_init(ssaGen *s, Checker *c) {
|
||||
if (c->error_collector.count != 0)
|
||||
return false;
|
||||
|
||||
gb_for_array(i, c->parser->files) {
|
||||
AstFile *f = &c->parser->files[i];
|
||||
if (f->error_collector.count != 0)
|
||||
return false;
|
||||
if (f->tokenizer.error_count != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
ssa_module_init(&s->module, c);
|
||||
|
||||
gbFileError err = gb_file_create(&s->output_file, "../examples/test.ll");
|
||||
if (err != gbFileError_None)
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ssa_gen_destroy(ssaGen *s) {
|
||||
ssa_module_destroy(&s->module);
|
||||
gb_file_close(&s->output_file);
|
||||
}
|
||||
|
||||
void ssa_gen_code(ssaGen *s) {
|
||||
ssaModule *m = &s->module;
|
||||
CheckerInfo *info = m->info;
|
||||
gbAllocator a = m->allocator;
|
||||
gb_for_array(i, info->entities.entries) {
|
||||
auto *entry = &info->entities.entries[i];
|
||||
Entity *e = cast(Entity *)cast(uintptr)entry->key;
|
||||
DeclarationInfo *decl = entry->value;
|
||||
|
||||
String name = e->token.string;
|
||||
|
||||
switch (e->kind) {
|
||||
case Entity_TypeName: {
|
||||
ssaValue *t = ssa_make_value_type_name(a, e);
|
||||
map_set(&m->members, hash_string(name), t);
|
||||
} break;
|
||||
|
||||
case Entity_Variable: {
|
||||
ssaValue *g = ssa_make_value_global(a, e, NULL);
|
||||
map_set(&m->values, hash_pointer(e), g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
} break;
|
||||
|
||||
case Entity_Procedure: {
|
||||
ssaValue *p = ssa_make_value_procedure(a, e, decl, m);
|
||||
map_set(&m->values, hash_pointer(e), p);
|
||||
map_set(&m->members, hash_string(name), p);
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
gb_for_array(i, m->members.entries) {
|
||||
auto *entry = &m->members.entries[i];
|
||||
ssaValue *v = entry->value;
|
||||
if (v->kind == ssaValue_Procedure)
|
||||
ssa_build_procedure(v);
|
||||
}
|
||||
|
||||
ssa_print_llvm_ir(&s->output_file, &s->module);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#if 0
|
||||
#include "type.cpp"
|
||||
#include "ir.cpp"
|
||||
|
||||
struct Codegen {
|
||||
Checker *checker;
|
||||
gbFile file;
|
||||
gbAllocator allocator;
|
||||
|
||||
irModule module;
|
||||
|
||||
ErrorCollector error_collector;
|
||||
};
|
||||
|
||||
b32 init_codegen(Codegen *c, Checker *checker) {
|
||||
c->checker = checker;
|
||||
|
||||
if (c->error_collector.count != 0)
|
||||
return false;
|
||||
for (isize i = 0; i < gb_array_count(checker->parser->files); i++) {
|
||||
AstFile *f = &checker->parser->files[i];
|
||||
if (f->error_collector.count != 0)
|
||||
return false;
|
||||
if (f->tokenizer.error_count != 0)
|
||||
return false;
|
||||
}
|
||||
|
||||
c->allocator = gb_heap_allocator();
|
||||
|
||||
ir_module_init(&c->module, c->checker);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void destroy_codegen(Codegen *c) {
|
||||
ir_module_destroy(&c->module);
|
||||
}
|
||||
|
||||
b32 is_blank_identifier(AstNode *identifier) {
|
||||
if (identifier->kind == AstNode_Identifier) {
|
||||
return are_strings_equal(identifier->identifier.token.string, make_string("_"));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
irValue *ir_add_basic_block(gbAllocator a, irValue *p, String label) {
|
||||
irValue *b = ir_make_value_basic_block(a, gb_array_count(p->procedure.blocks), label, p);
|
||||
gb_array_append(p->procedure.blocks, b);
|
||||
return b;
|
||||
}
|
||||
|
||||
irValue *ir_emit_from_block(irValue *b, irInstruction *i) {
|
||||
GB_ASSERT(b->kind == irValue_BasicBlock);
|
||||
i->block = b;
|
||||
gb_array_append(b->basic_block.instructions, i);
|
||||
return ir_make_value_instruction(gb_heap_allocator(), i);
|
||||
}
|
||||
|
||||
|
||||
irValue *ir_emit(irValue *p, irInstruction *i) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
return ir_emit_from_block(p->procedure.curr_block, i);
|
||||
}
|
||||
|
||||
|
||||
irInstruction *ir_add_local(irValue *p, Type *type, TokenPos pos) {
|
||||
irInstruction *i = ir_alloc_instruction(gb_heap_allocator(), irInstruction_Alloca);
|
||||
i->reg.type = type;
|
||||
i->reg.pos = pos;
|
||||
gb_array_append(p->procedure.locals, ir_emit(p, i));
|
||||
return i;
|
||||
}
|
||||
|
||||
irInstruction *ir_add_named_local(irValue *p, Entity *e) {
|
||||
irInstruction *i = ir_add_local(p, e->type, e->token.pos);
|
||||
i->alloca.label = e->token.string;
|
||||
// map_set(&p->procedure.variables, hash_pointer(e), );
|
||||
return i;
|
||||
}
|
||||
|
||||
irInstruction *ir_add_local_for_identifier(irValue *p, AstNode *i) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
GB_ASSERT(i->kind == AstNode_Identifier);
|
||||
auto *found = map_get(&p->procedure.module->checker->definitions, hash_pointer(i));
|
||||
return ir_add_named_local(p, *found);
|
||||
}
|
||||
|
||||
|
||||
void ir_build_variable_declaration(irValue *p, AstNode *d) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
auto *vd = &d->variable_declaration;
|
||||
|
||||
if (vd->name_count == vd->value_count) {
|
||||
AstNode *name = vd->name_list;
|
||||
AstNode *value = vd->value_list;
|
||||
for (;
|
||||
name != NULL && value != NULL;
|
||||
name = name->next, value = value->next) {
|
||||
if (!is_blank_identifier(name)) {
|
||||
ir_add_local_for_identifier(p, name);
|
||||
}
|
||||
// auto lvalue = build_address(p, name, false);
|
||||
// build_assignment(p, lvalue, value, true, NULL);
|
||||
}
|
||||
} else if (vd->value_count == 0) {
|
||||
AstNode *name = vd->name_list;
|
||||
for (;
|
||||
name != NULL;
|
||||
name = name->next) {
|
||||
if (!is_blank_identifier(name)) {
|
||||
|
||||
}
|
||||
|
||||
// build_assignment(p, )
|
||||
}
|
||||
} else {
|
||||
// TODO(bill): Tuple
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ir_build_expression(irValue *p, AstNode *e) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
|
||||
}
|
||||
|
||||
|
||||
void ir_build_statement(irValue *p, AstNode *s);
|
||||
|
||||
void ir_build_statement_list(irValue *p, AstNode *list) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
for (AstNode *item = list; item != NULL; item = item->next) {
|
||||
ir_build_statement(p, item);
|
||||
}
|
||||
}
|
||||
|
||||
void ir_build_statement(irValue *p, AstNode *s) {
|
||||
GB_ASSERT(p->kind == irValue_Procedure);
|
||||
|
||||
switch (s->kind) {
|
||||
case AstNode_EmptyStatement:
|
||||
break;
|
||||
|
||||
case AstNode_VariableDeclaration: {
|
||||
auto *vd = &s->variable_declaration;
|
||||
if (vd->kind == Declaration_Mutable) {
|
||||
ir_build_variable_declaration(p, s);
|
||||
}
|
||||
} break;
|
||||
|
||||
|
||||
case AstNode_ExpressionStatement:
|
||||
ir_build_expression(p, s->expression_statement.expression);
|
||||
break;
|
||||
|
||||
case AstNode_BlockStatement:
|
||||
ir_build_statement_list(p, s->block_statement.list);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ir_begin_procedure_body(irValue *p) {
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
p->procedure.curr_block = ir_add_basic_block(a, p, make_string("entry"));
|
||||
map_init(&p->procedure.variables, a);
|
||||
}
|
||||
|
||||
void ir_end_procedure_body(irValue *p) {
|
||||
p->procedure.curr_block = NULL;
|
||||
map_destroy(&p->procedure.variables);
|
||||
}
|
||||
|
||||
|
||||
void ir_build_procedure(irModule *m, irValue *p) {
|
||||
if (p->procedure.blocks != NULL)
|
||||
return;
|
||||
AstNode *proc_type = NULL;
|
||||
AstNode *body = NULL;
|
||||
switch (p->procedure.node->kind) {
|
||||
case AstNode_ProcedureDeclaration:
|
||||
proc_type = p->procedure.node->procedure_declaration.procedure_type;
|
||||
body = p->procedure.node->procedure_declaration.body;
|
||||
break;
|
||||
case AstNode_ProcedureLiteral:
|
||||
proc_type = p->procedure.node->procedure_literal.type;
|
||||
body = p->procedure.node->procedure_literal.body;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
if (body == NULL) {
|
||||
// NOTE(bill): External procedure
|
||||
return;
|
||||
}
|
||||
|
||||
defer (gb_printf("build procedure %.*s\n", LIT(p->procedure.token.string)));
|
||||
|
||||
|
||||
ir_begin_procedure_body(p);
|
||||
ir_build_statement(p, body);
|
||||
ir_end_procedure_body(p);
|
||||
}
|
||||
|
||||
void ir_build_proc_decl(irModule *m, AstNode *decl) {
|
||||
GB_ASSERT(decl != NULL);
|
||||
auto *pd = &decl->procedure_declaration;
|
||||
if (is_blank_identifier(pd->name))
|
||||
return;
|
||||
|
||||
Entity *e = entity_of_identifier(m->checker, pd->name);
|
||||
irValue *p = *map_get(&m->values, hash_pointer(e));
|
||||
ir_build_procedure(m, p);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void generate_code(Codegen *c) {
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
|
||||
ir_module_create(&c->module);
|
||||
|
||||
for (isize i = 0; i < gb_array_count(c->module.values.entries); i++) {
|
||||
irValue *v = c->module.values.entries[i].value;
|
||||
switch (v->kind) {
|
||||
case irValue_Procedure:
|
||||
ir_build_proc_decl(&c->module, v->procedure.node);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,401 @@
|
||||
void ssa_fprintf(gbFile *f, char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
gb_fprintf_va(f, fmt, va);
|
||||
#if 1
|
||||
gb_printf_va(fmt, va);
|
||||
#endif
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
|
||||
b32 ssa_valid_char(u8 c) {
|
||||
if (gb_char_is_alphanumeric(c))
|
||||
return true;
|
||||
|
||||
switch (c) {
|
||||
case '$':
|
||||
case '-':
|
||||
case '.':
|
||||
case '_':
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ssa_print_escape_string(gbFile *f, String name) {
|
||||
isize extra = 0;
|
||||
for (isize i = 0; i < name.len; i++) {
|
||||
u8 c = name.text[i];
|
||||
if (!ssa_valid_char(c))
|
||||
extra += 2;
|
||||
}
|
||||
|
||||
if (extra == 0) {
|
||||
ssa_fprintf(f, "%.*s", LIT(name));
|
||||
return;
|
||||
}
|
||||
|
||||
char hex_table[] = "0123456789ABCDEF";
|
||||
isize buf_len = name.len + extra;
|
||||
u8 *buf = gb_alloc_array(gb_heap_allocator(), u8, buf_len);
|
||||
defer (gb_free(gb_heap_allocator(), buf));
|
||||
|
||||
isize j = 0;
|
||||
for (isize i = 0; i < name.len; i++) {
|
||||
u8 c = name.text[i];
|
||||
if (ssa_valid_char(c)) {
|
||||
buf[j++] = c;
|
||||
} else {
|
||||
buf[j] = '\\';
|
||||
buf[j+1] = hex_table[c >> 4];
|
||||
buf[j+2] = hex_table[c & 0x0f];
|
||||
j += 3;
|
||||
}
|
||||
}
|
||||
|
||||
gb_file_write(f, buf, buf_len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ssa_print_encoded_local(gbFile *f, String name) {
|
||||
ssa_fprintf(f, "%%");
|
||||
ssa_print_escape_string(f, name);
|
||||
}
|
||||
|
||||
void ssa_print_encoded_global(gbFile *f, String name) {
|
||||
ssa_fprintf(f, "@");
|
||||
ssa_print_escape_string(f, name);
|
||||
}
|
||||
|
||||
|
||||
void ssa_print_type(gbFile *f, BaseTypeSizes s, Type *t) {
|
||||
i64 word_bits = 8*s.word_size;
|
||||
GB_ASSERT_NOT_NULL(t);
|
||||
t = default_type(t);
|
||||
|
||||
switch (t->kind) {
|
||||
case Type_Basic:
|
||||
switch (t->basic.kind) {
|
||||
case Basic_bool: ssa_fprintf(f, "i1"); break;
|
||||
case Basic_i8: ssa_fprintf(f, "i8"); break;
|
||||
case Basic_i16: ssa_fprintf(f, "i16"); break;
|
||||
case Basic_i32: ssa_fprintf(f, "i32"); break;
|
||||
case Basic_i64: ssa_fprintf(f, "i64"); break;
|
||||
case Basic_u8: ssa_fprintf(f, "i8"); break;
|
||||
case Basic_u16: ssa_fprintf(f, "i16"); break;
|
||||
case Basic_u32: ssa_fprintf(f, "i32"); break;
|
||||
case Basic_u64: ssa_fprintf(f, "i64"); break;
|
||||
case Basic_f32: ssa_fprintf(f, "float"); break;
|
||||
case Basic_f64: ssa_fprintf(f, "double"); break;
|
||||
case Basic_rawptr: ssa_fprintf(f, "void*"); break;
|
||||
case Basic_string: ssa_fprintf(f, "{i8*, i%lld}", word_bits); break;
|
||||
case Basic_int: ssa_fprintf(f, "i%lld", word_bits); break;
|
||||
case Basic_uint: ssa_fprintf(f, "i%lld", word_bits); break;
|
||||
}
|
||||
break;
|
||||
case Type_Array:
|
||||
ssa_fprintf(f, "[%lld x ", t->array.count);
|
||||
ssa_print_type(f, s, t->array.element);
|
||||
ssa_fprintf(f, "]");
|
||||
break;
|
||||
case Type_Slice:
|
||||
ssa_fprintf(f, "{");
|
||||
ssa_print_type(f, s, t->slice.element);
|
||||
ssa_fprintf(f, "*, %lld, %lld}", word_bits, word_bits);
|
||||
break;
|
||||
case Type_Structure:
|
||||
ssa_fprintf(f, "{");
|
||||
for (isize i = 0; i < t->structure.field_count; i++) {
|
||||
if (i > 0) ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, s, t->structure.fields[i]->type);
|
||||
}
|
||||
ssa_fprintf(f, "}");
|
||||
break;
|
||||
case Type_Pointer:
|
||||
ssa_print_type(f, s, t->pointer.element);
|
||||
ssa_fprintf(f, "*");
|
||||
break;
|
||||
case Type_Named:
|
||||
ssa_print_encoded_local(f, t->named.name);
|
||||
break;
|
||||
case Type_Alias:
|
||||
ssa_print_type(f, s, t->alias.base);
|
||||
break;
|
||||
case Type_Tuple:
|
||||
if (t->tuple.variable_count == 1) {
|
||||
ssa_print_type(f, s, t->tuple.variables[0]->type);
|
||||
} else {
|
||||
ssa_fprintf(f, "{");
|
||||
for (isize i = 0; i < t->tuple.variable_count; i++) {
|
||||
if (i > 0) ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, s, t->tuple.variables[i]->type);
|
||||
}
|
||||
ssa_fprintf(f, "}");
|
||||
}
|
||||
break;
|
||||
case Type_Procedure:
|
||||
if (t->procedure.result_count == 0)
|
||||
ssa_fprintf(f, "void");
|
||||
else
|
||||
ssa_print_type(f, s, t->procedure.results);
|
||||
ssa_fprintf(f, " (");
|
||||
for (isize i = 0; i < t->procedure.param_count; i++) {
|
||||
if (i > 0) ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, s, &t->procedure.params[i]);
|
||||
}
|
||||
ssa_fprintf(f, ")");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_print_exact_value(gbFile *f, ssaModule *m, ExactValue value, Type *type) {
|
||||
switch (value.kind) {
|
||||
case ExactValue_Bool:
|
||||
ssa_fprintf(f, (value.value_bool ? "true" : "false"));
|
||||
break;
|
||||
case ExactValue_String: {
|
||||
ssa_fprintf(f, "{");
|
||||
ssa_print_type(f, m->sizes, &basic_types[Basic_i8]);
|
||||
ssa_fprintf(f, "* \"");
|
||||
// TODO(bill): Make unquote string function
|
||||
String unquoted = value.value_string;
|
||||
unquoted.text++;
|
||||
unquoted.len -= 2;
|
||||
ssa_print_escape_string(f, unquoted);
|
||||
ssa_fprintf(f, "\", ");
|
||||
ssa_print_type(f, m->sizes, &basic_types[Basic_int]);
|
||||
ssa_fprintf(f, " %td}", value.value_string.len);
|
||||
} break;
|
||||
case ExactValue_Integer:
|
||||
ssa_fprintf(f, "%lld", value.value_integer);
|
||||
break;
|
||||
case ExactValue_Float: {
|
||||
u64 u = 0;
|
||||
if (is_type_float(type) && type->basic.kind == Basic_f32) {
|
||||
// IMPORTANT NOTE(bill): LLVM requires all floating point constants to be
|
||||
// a 64 bit number if bits_of(float type) <= 64.
|
||||
// To overcome this problem, fill the "bottom" 32 bits with zeros
|
||||
// https://groups.google.com/forum/#!topic/llvm-dev/IlqV3TbSk6M
|
||||
f32 fp = cast(f32)value.value_float;
|
||||
u = *cast(u32 *)&fp;
|
||||
u <<= 32;
|
||||
|
||||
} else {
|
||||
u = *cast(u64 *)&value.value_float;
|
||||
}
|
||||
ssa_fprintf(f, "0x%llx", u);
|
||||
} break;
|
||||
case ExactValue_Pointer:
|
||||
if (value.value_float == NULL) {
|
||||
ssa_fprintf(f, "null");
|
||||
} else {
|
||||
GB_PANIC("TODO(bill): ExactValue_Pointer");
|
||||
}
|
||||
break;
|
||||
default:
|
||||
GB_PANIC("Invalid ExactValue");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ssa_print_value(gbFile *f, ssaModule *m, ssaValue *value, Type *type_hint) {
|
||||
if (value == NULL) {
|
||||
ssa_fprintf(f, "!!!NULL_VALUE");
|
||||
return;
|
||||
}
|
||||
switch (value->kind) {
|
||||
case ssaValue_TypeName:
|
||||
ssa_print_encoded_local(f, value->type_name->token.string);
|
||||
break;
|
||||
case ssaValue_Global:
|
||||
ssa_print_encoded_global(f, value->global.entity->token.string);
|
||||
break;
|
||||
case ssaValue_Procedure:
|
||||
ssa_print_encoded_global(f, value->procedure.entity->token.string);
|
||||
break;
|
||||
case ssaValue_Constant: {
|
||||
ssa_print_exact_value(f, m, value->constant.value, type_hint);
|
||||
} break;
|
||||
case ssaValue_Instruction:
|
||||
ssa_fprintf(f, "%%%d", value->id);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_print_instruction(gbFile *f, ssaModule *m, ssaValue *value) {
|
||||
GB_ASSERT(value->kind == ssaValue_Instruction);
|
||||
|
||||
ssa_fprintf(f, "\t");
|
||||
|
||||
ssaInstruction *instr = &value->instruction;
|
||||
switch (instr->kind) {
|
||||
case ssaInstruction_Local: {
|
||||
Type *type = instr->local.entity->type;
|
||||
ssa_fprintf(f, "%%%d = alloca ", value->id);
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, ", align %lld\n", type_align_of(m->sizes, gb_heap_allocator(), type));
|
||||
ssa_fprintf(f, "\tstore ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, " zeroinitializer, ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, "* %%%d\n", value->id);
|
||||
} break;
|
||||
|
||||
case ssaInstruction_Store: {
|
||||
Type *type = ssa_value_type(instr->store.address);
|
||||
ssa_fprintf(f, "store ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_value(f, m, instr->store.value, type);
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, "* ");
|
||||
ssa_print_value(f, m, instr->store.address, type);
|
||||
ssa_fprintf(f, "\n");
|
||||
} break;
|
||||
|
||||
case ssaInstruction_Load: {
|
||||
Type *type = ssa_value_type(instr->load.address);
|
||||
ssa_fprintf(f, "%%%d = load ", value->id);
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, "* ");
|
||||
ssa_print_value(f, m, instr->load.address, type);
|
||||
ssa_fprintf(f, "\n");
|
||||
} break;
|
||||
|
||||
|
||||
case ssaInstruction_BinaryOp: {
|
||||
ssaBinaryOp *bo = &value->instruction.binary_op;
|
||||
Type *type = ssa_value_type(bo->left);
|
||||
|
||||
if (is_type_float(type))
|
||||
ssa_fprintf(f, "f");
|
||||
|
||||
switch (bo->op.kind) {
|
||||
case Token_Add: ssa_fprintf(f, "add"); break;
|
||||
case Token_Sub: ssa_fprintf(f, "sub"); break;
|
||||
case Token_And: ssa_fprintf(f, "and"); break;
|
||||
case Token_Or: ssa_fprintf(f, "or"); break;
|
||||
case Token_Xor: ssa_fprintf(f, "xor"); break;
|
||||
|
||||
case Token_AndNot: GB_PANIC("TODO(bill): print Token_AndNot");
|
||||
|
||||
case Token_Mul: ssa_fprintf(f, "mul"); break;
|
||||
|
||||
default: {
|
||||
if (!is_type_float(type)) {
|
||||
if (is_type_unsigned(type))
|
||||
ssa_fprintf(f, "u");
|
||||
else
|
||||
ssa_fprintf(f, "s");
|
||||
}
|
||||
|
||||
switch (bo->op.kind) {
|
||||
case Token_Quo: ssa_fprintf(f, "div"); break;
|
||||
case Token_Mod: ssa_fprintf(f, "rem"); break;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_type(f, m->sizes, type);
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_value(f, m, bo->left, type);
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_value(f, m, bo->right, type);
|
||||
ssa_fprintf(f, "\n");
|
||||
|
||||
} break;
|
||||
|
||||
default:
|
||||
ssa_fprintf(f, "; <unknown instr> %d\n", instr->kind);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_print_llvm_ir(gbFile *f, ssaModule *m) {
|
||||
gb_printf("-- Printing LLVM-IR -- \n\n");
|
||||
gb_for_array(member_index, m->members.entries) {
|
||||
auto *entry = &m->members.entries[member_index];
|
||||
ssaValue *v = entry->value;
|
||||
switch (v->kind) {
|
||||
case ssaValue_TypeName: {
|
||||
ssa_print_encoded_local(f, v->type_name->token.string);
|
||||
ssa_fprintf(f, " = type ");
|
||||
ssa_print_type(f, m->sizes, get_base_type(v->type_name->type));
|
||||
ssa_fprintf(f, "\n");
|
||||
} break;
|
||||
|
||||
case ssaValue_Global: {
|
||||
ssaGlobal *g = &v->global;
|
||||
ssa_print_encoded_global(f, g->entity->token.string);
|
||||
ssa_fprintf(f, " = global ");
|
||||
ssa_print_type(f, m->sizes, get_base_type(g->entity->type));
|
||||
ssa_fprintf(f, " ");
|
||||
ssa_print_value(f, m, g->value, g->entity->type);
|
||||
ssa_fprintf(f, ", align %td\n", type_align_of(m->sizes, gb_heap_allocator(), g->entity->type));
|
||||
} break;
|
||||
|
||||
case ssaValue_Procedure: {
|
||||
ssaProcedure *proc = &v->procedure;
|
||||
if (proc->body == NULL) {
|
||||
ssa_fprintf(f, "declare ");
|
||||
} else {
|
||||
ssa_fprintf(f, "define ");
|
||||
}
|
||||
|
||||
auto *proc_type = &proc->entity->type->procedure;
|
||||
|
||||
if (proc_type->result_count == 0) {
|
||||
ssa_fprintf(f, "void");
|
||||
} else {
|
||||
ssa_print_type(f, m->sizes, proc_type->results);
|
||||
}
|
||||
|
||||
ssa_fprintf(f, " ");
|
||||
|
||||
ssa_print_encoded_global(f, proc->name);
|
||||
ssa_fprintf(f, "(");
|
||||
|
||||
if (proc_type->param_count > 0) {
|
||||
auto *params = &proc_type->params->tuple;
|
||||
for (isize i = 0; i < params->variable_count; i++) {
|
||||
Entity *e = params->variables[i];
|
||||
if (i > 0)
|
||||
ssa_fprintf(f, ", ");
|
||||
ssa_print_type(f, m->sizes, e->type);
|
||||
ssa_fprintf(f, " %%%.*s", LIT(e->token.string));
|
||||
}
|
||||
}
|
||||
|
||||
ssa_fprintf(f, ") ");
|
||||
|
||||
if (proc->body == NULL) {
|
||||
ssa_fprintf(f, "\n");
|
||||
} else {
|
||||
ssa_fprintf(f, "{\n");
|
||||
gb_for_array(i, proc->blocks) {
|
||||
ssaBlock *block = &proc->blocks[i]->block;
|
||||
ssa_fprintf(f, "%.*s:\n", LIT(block->label));
|
||||
gb_for_array(j, block->instructions) {
|
||||
ssaValue *value = block->instructions[j];
|
||||
ssa_print_instruction(f, m, value);
|
||||
}
|
||||
}
|
||||
|
||||
if (proc_type->result_count == 0) {
|
||||
ssa_fprintf(f, "\tret void\n");
|
||||
}
|
||||
ssa_fprintf(f, "}\n");
|
||||
}
|
||||
|
||||
} break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,746 @@
|
||||
struct ssaModule;
|
||||
struct ssaProcedure;
|
||||
struct ssaBlock;
|
||||
struct ssaValue;
|
||||
|
||||
|
||||
struct ssaModule {
|
||||
CheckerInfo *info;
|
||||
BaseTypeSizes sizes;
|
||||
gbAllocator allocator;
|
||||
|
||||
Map<ssaValue *> values; // Key: Entity *
|
||||
Map<ssaValue *> members; // Key: String
|
||||
i32 global_string_index;
|
||||
};
|
||||
|
||||
|
||||
struct ssaBlock {
|
||||
i32 id;
|
||||
AstNode *node;
|
||||
Scope *scope;
|
||||
String label;
|
||||
ssaProcedure *parent;
|
||||
|
||||
gbArray(ssaValue *) instructions;
|
||||
gbArray(ssaValue *) values;
|
||||
};
|
||||
|
||||
struct ssaProcedure {
|
||||
ssaModule *module;
|
||||
String name;
|
||||
Entity *entity;
|
||||
DeclarationInfo *decl;
|
||||
AstNode *type_expr;
|
||||
AstNode *body;
|
||||
|
||||
gbArray(ssaValue *) blocks;
|
||||
ssaBlock *curr_block;
|
||||
};
|
||||
|
||||
|
||||
struct ssaLocal {
|
||||
Entity *entity;
|
||||
};
|
||||
struct ssaGlobal {
|
||||
b32 generated;
|
||||
Entity *entity;
|
||||
ssaValue *value;
|
||||
};
|
||||
struct ssaStore {
|
||||
ssaValue *address;
|
||||
ssaValue *value;
|
||||
};
|
||||
struct ssaLoad {
|
||||
ssaValue *address;
|
||||
};
|
||||
struct ssaBinaryOp {
|
||||
Token op;
|
||||
ssaValue *left, *right;
|
||||
};
|
||||
struct ssaGetElementPtr {
|
||||
ssaValue *address;
|
||||
Type *result_type;
|
||||
Type *element_type;
|
||||
gbArray(ssaValue *) indices;
|
||||
};
|
||||
|
||||
enum ssaInstructionKind {
|
||||
ssaInstruction_Invalid,
|
||||
|
||||
ssaInstruction_Local,
|
||||
ssaInstruction_Store,
|
||||
ssaInstruction_Load,
|
||||
ssaInstruction_GetElementPtr,
|
||||
|
||||
ssaInstruction_BinaryOp,
|
||||
|
||||
ssaInstruction_Count,
|
||||
};
|
||||
|
||||
struct ssaInstruction {
|
||||
ssaInstructionKind kind;
|
||||
|
||||
ssaBlock *parent;
|
||||
Type *type;
|
||||
TokenPos pos;
|
||||
|
||||
union {
|
||||
ssaLocal local;
|
||||
ssaStore store;
|
||||
ssaLoad load;
|
||||
ssaGetElementPtr get_element_ptr;
|
||||
|
||||
ssaBinaryOp binary_op;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
enum ssaValueKind {
|
||||
ssaValue_Invalid,
|
||||
|
||||
ssaValue_TypeName,
|
||||
ssaValue_Global,
|
||||
ssaValue_Procedure,
|
||||
ssaValue_Constant,
|
||||
|
||||
ssaValue_Block,
|
||||
ssaValue_Instruction,
|
||||
|
||||
ssaValue_Count,
|
||||
};
|
||||
|
||||
struct ssaValue {
|
||||
ssaValueKind kind;
|
||||
i32 id;
|
||||
|
||||
union {
|
||||
Entity * type_name;
|
||||
ssaGlobal global;
|
||||
ssaProcedure procedure;
|
||||
TypeAndValue constant;
|
||||
ssaBlock block;
|
||||
ssaInstruction instruction;
|
||||
};
|
||||
};
|
||||
|
||||
enum ssaLvalueKind {
|
||||
ssaLvalue_Blank,
|
||||
ssaLvalue_Address,
|
||||
|
||||
ssaLvalue_Count,
|
||||
};
|
||||
|
||||
struct ssaLvalue {
|
||||
ssaLvalueKind kind;
|
||||
union {
|
||||
struct {} blank;
|
||||
struct {
|
||||
ssaValue *value;
|
||||
AstNode *expr;
|
||||
} address;
|
||||
};
|
||||
};
|
||||
|
||||
void ssa_module_init(ssaModule *m, Checker *c) {
|
||||
m->allocator = gb_heap_allocator();
|
||||
m->info = &c->info;
|
||||
m->sizes = c->sizes;
|
||||
|
||||
map_init(&m->values, m->allocator);
|
||||
map_init(&m->members, m->allocator);
|
||||
}
|
||||
|
||||
void ssa_module_destroy(ssaModule *m) {
|
||||
map_destroy(&m->values);
|
||||
map_destroy(&m->members);
|
||||
}
|
||||
|
||||
void ssa_module_add_value(ssaModule *m, Entity *e, ssaValue *v) {
|
||||
map_set(&m->values, hash_pointer(e), v);
|
||||
}
|
||||
|
||||
|
||||
Type *ssa_value_type(ssaValue *value);
|
||||
|
||||
Type *ssa_instruction_type(ssaInstruction *instr) {
|
||||
switch (instr->kind) {
|
||||
case ssaInstruction_Local:
|
||||
return instr->local.entity->type;
|
||||
case ssaInstruction_Store:
|
||||
return ssa_value_type(instr->store.address);
|
||||
case ssaInstruction_Load:
|
||||
return ssa_value_type(instr->load.address);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Type *ssa_value_type(ssaValue *value) {
|
||||
switch (value->kind) {
|
||||
case ssaValue_TypeName:
|
||||
return value->type_name->type;
|
||||
case ssaValue_Global:
|
||||
return value->global.entity->type;
|
||||
case ssaValue_Procedure:
|
||||
return value->procedure.entity->type;
|
||||
case ssaValue_Constant:
|
||||
return value->constant.type;
|
||||
case ssaValue_Instruction:
|
||||
return ssa_instruction_type(&value->instruction);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_alloc_value(gbAllocator a, ssaValueKind kind) {
|
||||
ssaValue *v = gb_alloc_item(a, ssaValue);
|
||||
v->kind = kind;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_alloc_instruction(gbAllocator a, ssaInstructionKind kind) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Instruction);
|
||||
v->instruction.kind = kind;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_type_name(gbAllocator a, Entity *e) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_TypeName);
|
||||
v->type_name = e;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_global(gbAllocator a, Entity *e, ssaValue *value) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Global);
|
||||
v->global.entity = e;
|
||||
v->global.value = value;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_make_instruction_local(ssaProcedure *p, Entity *e) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Local);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->local.entity = e;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
ssa_module_add_value(p->module, e, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_instruction_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Store);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->store.address = address;
|
||||
i->store.value = value;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instruction_load(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_Load);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->load.address = address;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instruction_get_element_ptr(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_GetElementPtr);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->get_element_ptr.address = address;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instruction_binary_op(ssaProcedure *p, Token op, ssaValue *left, ssaValue *right) {
|
||||
ssaValue *v = ssa_alloc_instruction(p->module->allocator, ssaInstruction_BinaryOp);
|
||||
ssaInstruction *i = &v->instruction;
|
||||
i->binary_op.op = op;
|
||||
i->binary_op.left = left;
|
||||
i->binary_op.right = right;
|
||||
if (p->curr_block) {
|
||||
gb_array_append(p->curr_block->values, v);
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_value_constant(gbAllocator a, Type *type, ExactValue value) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Constant);
|
||||
v->constant.type = type;
|
||||
v->constant.value = value;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_procedure(gbAllocator a, Entity *e, DeclarationInfo *decl, ssaModule *m) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Procedure);
|
||||
v->procedure.module = m;
|
||||
v->procedure.entity = e;
|
||||
v->procedure.decl = decl;
|
||||
v->procedure.name = e->token.string;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_block(gbAllocator a, ssaProcedure *proc, AstNode *node, Scope *scope, String label) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Block);
|
||||
v->block.label = label;
|
||||
v->block.node = node;
|
||||
v->block.scope = scope;
|
||||
v->block.parent = proc;
|
||||
|
||||
gb_array_init(v->block.instructions, gb_heap_allocator());
|
||||
gb_array_init(v->block.values, gb_heap_allocator());
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_add_global_string(ssaProcedure *proc, ExactValue value) {
|
||||
GB_ASSERT(value.kind == ExactValue_String);
|
||||
gbAllocator a = gb_heap_allocator();
|
||||
|
||||
|
||||
isize max_len = 4+8+1;
|
||||
u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len);
|
||||
isize len = gb_snprintf(cast(char *)str, max_len, ".str%x", proc->module->global_string_index);
|
||||
proc->module->global_string_index++;
|
||||
|
||||
String name = make_string(str, len-1);
|
||||
Token token = {Token_String};
|
||||
token.string = name;
|
||||
Type *type = &basic_types[Basic_string];
|
||||
Entity *entity = make_entity_constant(a, NULL, token, type, value);
|
||||
ssaValue *v = ssa_make_value_constant(a, type, value);
|
||||
|
||||
|
||||
ssaValue *g = ssa_make_value_global(a, entity, v);
|
||||
g->global.generated = true;
|
||||
|
||||
map_set(&proc->module->values, hash_pointer(entity), g);
|
||||
map_set(&proc->module->members, hash_string(name), g);
|
||||
|
||||
return g;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_add_block(ssaProcedure *proc, AstNode *node, String label) {
|
||||
gbAllocator a = proc->module->allocator;
|
||||
Scope *scope = NULL;
|
||||
Scope **found = map_get(&proc->module->info->scopes, hash_pointer(node));
|
||||
if (found) scope = *found;
|
||||
ssaValue *block = ssa_make_value_block(a, proc, node, scope, label);
|
||||
gb_array_append(proc->blocks, block);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ssa_begin_procedure_body(ssaProcedure *proc) {
|
||||
gb_array_init(proc->blocks, gb_heap_allocator());
|
||||
ssaValue *b = ssa_add_block(proc, proc->body, make_string("entry"));
|
||||
proc->curr_block = &b->block;
|
||||
}
|
||||
|
||||
|
||||
void ssa_end_procedure_body(ssaProcedure *proc) {
|
||||
|
||||
// Number registers
|
||||
i32 reg_id = 0;
|
||||
gb_for_array(i, proc->blocks) {
|
||||
ssaBlock *b = &proc->blocks[i]->block;
|
||||
gb_for_array(j, b->instructions) {
|
||||
ssaValue *value = b->instructions[j];
|
||||
ssaInstruction *instr = &value->instruction;
|
||||
if (instr->kind == ssaInstruction_Store) {
|
||||
continue;
|
||||
}
|
||||
value->id = reg_id;
|
||||
reg_id++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
b32 ssa_is_blank_identifier(AstNode *i) {
|
||||
GB_ASSERT(i->kind == AstNode_Identifier);
|
||||
return are_strings_equal(i->identifier.token.string, make_string("_"));
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_block_emit(ssaBlock *b, ssaValue *instr) {
|
||||
instr->instruction.parent = b;
|
||||
gb_array_append(b->instructions, instr);
|
||||
return instr;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit(ssaProcedure *proc, ssaValue *instr) {
|
||||
return ssa_block_emit(proc->curr_block, instr);
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_add_local(ssaProcedure *proc, Entity *e) {
|
||||
ssaValue *instr = ssa_make_instruction_local(proc, e);
|
||||
ssa_emit(proc, instr);
|
||||
return instr;
|
||||
}
|
||||
|
||||
ssaValue *ssa_add_local_for_identifier(ssaProcedure *proc, AstNode *name) {
|
||||
Entity **found = map_get(&proc->module->info->definitions, hash_pointer(name));
|
||||
if (found) {
|
||||
return ssa_add_local(proc, *found);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_emit_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
|
||||
ssaValue *store = ssa_make_instruction_store(p, address, value);
|
||||
ssa_emit(p, store);
|
||||
return store;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit_load(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_make_instruction_load(p, address);
|
||||
ssa_emit(p, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_lvalue_store(ssaLvalue lval, ssaProcedure *p, ssaValue *value) {
|
||||
switch (lval.kind) {
|
||||
case ssaLvalue_Address:
|
||||
return ssa_emit_store(p, lval.address.value, value);
|
||||
}
|
||||
GB_PANIC("Illegal lvalue store");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssaValue *ssa_lvalue_load(ssaLvalue lval, ssaProcedure *p) {
|
||||
switch (lval.kind) {
|
||||
case ssaLvalue_Address:
|
||||
return ssa_emit_load(p, lval.address.value);
|
||||
}
|
||||
GB_PANIC("Illegal lvalue load");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_lvalue_address(ssaLvalue lval, ssaProcedure *p) {
|
||||
switch (lval.kind) {
|
||||
case ssaLvalue_Address:
|
||||
return lval.address.value;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr);
|
||||
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv);
|
||||
|
||||
ssaValue *ssa_emit_conversion(ssaProcedure *proc, ssaValue *value, Type *a_type) {
|
||||
Type *b_type = ssa_value_type(value);
|
||||
if (are_types_identical(a_type, b_type))
|
||||
return value;
|
||||
|
||||
Type *a = get_base_type(a_type);
|
||||
Type *b = get_base_type(b_type);
|
||||
|
||||
if (value->kind == ssaValue_Constant) {
|
||||
if (a->kind == Type_Basic)
|
||||
return ssa_make_value_constant(proc->module->allocator, a_type, value->constant.value);
|
||||
}
|
||||
|
||||
GB_PANIC("TODO(bill): ssa_emit_conversion");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
ssaValue *ssa_emit_arith(ssaProcedure *proc, Token op, ssaValue *left, ssaValue *right, Type *type) {
|
||||
switch (op.kind) {
|
||||
case Token_Add:
|
||||
case Token_Sub:
|
||||
case Token_Mul:
|
||||
case Token_Quo:
|
||||
case Token_Mod:
|
||||
case Token_And:
|
||||
case Token_Or:
|
||||
case Token_Xor:
|
||||
case Token_AndNot:
|
||||
left = ssa_emit_conversion(proc, left, type);
|
||||
right = ssa_emit_conversion(proc, right, type);
|
||||
break;
|
||||
}
|
||||
|
||||
ssaValue *v = ssa_make_instruction_binary_op(proc, op, left, right);
|
||||
return ssa_emit(proc, v);
|
||||
}
|
||||
|
||||
ssaValue *ssa_build_single_expression(ssaProcedure *proc, AstNode *expr, TypeAndValue *tv) {
|
||||
switch (expr->kind) {
|
||||
case AstNode_Identifier: {
|
||||
Entity *e = *map_get(&proc->module->info->uses, hash_pointer(expr));
|
||||
if (e->kind == Entity_Builtin) {
|
||||
// TODO(bill): Entity_Builtin
|
||||
return NULL;
|
||||
}
|
||||
|
||||
auto *found = map_get(&proc->module->values, hash_pointer(e));
|
||||
if (found) {
|
||||
return ssa_emit_load(proc, *found);
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_UnaryExpression: {
|
||||
auto *ue = &expr->unary_expression;
|
||||
switch (ue->op.kind) {
|
||||
case Token_Add:
|
||||
return ssa_build_expression(proc, ue->operand);
|
||||
case Token_Sub:
|
||||
return NULL;
|
||||
case Token_Xor:
|
||||
return NULL;
|
||||
case Token_Not:
|
||||
return NULL;
|
||||
case Token_Pointer:
|
||||
return NULL;
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_BinaryExpression: {
|
||||
auto *be = &expr->binary_expression;
|
||||
switch (be->op.kind) {
|
||||
case Token_Add:
|
||||
case Token_Sub:
|
||||
case Token_Mul:
|
||||
case Token_Quo:
|
||||
case Token_Mod:
|
||||
case Token_And:
|
||||
case Token_Or:
|
||||
case Token_Xor:
|
||||
case Token_AndNot:
|
||||
return ssa_emit_arith(proc, be->op,
|
||||
ssa_build_expression(proc, be->left),
|
||||
ssa_build_expression(proc, be->right),
|
||||
tv->type);
|
||||
}
|
||||
} break;
|
||||
case AstNode_ProcedureLiteral:
|
||||
break;
|
||||
case AstNode_CastExpression:
|
||||
break;
|
||||
case AstNode_CallExpression:
|
||||
break;
|
||||
case AstNode_SliceExpression:
|
||||
break;
|
||||
case AstNode_IndexExpression:
|
||||
break;
|
||||
case AstNode_SelectorExpression:
|
||||
break;
|
||||
}
|
||||
|
||||
GB_PANIC("Unexpected expression");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_build_expression(ssaProcedure *proc, AstNode *expr) {
|
||||
expr = unparen_expression(expr);
|
||||
|
||||
TypeAndValue *tv = map_get(&proc->module->info->types, hash_pointer(expr));
|
||||
if (tv) {
|
||||
if (tv->value.kind != ExactValue_Invalid) {
|
||||
if (tv->value.kind == ExactValue_String) {
|
||||
ssaValue *global_str = ssa_add_global_string(proc, tv->value);
|
||||
return ssa_emit_load(proc, global_str);
|
||||
}
|
||||
return ssa_make_value_constant(proc->module->allocator, tv->type, tv->value);
|
||||
}
|
||||
|
||||
ssaValue *value = NULL;
|
||||
if (tv->mode == Addressing_Variable) {
|
||||
gb_printf("!Addressable!\n");
|
||||
// TODO(bill): Addressing_Variable
|
||||
} else {
|
||||
value = ssa_build_single_expression(proc, expr, tv);
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
ssaLvalue ssa_build_address(ssaProcedure *proc, AstNode *expr) {
|
||||
switch (expr->kind) {
|
||||
case AstNode_Identifier: {
|
||||
if (!ssa_is_blank_identifier(expr)) {
|
||||
Entity *e = entity_of_identifier(proc->module->info, expr);
|
||||
|
||||
ssaLvalue val = {ssaLvalue_Address};
|
||||
val.address.expr = expr;
|
||||
ssaValue **found = map_get(&proc->module->values, hash_pointer(e));
|
||||
if (found) {
|
||||
val.address.value = *found;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_ParenExpression:
|
||||
return ssa_build_address(proc, unparen_expression(expr));
|
||||
|
||||
case AstNode_DereferenceExpression: {
|
||||
ssaLvalue val = {ssaLvalue_Address};
|
||||
val.address.value = ssa_build_expression(proc, expr);
|
||||
val.address.expr = expr;
|
||||
return val;
|
||||
} break;
|
||||
|
||||
case AstNode_SelectorExpression:
|
||||
break;
|
||||
|
||||
case AstNode_IndexExpression:
|
||||
break;
|
||||
|
||||
// TODO(bill): Others address
|
||||
}
|
||||
|
||||
ssaLvalue blank = {ssaLvalue_Blank};
|
||||
return blank;
|
||||
}
|
||||
|
||||
void ssa_build_statement(ssaProcedure *proc, AstNode *s);
|
||||
|
||||
void ssa_build_statement_list(ssaProcedure *proc, AstNode *list) {
|
||||
for (AstNode *stmt = list ; stmt != NULL; stmt = stmt->next)
|
||||
ssa_build_statement(proc, stmt);
|
||||
}
|
||||
|
||||
void ssa_build_statement(ssaProcedure *proc, AstNode *s) {
|
||||
switch (s->kind) {
|
||||
case AstNode_EmptyStatement:
|
||||
break;
|
||||
case AstNode_VariableDeclaration: {
|
||||
auto *vd = &s->variable_declaration;
|
||||
if (vd->kind == Declaration_Mutable) {
|
||||
if (vd->name_count == vd->value_count) { // 1:1 assigment
|
||||
|
||||
} else if (vd->value_count == 0) { // declared and zero-initialized
|
||||
for (AstNode *name = vd->name_list; name != NULL; name = name->next) {
|
||||
if (!ssa_is_blank_identifier(name)) {
|
||||
// TODO(bill): add local
|
||||
ssa_add_local_for_identifier(proc, name);
|
||||
}
|
||||
}
|
||||
} else { // Tuple(s)
|
||||
GB_PANIC("TODO(bill): tuple assignment variable declaration");
|
||||
}
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_AssignStatement: {
|
||||
auto *assign = &s->assign_statement;
|
||||
switch (assign->op.kind) {
|
||||
case Token_Eq: {
|
||||
gbArray(ssaLvalue) lvals;
|
||||
gb_array_init(lvals, gb_heap_allocator());
|
||||
defer (gb_array_free(lvals));
|
||||
|
||||
for (AstNode *lhs = assign->lhs_list;
|
||||
lhs != NULL;
|
||||
lhs = lhs->next) {
|
||||
ssaLvalue lval = {};
|
||||
if (!ssa_is_blank_identifier(lhs)) {
|
||||
lval = ssa_build_address(proc, lhs);
|
||||
}
|
||||
gb_array_append(lvals, lval);
|
||||
}
|
||||
|
||||
if (assign->lhs_count == assign->rhs_count) {
|
||||
if (assign->lhs_count == 1) {
|
||||
AstNode *lhs = assign->lhs_list;
|
||||
AstNode *rhs = assign->rhs_list;
|
||||
ssaValue *init = ssa_build_expression(proc, rhs);
|
||||
ssa_lvalue_store(lvals[0], proc, init);
|
||||
} else {
|
||||
GB_PANIC("TODO(bill): parallel assignment");
|
||||
}
|
||||
} else {
|
||||
GB_PANIC("TODO(bill): tuple assignment");
|
||||
}
|
||||
|
||||
} break;
|
||||
|
||||
default: // +=, -=, etc
|
||||
break;
|
||||
}
|
||||
} break;
|
||||
|
||||
case AstNode_ExpressionStatement:
|
||||
ssa_build_expression(proc, s->expression_statement.expression);
|
||||
break;
|
||||
|
||||
case AstNode_BlockStatement:
|
||||
ssa_build_statement_list(proc, s->block_statement.list);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void ssa_build_procedure(ssaValue *value) {
|
||||
ssaProcedure *proc = &value->procedure;
|
||||
|
||||
gb_printf("Building %.*s: %.*s\n", LIT(entity_strings[proc->entity->kind]), LIT(proc->name));
|
||||
|
||||
|
||||
AstNode *proc_decl = proc->decl->proc_decl;
|
||||
switch (proc_decl->kind) {
|
||||
case AstNode_ProcedureDeclaration:
|
||||
proc->type_expr = proc_decl->procedure_declaration.type;
|
||||
proc->body = proc_decl->procedure_declaration.body;
|
||||
break;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
|
||||
if (proc->body == NULL) {
|
||||
// TODO(bill): External procedure
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
ssa_begin_procedure_body(proc);
|
||||
ssa_build_statement(proc, proc->body);
|
||||
ssa_end_procedure_body(proc);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user