mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 20:58:15 +00:00
Code reorganization - Separate files and slice refactoring
This commit is contained in:
+2030
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,675 @@
|
||||
struct ssaGen {
|
||||
ssaModule module;
|
||||
gbFile output_file;
|
||||
};
|
||||
|
||||
b32 ssa_gen_init(ssaGen *s, Checker *c) {
|
||||
if (global_error_collector.count != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
isize tc = c->parser->total_token_count;
|
||||
if (tc < 2) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ssa_init_module(&s->module, c);
|
||||
s->module.generate_debug_info = false;
|
||||
|
||||
// TODO(bill): generate appropriate output name
|
||||
int pos = cast(int)string_extension_position(c->parser->init_fullpath);
|
||||
gbFileError err = gb_file_create(&s->output_file, gb_bprintf("%.*s.ll", pos, c->parser->init_fullpath.text));
|
||||
if (err != gbFileError_None) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void ssa_gen_destroy(ssaGen *s) {
|
||||
ssa_destroy_module(&s->module);
|
||||
gb_file_close(&s->output_file);
|
||||
}
|
||||
|
||||
String ssa_mangle_name(ssaGen *s, String path, String name) {
|
||||
// NOTE(bill): prefix names not in the init scope
|
||||
// TODO(bill): make robust and not just rely on the file's name
|
||||
|
||||
ssaModule *m = &s->module;
|
||||
CheckerInfo *info = m->info;
|
||||
gbAllocator a = m->allocator;
|
||||
AstFile *file = *map_get(&info->files, hash_string(path));
|
||||
|
||||
char *str = gb_alloc_array(a, char, path.len+1);
|
||||
gb_memmove(str, path.text, path.len);
|
||||
str[path.len] = 0;
|
||||
for (isize i = 0; i < path.len; i++) {
|
||||
if (str[i] == '\\') {
|
||||
str[i] = '/';
|
||||
}
|
||||
}
|
||||
|
||||
char const *base = gb_path_base_name(str);
|
||||
char const *ext = gb_path_extension(base);
|
||||
isize base_len = ext-1-base;
|
||||
|
||||
isize max_len = base_len + 1 + 10 + 1 + name.len;
|
||||
u8 *new_name = gb_alloc_array(a, u8, max_len);
|
||||
isize new_name_len = gb_snprintf(
|
||||
cast(char *)new_name, max_len,
|
||||
"%.*s-%u.%.*s",
|
||||
cast(int)base_len, base,
|
||||
file->id,
|
||||
LIT(name));
|
||||
|
||||
return make_string(new_name, new_name_len-1);
|
||||
}
|
||||
|
||||
|
||||
void ssa_gen_tree(ssaGen *s) {
|
||||
ssaModule *m = &s->module;
|
||||
CheckerInfo *info = m->info;
|
||||
gbAllocator a = m->allocator;
|
||||
|
||||
if (v_zero == NULL) {
|
||||
v_zero = ssa_make_const_int (m->allocator, 0);
|
||||
v_one = ssa_make_const_int (m->allocator, 1);
|
||||
v_zero32 = ssa_make_const_i32 (m->allocator, 0);
|
||||
v_one32 = ssa_make_const_i32 (m->allocator, 1);
|
||||
v_two32 = ssa_make_const_i32 (m->allocator, 2);
|
||||
v_false = ssa_make_const_bool(m->allocator, false);
|
||||
v_true = ssa_make_const_bool(m->allocator, true);
|
||||
}
|
||||
|
||||
isize global_variable_max_count = 0;
|
||||
Entity *entry_point = NULL;
|
||||
|
||||
for_array(i, info->entities.entries) {
|
||||
auto *entry = &info->entities.entries[i];
|
||||
Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
|
||||
String name = e->token.string;
|
||||
if (e->kind == Entity_Variable) {
|
||||
global_variable_max_count++;
|
||||
} else if (e->kind == Entity_Procedure) {
|
||||
if (e->scope->is_init && name == "main") {
|
||||
entry_point = e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ssaGlobalVariable {
|
||||
ssaValue *var, *init;
|
||||
DeclInfo *decl;
|
||||
};
|
||||
Array<ssaGlobalVariable> global_variables;
|
||||
array_init(&global_variables, m->tmp_allocator, global_variable_max_count);
|
||||
|
||||
auto min_dep_map = generate_minimum_dependency_map(info, entry_point);
|
||||
defer (map_destroy(&min_dep_map));
|
||||
|
||||
for_array(i, info->entities.entries) {
|
||||
auto *entry = &info->entities.entries[i];
|
||||
Entity *e = cast(Entity *)cast(uintptr)entry->key.key;
|
||||
String name = e->token.string;
|
||||
DeclInfo *decl = entry->value;
|
||||
Scope *scope = e->scope;
|
||||
|
||||
if (!scope->is_file) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (map_get(&min_dep_map, hash_pointer(e)) == NULL) {
|
||||
// NOTE(bill): Nothing depends upon it so doesn't need to be built
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!scope->is_global && !scope->is_init) {
|
||||
name = ssa_mangle_name(s, e->token.pos.file, name);
|
||||
}
|
||||
|
||||
|
||||
switch (e->kind) {
|
||||
case Entity_TypeName:
|
||||
GB_ASSERT(e->type->kind == Type_Named);
|
||||
map_set(&m->type_names, hash_pointer(e->type), name);
|
||||
ssa_gen_global_type_name(m, e, name);
|
||||
break;
|
||||
|
||||
case Entity_Variable: {
|
||||
ssaValue *g = ssa_make_value_global(a, e, NULL);
|
||||
if (decl->var_decl_tags & VarDeclTag_thread_local) {
|
||||
g->Global.is_thread_local = true;
|
||||
}
|
||||
ssaGlobalVariable var = {};
|
||||
var.var = g;
|
||||
var.decl = decl;
|
||||
|
||||
if (decl->init_expr != NULL) {
|
||||
TypeAndValue *tav = map_get(&info->types, hash_pointer(decl->init_expr));
|
||||
if (tav != NULL) {
|
||||
if (tav->value.kind != ExactValue_Invalid) {
|
||||
ExactValue v = tav->value;
|
||||
if (v.kind != ExactValue_String) {
|
||||
g->Global.value = ssa_add_module_constant(m, tav->type, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (g->Global.value == NULL) {
|
||||
array_add(&global_variables, var);
|
||||
}
|
||||
|
||||
map_set(&m->values, hash_pointer(e), g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
} break;
|
||||
|
||||
case Entity_Procedure: {
|
||||
auto *pd = &decl->proc_decl->ProcDecl;
|
||||
String original_name = name;
|
||||
AstNode *body = pd->body;
|
||||
if (pd->tags & ProcTag_foreign) {
|
||||
name = pd->name->Ident.string;
|
||||
}
|
||||
if (pd->foreign_name.len > 0) {
|
||||
name = pd->foreign_name;
|
||||
} else if (pd->link_name.len > 0) {
|
||||
name = pd->link_name;
|
||||
}
|
||||
|
||||
ssaValue *p = ssa_make_value_procedure(a, m, e, e->type, decl->type_expr, body, name);
|
||||
p->Proc.tags = pd->tags;
|
||||
|
||||
map_set(&m->values, hash_pointer(e), p);
|
||||
HashKey hash_name = hash_string(name);
|
||||
if (map_get(&m->members, hash_name) == NULL) {
|
||||
map_set(&m->members, hash_name, p);
|
||||
}
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
for_array(i, m->members.entries) {
|
||||
auto *entry = &m->members.entries[i];
|
||||
ssaValue *v = entry->value;
|
||||
if (v->kind == ssaValue_Proc)
|
||||
ssa_build_proc(v, NULL);
|
||||
}
|
||||
|
||||
ssaDebugInfo *compile_unit = m->debug_info.entries[0].value;
|
||||
GB_ASSERT(compile_unit->kind == ssaDebugInfo_CompileUnit);
|
||||
ssaDebugInfo *all_procs = ssa_alloc_debug_info(m->allocator, ssaDebugInfo_AllProcs);
|
||||
|
||||
isize all_proc_max_count = 0;
|
||||
for_array(i, m->debug_info.entries) {
|
||||
auto *entry = &m->debug_info.entries[i];
|
||||
ssaDebugInfo *di = entry->value;
|
||||
di->id = i;
|
||||
if (di->kind == ssaDebugInfo_Proc) {
|
||||
all_proc_max_count++;
|
||||
}
|
||||
}
|
||||
|
||||
array_init(&all_procs->AllProcs.procs, m->allocator, all_proc_max_count);
|
||||
map_set(&m->debug_info, hash_pointer(all_procs), all_procs); // NOTE(bill): This doesn't need to be mapped
|
||||
compile_unit->CompileUnit.all_procs = all_procs;
|
||||
|
||||
|
||||
for_array(i, m->debug_info.entries) {
|
||||
auto *entry = &m->debug_info.entries[i];
|
||||
ssaDebugInfo *di = entry->value;
|
||||
di->id = i;
|
||||
if (di->kind == ssaDebugInfo_Proc) {
|
||||
array_add(&all_procs->AllProcs.procs, di);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
{ // Startup Runtime
|
||||
// Cleanup(bill): probably better way of doing code insertion
|
||||
String name = make_string(SSA_STARTUP_RUNTIME_PROC_NAME);
|
||||
Type *proc_type = make_type_proc(a, gb_alloc_item(a, Scope),
|
||||
NULL, 0,
|
||||
NULL, 0, false);
|
||||
AstNode *body = gb_alloc_item(a, AstNode);
|
||||
ssaValue *p = ssa_make_value_procedure(a, m, NULL, proc_type, NULL, body, name);
|
||||
Token token = {};
|
||||
token.string = name;
|
||||
Entity *e = make_entity_procedure(a, NULL, token, proc_type);
|
||||
|
||||
map_set(&m->values, hash_pointer(e), p);
|
||||
map_set(&m->members, hash_string(name), p);
|
||||
|
||||
ssaProcedure *proc = &p->Proc;
|
||||
proc->tags = ProcTag_no_inline; // TODO(bill): is no_inline a good idea?
|
||||
|
||||
ssa_begin_procedure_body(proc);
|
||||
|
||||
// TODO(bill): Should do a dependency graph do check which order to initialize them in?
|
||||
for_array(i, global_variables) {
|
||||
ssaGlobalVariable *var = &global_variables[i];
|
||||
if (var->decl->init_expr != NULL) {
|
||||
var->init = ssa_build_expr(proc, var->decl->init_expr);
|
||||
}
|
||||
}
|
||||
|
||||
// NOTE(bill): Initialize constants first
|
||||
for_array(i, global_variables) {
|
||||
ssaGlobalVariable *var = &global_variables[i];
|
||||
if (var->init != NULL) {
|
||||
if (var->init->kind == ssaValue_Constant) {
|
||||
ssa_emit_store(proc, var->var, var->init);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for_array(i, global_variables) {
|
||||
ssaGlobalVariable *var = &global_variables[i];
|
||||
if (var->init != NULL) {
|
||||
if (var->init->kind != ssaValue_Constant) {
|
||||
ssa_emit_store(proc, var->var, var->init);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{ // NOTE(bill): Setup type_info data
|
||||
// TODO(bill): Try and make a lot of this constant aggregate literals in LLVM IR
|
||||
ssaValue *type_info_data = NULL;
|
||||
ssaValue *type_info_member_data = NULL;
|
||||
|
||||
ssaValue **found = NULL;
|
||||
found = map_get(&proc->module->members, hash_string(make_string(SSA_TYPE_INFO_DATA_NAME)));
|
||||
GB_ASSERT(found != NULL);
|
||||
type_info_data = *found;
|
||||
|
||||
found = map_get(&proc->module->members, hash_string(make_string(SSA_TYPE_INFO_DATA_MEMBER_NAME)));
|
||||
GB_ASSERT(found != NULL);
|
||||
type_info_member_data = *found;
|
||||
|
||||
CheckerInfo *info = proc->module->info;
|
||||
|
||||
// Useful types
|
||||
Type *t_i64_slice_ptr = make_type_pointer(a, make_type_slice(a, t_i64));
|
||||
Type *t_string_slice_ptr = make_type_pointer(a, make_type_slice(a, t_string));
|
||||
|
||||
auto get_type_info_ptr = [](ssaProcedure *proc, ssaValue *type_info_data, Type *type) -> ssaValue * {
|
||||
return ssa_emit_array_ep(proc, type_info_data, cast(i32)ssa_type_info_index(proc->module->info, type));
|
||||
};
|
||||
|
||||
i32 type_info_member_index = 0;
|
||||
|
||||
auto type_info_member_offset = [](ssaProcedure *proc, ssaValue *data, isize count, i32 *index) -> ssaValue * {
|
||||
ssaValue *offset = ssa_emit_array_ep(proc, data, *index);
|
||||
*index += count;
|
||||
return offset;
|
||||
};
|
||||
|
||||
|
||||
|
||||
for_array(type_info_map_index, info->type_info_map.entries) {
|
||||
auto *entry = &info->type_info_map.entries[type_info_map_index];
|
||||
Type *t = cast(Type *)cast(uintptr)entry->key.key;
|
||||
t = default_type(t);
|
||||
isize entry_index = entry->value;
|
||||
|
||||
ssaValue *tag = NULL;
|
||||
|
||||
switch (t->kind) {
|
||||
case Type_Named: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_named);
|
||||
|
||||
// TODO(bill): Which is better? The mangled name or actual name?
|
||||
// ssaValue *gsa = ssa_add_global_string_array(proc, make_exact_value_string(t->Named.name));
|
||||
ssaValue *gsa = ssa_add_global_string_array(m, t->Named.type_name->token.string);
|
||||
ssaValue *elem = ssa_array_elem(proc, gsa);
|
||||
ssaValue *len = ssa_array_len(proc, ssa_emit_load(proc, gsa));
|
||||
ssaValue *name = ssa_emit_string(proc, elem, len);
|
||||
|
||||
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Named.base);
|
||||
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), name);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 1), gep);
|
||||
} break;
|
||||
|
||||
case Type_Basic:
|
||||
switch (t->Basic.kind) {
|
||||
case Basic_bool:
|
||||
tag = ssa_add_local_generated(proc, t_type_info_boolean);
|
||||
break;
|
||||
case Basic_i8:
|
||||
case Basic_i16:
|
||||
case Basic_i32:
|
||||
case Basic_i64:
|
||||
case Basic_u8:
|
||||
case Basic_u16:
|
||||
case Basic_u32:
|
||||
case Basic_u64:
|
||||
case Basic_int:
|
||||
case Basic_uint: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_integer);
|
||||
b32 is_unsigned = (t->Basic.flags & BasicFlag_Unsigned) != 0;
|
||||
ssaValue *bits = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
|
||||
ssaValue *is_signed = ssa_make_const_bool(a, !is_unsigned);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), bits);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 1), is_signed);
|
||||
} break;
|
||||
|
||||
case Basic_f32:
|
||||
case Basic_f64: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_float);
|
||||
ssaValue *bits = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), bits);
|
||||
} break;
|
||||
|
||||
case Basic_rawptr:
|
||||
tag = ssa_add_local_generated(proc, t_type_info_pointer);
|
||||
break;
|
||||
|
||||
case Basic_string:
|
||||
tag = ssa_add_local_generated(proc, t_type_info_string);
|
||||
break;
|
||||
|
||||
case Basic_any:
|
||||
tag = ssa_add_local_generated(proc, t_type_info_any);
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case Type_Pointer: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_pointer);
|
||||
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Pointer.elem);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
|
||||
} break;
|
||||
case Type_Maybe: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_maybe);
|
||||
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Maybe.elem);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
|
||||
} break;
|
||||
case Type_Array: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_array);
|
||||
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Array.elem);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
|
||||
|
||||
isize ez = type_size_of(m->sizes, a, t->Array.elem);
|
||||
ssaValue *elem_size = ssa_emit_struct_ep(proc, tag, 1);
|
||||
ssa_emit_store(proc, elem_size, ssa_make_const_int(a, ez));
|
||||
|
||||
ssaValue *count = ssa_emit_struct_ep(proc, tag, 2);
|
||||
ssa_emit_store(proc, count, ssa_make_const_int(a, t->Array.count));
|
||||
|
||||
} break;
|
||||
case Type_Slice: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_slice);
|
||||
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Slice.elem);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
|
||||
|
||||
isize ez = type_size_of(m->sizes, a, t->Slice.elem);
|
||||
ssaValue *elem_size = ssa_emit_struct_ep(proc, tag, 1);
|
||||
ssa_emit_store(proc, elem_size, ssa_make_const_int(a, ez));
|
||||
|
||||
} break;
|
||||
case Type_Vector: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_vector);
|
||||
ssaValue *gep = get_type_info_ptr(proc, type_info_data, t->Vector.elem);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 0), gep);
|
||||
|
||||
isize ez = type_size_of(m->sizes, a, t->Vector.elem);
|
||||
ssaValue *elem_size = ssa_emit_struct_ep(proc, tag, 1);
|
||||
ssa_emit_store(proc, elem_size, ssa_make_const_int(a, ez));
|
||||
|
||||
ssaValue *count = ssa_emit_struct_ep(proc, tag, 2);
|
||||
ssa_emit_store(proc, count, ssa_make_const_int(a, t->Vector.count));
|
||||
|
||||
} break;
|
||||
case Type_Record: {
|
||||
switch (t->Record.kind) {
|
||||
case TypeRecord_Struct: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_struct);
|
||||
|
||||
{
|
||||
ssaValue *packed = ssa_make_const_bool(a, t->Record.struct_is_packed);
|
||||
ssaValue *ordered = ssa_make_const_bool(a, t->Record.struct_is_ordered);
|
||||
ssaValue *size = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
|
||||
ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 1), size);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 3), packed);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 4), ordered);
|
||||
}
|
||||
|
||||
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
|
||||
|
||||
type_set_offsets(m->sizes, a, t); // NOTE(bill): Just incase the offsets have not been set yet
|
||||
for (isize source_index = 0; source_index < t->Record.field_count; source_index++) {
|
||||
// TODO(bill): Order fields in source order not layout order
|
||||
Entity *f = t->Record.fields_in_src_order[source_index];
|
||||
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
|
||||
i64 foffset = t->Record.struct_offsets[f->Variable.field_index];
|
||||
GB_ASSERT(f->kind == Entity_Variable && f->Variable.field);
|
||||
|
||||
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, source_index));
|
||||
ssaValue *name = ssa_emit_struct_ep(proc, field, 0);
|
||||
ssaValue *type_info = ssa_emit_struct_ep(proc, field, 1);
|
||||
ssaValue *offset = ssa_emit_struct_ep(proc, field, 2);
|
||||
|
||||
if (f->token.string.len > 0) {
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, f->token.string));
|
||||
}
|
||||
ssa_emit_store(proc, type_info, tip);
|
||||
ssa_emit_store(proc, offset, ssa_make_const_int(a, foffset));
|
||||
}
|
||||
|
||||
Type *slice_type = make_type_slice(a, t_type_info_member);
|
||||
Type *slice_type_ptr = make_type_pointer(a, slice_type);
|
||||
ssaValue *slice = ssa_emit_struct_ep(proc, tag, 0);
|
||||
ssaValue *field_count = ssa_make_const_int(a, t->Record.field_count);
|
||||
|
||||
ssaValue *elem = ssa_emit_struct_ep(proc, slice, 0);
|
||||
ssaValue *len = ssa_emit_struct_ep(proc, slice, 1);
|
||||
ssaValue *cap = ssa_emit_struct_ep(proc, slice, 2);
|
||||
|
||||
ssa_emit_store(proc, elem, memory);
|
||||
ssa_emit_store(proc, len, field_count);
|
||||
ssa_emit_store(proc, cap, field_count);
|
||||
} break;
|
||||
case TypeRecord_Union:
|
||||
tag = ssa_add_local_generated(proc, t_type_info_union);
|
||||
{
|
||||
ssaValue *size = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
|
||||
ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 1), size);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align);
|
||||
}
|
||||
break;
|
||||
case TypeRecord_RawUnion: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_raw_union);
|
||||
{
|
||||
ssaValue *size = ssa_make_const_int(a, type_size_of(m->sizes, a, t));
|
||||
ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 1), size);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align);
|
||||
}
|
||||
|
||||
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Record.field_count, &type_info_member_index);
|
||||
|
||||
for (isize i = 0; i < t->Record.field_count; i++) {
|
||||
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i));
|
||||
ssaValue *name = ssa_emit_struct_ep(proc, field, 0);
|
||||
ssaValue *type_info = ssa_emit_struct_ep(proc, field, 1);
|
||||
ssaValue *offset = ssa_emit_struct_ep(proc, field, 2);
|
||||
|
||||
Entity *f = t->Record.fields[i];
|
||||
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
|
||||
|
||||
if (f->token.string.len > 0) {
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, f->token.string));
|
||||
}
|
||||
ssa_emit_store(proc, type_info, tip);
|
||||
ssa_emit_store(proc, offset, ssa_make_const_int(a, 0));
|
||||
}
|
||||
|
||||
Type *slice_type = make_type_slice(a, t_type_info_member);
|
||||
Type *slice_type_ptr = make_type_pointer(a, slice_type);
|
||||
ssaValue *slice = ssa_emit_struct_ep(proc, tag, 0);
|
||||
ssaValue *field_count = ssa_make_const_int(a, t->Record.field_count);
|
||||
|
||||
ssaValue *elem = ssa_emit_struct_ep(proc, slice, 0);
|
||||
ssaValue *len = ssa_emit_struct_ep(proc, slice, 1);
|
||||
ssaValue *cap = ssa_emit_struct_ep(proc, slice, 2);
|
||||
|
||||
ssa_emit_store(proc, elem, memory);
|
||||
ssa_emit_store(proc, len, field_count);
|
||||
ssa_emit_store(proc, cap, field_count);
|
||||
} break;
|
||||
case TypeRecord_Enum: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_enum);
|
||||
Type *enum_base = t->Record.enum_base;
|
||||
if (enum_base == NULL) {
|
||||
enum_base = t_int;
|
||||
}
|
||||
ssaValue *base = ssa_emit_struct_ep(proc, tag, 0);
|
||||
ssa_emit_store(proc, base, get_type_info_ptr(proc, type_info_data, enum_base));
|
||||
|
||||
if (t->Record.other_field_count > 0) {
|
||||
Entity **fields = t->Record.other_fields;
|
||||
isize count = t->Record.other_field_count;
|
||||
ssaValue *value_array = NULL;
|
||||
ssaValue *name_array = NULL;
|
||||
|
||||
|
||||
{
|
||||
Token token = {Token_Identifier};
|
||||
i32 id = cast(i32)entry_index;
|
||||
char name_base[] = "__$enum_values";
|
||||
isize name_len = gb_size_of(name_base) + 10;
|
||||
token.string.text = gb_alloc_array(a, u8, name_len);
|
||||
token.string.len = gb_snprintf(cast(char *)token.string.text, name_len,
|
||||
"%s-%d", name_base, id)-1;
|
||||
Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_i64, count));
|
||||
value_array = ssa_make_value_global(a, e, NULL);
|
||||
value_array->Global.is_private = true;
|
||||
ssa_module_add_value(m, e, value_array);
|
||||
map_set(&m->members, hash_string(token.string), value_array);
|
||||
}
|
||||
{
|
||||
Token token = {Token_Identifier};
|
||||
i32 id = cast(i32)entry_index;
|
||||
char name_base[] = "__$enum_names";
|
||||
isize name_len = gb_size_of(name_base) + 10;
|
||||
token.string.text = gb_alloc_array(a, u8, name_len);
|
||||
token.string.len = gb_snprintf(cast(char *)token.string.text, name_len,
|
||||
"%s-%d", name_base, id)-1;
|
||||
Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_string, count));
|
||||
name_array = ssa_make_value_global(a, e, NULL);
|
||||
name_array->Global.is_private = true;
|
||||
ssa_module_add_value(m, e, name_array);
|
||||
map_set(&m->members, hash_string(token.string), name_array);
|
||||
}
|
||||
|
||||
for (isize i = 0; i < count; i++) {
|
||||
ssaValue *value_gep = ssa_emit_struct_ep(proc, value_array, i);
|
||||
ssaValue *name_gep = ssa_emit_struct_ep(proc, name_array, i);
|
||||
|
||||
ssa_emit_store(proc, value_gep, ssa_make_const_i64(a, fields[i]->Constant.value.value_integer));
|
||||
ssa_emit_store(proc, name_gep, ssa_emit_global_string(proc, fields[i]->token.string));
|
||||
}
|
||||
|
||||
ssaValue *v_count = ssa_make_const_int(a, count);
|
||||
|
||||
|
||||
ssaValue *values = ssa_emit_struct_ep(proc, tag, 1);
|
||||
ssaValue *names = ssa_emit_struct_ep(proc, tag, 2);
|
||||
ssaValue *value_slice = ssa_add_local_generated(proc, type_deref(t_i64_slice_ptr));
|
||||
ssaValue *name_slice = ssa_add_local_generated(proc, type_deref(t_string_slice_ptr));
|
||||
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, value_slice, 0), ssa_array_elem(proc, value_array));
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, value_slice, 1), v_count);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, value_slice, 2), v_count);
|
||||
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, name_slice, 0), ssa_array_elem(proc, name_array));
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, name_slice, 1), v_count);
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, name_slice, 2), v_count);
|
||||
|
||||
ssa_emit_store(proc, values, ssa_emit_load(proc, value_slice));
|
||||
ssa_emit_store(proc, names, ssa_emit_load(proc, name_slice));
|
||||
}
|
||||
} break;
|
||||
}
|
||||
} break;
|
||||
|
||||
case Type_Tuple: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_tuple);
|
||||
|
||||
{
|
||||
ssaValue *align = ssa_make_const_int(a, type_align_of(m->sizes, a, t));
|
||||
ssa_emit_store(proc, ssa_emit_struct_ep(proc, tag, 2), align);
|
||||
}
|
||||
|
||||
ssaValue *memory = type_info_member_offset(proc, type_info_member_data, t->Tuple.variable_count, &type_info_member_index);
|
||||
|
||||
for (isize i = 0; i < t->Tuple.variable_count; i++) {
|
||||
ssaValue *field = ssa_emit_ptr_offset(proc, memory, ssa_make_const_int(a, i));
|
||||
ssaValue *name = ssa_emit_struct_ep(proc, field, 0);
|
||||
ssaValue *type_info = ssa_emit_struct_ep(proc, field, 1);
|
||||
// NOTE(bill): offset is not used for tuples
|
||||
|
||||
Entity *f = t->Tuple.variables[i];
|
||||
ssaValue *tip = get_type_info_ptr(proc, type_info_data, f->type);
|
||||
|
||||
if (f->token.string.len > 0) {
|
||||
ssa_emit_store(proc, name, ssa_emit_global_string(proc, f->token.string));
|
||||
}
|
||||
ssa_emit_store(proc, type_info, tip);
|
||||
}
|
||||
|
||||
Type *slice_type = make_type_slice(a, t_type_info_member);
|
||||
Type *slice_type_ptr = make_type_pointer(a, slice_type);
|
||||
ssaValue *slice = ssa_emit_struct_ep(proc, tag, 0);
|
||||
ssaValue *variable_count = ssa_make_const_int(a, t->Tuple.variable_count);
|
||||
|
||||
ssaValue *elem = ssa_emit_struct_ep(proc, slice, 0);
|
||||
ssaValue *len = ssa_emit_struct_ep(proc, slice, 1);
|
||||
ssaValue *cap = ssa_emit_struct_ep(proc, slice, 2);
|
||||
|
||||
ssa_emit_store(proc, elem, memory);
|
||||
ssa_emit_store(proc, len, variable_count);
|
||||
ssa_emit_store(proc, cap, variable_count);
|
||||
} break;
|
||||
|
||||
case Type_Proc: {
|
||||
tag = ssa_add_local_generated(proc, t_type_info_procedure);
|
||||
|
||||
ssaValue *params = ssa_emit_struct_ep(proc, tag, 0);
|
||||
ssaValue *results = ssa_emit_struct_ep(proc, tag, 1);
|
||||
ssaValue *variadic = ssa_emit_struct_ep(proc, tag, 2);
|
||||
|
||||
if (t->Proc.params) {
|
||||
ssa_emit_store(proc, params, get_type_info_ptr(proc, type_info_data, t->Proc.params));
|
||||
}
|
||||
if (t->Proc.results) {
|
||||
ssa_emit_store(proc, results, get_type_info_ptr(proc, type_info_data, t->Proc.results));
|
||||
}
|
||||
ssa_emit_store(proc, variadic, ssa_make_const_bool(a, t->Proc.variadic));
|
||||
|
||||
// TODO(bill): Type_Info for procedures
|
||||
} break;
|
||||
}
|
||||
|
||||
if (tag != NULL) {
|
||||
ssaValue *gep = ssa_emit_array_ep(proc, type_info_data, entry_index);
|
||||
ssaValue *val = ssa_emit_conv(proc, ssa_emit_load(proc, tag), t_type_info);
|
||||
ssa_emit_store(proc, gep, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ssa_end_procedure_body(proc);
|
||||
}
|
||||
|
||||
for_array(i, m->procs) {
|
||||
ssa_build_proc(m->procs[i], m->procs[i]->Proc.parent);
|
||||
}
|
||||
|
||||
|
||||
// m->layout = make_string("e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64");
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
ssaDebugInfo *ssa_add_debug_info_file(ssaProcedure *proc, AstFile *file) {
|
||||
if (!proc->module->generate_debug_info) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GB_ASSERT(file != NULL);
|
||||
ssaDebugInfo *di = ssa_alloc_debug_info(proc->module->allocator, ssaDebugInfo_File);
|
||||
di->File.file = file;
|
||||
|
||||
String filename = file->tokenizer.fullpath;
|
||||
String directory = filename;
|
||||
isize slash_index = 0;
|
||||
for (isize i = filename.len-1; i >= 0; i--) {
|
||||
if (filename.text[i] == '\\' ||
|
||||
filename.text[i] == '/') {
|
||||
break;
|
||||
}
|
||||
slash_index = i;
|
||||
}
|
||||
directory.len = slash_index-1;
|
||||
filename.text = filename.text + slash_index;
|
||||
filename.len -= slash_index;
|
||||
|
||||
|
||||
di->File.filename = filename;
|
||||
di->File.directory = directory;
|
||||
|
||||
map_set(&proc->module->debug_info, hash_pointer(file), di);
|
||||
return di;
|
||||
}
|
||||
|
||||
|
||||
ssaDebugInfo *ssa_add_debug_info_proc(ssaProcedure *proc, Entity *entity, String name, ssaDebugInfo *file) {
|
||||
if (!proc->module->generate_debug_info) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
GB_ASSERT(entity != NULL);
|
||||
ssaDebugInfo *di = ssa_alloc_debug_info(proc->module->allocator, ssaDebugInfo_Proc);
|
||||
di->Proc.entity = entity;
|
||||
di->Proc.name = name;
|
||||
di->Proc.file = file;
|
||||
di->Proc.pos = entity->token.pos;
|
||||
|
||||
map_set(&proc->module->debug_info, hash_pointer(entity), di);
|
||||
return di;
|
||||
}
|
||||
|
||||
+1177
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,394 @@
|
||||
void ssa_module_add_value(ssaModule *m, Entity *e, ssaValue *v);
|
||||
|
||||
|
||||
ssaValue *ssa_alloc_value(gbAllocator a, ssaValueKind kind) {
|
||||
ssaValue *v = gb_alloc_item(a, ssaValue);
|
||||
v->kind = kind;
|
||||
return v;
|
||||
}
|
||||
ssaValue *ssa_alloc_instr(ssaProcedure *proc, ssaInstrKind kind) {
|
||||
ssaValue *v = ssa_alloc_value(proc->module->allocator, ssaValue_Instr);
|
||||
v->Instr.kind = kind;
|
||||
return v;
|
||||
}
|
||||
ssaDebugInfo *ssa_alloc_debug_info(gbAllocator a, ssaDebugInfoKind kind) {
|
||||
ssaDebugInfo *di = gb_alloc_item(a, ssaDebugInfo);
|
||||
di->kind = kind;
|
||||
return di;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_make_value_type_name(gbAllocator a, String name, Type *type) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_TypeName);
|
||||
v->TypeName.name = name;
|
||||
v->TypeName.type = type;
|
||||
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.type = make_type_pointer(a, e->type);
|
||||
v->Global.value = value;
|
||||
array_init(&v->Global.referrers, heap_allocator()); // TODO(bill): Replace heap allocator here
|
||||
return v;
|
||||
}
|
||||
ssaValue *ssa_make_value_param(gbAllocator a, ssaProcedure *parent, Entity *e) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Param);
|
||||
v->Param.parent = parent;
|
||||
v->Param.entity = e;
|
||||
v->Param.type = e->type;
|
||||
array_init(&v->Param.referrers, heap_allocator()); // TODO(bill): Replace heap allocator here
|
||||
return v;
|
||||
}
|
||||
ssaValue *ssa_make_value_nil(gbAllocator a, Type *type) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Nil);
|
||||
v->Nil.type = type;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_make_instr_local(ssaProcedure *p, Entity *e, b32 zero_initialized) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Local);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->Local.entity = e;
|
||||
i->Local.type = make_type_pointer(p->module->allocator, e->type);
|
||||
i->Local.zero_initialized = zero_initialized;
|
||||
array_init(&i->Local.referrers, heap_allocator()); // TODO(bill): Replace heap allocator here
|
||||
ssa_module_add_value(p->module, e, v);
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_instr_store(ssaProcedure *p, ssaValue *address, ssaValue *value) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Store);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->Store.address = address;
|
||||
i->Store.value = value;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_zero_init(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_ZeroInit);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->ZeroInit.address = address;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_load(ssaProcedure *p, ssaValue *address) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Load);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->Load.address = address;
|
||||
i->Load.type = type_deref(ssa_type(address));
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_array_element_ptr(ssaProcedure *p, ssaValue *address, ssaValue *elem_index) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_ArrayElementPtr);
|
||||
ssaInstr *i = &v->Instr;
|
||||
Type *t = ssa_type(address);
|
||||
GB_ASSERT(is_type_pointer(t));
|
||||
t = base_type(type_deref(t));
|
||||
GB_ASSERT(is_type_array(t));
|
||||
|
||||
Type *result_type = make_type_pointer(p->module->allocator, t->Array.elem);
|
||||
|
||||
i->ArrayElementPtr.address = address;
|
||||
i->ArrayElementPtr.elem_index = elem_index;
|
||||
i->ArrayElementPtr.result_type = result_type;
|
||||
|
||||
GB_ASSERT_MSG(is_type_pointer(ssa_type(address)),
|
||||
"%s", type_to_string(ssa_type(address)));
|
||||
return v;
|
||||
}
|
||||
ssaValue *ssa_make_instr_struct_element_ptr(ssaProcedure *p, ssaValue *address, i32 elem_index, Type *result_type) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_StructElementPtr);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->StructElementPtr.address = address;
|
||||
i->StructElementPtr.elem_index = elem_index;
|
||||
i->StructElementPtr.result_type = result_type;
|
||||
|
||||
GB_ASSERT_MSG(is_type_pointer(ssa_type(address)),
|
||||
"%s", type_to_string(ssa_type(address)));
|
||||
return v;
|
||||
}
|
||||
ssaValue *ssa_make_instr_ptr_offset(ssaProcedure *p, ssaValue *address, ssaValue *offset) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_PtrOffset);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->PtrOffset.address = address;
|
||||
i->PtrOffset.offset = offset;
|
||||
|
||||
GB_ASSERT_MSG(is_type_pointer(ssa_type(address)),
|
||||
"%s", type_to_string(ssa_type(address)));
|
||||
GB_ASSERT_MSG(is_type_integer(ssa_type(offset)),
|
||||
"%s", type_to_string(ssa_type(address)));
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ssaValue *ssa_make_instr_array_extract_value(ssaProcedure *p, ssaValue *address, i32 index) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_ArrayExtractValue);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->ArrayExtractValue.address = address;
|
||||
i->ArrayExtractValue.index = index;
|
||||
Type *t = base_type(ssa_type(address));
|
||||
GB_ASSERT(is_type_array(t));
|
||||
i->ArrayExtractValue.result_type = t->Array.elem;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_struct_extract_value(ssaProcedure *p, ssaValue *address, i32 index, Type *result_type) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_StructExtractValue);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->StructExtractValue.address = address;
|
||||
i->StructExtractValue.index = index;
|
||||
i->StructExtractValue.result_type = result_type;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_binary_op(ssaProcedure *p, TokenKind op, ssaValue *left, ssaValue *right, Type *type) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_BinaryOp);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->BinaryOp.op = op;
|
||||
i->BinaryOp.left = left;
|
||||
i->BinaryOp.right = right;
|
||||
i->BinaryOp.type = type;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_jump(ssaProcedure *p, ssaBlock *block) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Jump);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->Jump.block = block;
|
||||
return v;
|
||||
}
|
||||
ssaValue *ssa_make_instr_if(ssaProcedure *p, ssaValue *cond, ssaBlock *true_block, ssaBlock *false_block) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_If);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->If.cond = cond;
|
||||
i->If.true_block = true_block;
|
||||
i->If.false_block = false_block;
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_instr_phi(ssaProcedure *p, Array<ssaValue *> edges, Type *type) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Phi);
|
||||
ssaInstr *i = &v->Instr;
|
||||
i->Phi.edges = edges;
|
||||
i->Phi.type = type;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_unreachable(ssaProcedure *p) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Unreachable);
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_return(ssaProcedure *p, ssaValue *value) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Return);
|
||||
v->Instr.Return.value = value;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_select(ssaProcedure *p, ssaValue *cond, ssaValue *t, ssaValue *f) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Select);
|
||||
v->Instr.Select.cond = cond;
|
||||
v->Instr.Select.true_value = t;
|
||||
v->Instr.Select.false_value = f;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_call(ssaProcedure *p, ssaValue *value, ssaValue **args, isize arg_count, Type *result_type) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Call);
|
||||
v->Instr.Call.value = value;
|
||||
v->Instr.Call.args = args;
|
||||
v->Instr.Call.arg_count = arg_count;
|
||||
v->Instr.Call.type = result_type;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_conv(ssaProcedure *p, ssaConvKind kind, ssaValue *value, Type *from, Type *to) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Conv);
|
||||
v->Instr.Conv.kind = kind;
|
||||
v->Instr.Conv.value = value;
|
||||
v->Instr.Conv.from = from;
|
||||
v->Instr.Conv.to = to;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_extract_element(ssaProcedure *p, ssaValue *vector, ssaValue *index) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_VectorExtractElement);
|
||||
v->Instr.VectorExtractElement.vector = vector;
|
||||
v->Instr.VectorExtractElement.index = index;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_insert_element(ssaProcedure *p, ssaValue *vector, ssaValue *elem, ssaValue *index) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_VectorInsertElement);
|
||||
v->Instr.VectorInsertElement.vector = vector;
|
||||
v->Instr.VectorInsertElement.elem = elem;
|
||||
v->Instr.VectorInsertElement.index = index;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_vector_shuffle(ssaProcedure *p, ssaValue *vector, i32 *indices, isize index_count) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_VectorShuffle);
|
||||
v->Instr.VectorShuffle.vector = vector;
|
||||
v->Instr.VectorShuffle.indices = indices;
|
||||
v->Instr.VectorShuffle.index_count = index_count;
|
||||
|
||||
Type *vt = base_type(ssa_type(vector));
|
||||
v->Instr.VectorShuffle.type = make_type_vector(p->module->allocator, vt->Vector.elem, index_count);
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_instr_comment(ssaProcedure *p, String text) {
|
||||
ssaValue *v = ssa_alloc_instr(p, ssaInstr_Comment);
|
||||
v->Instr.Comment.text = text;
|
||||
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_constant_slice(gbAllocator a, Type *type, ssaValue *backing_array, i64 count) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_ConstantSlice);
|
||||
v->ConstantSlice.type = type;
|
||||
v->ConstantSlice.backing_array = backing_array;
|
||||
v->ConstantSlice.count = count;
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_const_int(gbAllocator a, i64 i) {
|
||||
return ssa_make_value_constant(a, t_int, make_exact_value_integer(i));
|
||||
}
|
||||
ssaValue *ssa_make_const_i32(gbAllocator a, i64 i) {
|
||||
return ssa_make_value_constant(a, t_i32, make_exact_value_integer(i));
|
||||
}
|
||||
ssaValue *ssa_make_const_i64(gbAllocator a, i64 i) {
|
||||
return ssa_make_value_constant(a, t_i64, make_exact_value_integer(i));
|
||||
}
|
||||
ssaValue *ssa_make_const_bool(gbAllocator a, b32 b) {
|
||||
return ssa_make_value_constant(a, t_bool, make_exact_value_bool(b != 0));
|
||||
}
|
||||
|
||||
ssaValue *ssa_add_module_constant(ssaModule *m, Type *type, ExactValue value) {
|
||||
if (is_type_slice(type)) {
|
||||
ast_node(cl, CompoundLit, value.value_compound);
|
||||
gbAllocator a = m->allocator;
|
||||
|
||||
isize count = cl->elems.count;
|
||||
if (count > 0) {
|
||||
Type *elem = base_type(type)->Slice.elem;
|
||||
Type *t = make_type_array(a, elem, count);
|
||||
ssaValue *backing_array = ssa_add_module_constant(m, t, value);
|
||||
|
||||
|
||||
isize max_len = 7+8+1;
|
||||
u8 *str = cast(u8 *)gb_alloc_array(a, u8, max_len);
|
||||
isize len = gb_snprintf(cast(char *)str, max_len, "__csba$%x", m->global_array_index);
|
||||
m->global_array_index++;
|
||||
|
||||
String name = make_string(str, len-1);
|
||||
|
||||
Entity *e = make_entity_constant(a, NULL, make_token_ident(name), t, value);
|
||||
ssaValue *g = ssa_make_value_global(a, e, backing_array);
|
||||
ssa_module_add_value(m, e, g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
|
||||
return ssa_make_value_constant_slice(a, type, g, count);
|
||||
} else {
|
||||
return ssa_make_value_constant_slice(a, type, NULL, 0);
|
||||
}
|
||||
}
|
||||
|
||||
return ssa_make_value_constant(m->allocator, type, value);
|
||||
}
|
||||
|
||||
|
||||
ssaValue *ssa_make_value_procedure(gbAllocator a, ssaModule *m, Entity *entity, Type *type, AstNode *type_expr, AstNode *body, String name) {
|
||||
ssaValue *v = ssa_alloc_value(a, ssaValue_Proc);
|
||||
v->Proc.module = m;
|
||||
v->Proc.entity = entity;
|
||||
v->Proc.type = type;
|
||||
v->Proc.type_expr = type_expr;
|
||||
v->Proc.body = body;
|
||||
v->Proc.name = name;
|
||||
array_init(&v->Proc.referrers, heap_allocator(), 0); // TODO(bill): replace heap allocator
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaValue *ssa_make_value_block(ssaProcedure *proc, AstNode *node, Scope *scope, String label) {
|
||||
ssaValue *v = ssa_alloc_value(proc->module->allocator, ssaValue_Block);
|
||||
v->Block.label = label;
|
||||
v->Block.node = node;
|
||||
v->Block.scope = scope;
|
||||
v->Block.parent = proc;
|
||||
|
||||
array_init(&v->Block.instrs, heap_allocator());
|
||||
array_init(&v->Block.locals, heap_allocator());
|
||||
|
||||
array_init(&v->Block.preds, heap_allocator());
|
||||
array_init(&v->Block.succs, heap_allocator());
|
||||
|
||||
return v;
|
||||
}
|
||||
|
||||
ssaBlock *ssa_add_block(ssaProcedure *proc, AstNode *node, char *label) {
|
||||
Scope *scope = NULL;
|
||||
if (node != NULL) {
|
||||
Scope **found = map_get(&proc->module->info->scopes, hash_pointer(node));
|
||||
if (found) {
|
||||
scope = *found;
|
||||
} else {
|
||||
GB_PANIC("Block scope not found for %.*s", LIT(ast_node_strings[node->kind]));
|
||||
}
|
||||
}
|
||||
|
||||
ssaValue *value = ssa_make_value_block(proc, node, scope, make_string(label));
|
||||
ssaBlock *block = &value->Block;
|
||||
array_add(&proc->blocks, block);
|
||||
return block;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
ssaDefer ssa_add_defer_node(ssaProcedure *proc, isize scope_index, AstNode *stmt) {
|
||||
ssaDefer d = {ssaDefer_Node};
|
||||
d.scope_index = scope_index;
|
||||
d.block = proc->curr_block;
|
||||
d.stmt = stmt;
|
||||
array_add(&proc->defer_stmts, d);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
ssaDefer ssa_add_defer_instr(ssaProcedure *proc, isize scope_index, ssaValue *instr) {
|
||||
ssaDefer d = {ssaDefer_Instr};
|
||||
d.scope_index = proc->scope_index;
|
||||
d.block = proc->curr_block;
|
||||
d.instr = instr; // NOTE(bill): It will make a copy everytime it is called
|
||||
array_add(&proc->defer_stmts, d);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
+452
@@ -0,0 +1,452 @@
|
||||
void ssa_add_operands(Array<ssaValue *> *ops, ssaInstr *i) {
|
||||
switch (i->kind) {
|
||||
case ssaInstr_Comment:
|
||||
break;
|
||||
case ssaInstr_Local:
|
||||
break;
|
||||
case ssaInstr_ZeroInit:
|
||||
array_add(ops, i->ZeroInit.address);
|
||||
break;
|
||||
case ssaInstr_Store:
|
||||
array_add(ops, i->Store.address);
|
||||
array_add(ops, i->Store.value);
|
||||
break;
|
||||
case ssaInstr_Load:
|
||||
array_add(ops, i->Load.address);
|
||||
break;
|
||||
case ssaInstr_ArrayElementPtr:
|
||||
array_add(ops, i->ArrayElementPtr.address);
|
||||
array_add(ops, i->ArrayElementPtr.elem_index);
|
||||
break;
|
||||
case ssaInstr_StructElementPtr:
|
||||
array_add(ops, i->StructElementPtr.address);
|
||||
break;
|
||||
case ssaInstr_PtrOffset:
|
||||
array_add(ops, i->PtrOffset.address);
|
||||
array_add(ops, i->PtrOffset.offset);
|
||||
break;
|
||||
case ssaInstr_ArrayExtractValue:
|
||||
array_add(ops, i->ArrayExtractValue.address);
|
||||
break;
|
||||
case ssaInstr_StructExtractValue:
|
||||
array_add(ops, i->StructExtractValue.address);
|
||||
break;
|
||||
case ssaInstr_Conv:
|
||||
array_add(ops, i->Conv.value);
|
||||
break;
|
||||
case ssaInstr_Jump:
|
||||
break;
|
||||
case ssaInstr_If:
|
||||
array_add(ops, i->If.cond);
|
||||
break;
|
||||
case ssaInstr_Return:
|
||||
if (i->Return.value != NULL) {
|
||||
array_add(ops, i->Return.value);
|
||||
}
|
||||
break;
|
||||
case ssaInstr_Select:
|
||||
array_add(ops, i->Select.cond);
|
||||
break;
|
||||
case ssaInstr_Phi:
|
||||
for_array(j, i->Phi.edges) {
|
||||
array_add(ops, i->Phi.edges[j]);
|
||||
}
|
||||
break;
|
||||
case ssaInstr_Unreachable: break;
|
||||
case ssaInstr_BinaryOp:
|
||||
array_add(ops, i->BinaryOp.left);
|
||||
array_add(ops, i->BinaryOp.right);
|
||||
break;
|
||||
case ssaInstr_Call:
|
||||
array_add(ops, i->Call.value);
|
||||
for (isize j = 0; j < i->Call.arg_count; j++) {
|
||||
array_add(ops, i->Call.args[j]);
|
||||
}
|
||||
break;
|
||||
case ssaInstr_VectorExtractElement:
|
||||
array_add(ops, i->VectorExtractElement.vector);
|
||||
array_add(ops, i->VectorExtractElement.index);
|
||||
break;
|
||||
case ssaInstr_VectorInsertElement:
|
||||
array_add(ops, i->VectorInsertElement.vector);
|
||||
array_add(ops, i->VectorInsertElement.elem);
|
||||
array_add(ops, i->VectorInsertElement.index);
|
||||
break;
|
||||
case ssaInstr_VectorShuffle:
|
||||
array_add(ops, i->VectorShuffle.vector);
|
||||
break;
|
||||
case ssaInstr_StartupRuntime:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ssa_block_replace_pred(ssaBlock *b, ssaBlock *from, ssaBlock *to) {
|
||||
for_array(i, b->preds) {
|
||||
ssaBlock *pred = b->preds[i];
|
||||
if (pred == from) {
|
||||
b->preds[i] = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_block_replace_succ(ssaBlock *b, ssaBlock *from, ssaBlock *to) {
|
||||
for_array(i, b->succs) {
|
||||
ssaBlock *succ = b->succs[i];
|
||||
if (succ == from) {
|
||||
b->succs[i] = to;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b32 ssa_block_has_phi(ssaBlock *b) {
|
||||
return b->instrs[0]->Instr.kind == ssaInstr_Phi;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void ssa_optimize_blocks(ssaProcedure *proc);
|
||||
void ssa_build_referrers(ssaProcedure *proc);
|
||||
void ssa_build_dom_tree (ssaProcedure *proc);
|
||||
void ssa_opt_mem2reg (ssaProcedure *proc);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Array<ssaValue *> ssa_get_block_phi_nodes(ssaBlock *b) {
|
||||
Array<ssaValue *> phis = {};
|
||||
for_array(i, b->instrs) {
|
||||
ssaInstr *instr = &b->instrs[i]->Instr;
|
||||
if (instr->kind != ssaInstr_Phi) {
|
||||
phis = b->instrs;
|
||||
phis.count = i;
|
||||
return phis;
|
||||
}
|
||||
}
|
||||
return phis;
|
||||
}
|
||||
|
||||
void ssa_remove_pred(ssaBlock *b, ssaBlock *p) {
|
||||
auto phis = ssa_get_block_phi_nodes(b);
|
||||
isize i = 0;
|
||||
for_array(j, b->preds) {
|
||||
ssaBlock *pred = b->preds[j];
|
||||
if (pred != p) {
|
||||
b->preds[i] = b->preds[j];
|
||||
for_array(k, phis) {
|
||||
auto *phi = &phis[k]->Instr.Phi;
|
||||
phi->edges[i] = phi->edges[j];
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
b->preds.count = i;
|
||||
for_array(k, phis) {
|
||||
auto *phi = &phis[k]->Instr.Phi;
|
||||
phi->edges.count = i;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void ssa_remove_dead_blocks(ssaProcedure *proc) {
|
||||
isize j = 0;
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks[i];
|
||||
if (b == NULL) {
|
||||
continue;
|
||||
}
|
||||
// NOTE(bill): Swap order
|
||||
b->index = j;
|
||||
proc->blocks[j++] = b;
|
||||
}
|
||||
proc->blocks.count = j;
|
||||
}
|
||||
|
||||
void ssa_mark_reachable(ssaBlock *b) {
|
||||
isize const WHITE = 0;
|
||||
isize const BLACK = -1;
|
||||
b->index = BLACK;
|
||||
for_array(i, b->succs) {
|
||||
ssaBlock *succ = b->succs[i];
|
||||
if (succ->index == WHITE) {
|
||||
ssa_mark_reachable(succ);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_remove_unreachable_blocks(ssaProcedure *proc) {
|
||||
isize const WHITE = 0;
|
||||
isize const BLACK = -1;
|
||||
for_array(i, proc->blocks) {
|
||||
proc->blocks[i]->index = WHITE;
|
||||
}
|
||||
|
||||
ssa_mark_reachable(proc->blocks[0]);
|
||||
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks[i];
|
||||
if (b->index == WHITE) {
|
||||
for_array(j, b->succs) {
|
||||
ssaBlock *c = b->succs[j];
|
||||
if (c->index == BLACK) {
|
||||
ssa_remove_pred(c, b);
|
||||
}
|
||||
}
|
||||
// NOTE(bill): Mark as empty but don't actually free it
|
||||
// As it's been allocated with an arena
|
||||
proc->blocks[i] = NULL;
|
||||
}
|
||||
}
|
||||
ssa_remove_dead_blocks(proc);
|
||||
}
|
||||
|
||||
b32 ssa_opt_block_fusion(ssaProcedure *proc, ssaBlock *a) {
|
||||
if (a->succs.count != 1) {
|
||||
return false;
|
||||
}
|
||||
ssaBlock *b = a->succs[0];
|
||||
if (b->preds.count != 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ssa_block_has_phi(b)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
array_pop(&a->instrs); // Remove branch at end
|
||||
for_array(i, b->instrs) {
|
||||
array_add(&a->instrs, b->instrs[i]);
|
||||
ssa_set_instr_parent(b->instrs[i], a);
|
||||
}
|
||||
|
||||
array_clear(&a->succs);
|
||||
for_array(i, b->succs) {
|
||||
array_add(&a->succs, b->succs[i]);
|
||||
}
|
||||
|
||||
// Fix preds links
|
||||
for_array(i, b->succs) {
|
||||
ssa_block_replace_pred(b->succs[i], b, a);
|
||||
}
|
||||
|
||||
proc->blocks[b->index] = NULL;
|
||||
return true;
|
||||
}
|
||||
|
||||
void ssa_optimize_blocks(ssaProcedure *proc) {
|
||||
ssa_remove_unreachable_blocks(proc);
|
||||
|
||||
#if 1
|
||||
b32 changed = true;
|
||||
while (changed) {
|
||||
changed = false;
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks[i];
|
||||
if (b == NULL) {
|
||||
continue;
|
||||
}
|
||||
GB_ASSERT(b->index == i);
|
||||
|
||||
if (ssa_opt_block_fusion(proc, b)) {
|
||||
changed = true;
|
||||
}
|
||||
// TODO(bill): other simple block optimizations
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
ssa_remove_dead_blocks(proc);
|
||||
}
|
||||
void ssa_build_referrers(ssaProcedure *proc) {
|
||||
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&proc->module->tmp_arena);
|
||||
defer (gb_temp_arena_memory_end(tmp));
|
||||
|
||||
Array<ssaValue *> ops = {}; // NOTE(bill): Act as a buffer
|
||||
array_init(&ops, proc->module->tmp_allocator, 64); // HACK(bill): This _could_ overflow the temp arena
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks[i];
|
||||
for_array(j, b->instrs) {
|
||||
ssaValue *instr = b->instrs[j];
|
||||
array_clear(&ops);
|
||||
ssa_add_operands(&ops, &instr->Instr);
|
||||
for_array(k, ops) {
|
||||
ssaValue *op = ops[k];
|
||||
if (op == NULL) {
|
||||
continue;
|
||||
}
|
||||
auto *refs = ssa_value_referrers(op);
|
||||
if (refs != NULL) {
|
||||
array_add(refs, instr);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// State of Lengauer-Tarjan algorithm
|
||||
// Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
|
||||
struct ssaLTState {
|
||||
isize count;
|
||||
// NOTE(bill): These are arrays
|
||||
ssaBlock **sdom; // Semidominator
|
||||
ssaBlock **parent; // Parent in DFS traversal of CFG
|
||||
ssaBlock **ancestor;
|
||||
};
|
||||
|
||||
// §2.2 - bottom of page
|
||||
void ssa_lt_link(ssaLTState *lt, ssaBlock *p, ssaBlock *q) {
|
||||
lt->ancestor[q->index] = p;
|
||||
}
|
||||
|
||||
i32 ssa_lt_depth_first_search(ssaLTState *lt, ssaBlock *p, i32 i, ssaBlock **preorder) {
|
||||
preorder[i] = p;
|
||||
p->dom.pre = i++;
|
||||
lt->sdom[p->index] = p;
|
||||
ssa_lt_link(lt, NULL, p);
|
||||
for_array(index, p->succs) {
|
||||
ssaBlock *q = p->succs[index];
|
||||
if (lt->sdom[q->index] == NULL) {
|
||||
lt->parent[q->index] = p;
|
||||
i = ssa_lt_depth_first_search(lt, q, i, preorder);
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
ssaBlock *ssa_lt_eval(ssaLTState *lt, ssaBlock *v) {
|
||||
ssaBlock *u = v;
|
||||
for (;
|
||||
lt->ancestor[v->index] != NULL;
|
||||
v = lt->ancestor[v->index]) {
|
||||
if (lt->sdom[v->index]->dom.pre < lt->sdom[u->index]->dom.pre) {
|
||||
u = v;
|
||||
}
|
||||
}
|
||||
return u;
|
||||
}
|
||||
|
||||
void ssa_number_dom_tree(ssaBlock *v, i32 pre, i32 post, i32 *pre_out, i32 *post_out) {
|
||||
v->dom.pre = pre++;
|
||||
for_array(i, v->dom.children) {
|
||||
ssaBlock *child = v->dom.children[i];
|
||||
i32 new_pre = 0, new_post = 0;
|
||||
ssa_number_dom_tree(child, pre, post, &new_pre, &new_post);
|
||||
pre = new_pre;
|
||||
post = new_post;
|
||||
}
|
||||
v->dom.post = post++;
|
||||
*pre_out = pre;
|
||||
*post_out = post;
|
||||
}
|
||||
|
||||
|
||||
// NOTE(bill): Requires `ssa_optimize_blocks` to be called before this
|
||||
void ssa_build_dom_tree(ssaProcedure *proc) {
|
||||
// Based on this paper: http://jgaa.info/accepted/2006/GeorgiadisTarjanWerneck2006.10.1.pdf
|
||||
|
||||
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&proc->module->tmp_arena);
|
||||
defer (gb_temp_arena_memory_end(tmp));
|
||||
|
||||
isize n = proc->blocks.count;
|
||||
ssaBlock **buf = gb_alloc_array(proc->module->tmp_allocator, ssaBlock *, 5*n);
|
||||
|
||||
ssaLTState lt = {};
|
||||
lt.count = n;
|
||||
lt.sdom = &buf[0*n];
|
||||
lt.parent = &buf[1*n];
|
||||
lt.ancestor = &buf[2*n];
|
||||
|
||||
ssaBlock **preorder = &buf[3*n];
|
||||
ssaBlock **buckets = &buf[4*n];
|
||||
ssaBlock *root = proc->blocks[0];
|
||||
|
||||
// Step 1 - number vertices
|
||||
i32 pre_num = ssa_lt_depth_first_search(<, root, 0, preorder);
|
||||
gb_memmove(buckets, preorder, n*gb_size_of(preorder[0]));
|
||||
|
||||
for (i32 i = n-1; i > 0; i--) {
|
||||
ssaBlock *w = preorder[i];
|
||||
|
||||
// Step 3 - Implicitly define idom for nodes
|
||||
for (ssaBlock *v = buckets[i]; v != w; v = buckets[v->dom.pre]) {
|
||||
ssaBlock *u = ssa_lt_eval(<, v);
|
||||
if (lt.sdom[u->index]->dom.pre < i) {
|
||||
v->dom.idom = u;
|
||||
} else {
|
||||
v->dom.idom = w;
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2 - Compute all sdoms
|
||||
lt.sdom[w->index] = lt.parent[w->index];
|
||||
for_array(pred_index, w->preds) {
|
||||
ssaBlock *v = w->preds[pred_index];
|
||||
ssaBlock *u = ssa_lt_eval(<, v);
|
||||
if (lt.sdom[u->index]->dom.pre < lt.sdom[w->index]->dom.pre) {
|
||||
lt.sdom[w->index] = lt.sdom[u->index];
|
||||
}
|
||||
}
|
||||
|
||||
ssa_lt_link(<, lt.parent[w->index], w);
|
||||
|
||||
if (lt.parent[w->index] == lt.sdom[w->index]) {
|
||||
w->dom.idom = lt.parent[w->index];
|
||||
} else {
|
||||
buckets[i] = buckets[lt.sdom[w->index]->dom.pre];
|
||||
buckets[lt.sdom[w->index]->dom.pre] = w;
|
||||
}
|
||||
}
|
||||
|
||||
// The rest of Step 3
|
||||
for (ssaBlock *v = buckets[0]; v != root; v = buckets[v->dom.pre]) {
|
||||
v->dom.idom = root;
|
||||
}
|
||||
|
||||
// Step 4 - Explicitly define idom for nodes (in preorder)
|
||||
for (isize i = 1; i < n; i++) {
|
||||
ssaBlock *w = preorder[i];
|
||||
if (w == root) {
|
||||
w->dom.idom = NULL;
|
||||
} else {
|
||||
// Weird tree relationships here!
|
||||
|
||||
if (w->dom.idom != lt.sdom[w->index]) {
|
||||
w->dom.idom = w->dom.idom->dom.idom;
|
||||
}
|
||||
|
||||
// Calculate children relation as inverse of idom
|
||||
auto *children = &w->dom.idom->dom.children;
|
||||
if (children->data == NULL) {
|
||||
// TODO(bill): Is this good enough for memory allocations?
|
||||
array_init(children, heap_allocator());
|
||||
}
|
||||
array_add(children, w);
|
||||
}
|
||||
}
|
||||
|
||||
i32 pre = 0;
|
||||
i32 pos = 0;
|
||||
ssa_number_dom_tree(root, 0, 0, &pre, &pos);
|
||||
}
|
||||
|
||||
void ssa_opt_mem2reg(ssaProcedure *proc) {
|
||||
// TODO(bill):
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
void ssa_begin_procedure_body(ssaProcedure *proc) {
|
||||
array_init(&proc->blocks, heap_allocator());
|
||||
array_init(&proc->defer_stmts, heap_allocator());
|
||||
array_init(&proc->children, heap_allocator());
|
||||
|
||||
proc->decl_block = ssa_add_block(proc, proc->type_expr, "decls");
|
||||
proc->entry_block = ssa_add_block(proc, proc->type_expr, "entry");
|
||||
proc->curr_block = proc->entry_block;
|
||||
|
||||
if (proc->type->Proc.params != NULL) {
|
||||
auto *params = &proc->type->Proc.params->Tuple;
|
||||
for (isize i = 0; i < params->variable_count; i++) {
|
||||
Entity *e = params->variables[i];
|
||||
ssa_add_param(proc, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ssa_end_procedure_body(ssaProcedure *proc) {
|
||||
if (proc->type->Proc.result_count == 0) {
|
||||
ssa_emit_return(proc, NULL);
|
||||
}
|
||||
|
||||
if (proc->curr_block->instrs.count == 0) {
|
||||
ssa_emit_unreachable(proc);
|
||||
}
|
||||
|
||||
proc->curr_block = proc->decl_block;
|
||||
ssa_emit_jump(proc, proc->entry_block);
|
||||
|
||||
#if 0
|
||||
ssa_optimize_blocks(proc);
|
||||
ssa_build_referrers(proc);
|
||||
ssa_build_dom_tree(proc);
|
||||
|
||||
// TODO(bill): mem2reg optimization
|
||||
// [ ] Local never loaded? Eliminate
|
||||
// [ ] Local never stored? Replace all loads with `Nil`
|
||||
// [ ] Local stored once? Replace loads with dominating store
|
||||
// [ ] Convert to phi nodes
|
||||
ssa_opt_mem2reg(proc);
|
||||
#endif
|
||||
|
||||
// Number registers
|
||||
i32 reg_index = 0;
|
||||
for_array(i, proc->blocks) {
|
||||
ssaBlock *b = proc->blocks[i];
|
||||
b->index = i;
|
||||
for_array(j, b->instrs) {
|
||||
ssaValue *value = b->instrs[j];
|
||||
GB_ASSERT(value->kind == ssaValue_Instr);
|
||||
ssaInstr *instr = &value->Instr;
|
||||
if (ssa_instr_type(instr) == NULL) { // NOTE(bill): Ignore non-returning instructions
|
||||
continue;
|
||||
}
|
||||
value->index = reg_index;
|
||||
reg_index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void ssa_insert_code_before_proc(ssaProcedure* proc, ssaProcedure *parent) {
|
||||
if (parent == NULL) {
|
||||
if (proc->name == "main") {
|
||||
ssa_emit_startup_runtime(proc);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_build_proc(ssaValue *value, ssaProcedure *parent) {
|
||||
ssaProcedure *proc = &value->Proc;
|
||||
|
||||
proc->parent = parent;
|
||||
|
||||
if (proc->entity != NULL) {
|
||||
ssaModule *m = proc->module;
|
||||
CheckerInfo *info = m->info;
|
||||
Entity *e = proc->entity;
|
||||
String filename = e->token.pos.file;
|
||||
AstFile **found = map_get(&info->files, hash_string(filename));
|
||||
GB_ASSERT(found != NULL);
|
||||
AstFile *f = *found;
|
||||
ssaDebugInfo *di_file = NULL;
|
||||
|
||||
ssaDebugInfo **di_file_found = map_get(&m->debug_info, hash_pointer(f));
|
||||
if (di_file_found) {
|
||||
di_file = *di_file_found;
|
||||
GB_ASSERT(di_file->kind == ssaDebugInfo_File);
|
||||
} else {
|
||||
di_file = ssa_add_debug_info_file(proc, f);
|
||||
}
|
||||
|
||||
ssa_add_debug_info_proc(proc, e, proc->name, di_file);
|
||||
}
|
||||
|
||||
if (proc->body != NULL) {
|
||||
u32 prev_stmt_state_flags = proc->module->stmt_state_flags;
|
||||
defer (proc->module->stmt_state_flags = prev_stmt_state_flags);
|
||||
|
||||
if (proc->tags != 0) {
|
||||
u32 in = proc->tags;
|
||||
u32 out = proc->module->stmt_state_flags;
|
||||
defer (proc->module->stmt_state_flags = out);
|
||||
|
||||
if (in & ProcTag_bounds_check) {
|
||||
out |= StmtStateFlag_bounds_check;
|
||||
out &= ~StmtStateFlag_no_bounds_check;
|
||||
} else if (in & ProcTag_no_bounds_check) {
|
||||
out |= StmtStateFlag_no_bounds_check;
|
||||
out &= ~StmtStateFlag_bounds_check;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ssa_begin_procedure_body(proc);
|
||||
ssa_insert_code_before_proc(proc, parent);
|
||||
ssa_build_stmt(proc, proc->body);
|
||||
ssa_end_procedure_body(proc);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+727
@@ -0,0 +1,727 @@
|
||||
struct ssaProcedure;
|
||||
struct ssaBlock;
|
||||
struct ssaValue;
|
||||
|
||||
enum ssaDebugInfoKind {
|
||||
ssaDebugInfo_Invalid,
|
||||
|
||||
ssaDebugInfo_CompileUnit,
|
||||
ssaDebugInfo_File,
|
||||
ssaDebugInfo_Proc,
|
||||
ssaDebugInfo_AllProcs,
|
||||
|
||||
ssaDebugInfo_Count,
|
||||
};
|
||||
|
||||
struct ssaDebugInfo {
|
||||
ssaDebugInfoKind kind;
|
||||
i32 id;
|
||||
|
||||
union {
|
||||
struct {
|
||||
AstFile * file;
|
||||
String producer;
|
||||
ssaDebugInfo *all_procs;
|
||||
} CompileUnit;
|
||||
struct {
|
||||
AstFile *file;
|
||||
String filename;
|
||||
String directory;
|
||||
} File;
|
||||
struct {
|
||||
Entity * entity;
|
||||
String name;
|
||||
ssaDebugInfo *file;
|
||||
TokenPos pos;
|
||||
} Proc;
|
||||
struct {
|
||||
Array<ssaDebugInfo *> procs;
|
||||
} AllProcs;
|
||||
};
|
||||
};
|
||||
|
||||
struct ssaModule {
|
||||
CheckerInfo * info;
|
||||
BaseTypeSizes sizes;
|
||||
gbArena arena;
|
||||
gbArena tmp_arena;
|
||||
gbAllocator allocator;
|
||||
gbAllocator tmp_allocator;
|
||||
b32 generate_debug_info;
|
||||
|
||||
u32 stmt_state_flags;
|
||||
|
||||
// String source_filename;
|
||||
String layout;
|
||||
// String triple;
|
||||
|
||||
Map<ssaValue *> values; // Key: Entity *
|
||||
Map<ssaValue *> members; // Key: String
|
||||
Map<String> type_names; // Key: Type *
|
||||
Map<ssaDebugInfo *> debug_info; // Key: Unique pointer
|
||||
i32 global_string_index;
|
||||
i32 global_array_index; // For ConstantSlice
|
||||
|
||||
Array<ssaValue *> procs; // NOTE(bill): Procedures to generate
|
||||
};
|
||||
|
||||
// NOTE(bill): For more info, see https://en.wikipedia.org/wiki/Dominator_(graph_theory)
|
||||
struct ssaDomNode {
|
||||
ssaBlock * idom; // Parent (Immediate Dominator)
|
||||
Array<ssaBlock *> children;
|
||||
i32 pre, post; // Ordering in tree
|
||||
};
|
||||
|
||||
|
||||
struct ssaBlock {
|
||||
i32 index;
|
||||
String label;
|
||||
ssaProcedure *parent;
|
||||
AstNode * node; // Can be NULL
|
||||
Scope * scope;
|
||||
isize scope_index;
|
||||
ssaDomNode dom;
|
||||
i32 gaps;
|
||||
|
||||
Array<ssaValue *> instrs;
|
||||
Array<ssaValue *> locals;
|
||||
|
||||
Array<ssaBlock *> preds;
|
||||
Array<ssaBlock *> succs;
|
||||
};
|
||||
|
||||
struct ssaTargetList {
|
||||
ssaTargetList *prev;
|
||||
ssaBlock * break_;
|
||||
ssaBlock * continue_;
|
||||
ssaBlock * fallthrough_;
|
||||
};
|
||||
|
||||
enum ssaDeferExitKind {
|
||||
ssaDeferExit_Default,
|
||||
ssaDeferExit_Return,
|
||||
ssaDeferExit_Branch,
|
||||
};
|
||||
enum ssaDeferKind {
|
||||
ssaDefer_Node,
|
||||
ssaDefer_Instr,
|
||||
};
|
||||
|
||||
struct ssaDefer {
|
||||
ssaDeferKind kind;
|
||||
isize scope_index;
|
||||
ssaBlock * block;
|
||||
union {
|
||||
AstNode *stmt;
|
||||
// NOTE(bill): `instr` will be copied every time to create a new one
|
||||
ssaValue *instr;
|
||||
};
|
||||
};
|
||||
|
||||
struct ssaProcedure {
|
||||
ssaProcedure * parent;
|
||||
Array<ssaProcedure *> children;
|
||||
|
||||
Entity * entity;
|
||||
ssaModule * module;
|
||||
String name;
|
||||
Type * type;
|
||||
AstNode * type_expr;
|
||||
AstNode * body;
|
||||
u64 tags;
|
||||
|
||||
isize scope_index;
|
||||
Array<ssaDefer> defer_stmts;
|
||||
Array<ssaBlock *> blocks;
|
||||
ssaBlock * decl_block;
|
||||
ssaBlock * entry_block;
|
||||
ssaBlock * curr_block;
|
||||
ssaTargetList * target_list;
|
||||
Array<ssaValue *> referrers;
|
||||
};
|
||||
|
||||
#define SSA_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
|
||||
#define SSA_TYPE_INFO_DATA_NAME "__$type_info_data"
|
||||
#define SSA_TYPE_INFO_DATA_MEMBER_NAME "__$type_info_data_member"
|
||||
|
||||
|
||||
#define SSA_INSTR_KINDS \
|
||||
SSA_INSTR_KIND(Invalid), \
|
||||
SSA_INSTR_KIND(Comment), \
|
||||
SSA_INSTR_KIND(Local), \
|
||||
SSA_INSTR_KIND(ZeroInit), \
|
||||
SSA_INSTR_KIND(Store), \
|
||||
SSA_INSTR_KIND(Load), \
|
||||
SSA_INSTR_KIND(PtrOffset), \
|
||||
SSA_INSTR_KIND(ArrayElementPtr), \
|
||||
SSA_INSTR_KIND(StructElementPtr), \
|
||||
SSA_INSTR_KIND(ArrayExtractValue), \
|
||||
SSA_INSTR_KIND(StructExtractValue), \
|
||||
SSA_INSTR_KIND(Conv), \
|
||||
SSA_INSTR_KIND(Jump), \
|
||||
SSA_INSTR_KIND(If), \
|
||||
SSA_INSTR_KIND(Return), \
|
||||
SSA_INSTR_KIND(Select), \
|
||||
SSA_INSTR_KIND(Phi), \
|
||||
SSA_INSTR_KIND(Unreachable), \
|
||||
SSA_INSTR_KIND(BinaryOp), \
|
||||
SSA_INSTR_KIND(Call), \
|
||||
SSA_INSTR_KIND(VectorExtractElement), \
|
||||
SSA_INSTR_KIND(VectorInsertElement), \
|
||||
SSA_INSTR_KIND(VectorShuffle), \
|
||||
SSA_INSTR_KIND(StartupRuntime),
|
||||
|
||||
#define SSA_CONV_KINDS \
|
||||
SSA_CONV_KIND(Invalid), \
|
||||
SSA_CONV_KIND(trunc), \
|
||||
SSA_CONV_KIND(zext), \
|
||||
SSA_CONV_KIND(fptrunc), \
|
||||
SSA_CONV_KIND(fpext), \
|
||||
SSA_CONV_KIND(fptoui), \
|
||||
SSA_CONV_KIND(fptosi), \
|
||||
SSA_CONV_KIND(uitofp), \
|
||||
SSA_CONV_KIND(sitofp), \
|
||||
SSA_CONV_KIND(ptrtoint), \
|
||||
SSA_CONV_KIND(inttoptr), \
|
||||
SSA_CONV_KIND(bitcast),
|
||||
|
||||
enum ssaInstrKind {
|
||||
#define SSA_INSTR_KIND(x) GB_JOIN2(ssaInstr_, x)
|
||||
SSA_INSTR_KINDS
|
||||
#undef SSA_INSTR_KIND
|
||||
};
|
||||
|
||||
String const ssa_instr_strings[] = {
|
||||
#define SSA_INSTR_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1}
|
||||
SSA_INSTR_KINDS
|
||||
#undef SSA_INSTR_KIND
|
||||
};
|
||||
|
||||
enum ssaConvKind {
|
||||
#define SSA_CONV_KIND(x) GB_JOIN2(ssaConv_, x)
|
||||
SSA_CONV_KINDS
|
||||
#undef SSA_CONV_KIND
|
||||
};
|
||||
|
||||
String const ssa_conv_strings[] = {
|
||||
#define SSA_CONV_KIND(x) {cast(u8 *)#x, gb_size_of(#x)-1}
|
||||
SSA_CONV_KINDS
|
||||
#undef SSA_CONV_KIND
|
||||
};
|
||||
|
||||
struct ssaInstr {
|
||||
ssaInstrKind kind;
|
||||
|
||||
ssaBlock *parent;
|
||||
Type *type;
|
||||
|
||||
union {
|
||||
struct {
|
||||
String text;
|
||||
} Comment;
|
||||
struct {
|
||||
Entity * entity;
|
||||
Type * type;
|
||||
b32 zero_initialized;
|
||||
Array<ssaValue *> referrers;
|
||||
} Local;
|
||||
struct {
|
||||
ssaValue *address;
|
||||
} ZeroInit;
|
||||
struct {
|
||||
ssaValue *address;
|
||||
ssaValue *value;
|
||||
} Store;
|
||||
struct {
|
||||
Type *type;
|
||||
ssaValue *address;
|
||||
} Load;
|
||||
struct {
|
||||
ssaValue *address;
|
||||
Type * result_type;
|
||||
ssaValue *elem_index;
|
||||
} ArrayElementPtr;
|
||||
struct {
|
||||
ssaValue *address;
|
||||
Type * result_type;
|
||||
i32 elem_index;
|
||||
} StructElementPtr;
|
||||
struct {
|
||||
ssaValue *address;
|
||||
ssaValue *offset;
|
||||
} PtrOffset;
|
||||
struct {
|
||||
ssaValue *address;
|
||||
Type * result_type;
|
||||
i32 index;
|
||||
} ArrayExtractValue;
|
||||
struct {
|
||||
ssaValue *address;
|
||||
Type * result_type;
|
||||
i32 index;
|
||||
} StructExtractValue;
|
||||
struct {
|
||||
ssaValue *value;
|
||||
ssaValue *elem;
|
||||
i32 index;
|
||||
} InsertValue;
|
||||
struct {
|
||||
ssaConvKind kind;
|
||||
ssaValue *value;
|
||||
Type *from, *to;
|
||||
} Conv;
|
||||
struct {
|
||||
ssaBlock *block;
|
||||
} Jump;
|
||||
struct {
|
||||
ssaValue *cond;
|
||||
ssaBlock *true_block;
|
||||
ssaBlock *false_block;
|
||||
} If;
|
||||
struct {
|
||||
ssaValue *value;
|
||||
} Return;
|
||||
struct {} Unreachable;
|
||||
struct {
|
||||
ssaValue *cond;
|
||||
ssaValue *true_value;
|
||||
ssaValue *false_value;
|
||||
} Select;
|
||||
struct {
|
||||
Array<ssaValue *> edges;
|
||||
Type *type;
|
||||
} Phi;
|
||||
struct {
|
||||
Type *type;
|
||||
TokenKind op;
|
||||
ssaValue *left, *right;
|
||||
} BinaryOp;
|
||||
struct {
|
||||
Type *type; // return type
|
||||
ssaValue *value;
|
||||
ssaValue **args;
|
||||
isize arg_count;
|
||||
} Call;
|
||||
struct {
|
||||
ssaValue *vector;
|
||||
ssaValue *index;
|
||||
} VectorExtractElement;
|
||||
struct {
|
||||
ssaValue *vector;
|
||||
ssaValue *elem;
|
||||
ssaValue *index;
|
||||
} VectorInsertElement;
|
||||
struct {
|
||||
ssaValue *vector;
|
||||
i32 *indices;
|
||||
isize index_count;
|
||||
Type *type;
|
||||
} VectorShuffle;
|
||||
|
||||
struct {} StartupRuntime;
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
enum ssaValueKind {
|
||||
ssaValue_Invalid,
|
||||
|
||||
ssaValue_Constant,
|
||||
ssaValue_ConstantSlice,
|
||||
ssaValue_Nil,
|
||||
ssaValue_TypeName,
|
||||
ssaValue_Global,
|
||||
ssaValue_Param,
|
||||
|
||||
ssaValue_Proc,
|
||||
ssaValue_Block,
|
||||
ssaValue_Instr,
|
||||
|
||||
ssaValue_Count,
|
||||
};
|
||||
|
||||
struct ssaValue {
|
||||
ssaValueKind kind;
|
||||
i32 index;
|
||||
union {
|
||||
struct {
|
||||
Type * type;
|
||||
ExactValue value;
|
||||
} Constant;
|
||||
struct {
|
||||
Type *type;
|
||||
ssaValue *backing_array;
|
||||
i64 count;
|
||||
} ConstantSlice;
|
||||
struct {
|
||||
Type *type;
|
||||
} Nil;
|
||||
struct {
|
||||
String name;
|
||||
Type * type;
|
||||
} TypeName;
|
||||
struct {
|
||||
b32 is_constant;
|
||||
b32 is_private;
|
||||
b32 is_thread_local;
|
||||
Entity * entity;
|
||||
Type * type;
|
||||
ssaValue *value;
|
||||
Array<ssaValue *> referrers;
|
||||
} Global;
|
||||
struct {
|
||||
ssaProcedure *parent;
|
||||
Entity *entity;
|
||||
Type * type;
|
||||
Array<ssaValue *> referrers;
|
||||
} Param;
|
||||
ssaProcedure Proc;
|
||||
ssaBlock Block;
|
||||
ssaInstr Instr;
|
||||
};
|
||||
};
|
||||
|
||||
gb_global ssaValue *v_zero = NULL;
|
||||
gb_global ssaValue *v_one = NULL;
|
||||
gb_global ssaValue *v_zero32 = NULL;
|
||||
gb_global ssaValue *v_one32 = NULL;
|
||||
gb_global ssaValue *v_two32 = NULL;
|
||||
gb_global ssaValue *v_false = NULL;
|
||||
gb_global ssaValue *v_true = NULL;
|
||||
|
||||
enum ssaAddrKind {
|
||||
ssaAddr_Default,
|
||||
ssaAddr_Vector,
|
||||
};
|
||||
|
||||
struct ssaAddr {
|
||||
ssaValue * addr;
|
||||
AstNode * expr; // NOTE(bill): Just for testing - probably remove later
|
||||
ssaAddrKind kind;
|
||||
union {
|
||||
struct { ssaValue *index; } Vector;
|
||||
};
|
||||
};
|
||||
|
||||
ssaAddr ssa_make_addr(ssaValue *addr, AstNode *expr) {
|
||||
ssaAddr v = {addr, expr};
|
||||
return v;
|
||||
}
|
||||
ssaAddr ssa_make_addr_vector(ssaValue *addr, ssaValue *index, AstNode *expr) {
|
||||
ssaAddr v = ssa_make_addr(addr, expr);
|
||||
v.kind = ssaAddr_Vector;
|
||||
v.Vector.index = index;
|
||||
return v;
|
||||
}
|
||||
|
||||
struct ssaFileBuffer {
|
||||
gbVirtualMemory vm;
|
||||
isize offset;
|
||||
gbFile *output;
|
||||
};
|
||||
|
||||
void ssa_file_buffer_init(ssaFileBuffer *f, gbFile *output) {
|
||||
isize size = 8*gb_virtual_memory_page_size(NULL);
|
||||
f->vm = gb_vm_alloc(NULL, size);
|
||||
f->offset = 0;
|
||||
f->output = output;
|
||||
}
|
||||
|
||||
void ssa_file_buffer_destroy(ssaFileBuffer *f) {
|
||||
if (f->offset > 0) {
|
||||
// NOTE(bill): finish writing buffered data
|
||||
gb_file_write(f->output, f->vm.data, f->offset);
|
||||
}
|
||||
|
||||
gb_vm_free(f->vm);
|
||||
}
|
||||
|
||||
void ssa_file_buffer_write(ssaFileBuffer *f, void *data, isize len) {
|
||||
if (len > f->vm.size) {
|
||||
gb_file_write(f->output, data, len);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((f->vm.size - f->offset) < len) {
|
||||
gb_file_write(f->output, f->vm.data, f->offset);
|
||||
f->offset = 0;
|
||||
}
|
||||
u8 *cursor = cast(u8 *)f->vm.data + f->offset;
|
||||
gb_memmove(cursor, data, len);
|
||||
f->offset += len;
|
||||
}
|
||||
|
||||
|
||||
void ssa_fprintf(ssaFileBuffer *f, char *fmt, ...) {
|
||||
va_list va;
|
||||
va_start(va, fmt);
|
||||
char buf[4096] = {};
|
||||
isize len = gb_snprintf_va(buf, gb_size_of(buf), fmt, va);
|
||||
ssa_file_buffer_write(f, buf, len-1);
|
||||
va_end(va);
|
||||
}
|
||||
|
||||
|
||||
void ssa_file_write(ssaFileBuffer *f, void *data, isize len) {
|
||||
ssa_file_buffer_write(f, data, len);
|
||||
}
|
||||
|
||||
|
||||
|
||||
Type *ssa_type(ssaValue *value);
|
||||
Type *ssa_instr_type(ssaInstr *instr) {
|
||||
switch (instr->kind) {
|
||||
case ssaInstr_Local:
|
||||
return instr->Local.type;
|
||||
case ssaInstr_Load:
|
||||
return instr->Load.type;
|
||||
case ssaInstr_StructElementPtr:
|
||||
return instr->StructElementPtr.result_type;
|
||||
case ssaInstr_ArrayElementPtr:
|
||||
return instr->ArrayElementPtr.result_type;
|
||||
case ssaInstr_PtrOffset:
|
||||
return ssa_type(instr->PtrOffset.address);
|
||||
case ssaInstr_Phi:
|
||||
return instr->Phi.type;
|
||||
case ssaInstr_ArrayExtractValue:
|
||||
return instr->ArrayExtractValue.result_type;
|
||||
case ssaInstr_StructExtractValue:
|
||||
return instr->StructExtractValue.result_type;
|
||||
case ssaInstr_BinaryOp:
|
||||
return instr->BinaryOp.type;
|
||||
case ssaInstr_Conv:
|
||||
return instr->Conv.to;
|
||||
case ssaInstr_Select:
|
||||
return ssa_type(instr->Select.true_value);
|
||||
case ssaInstr_Call: {
|
||||
Type *pt = base_type(instr->Call.type);
|
||||
if (pt != NULL) {
|
||||
if (pt->kind == Type_Tuple && pt->Tuple.variable_count == 1) {
|
||||
return pt->Tuple.variables[0]->type;
|
||||
}
|
||||
return pt;
|
||||
}
|
||||
return NULL;
|
||||
} break;
|
||||
case ssaInstr_VectorExtractElement: {
|
||||
Type *vt = ssa_type(instr->VectorExtractElement.vector);
|
||||
Type *bt = base_vector_type(vt);
|
||||
GB_ASSERT(!is_type_vector(bt));
|
||||
return bt;
|
||||
} break;
|
||||
case ssaInstr_VectorInsertElement:
|
||||
return ssa_type(instr->VectorInsertElement.vector);
|
||||
case ssaInstr_VectorShuffle:
|
||||
return instr->VectorShuffle.type;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Type *ssa_type(ssaValue *value) {
|
||||
switch (value->kind) {
|
||||
case ssaValue_Constant:
|
||||
return value->Constant.type;
|
||||
case ssaValue_ConstantSlice:
|
||||
return value->ConstantSlice.type;
|
||||
case ssaValue_Nil:
|
||||
return value->Nil.type;
|
||||
case ssaValue_TypeName:
|
||||
return value->TypeName.type;
|
||||
case ssaValue_Global:
|
||||
return value->Global.type;
|
||||
case ssaValue_Param:
|
||||
return value->Param.type;
|
||||
case ssaValue_Proc:
|
||||
return value->Proc.type;
|
||||
case ssaValue_Instr:
|
||||
return ssa_instr_type(&value->Instr);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Type *ssa_addr_type(ssaAddr lval) {
|
||||
if (lval.addr != NULL) {
|
||||
Type *t = ssa_type(lval.addr);
|
||||
GB_ASSERT(is_type_pointer(t));
|
||||
return type_deref(t);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
b32 ssa_is_blank_ident(AstNode *node) {
|
||||
if (node->kind == AstNode_Ident) {
|
||||
ast_node(i, Ident, node);
|
||||
return is_blank_ident(i->string);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
ssaInstr *ssa_get_last_instr(ssaBlock *block) {
|
||||
if (block != NULL) {
|
||||
isize len = block->instrs.count;
|
||||
if (len > 0) {
|
||||
ssaValue *v = block->instrs[len-1];
|
||||
GB_ASSERT(v->kind == ssaValue_Instr);
|
||||
return &v->Instr;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
|
||||
}
|
||||
|
||||
b32 ssa_is_instr_terminating(ssaInstr *i) {
|
||||
if (i != NULL) {
|
||||
switch (i->kind) {
|
||||
case ssaInstr_Return:
|
||||
case ssaInstr_Unreachable:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
void ssa_add_edge(ssaBlock *from, ssaBlock *to) {
|
||||
array_add(&from->succs, to);
|
||||
array_add(&to->preds, from);
|
||||
}
|
||||
|
||||
void ssa_set_instr_parent(ssaValue *instr, ssaBlock *parent) {
|
||||
if (instr->kind == ssaValue_Instr) {
|
||||
instr->Instr.parent = parent;
|
||||
}
|
||||
}
|
||||
|
||||
Array<ssaValue *> *ssa_value_referrers(ssaValue *v) {
|
||||
switch (v->kind) {
|
||||
case ssaValue_Global:
|
||||
return &v->Global.referrers;
|
||||
case ssaValue_Param:
|
||||
return &v->Param.referrers;
|
||||
case ssaValue_Proc: {
|
||||
if (v->Proc.parent != NULL) {
|
||||
return &v->Proc.referrers;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
case ssaValue_Instr: {
|
||||
ssaInstr *i = &v->Instr;
|
||||
switch (i->kind) {
|
||||
case ssaInstr_Local:
|
||||
return &i->Local.referrers;
|
||||
}
|
||||
} break;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
#include "make.cpp"
|
||||
#include "debug.cpp"
|
||||
#include "emit.cpp"
|
||||
#include "build.cpp"
|
||||
#include "opt.cpp"
|
||||
#include "proc.cpp"
|
||||
|
||||
|
||||
|
||||
|
||||
void ssa_module_add_value(ssaModule *m, Entity *e, ssaValue *v) {
|
||||
map_set(&m->values, hash_pointer(e), v);
|
||||
}
|
||||
|
||||
void ssa_init_module(ssaModule *m, Checker *c) {
|
||||
// TODO(bill): Determine a decent size for the arena
|
||||
isize token_count = c->parser->total_token_count;
|
||||
isize arena_size = 4 * token_count * gb_size_of(ssaValue);
|
||||
gb_arena_init_from_allocator(&m->arena, heap_allocator(), arena_size);
|
||||
gb_arena_init_from_allocator(&m->tmp_arena, heap_allocator(), arena_size);
|
||||
m->allocator = gb_arena_allocator(&m->arena);
|
||||
m->tmp_allocator = gb_arena_allocator(&m->tmp_arena);
|
||||
m->info = &c->info;
|
||||
m->sizes = c->sizes;
|
||||
|
||||
map_init(&m->values, heap_allocator());
|
||||
map_init(&m->members, heap_allocator());
|
||||
map_init(&m->debug_info, heap_allocator());
|
||||
map_init(&m->type_names, heap_allocator());
|
||||
array_init(&m->procs, heap_allocator());
|
||||
|
||||
// Default states
|
||||
m->stmt_state_flags = 0;
|
||||
m->stmt_state_flags |= StmtStateFlag_bounds_check;
|
||||
|
||||
{
|
||||
// Add type info data
|
||||
{
|
||||
String name = make_string(SSA_TYPE_INFO_DATA_NAME);
|
||||
isize count = c->info.type_info_map.entries.count;
|
||||
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), make_type_array(m->allocator, t_type_info, count));
|
||||
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
|
||||
g->Global.is_private = true;
|
||||
ssa_module_add_value(m, e, g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
}
|
||||
|
||||
// Type info member buffer
|
||||
{
|
||||
// NOTE(bill): Removes need for heap allocation by making it global memory
|
||||
isize count = 0;
|
||||
|
||||
for_array(entry_index, m->info->type_info_map.entries) {
|
||||
auto *entry = &m->info->type_info_map.entries[entry_index];
|
||||
Type *t = cast(Type *)cast(uintptr)entry->key.key;
|
||||
|
||||
switch (t->kind) {
|
||||
case Type_Record:
|
||||
switch (t->Record.kind) {
|
||||
case TypeRecord_Struct:
|
||||
case TypeRecord_RawUnion:
|
||||
count += t->Record.field_count;
|
||||
}
|
||||
break;
|
||||
case Type_Tuple:
|
||||
count += t->Tuple.variable_count;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
String name = make_string(SSA_TYPE_INFO_DATA_MEMBER_NAME);
|
||||
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name),
|
||||
make_type_array(m->allocator, t_type_info_member, count));
|
||||
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
|
||||
ssa_module_add_value(m, e, g);
|
||||
map_set(&m->members, hash_string(name), g);
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
ssaDebugInfo *di = ssa_alloc_debug_info(m->allocator, ssaDebugInfo_CompileUnit);
|
||||
di->CompileUnit.file = m->info->files.entries[0].value; // Zeroth is the init file
|
||||
di->CompileUnit.producer = make_string("odin");
|
||||
|
||||
map_set(&m->debug_info, hash_pointer(m), di);
|
||||
}
|
||||
}
|
||||
|
||||
void ssa_destroy_module(ssaModule *m) {
|
||||
map_destroy(&m->values);
|
||||
map_destroy(&m->members);
|
||||
map_destroy(&m->type_names);
|
||||
map_destroy(&m->debug_info);
|
||||
array_free(&m->procs);
|
||||
gb_arena_free(&m->arena);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
#include "codegen.cpp"
|
||||
|
||||
|
||||
Reference in New Issue
Block a user