Begin work on the Interpreter

This commit is contained in:
Ginger Bill
2016-10-30 17:49:30 +00:00
parent 3ec67853e1
commit ca311c4a59
6 changed files with 34 additions and 19 deletions
+2 -1
View File
@@ -1,5 +1,6 @@
#import "fmt.odin" #import "fmt.odin"
main :: proc() { main :: proc() {
x, y: i64 = 123, 321
y = x + 2 - y
} }
+2 -1
View File
@@ -811,8 +811,9 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
} }
} }
} else { } else {
if (is_type_float(elem_type)) if (is_type_float(elem_type)) {
ssa_fprintf(f, "f"); ssa_fprintf(f, "f");
}
switch (bo->op) { switch (bo->op) {
case Token_Add: ssa_fprintf(f, "add"); break; case Token_Add: ssa_fprintf(f, "add"); break;
+15 -1
View File
@@ -9,6 +9,7 @@
#include "checker/checker.cpp" #include "checker/checker.cpp"
#include "ssa/ssa.cpp" #include "ssa/ssa.cpp"
#include "llvm/ssa_to_text.cpp" #include "llvm/ssa_to_text.cpp"
#include "vm/vm.cpp"
// NOTE(bill): `name` is used in debugging and profiling modes // NOTE(bill): `name` is used in debugging and profiling modes
i32 win32_exec_command_line_app(char *name, char *fmt, ...) { i32 win32_exec_command_line_app(char *name, char *fmt, ...) {
@@ -165,7 +166,20 @@ int main(int argc, char **argv) {
ssa_gen_tree(&ssa); ssa_gen_tree(&ssa);
// TODO(bill): Speedup writing to file for IR code {
VirtualMachine vm = {};
vm_init(&vm, &ssa.module);
defer (vm_destroy(&vm));
String name = make_string("main");
ssaValue *main_proc_value = *map_get(&vm.module->members, hash_string(name));
GB_ASSERT(main_proc_value->kind == ssaValue_Proc);
ssaProcedure *start_proc = &main_proc_value->Proc;
Array<vmValue> args = {}; // Empty
vm_call_procedure(&vm, start_proc, args);
}
{ {
ssaFileBuffer buf = {}; ssaFileBuffer buf = {};
ssa_file_buffer_init(&buf, &ssa.output_file); ssa_file_buffer_init(&buf, &ssa.output_file);
+5 -8
View File
@@ -295,14 +295,6 @@ ssaValue *ssa_make_value_constant_slice(gbAllocator a, Type *type, ssaValue *bac
return v; return v;
} }
ssaValue *ssa_make_value_constant_string(gbAllocator a, Type *type, String string) {
ssaValue *v = ssa_alloc_value(a, ssaValue_ConstantString);
v->ConstantString.type = type;
v->ConstantString.string = string;
return v;
}
ssaValue *ssa_make_const_int(gbAllocator a, i64 i) { ssaValue *ssa_make_const_int(gbAllocator a, i64 i) {
return ssa_make_value_constant(a, t_int, make_exact_value_integer(i)); return ssa_make_value_constant(a, t_int, make_exact_value_integer(i));
} }
@@ -328,6 +320,11 @@ ssaValue *ssa_make_value_procedure(gbAllocator a, ssaModule *m, Entity *entity,
v->Proc.body = body; v->Proc.body = body;
v->Proc.name = name; v->Proc.name = name;
array_init(&v->Proc.referrers, heap_allocator(), 0); // TODO(bill): replace heap allocator array_init(&v->Proc.referrers, heap_allocator(), 0); // TODO(bill): replace heap allocator
Type *t = base_type(type);
GB_ASSERT(is_type_proc(t));
array_init(&v->Proc.params, heap_allocator(), t->Proc.param_count);
return v; return v;
} }
+2 -1
View File
@@ -11,7 +11,8 @@ void ssa_begin_procedure_body(ssaProcedure *proc) {
auto *params = &proc->type->Proc.params->Tuple; auto *params = &proc->type->Proc.params->Tuple;
for (isize i = 0; i < params->variable_count; i++) { for (isize i = 0; i < params->variable_count; i++) {
Entity *e = params->variables[i]; Entity *e = params->variables[i];
ssa_add_param(proc, e); ssaValue *param = ssa_add_param(proc, e);
array_add(&proc->params, param);
} }
} }
} }
+8 -7
View File
@@ -93,6 +93,7 @@ struct ssaProcedure {
AstNode * body; AstNode * body;
u64 tags; u64 tags;
Array<ssaValue *> params;
Array<ssaDefer> defer_stmts; Array<ssaDefer> defer_stmts;
Array<ssaBlock *> blocks; Array<ssaBlock *> blocks;
i32 scope_index; i32 scope_index;
@@ -308,7 +309,6 @@ enum ssaValueKind {
ssaValue_Constant, ssaValue_Constant,
ssaValue_ConstantSlice, ssaValue_ConstantSlice,
ssaValue_ConstantString,
ssaValue_Nil, ssaValue_Nil,
ssaValue_TypeName, ssaValue_TypeName,
ssaValue_Global, ssaValue_Global,
@@ -334,10 +334,6 @@ struct ssaValue {
ssaValue *backing_array; ssaValue *backing_array;
i64 count; i64 count;
} ConstantSlice; } ConstantSlice;
struct {
Type * type;
String string;
} ConstantString;
struct { struct {
Type *type; Type *type;
} Nil; } Nil;
@@ -579,6 +575,13 @@ void ssa_file_write(ssaFileBuffer *f, void *data, isize len) {
ssa_file_buffer_write(f, data, len); ssa_file_buffer_write(f, data, len);
} }
ssaValue *ssa_lookup_member(ssaModule *m, String name) {
ssaValue **v = map_get(&m->members, hash_string(name));
if (v != NULL) {
return *v;
}
return NULL;
}
Type *ssa_type(ssaValue *value); Type *ssa_type(ssaValue *value);
@@ -636,8 +639,6 @@ Type *ssa_type(ssaValue *value) {
return value->Constant.type; return value->Constant.type;
case ssaValue_ConstantSlice: case ssaValue_ConstantSlice:
return value->ConstantSlice.type; return value->ConstantSlice.type;
case ssaValue_ConstantString:
return value->ConstantString.type;
case ssaValue_Nil: case ssaValue_Nil:
return value->Nil.type; return value->Nil.type;
case ssaValue_TypeName: case ssaValue_TypeName: