Browse Source

Fix `context` initialization

gingerBill 6 years ago
parent
commit
ce2e23849e
7 changed files with 28 additions and 31 deletions
  1. 7 2
      core/mem/alloc.odin
  2. 1 1
      core/os/os_essence.odin
  3. 1 1
      core/os/os_linux.odin
  4. 1 1
      core/os/os_osx.odin
  5. 1 1
      core/os/os_windows.odin
  6. 13 8
      core/runtime/core.odin
  7. 4 17
      src/ir.cpp

+ 7 - 2
core/mem/alloc.odin

@@ -25,6 +25,7 @@ Allocator :: struct {
 
 alloc :: inline proc(size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
 	if size == 0 do return nil;
+	if allocator.procedure == nil do return nil;
 	return allocator.procedure(allocator.data, Allocator_Mode.Alloc, size, alignment, nil, 0, 0, loc);
 }
 
@@ -35,11 +36,15 @@ free :: inline proc(ptr: rawptr, allocator := context.allocator, loc := #caller_
 }
 
 free_all :: inline proc(allocator := context.allocator, loc := #caller_location) {
-	allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc);
+	if allocator.procedure != nil {
+		allocator.procedure(allocator.data, Allocator_Mode.Free_All, 0, 0, nil, 0, 0, loc);
+	}
 }
 
 resize :: inline proc(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGNMENT, allocator := context.allocator, loc := #caller_location) -> rawptr {
-	assert(allocator.procedure != nil);
+	if allocator.procedure == nil {
+		return nil;
+	}
 	if new_size == 0 {
 		free(ptr, allocator, loc);
 		return nil;

+ 1 - 1
core/os/os_essence.odin

@@ -71,7 +71,7 @@ stdin  := Handle(-1); // Not implemented
 stdout := Handle(0);
 stderr := Handle(0);
 
-current_thread_id :: proc() -> int {
+current_thread_id :: proc "contextless" () -> int {
 	return OSGetThreadID(Handle(0x1000));
 }
 

+ 1 - 1
core/os/os_linux.odin

@@ -245,7 +245,7 @@ exit :: proc(code: int) -> ! {
 	_unix_exit(code);
 }
 
-current_thread_id :: proc() -> int {
+current_thread_id :: proc "contextless" () -> int {
 	// return int(_unix_gettid());
 	return 0;
 }

+ 1 - 1
core/os/os_osx.odin

@@ -260,7 +260,7 @@ exit :: inline proc(code: int) -> ! {
 }
 
 
-current_thread_id :: proc() -> int {
+current_thread_id :: proc "contextless" () -> int {
 	// return int(_unix_gettid());
 	return 0;
 }

+ 1 - 1
core/os/os_windows.odin

@@ -251,7 +251,7 @@ exit :: proc(code: int) -> ! {
 
 
 
-current_thread_id :: proc() -> int {
+current_thread_id :: proc "contextless" () -> int {
 	return int(win32.get_current_thread_id());
 }
 

+ 13 - 8
core/runtime/core.odin

@@ -190,12 +190,11 @@ Assertion_Failure_Proc :: #type proc(prefix, message: string, loc: Source_Code_L
 Context :: struct {
 	allocator:      mem.Allocator,
 	temp_allocator: mem.Allocator,
-	thread_id:  int,
-
-	assertion_failure_proc:  Assertion_Failure_Proc,
-
+	assertion_failure_proc: Assertion_Failure_Proc,
 	logger: log.Logger,
 
+	thread_id:  int,
+
 	user_data:  any,
 	user_index: int,
 
@@ -320,11 +319,17 @@ __init_context_from_ptr :: proc "contextless" (c: ^Context, other: ^Context) {
 __init_context :: proc "contextless" (c: ^Context) {
 	if c == nil do return;
 
-	c.allocator = os.heap_allocator();
-	c.temp_allocator = mem.scratch_allocator(&global_scratch_allocator_data);
-	c.thread_id = os.current_thread_id();
+	c.allocator.procedure = os.heap_allocator_proc;
+	c.allocator.data = nil;
+
+	c.temp_allocator.procedure = mem.scratch_allocator_proc;
+	c.temp_allocator.data = &global_scratch_allocator_data;
+
+	c.thread_id = os.current_thread_id(); // NOTE(bill): This is "contextless" so it is okay to call
 	c.assertion_failure_proc = default_assertion_failure_proc;
-	c.logger = log.nil_logger();
+
+	c.logger.procedure = log.nil_logger_proc;
+	c.logger.data = nil;
 }
 
 @(builtin)

+ 4 - 17
src/ir.cpp

@@ -1759,22 +1759,7 @@ irValue *ir_find_or_generate_context_ptr(irProcedure *proc) {
 	irValue *c = ir_add_local_generated(proc, t_context);
 	ir_push_context_onto_stack(proc, c);
 	ir_emit_store(proc, c, ir_emit_load(proc, proc->module->global_default_context));
-
-
-#if 1
-	Array<irValue *> args = {};
-	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 0), ir_emit_package_call(proc, "os", "heap_allocator", args));
-	// 1 will be handled later
-	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 2), ir_emit_package_call(proc, "os", "current_thread_id", args));
-	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 3), ir_get_package_value(proc->module, str_lit("runtime"), str_lit("default_assertion_failure_proc")));
-	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 4), ir_emit_package_call(proc, "log", "nil_logger", args));
-
-	array_init(&args, heap_allocator(), 1);
-	args[0] = ir_get_package_value(proc->module, str_lit("runtime"), str_lit("global_scratch_allocator_data"));
-	ir_emit_store(proc, ir_emit_struct_ep(proc, c, 1), ir_emit_package_call(proc, "mem", "scratch_allocator", args));
-#else
 	ir_emit_init_context(proc, c);
-#endif
 	return c;
 }
 
@@ -2178,7 +2163,11 @@ void ir_addr_store(irProcedure *proc, irAddr const &addr, irValue *value) {
 			return;
 		}
 	} else if (addr.kind == irAddr_Context) {
+		irValue *old = ir_emit_load(proc, ir_find_or_generate_context_ptr(proc));
 		irValue *next = ir_add_local_generated(proc, t_context);
+		ir_emit_store(proc, next, old);
+		ir_push_context_onto_stack(proc, next);
+
 		if (addr.ctx.sel.index.count > 0) {
 			irValue *lhs = ir_emit_deep_field_gep(proc, next, addr.ctx.sel);
 			irValue *rhs = ir_emit_conv(proc, value, type_deref(ir_type(lhs)));
@@ -2189,8 +2178,6 @@ void ir_addr_store(irProcedure *proc, irAddr const &addr, irValue *value) {
 			ir_emit_store(proc, lhs, rhs);
 		}
 
-		ir_push_context_onto_stack(proc, next);
-
 		return;
 	}