浏览代码

Update Tilde

gingerBill 1 年之前
父节点
当前提交
d04039ba43
共有 6 个文件被更改,包括 76 次插入54 次删除
  1. 41 9
      src/tilde.cpp
  2. 3 1
      src/tilde.hpp
  3. 二进制
      src/tilde/tb.lib
  4. 15 0
      src/tilde_builtin.cpp
  5. 16 42
      src/tilde_proc.cpp
  6. 1 2
      src/tilde_stmt.cpp

+ 41 - 9
src/tilde.cpp

@@ -1,16 +1,42 @@
 #include "tilde.hpp"
 
 
-gb_global Slice<TB_Arena *> global_tb_arenas;
+struct CGArenaPair {
+	TB_Arena *arenas[2];
+};
+
+gb_global Slice<CGArenaPair> global_tb_arenas;
 
-gb_internal TB_Arena *cg_arena(void) {
-	return global_tb_arenas[current_thread_index()];
+gb_internal TB_Arena *cg_arena(int index) {
+	return global_tb_arenas[current_thread_index()].arenas[index];
 }
 
 gb_internal void cg_global_arena_init(void) {
-	global_tb_arenas = slice_make<TB_Arena *>(permanent_allocator(), global_thread_pool.threads.count);
-	for_array(i, global_tb_arenas) {
-		global_tb_arenas[i] = tb_arena_create(2ull<<20);
+	global_tb_arenas = slice_make<CGArenaPair>(permanent_allocator(), global_thread_pool.threads.count);
+	for (CGArenaPair &pair : global_tb_arenas) {
+		for (TB_Arena *&arena : pair.arenas) {
+			arena = tb_arena_create(1ull<<20);
+		}
+	}
+}
+
+
+gb_global Slice<TB_Worklist *> global_tb_worklists;
+
+gb_internal TB_Worklist *cg_worklist(void) {
+	return global_tb_worklists[current_thread_index()];
+}
+
+gb_internal void cg_global_worklist_init(void) {
+	global_tb_worklists = slice_make<TB_Worklist *>(permanent_allocator(), global_thread_pool.threads.count);
+	for (TB_Worklist *&worklist : global_tb_worklists) {
+		worklist = tb_worklist_alloc();
+	}
+}
+
+gb_internal void cg_global_worklist_destroy(void) {
+	for (TB_Worklist *worklist : global_tb_worklists) {
+		tb_worklist_free(worklist);
 	}
 }
 
@@ -248,7 +274,9 @@ gb_internal void cg_add_member(cgModule *m, String const &name, cgValue const &v
 gb_internal void cg_add_procedure_value(cgModule *m, cgProcedure *p) {
 	rw_mutex_lock(&m->values_mutex);
 	if (p->entity != nullptr) {
-		map_set(&m->procedure_values, p->func, p->entity);
+		if (p->func != nullptr) {
+			map_set(&m->procedure_values, p->func, p->entity);
+		}
 		if (p->symbol != nullptr) {
 			map_set(&m->symbols, p->entity, p->symbol);
 		}
@@ -729,6 +757,7 @@ gb_internal bool cg_generate_code(Checker *c, LinkerData *linker_data) {
 	#endif
 
 	cg_global_arena_init();
+	cg_global_worklist_init();
 
 	cgModule *m = cg_module_create(c);
 	defer (cg_module_destroy(m));
@@ -828,8 +857,11 @@ gb_internal bool cg_generate_code(Checker *c, LinkerData *linker_data) {
 			break;
 		}
 	}
-	TB_ExportBuffer export_buffer = tb_module_object_export(m->mod, cg_arena(), debug_format);
-	// defer (tb_export_buffer_free(export_buffer)); // THIS IS MISSING
+
+	TB_Arena *export_arena = tb_arena_create(0);
+	defer (tb_arena_destroy(export_arena));
+
+	TB_ExportBuffer export_buffer = tb_module_object_export(m->mod, export_arena, debug_format);
 
 	String filepath_obj = cg_filepath_obj_for_module(m, false);
 	array_add(&linker_data->output_object_paths, filepath_obj);

+ 3 - 1
src/tilde.hpp

@@ -167,6 +167,8 @@ struct cgProcedure {
 	TB_FunctionPrototype *proto;
 	TB_Symbol *symbol;
 
+	TB_Arena *arenas[2];
+
 	Entity *  entity;
 	cgModule *module;
 	String    name;
@@ -272,7 +274,7 @@ gb_global cgProcedure *cg_cleanup_runtime_proc = nullptr;
 
 
 
-gb_internal TB_Arena *cg_arena(void);
+gb_internal TB_Arena *cg_arena(int index=0);
 
 gb_internal cgProcedure *cg_procedure_create(cgModule *m, Entity *entity, bool ignore_body=false);
 gb_internal void cg_add_procedure_to_queue(cgProcedure *p);

二进制
src/tilde/tb.lib


+ 15 - 0
src/tilde_builtin.cpp

@@ -581,6 +581,21 @@ gb_internal cgValue cg_build_builtin(cgProcedure *p, BuiltinProcId id, Ast *expr
 			return cg_emit_arith(p, Token_Sub, size, cg_value(ones, n.type), n.type);
 		}
 
+	case BuiltinProc_volatile_load:
+		{
+			cgValue ptr = cg_build_expr(p, ce->args[0]);
+			ptr = cg_flatten_value(p, ptr);
+			GB_ASSERT(ptr.kind == cgValue_Value);
+			return cg_emit_load(p, ptr, true);
+		}
+
+	case BuiltinProc_volatile_store:
+		{
+			cgValue ptr = cg_build_expr(p, ce->args[0]);
+			cgValue val = cg_build_expr(p, ce->args[1]);
+			cg_emit_store(p, ptr, val, true);
+			return {};
+		}
 	}
 
 

+ 16 - 42
src/tilde_proc.cpp

@@ -96,6 +96,10 @@ gb_internal cgProcedure *cg_procedure_create(cgModule *m, Entity *entity, bool i
 
 	if (p->symbol == nullptr)  {
 		p->func = tb_function_create(m->mod, link_name.len, cast(char const *)link_name.text, linkage);
+		p->arenas[0] = tb_arena_create(0);
+		p->arenas[1] = tb_arena_create(0);
+
+		tb_function_set_arenas(p->func, p->arenas[0], p->arenas[1]);
 
 		p->debug_type = cg_debug_type_for_proc(m, p->type);
 		p->proto = tb_prototype_from_dbg(m->mod, p->debug_type);
@@ -105,6 +109,8 @@ gb_internal cgProcedure *cg_procedure_create(cgModule *m, Entity *entity, bool i
 
 	p->value = cg_value(p->symbol, p->type);
 
+	GB_ASSERT(p->symbol != nullptr || p->func != nullptr);
+
 	cg_add_symbol(m, entity, p->symbol);
 	cg_add_entity(m, entity, p->value);
 	cg_add_member(m, p->name, p->value);
@@ -225,6 +231,11 @@ gb_internal void cg_procedure_begin(cgProcedure *p) {
 	}
 
 	TB_ModuleSectionHandle section = tb_module_get_text(p->module->mod);
+	if (p->arenas[0] == nullptr) {
+		p->arenas[0] = tb_arena_create(0);
+		p->arenas[1] = tb_arena_create(0);
+	}
+	tb_function_set_arenas(p->func, p->arenas[0], p->arenas[1]);
 	tb_function_set_prototype(p->func, section, p->proto);
 
 	if (p->body == nullptr) {
@@ -370,48 +381,11 @@ gb_internal WORKER_TASK_PROC(cg_procedure_compile_worker_proc) {
 	cgProcedure *p = cast(cgProcedure *)data;
 	gb_unused(p);
 
-	// TB_Passes *opt = tb_pass_enter(p->func, cg_arena());
-	// defer (tb_pass_exit(opt));
-
-	// // optimization passes
-	// if (false) {
-	// 	tb_pass_peephole(opt, TB_PEEPHOLE_ALL);
-	// 	tb_pass_mem2reg(opt);
-	// 	tb_pass_peephole(opt, TB_PEEPHOLE_ALL);
-	// }
-
-	// bool emit_asm = false;
-	// if (
-	//     // string_starts_with(p->name, str_lit("runtime@_windows_default_alloc_or_resize")) ||
-	//     false
-	// ) {
-	// 	emit_asm = true;
-	// }
-
-	// // emit ir
-	// if (
-	//     // string_starts_with(p->name, str_lit("main@")) ||
-	//     false
-	// ) { // IR Printing
-	// 	TB_Arena *arena = cg_arena();
-	// 	TB_Passes *passes = tb_pass_enter(p->func, arena);
-	// 	defer (tb_pass_exit(passes));
-
-	// 	tb_pass_print(passes);
-	// 	fprintf(stdout, "\n");
-	// 	fflush(stdout);
-	// }
-	// if (false) { // GraphViz printing
-	// 	tb_pass_print_dot(opt, tb_default_print_callback, stdout);
-	// }
-
-	// // compile
-	// TB_FunctionOutput *output = tb_pass_codegen(opt, emit_asm);
-	// if (emit_asm) {
-	// 	tb_output_print_asm(output, stdout);
-	// 	fprintf(stdout, "\n");
-	// 	fflush(stdout);
-	// }
+	tb_print(p->func, cg_arena());
+	fprintf(stdout, "\n");
+
+	// bool preserve_types = true;
+	// tb_opt(p->func, cg_worklist(), preserve_types);
 
 	return 0;
 }

+ 1 - 2
src/tilde_stmt.cpp

@@ -1257,8 +1257,7 @@ gb_internal void cg_build_return_stmt(cgProcedure *p, Slice<Ast *> const &return
 			cg_append_tuple_values(p, &results, res);
 		}
 	} else {
-		for_array(i, tuple->variables) {
-			Entity *e = tuple->variables[i];
+		for (Entity *e : tuple->variables) {
 			cgAddr addr = map_must_get(&p->variable_map, e);
 			cgValue res = cg_addr_load(p, addr);
 			array_add(&results, res);