Wrap entry point main around the C style main in the IR

This commit is contained in:
Ginger Bill
2017-09-30 11:20:35 +01:00
parent 8c7cf0dbb0
commit e2b9c87aa8
5 changed files with 191 additions and 95 deletions

View File

@@ -138,8 +138,8 @@ Type_Info :: struct #ordered {
// This will be set by the compiler
__type_table: []Type_Info;
__argv__: ^^u8;
__argc__: i32;
__argv__: ^^u8;
// IMPORTANT NOTE(bill): Must be in this order (as the compiler relies upon it)

View File

@@ -267,6 +267,9 @@ dlerror :: proc() -> string {
_alloc_command_line_arguments :: proc() -> []string {
// TODO(bill):
return nil;
args := make([]string, __argc__);
for i in 0..__argc__ {
args[i] = strings.to_odin_string((__argv__+i)^);
}
return args;
}

View File

@@ -42,7 +42,8 @@ RTLD_NOLOAD :: 0x10;
RTLD_FIRST :: 0x100;
args: [dynamic]string;
// "Argv" arguments converted to Odin strings
args := _alloc_command_line_arguments();
_File_Time :: struct #ordered {
seconds: i64,
@@ -279,3 +280,12 @@ dlclose :: proc(handle: rawptr) -> bool #inline {
dlerror :: proc() -> string {
return strings.to_odin_string(unix_dlerror());
}
_alloc_command_line_arguments :: proc() -> []string {
args := make([]string, __argc__);
for i in 0..__argc__ {
args[i] = strings.to_odin_string((__argv__+i)^);
}
return args;
}

View File

@@ -1233,7 +1233,82 @@ bool check_custom_align(Checker *c, AstNode *node, i64 *align_) {
return false;
}
void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Operand> *poly_operands) {
Entity *find_polymorphic_struct_entity(Checker *c, Type *original_type, isize param_count, Array<Operand> ordered_operands) {
auto *found_gen_types = map_get(&c->info.gen_types, hash_pointer(original_type));
if (found_gen_types != nullptr) {
for_array(i, *found_gen_types) {
Entity *e = (*found_gen_types)[i];
Type *t = base_type(e->type);
TypeTuple *tuple = &t->Struct.polymorphic_params->Tuple;
bool ok = true;
GB_ASSERT(param_count == tuple->variables.count);
for (isize j = 0; j < param_count; j++) {
Entity *p = tuple->variables[j];
Operand o = ordered_operands[j];
if (p->kind == Entity_TypeName) {
if (is_type_polymorphic(o.type)) {
// NOTE(bill): Do not add polymorphic version to the gen_types
ok = false;
}
if (!are_types_identical(o.type, p->type)) {
ok = false;
}
} else if (p->kind == Entity_Constant) {
if (!are_types_identical(o.type, p->type)) {
ok = false;
}
if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
ok = false;
}
} else {
GB_PANIC("Unknown entity kind");
}
}
if (ok) {
return e;
}
}
}
return nullptr;
}
void add_polymorphic_struct_entity(Checker *c, AstNode *node, Type *original_type, Type *named_type) {
GB_ASSERT(is_type_named(named_type));
gbAllocator a = heap_allocator();
Entity *e = nullptr;
{
Token token = ast_node_token(node);
token.kind = Token_String;
token.string = named_type->Named.name;
AstNode *node = gb_alloc_item(a, AstNode);
node->kind = AstNode_Ident;
node->Ident.token = token;
e = make_entity_type_name(a, c->context.scope, token, named_type);
add_entity(c, c->context.scope, node, e);
add_entity_use(c, node, e);
}
named_type->Named.type_name = e;
auto *found_gen_types = map_get(&c->info.gen_types, hash_pointer(original_type));
if (found_gen_types) {
array_add(found_gen_types, e);
} else {
Array<Entity *> array = {};
array_init(&array, heap_allocator());
array_add(&array, e);
map_set(&c->info.gen_types, hash_pointer(original_type), array);
}
}
void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Operand> *poly_operands, Type *named_type = nullptr) {
GB_ASSERT(is_type_struct(struct_type));
ast_node(st, StructType, node);
@@ -1402,20 +1477,21 @@ void check_struct_type(Checker *c, Type *struct_type, AstNode *node, Array<Opera
}
}
struct_type->Struct.scope = c->context.scope;
struct_type->Struct.is_packed = st->is_packed;
struct_type->Struct.is_ordered = st->is_ordered;
struct_type->Struct.polymorphic_params = polymorphic_params;
struct_type->Struct.is_polymorphic = is_polymorphic;
struct_type->Struct.is_poly_specialized = is_poly_specialized;
Array<Entity *> fields = {};
if (!is_polymorphic) {
fields = check_struct_fields(c, node, st->fields, min_field_count, context);
}
struct_type->Struct.scope = c->context.scope;
struct_type->Struct.is_packed = st->is_packed;
struct_type->Struct.is_ordered = st->is_ordered;
struct_type->Struct.fields = fields;
struct_type->Struct.fields_in_src_order = fields;
struct_type->Struct.polymorphic_params = polymorphic_params;
struct_type->Struct.is_polymorphic = is_polymorphic;
struct_type->Struct.is_poly_specialized = is_poly_specialized;
if (!struct_type->Struct.is_raw_union) {
@@ -3107,7 +3183,7 @@ bool check_type_internal(Checker *c, AstNode *e, Type **type, Type *named_type)
*type = make_type_struct(c->allocator);
set_base_type(named_type, *type);
check_open_scope(c, e);
check_struct_type(c, *type, e, nullptr);
check_struct_type(c, *type, e, nullptr, named_type);
check_close_scope(c);
(*type)->Struct.node = e;
return true;
@@ -6907,85 +6983,27 @@ CallArgumentError check_polymorphic_struct_type(Checker *c, Operand *operand, As
// TODO(bill): Check for previous types
gbAllocator a = c->allocator;
auto *found_gen_types = map_get(&c->info.gen_types, hash_pointer(original_type));
if (found_gen_types != nullptr) {
for_array(i, *found_gen_types) {
Entity *e = (*found_gen_types)[i];
Type *t = base_type(e->type);
TypeTuple *tuple = &t->Struct.polymorphic_params->Tuple;
bool ok = true;
GB_ASSERT(param_count == tuple->variables.count);
for (isize j = 0; j < param_count; j++) {
Entity *p = tuple->variables[j];
Operand o = ordered_operands[j];
if (p->kind == Entity_TypeName) {
if (is_type_polymorphic(o.type)) {
// NOTE(bill): Do not add polymorphic version to the gen_types
ok = false;
}
if (!are_types_identical(o.type, p->type)) {
ok = false;
}
} else if (p->kind == Entity_Constant) {
if (!are_types_identical(o.type, p->type)) {
ok = false;
}
if (!compare_exact_values(Token_CmpEq, o.value, p->Constant.value)) {
ok = false;
}
} else {
GB_PANIC("Unknown entity kind");
}
}
if (ok) {
operand->mode = Addressing_Type;
operand->type = e->type;
return err;
}
}
Entity *found_entity = find_polymorphic_struct_entity(c, original_type, param_count, ordered_operands);
if (found_entity) {
operand->mode = Addressing_Type;
operand->type = found_entity->type;
return err;
}
String generated_name = make_string_c(expr_to_string(call));
Type *named_type = make_type_named(a, generated_name, nullptr, nullptr);
Type *struct_type = make_type_struct(a);
AstNode *node = clone_ast_node(a, st->node);
set_base_type(named_type, struct_type);
check_open_scope(c, node);
check_struct_type(c, struct_type, node, &ordered_operands);
check_close_scope(c);
Type *struct_type = make_type_struct(a);
struct_type->Struct.node = node;
struct_type->Struct.polymorphic_parent = original_type;
set_base_type(named_type, struct_type);
Entity *e = nullptr;
check_open_scope(c, node);
check_struct_type(c, struct_type, node, &ordered_operands, named_type);
check_close_scope(c);
{
Token token = ast_node_token(node);
token.kind = Token_String;
token.string = generated_name;
AstNode *node = gb_alloc_item(a, AstNode);
node->kind = AstNode_Ident;
node->Ident.token = token;
e = make_entity_type_name(a, st->scope->parent, token, named_type);
add_entity(c, st->scope->parent, node, e);
add_entity_use(c, node, e);
}
named_type->Named.type_name = e;
if (!struct_type->Struct.is_polymorphic) {
if (found_gen_types) {
array_add(found_gen_types, e);
} else {
Array<Entity *> array = {};
array_init(&array, heap_allocator());
array_add(&array, e);
map_set(&c->info.gen_types, hash_pointer(original_type), array);
}
}
add_polymorphic_struct_entity(c, node, original_type, named_type);
operand->mode = Addressing_Type;
operand->type = named_type;

View File

@@ -7276,11 +7276,11 @@ void ir_end_procedure_body(irProcedure *proc) {
void ir_insert_code_before_proc(irProcedure* proc, irProcedure *parent) {
if (parent == nullptr) {
if (proc->name == "main") {
ir_emit_startup_runtime(proc);
}
}
// if (parent == nullptr) {
// if (proc->name == "main") {
// ir_emit_startup_runtime(proc);
// }
// }
}
void ir_build_proc(irValue *value, irProcedure *parent) {
@@ -7728,7 +7728,7 @@ void ir_gen_tree(irGen *s) {
if (e->kind == Entity_Procedure && (e->Procedure.tags & ProcTag_export) != 0) {
} else if (e->kind == Entity_Procedure && e->Procedure.link_name.len > 0) {
// Handle later
} else if (scope->is_init && e->kind == Entity_Procedure && name == "main") {
// } else if (scope->is_init && e->kind == Entity_Procedure && name == "main") {
} else {
name = ir_mangle_name(s, e->token.pos.file, e);
}
@@ -7807,7 +7807,7 @@ void ir_gen_tree(irGen *s) {
#if defined(GB_SYSTEM_WINDOWS)
if (build_context.is_dll && !has_dll_main) {
// proc DllMain(inst: rawptr, reason: u32, reserved: rawptr) -> i32
// DllMain :: proc(inst: rawptr, reason: u32, reserved: rawptr) -> i32
String name = str_lit("DllMain");
Type *proc_params = make_type_tuple(a);
Type *proc_results = make_type_tuple(a);
@@ -7840,7 +7840,7 @@ void ir_gen_tree(irGen *s) {
Entity *e = make_entity_procedure(a, nullptr, make_token_ident(name), proc_type, 0);
irValue *p = ir_value_procedure(a, m, e, proc_type, nullptr, body, name);
map_set(&m->values, hash_pointer(e), p);
map_set(&m->values, hash_entity(e), p);
map_set(&m->members, hash_string(name), p);
irProcedure *proc = &p->Proc;
@@ -7860,12 +7860,10 @@ void ir_gen_tree(irGen *s) {
ir_start_block(proc, then);
{
String main_name = str_lit("main");
irValue **found = map_get(&m->members, hash_string(main_name));
irValue **found = map_get(&m->values, hash_entity(entry_point));
ir_emit(proc, ir_alloc_instr(proc, irInstr_StartupRuntime));
if (found != nullptr) {
ir_emit_call(proc, *found, nullptr, 0);
} else {
ir_emit(proc, ir_alloc_instr(proc, irInstr_StartupRuntime));
}
}
@@ -7878,6 +7876,73 @@ void ir_gen_tree(irGen *s) {
ir_end_procedure_body(proc);
}
#endif
if (!(build_context.is_dll && !has_dll_main)) {
// main :: proc(argc: i32, argv: ^^u8) -> i32
String name = str_lit("main");
Type *proc_params = make_type_tuple(a);
Type *proc_results = make_type_tuple(a);
Scope *proc_scope = gb_alloc_item(a, Scope);
array_init_count(&proc_params->Tuple.variables, a, 2);
array_init_count(&proc_results->Tuple.variables, a, 1);
Type *char_ptr_ptr = make_type_pointer(a, make_type_pointer(a, t_u8));
proc_params->Tuple.variables[0] = make_entity_param(a, proc_scope, make_token_ident(str_lit("argc")), t_i32, false, false);
proc_params->Tuple.variables[1] = make_entity_param(a, proc_scope, make_token_ident(str_lit("argv")), char_ptr_ptr, false, false);
proc_results->Tuple.variables[0] = make_entity_param(a, proc_scope, empty_token, t_i32, false, false);
Type *proc_type = make_type_proc(a, proc_scope,
proc_params, 2,
proc_results, 1, false, ProcCC_C);
// TODO(bill): make this more robust
proc_type->Proc.abi_compat_params = gb_alloc_array(a, Type *, proc_params->Tuple.variables.count);
for_array(i, proc_params->Tuple.variables) {
proc_type->Proc.abi_compat_params[i] = proc_params->Tuple.variables[i]->type;
}
proc_type->Proc.abi_compat_result_type = proc_results->Tuple.variables[0]->type;
AstNode *body = gb_alloc_item(a, AstNode);
Entity *e = make_entity_procedure(a, nullptr, make_token_ident(name), proc_type, 0);
irValue *p = ir_value_procedure(a, m, e, proc_type, nullptr, body, name);
map_set(&m->values, hash_entity(e), p);
map_set(&m->members, hash_string(name), p);
irProcedure *proc = &p->Proc;
proc->tags = ProcTag_no_inline; // TODO(bill): is no_inline a good idea?
e->Procedure.link_name = name;
ir_begin_procedure_body(proc);
// NOTE(bill): https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
// DLL_PROCESS_ATTACH == 1
irValue *argc = ir_emit_load(proc, *map_get(&proc->module->values, hash_entity(proc_params->Tuple.variables[0])));
irValue *argv = ir_emit_load(proc, *map_get(&proc->module->values, hash_entity(proc_params->Tuple.variables[1])));
irValue *global_argc = ir_find_global_variable(proc, str_lit("__argc__"));
irValue *global_argv = ir_find_global_variable(proc, str_lit("__argv__"));
ir_emit_store(proc, global_argc, argc);
ir_emit_store(proc, global_argv, argv);
ir_emit(proc, ir_alloc_instr(proc, irInstr_StartupRuntime));
{
irValue **found = map_get(&proc->module->values, hash_entity(entry_point));
if (found != nullptr) {
ir_emit_call(proc, *found, nullptr, 0);
}
}
ir_emit_return(proc, v_zero32);
ir_end_procedure_body(proc);
}
#if 0 && defined(GB_SYSTEM_WINDOWS)
if (!m->build_context->is_dll && !has_win_main) {
// proc WinMain(inst, prev: rawptr, cmd_line: ^byte, cmd_show: i32) -> i32
@@ -7911,7 +7976,7 @@ void ir_gen_tree(irGen *s) {
m->entry_point_entity = e;
map_set(&m->values, hash_pointer(e), p);
map_set(&m->values, hash_entity(e), p);
map_set(&m->members, hash_string(name), p);
irProcedure *proc = &p->Proc;
@@ -7935,7 +8000,7 @@ void ir_gen_tree(irGen *s) {
Entity *e = make_entity_procedure(a, nullptr, make_token_ident(name), proc_type, 0);
irValue *p = ir_value_procedure(a, m, e, proc_type, nullptr, body, name);
map_set(&m->values, hash_pointer(e), p);
map_set(&m->values, hash_entity(e), p);
map_set(&m->members, hash_string(name), p);