Browse Source

Basic global struct example

gingerBill 2 years ago
parent
commit
8ebb84ad1e
2 changed files with 30 additions and 5 deletions
  1. 1 1
      src/tilde/tb.h
  2. 29 4
      src/tilde_backend.cpp

+ 1 - 1
src/tilde/tb.h

@@ -632,7 +632,7 @@ struct TB_Arena {
 };
 };
 
 
 // allocates in 16MiB chunks and does linear allocation in 'em
 // allocates in 16MiB chunks and does linear allocation in 'em
-TB_Arena* tb_default_arena(void);
+TB_API TB_Arena* tb_default_arena(void);
 
 
 ////////////////////////////////
 ////////////////////////////////
 // Module management
 // Module management

+ 29 - 4
src/tilde_backend.cpp

@@ -19,8 +19,24 @@ bool tb_generate_code(Checker *c) {
 	defer (tb_module_destroy(m));
 	defer (tb_module_destroy(m));
 	tb_module_set_tls_index(m, "_tls_index");
 	tb_module_set_tls_index(m, "_tls_index");
 
 
+	TB_Arena *arena = tb_default_arena();
 
 
-	TB_Arena *arena = nullptr;
+	TB_Global *str = nullptr;
+	{
+		TB_Global *str_data = nullptr;
+		{
+			str_data = tb_global_create(m, "csb$1", nullptr, TB_LINKAGE_PRIVATE);
+			tb_global_set_storage(m, tb_module_get_rdata(m), str_data, 8, 1, 1);
+			void *region = tb_global_add_region(m, str_data, 0, 8);
+			memcpy(region, "Hellope\x00", 8);
+		}
+
+		str = tb_global_create(m, "global$str", nullptr, TB_LINKAGE_PRIVATE);
+		tb_global_set_storage(m, tb_module_get_rdata(m), str, 16, 8, 2);
+		tb_global_add_symbol_reloc(m, str, 0, cast(TB_Symbol *)str_data);
+		void *len = tb_global_add_region(m, str, 8, 8);
+		*cast(i64 *)len = 7;
+	}
 
 
 	{
 	{
 		TB_PrototypeParam printf_ret = {TB_TYPE_I32};
 		TB_PrototypeParam printf_ret = {TB_TYPE_I32};
@@ -34,9 +50,18 @@ bool tb_generate_code(Checker *c) {
 		tb_function_set_prototype(p, main_proto, arena);
 		tb_function_set_prototype(p, main_proto, arena);
 
 
 
 
-		TB_Node *params[2] = {};
-		params[0] = tb_inst_cstring(p, "Hellope %s!\n");
-		params[1] = tb_inst_cstring(p, "World");
+		auto str_ptr = tb_inst_get_symbol_address(p, cast(TB_Symbol *)str);
+		auto str_data_ptr_ptr = tb_inst_member_access(p, str_ptr, 0);
+		auto str_data_ptr = tb_inst_load(p, TB_TYPE_PTR, str_data_ptr_ptr, 1, false);
+		auto str_len_ptr = tb_inst_member_access(p, str_ptr, 8);
+		auto str_len = tb_inst_load(p, TB_TYPE_I64, str_len_ptr, 8, false);
+
+
+		TB_Node *params[4] = {};
+		params[0] = tb_inst_cstring(p, "%.*s %s!\n");
+		params[1] = tb_inst_trunc(p, str_len, TB_TYPE_I32);
+		params[2] = str_data_ptr;
+		params[3] = tb_inst_cstring(p, "World");
 		TB_MultiOutput output = tb_inst_call(p, printf_proto, tb_inst_get_symbol_address(p, cast(TB_Symbol *)printf_proc), gb_count_of(params), params);
 		TB_MultiOutput output = tb_inst_call(p, printf_proto, tb_inst_get_symbol_address(p, cast(TB_Symbol *)printf_proc), gb_count_of(params), params);
 		gb_unused(output);
 		gb_unused(output);