Repository: oriansj/M2-Planet
Pull request #94: Refactor named immediate loading and add `emit_load_named_immediate`
Author: login: gtker
Created: 2025-03-06T16:59:08Z | Merged: 2025-03-06T20:13:00Z

Adds an `emit_` function for named immediates that are resolved later.

I'm not sure if this satisifes https://github.com/oriansj/M2-Planet/issues/77, but I'm unsure of what the proper fix should be then. From what I can tell we won't be able to get "proper" support for large sizes like in https://github.com/oriansj/M2-Planet/blob/df82ddcd82a26eb604811dddb21051c8bb9f7bdb/cc_core.c#L271 without support in M1/hex(?) for when we have the proper calculated offset.

I'm also unsure if x86 should use `mov` or `lea` like is done for x86 variables
https://github.com/oriansj/M2-Planet/blob/df82ddcd82a26eb604811dddb21051c8bb9f7bdb/cc_core.c#L986

@stikonas @oriansj 


Diff:
diff --git a/cc_core.c b/cc_core.c
index fbbcc256..f2aee87a 100644
--- a/cc_core.c
+++ b/cc_core.c
@@ -160,6 +160,83 @@ char* number_to_hex(int a, int bytes)
 	return result;
 }
 
+void emit_load_named_immediate(int reg, char* prefix, char* name, char* note)
+{
+	char* reg_name = register_from_string(reg);
+	if((KNIGHT_POSIX == Architecture) || (KNIGHT_NATIVE == Architecture))
+	{
+		emit_out("LOADR R");
+		emit_out(reg_name);
+		emit_out(" 4\nJUMP 4\n&");
+		emit_out(prefix);
+		emit_out(name);
+	}
+	else if(X86 == Architecture)
+	{
+		emit_out("mov_");
+		emit_out(reg_name);
+		emit_out(", &");
+		emit_out(prefix);
+		emit_out(name);
+	}
+	else if(AMD64 == Architecture)
+	{
+		emit_out("lea_");
+		emit_out(reg_name);
+		emit_out(",[rip+DWORD] %");
+		emit_out(prefix);
+		emit_out(name);
+	}
+	else if(ARMV7L == Architecture)
+	{
+		emit_out("!0 ");
+		emit_out(reg_name);
+		emit_out(" LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&");
+		emit_out(prefix);
+		emit_out(name);
+	}
+	else if(AARCH64 == Architecture)
+	{
+		emit_out("LOAD_W");
+		/* Normal register starts with X for 64bit wide
+		 * but we need W. */
+		emit_out(reg_name + 1);
+		emit_out("_AHEAD\nSKIP_32_DATA\n&");
+		emit_out(prefix);
+		emit_out(name);
+	}
+	else if((RISCV32 == Architecture) || (RISCV64 == Architecture))
+	{
+		emit_out("rd_");
+		emit_out(reg_name);
+		emit_out(" ~");
+		emit_out(prefix);
+		emit_out(name);
+		emit_out(" auipc\n");
+
+		emit_out("rd_");
+		emit_out(reg_name);
+		emit_out(" rs1_");
+		emit_out(reg_name);
+		emit_out(" !");
+		emit_out(prefix);
+		emit_out(name);
+		emit_out(" addi");
+	}
+
+
+	if(note == NULL)
+	{
+		emit_out("\n");
+	}
+	else
+	{
+		emit_out(" # ");
+		emit_out(note);
+		emit_out("\n");
+	}
+}
+
 void emit_load_immediate(int reg, int value, char* note)
 {
 	char* reg_name = register_from_string(reg);
@@ -487,6 +564,17 @@ void uniqueID_out(char* s, char* num)
 	output_list = uniqueID(s, output_list, num);
 }
 
+char* create_unique_id(char* prefix, char* s, char* num)
+{
+	char* buf = calloc(MAX_STRING, sizeof(char));
+	int written = copy_string(buf, prefix, MAX_STRING);
+	written = copy_string(buf + written, s, MAX_STRING - written) + written;
+	written = copy_string(buf + written, "_", MAX_STRING - written) + written;
+	copy_string(buf + written, num, MAX_STRING - written);
+
+	return buf;
+}
+
 struct token_list* sym_declare(char *s, struct type* t, struct token_list* list)
 {
 	struct token_list* a = calloc(1, sizeof(struct token_list));
@@ -1032,48 +1120,13 @@ void function_load(struct token_list* a)
 		return;
 	}
 
-	if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&FUNCTION_");
-	else if(X86 == Architecture) emit_out("mov_eax, &FUNCTION_");
-	else if(AMD64 == Architecture) emit_out("lea_rax,[rip+DWORD] %FUNCTION_");
-	else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&FUNCTION_");
-	else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&FUNCTION_");
-	else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 ~FUNCTION_");
-	emit_out(a->s);
-	if(RISCV32 == Architecture)
-	{
-		emit_out(" auipc\n");
-		emit_out("rd_a0 rs1_a0 !FUNCTION_");
-		emit_out(a->s);
-		emit_out(" addi");
-	}
-	else if(RISCV64 == Architecture)
-	{
-		emit_out(" auipc\n");
-		emit_out("rd_a0 rs1_a0 !FUNCTION_");
-		emit_out(a->s);
-		emit_out(" addiw");
-	}
-	emit_out("\n");
+	emit_load_named_immediate(REGISTER_ZERO, "FUNCTION_", a->s, NULL);
 }
 
 void global_load(struct token_list* a)
 {
 	current_target = a->type;
-	if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&GLOBAL_");
-	else if(X86 == Architecture) emit_out("mov_eax, &GLOBAL_");
-	else if(AMD64 == Architecture) emit_out("lea_rax,[rip+DWORD] %GLOBAL_");
-	else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&GLOBAL_");
-	else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&GLOBAL_");
-	else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 ~GLOBAL_");
-	emit_out(a->s);
-	if((RISCV32 == Architecture) || (RISCV64 == Architecture))
-	{
-		emit_out(" auipc\n");
-		emit_out("rd_a0 rs1_a0 !GLOBAL_");
-		emit_out(a->s);
-		emit_out(" addi");
-	}
-	emit_out("\n");
+	emit_load_named_immediate(REGISTER_ZERO, "GLOBAL_", a->s, NULL);
 
 	require(NULL != global_token, "unterminated global load\n");
 	if(TRUE == Address_of) return;
@@ -1111,24 +1164,12 @@ void primary_expr_string(void)
 {
 	char* number_string = int2str(current_count, 10, TRUE);
 	current_count = current_count + 1;
-	if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&STRING_");
-	else if(X86 == Architecture) emit_out("mov_eax, &STRING_");
-	else if(AMD64 == Architecture) emit_out("lea_rax,[rip+DWORD] %STRING_");
-	else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&STRING_");
-	else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&STRING_");
-	else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 ~STRING_");
-	uniqueID_out(function->s, number_string);
-	if((RISCV32 == Architecture) || (RISCV64 == Architecture))
-	{
-		emit_out("auipc\n");
-		emit_out("rd_a0 rs1_a0 !STRING_");
-		uniqueID_out(function->s, number_string);
-		emit_out("addi\n");
-	}
+	char* unique_id = create_unique_id("STRING_", function->s, number_string);
+
+	emit_load_named_immediate(REGISTER_ZERO, "", unique_id, NULL);
 
 	/* The target */
-	strings_list = emit(":STRING_", strings_list);
-	strings_list = uniqueID(function->s, strings_list, number_string);
+	strings_list = emit("\n", emit(unique_id, emit(":", strings_list)));
 
 	/* catch case of just "foo" from segfaulting */
 	require(NULL != global_token->next, "a string by itself is not valid C\n");
@@ -1675,14 +1716,14 @@ void additive_expr_stub_a(void)
 	else if(X86 == Architecture)
 	{
 		arithmetic_recursion(postfix_expr, "imul_ebx\n", "mul_ebx\n", "*", additive_expr_stub_a);
-		arithmetic_recursion(postfix_expr, "xchg_ebx,eax\ncdq\nidiv_ebx\n", "xchg_ebx,eax\nmov_edx, %0\ndiv_ebx\n", "/", additive_expr_stub_a);
-		arithmetic_recursion(postfix_expr, "xchg_ebx,eax\ncdq\nidiv_ebx\nmov_eax,edx\n", "xchg_ebx,eax\nmov_edx, %0\ndiv_ebx\nmov_eax,edx\n", "%", additive_expr_stub_a);
+		arithmetic_recursion(postfix_expr, "xchg_ebx,eax\ncdq\nidiv_ebx\n", "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\n", "/", additive_expr_stub_a);
+		arithmetic_recursion(postfix_expr, "xchg_ebx,eax\ncdq\nidiv_ebx\nmov_eax,edx\n", "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\nmov_eax,edx\n", "%", additive_expr_stub_a);
 	}
 	else if(AMD64 == Architecture)
 	{
 		arithmetic_recursion(postfix_expr, "imul_rbx\n", "mul_rbx\n", "*", additive_expr_stub_a);
-		arithmetic_recursion(postfix_expr, "xchg_rbx,rax\ncqo\nidiv_rbx\n", "xchg_rbx,rax\nmov_rdx, %0\ndiv_rbx\n", "/", additive_expr_stub_a);
-		arithmetic_recursion(postfix_expr, "xchg_rbx,rax\ncqo\nidiv_rbx\nmov_rax,rdx\n", "xchg_rbx,rax\nmov_rdx, %0\ndiv_rbx\nmov_rax,rdx\n", "%", additive_expr_stub_a);
+		arithmetic_recursion(postfix_expr, "xchg_rbx,rax\ncqo\nidiv_rbx\n", "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\n", "/", additive_expr_stub_a);
+		arithmetic_recursion(postfix_expr, "xchg_rbx,rax\ncqo\nidiv_rbx\nmov_rax,rdx\n", "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\nmov_rax,rdx\n", "%", additive_expr_stub_a);
 	}
 	else if(ARMV7L == Architecture)
 	{
@@ -2077,12 +2118,12 @@ char* compound_operation(char* operator, int is_signed)
 		else if(X86 == Architecture)
 		{
 			if (is_signed) operation = "xchg_ebx,eax\ncdq\nidiv_ebx\n";
-			else operation = "xchg_ebx,eax\nmov_edx, %0\ndiv_ebx\n";
+			else operation = "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\n";
 		}
 		else if(AMD64 == Architecture)
 		{
 			if(is_signed) operation = "xchg_rbx,rax\ncqo\nidiv_rbx\n";
-			else operation = "xchg_rbx,rax\nmov_rdx, %0\ndiv_rbx\n";
+			else operation = "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\n";
 		}
 		else if(ARMV7L == Architecture)
 		{
@@ -2110,12 +2151,12 @@ char* compound_operation(char* operator, int is_signed)
 		else if(X86 == Architecture)
 		{
 			if(is_signed) operation = "xchg_ebx,eax\ncdq\nidiv_ebx\nmov_eax,edx\n";
-			else operation = "xchg_ebx,eax\nmov_edx, %0\ndiv_ebx\nmov_eax,edx\n";
+			else operation = "xchg_ebx,eax\nxor_edx,edx\ndiv_ebx\nmov_eax,edx\n";
 		}
 		else if(AMD64 == Architecture)
 		{
 			if(is_signed) operation = "xchg_rbx,rax\ncqo\nidiv_rbx\nmov_rax,rdx\n";
-			else operation = "xchg_rbx,rax\nmov_rdx, %0\ndiv_rbx\nmov_rax,rdx\n";
+			else operation = "xchg_rbx,rax\nxor_edx,edx\ndiv_rbx\nmov_rax,rdx\n";
 		}
 		else if(ARMV7L == Architecture)
 		{
diff --git a/test/test.answers b/test/test.answers
index b2d38932..0454758b 100644
--- a/test/test.answers
+++ b/test/test.answers
@@ -148,19 +148,19 @@ b16265094c3482b6f0c2bb6480a435ae80c08b5353cfa73ef3a28ea7923ad6cf  test/results/t
 9e94306c02135c925a5a57ff21a5d28e9555183cccb53ba1229e2b34e50f5e32  test/results/test0018-riscv64-binary
 14cd2ae514ba30f24990eeb9e458d83624b575eb3766e7a2a54f2dd61ec6685a  test/results/test0018-x86-binary
 a94f557137d8501e27f8acc464d8b1c47313dbacb68d83fb2116cc55723e0637  test/results/test0019-aarch64-binary
-6ee0d02adb52c2bf88a70cdd1592a752ba683c962a0b0b55c5fbcd89a0b01236  test/results/test0019-amd64-binary
+bc3162e72e3b6cf68358a22d75f7f11d1ccb8df427cb15892e5ff806f355fcee  test/results/test0019-amd64-binary
 c5d4f0906edb9cab803ec1a65b191441425d7dd8307bf12a02095c3a09221e06  test/results/test0019-armv7l-binary
 b8ac867d60d3e827106c39884806c504a686de2d55b76b24ee66d89e1fac7ea2  test/results/test0019-knight-posix-binary
 3754cc72f9cde79f7e9a42d42547c777edee04ed685e9ff0925a9392d83ed711  test/results/test0019-riscv32-binary
 5ee4e18878f26c9c4b66975590a46d56620b65cfef80debc0f6b50c4dcfe193c  test/results/test0019-riscv64-binary
-8a5d4551f13070773cfeeab41c717d28e515e81150ddf836f2abf5ddf6bccd8f  test/results/test0019-x86-binary
+055db80eba7d46342b26ea7c73597bf6c3ebc3ea88fe556d23f9bf426ef86855  test/results/test0019-x86-binary
 38efe0edb6842fbf73e352150ea9c84ad84d375dff61e1ed443e4f5fc340f921  test/results/test0020-aarch64-binary
 3c57d5bedab2b75ca913afffa91724daf0a8950ae07b993ab5ab410e46d11ebd  test/results/test0020-amd64-binary
 0e31dd12b14c64baa61adf37b4a4a76b04f2f8bd47a22a27be034c38aaaf3024  test/results/test0020-armv7l-binary
 4fe5e49c75fc8519c89412db2b55cc7b2d7057c88abcafcb6985f6070d1f4edf  test/results/test0020-knight-native-binary
 7d048c173047bd4ba4f9b3f37863f268ba8d6a130532917ba4138db5ea2eba5d  test/results/test0020-knight-posix-binary
 8b88fb0be1bcd6d64b5e83d4c499c30ea78108fab1848ee1a3abea0a38c8a7e6  test/results/test0020-riscv32-binary
-f3988b82dca8128b19a2086816efd009a856851bf2c97d11c5e8776951b89864  test/results/test0020-riscv64-binary
+3a61d1adc38c734cdf482a67638443c5dae61f7996bfe37894f56f28bf6ede81  test/results/test0020-riscv64-binary
 86f770ffaf5592dde2af3aa43497c1aec91328b63cfd076231b660b2d09aa8ee  test/results/test0020-x86-binary
 8a9da3b495bc2f1e96b05b8b36cbbe42e3d4d303c09f1c533faad70f4197502e  test/results/test0021-aarch64-binary
 704de9fe2ff7ea0ebbb0ee74a0436b145cdd3fe1e0fa55b04c3e5f6dec8aa337  test/results/test0021-amd64-binary
@@ -170,12 +170,12 @@ dbd3f599b6ae117351f0f5572e58c1cbb70dfc9fdaf433be04fac495f9ebbe8f  test/results/t
 05c8e6f9edbe0c125d35eb5b08e08c39ca881e6814d8e100168b67b5d0d5afcc  test/results/test0021-riscv64-binary
 add93e67e062569964f5b0b44eb795346f4cf2ca733dcbba8b09cb708bf96de8  test/results/test0021-x86-binary
 9a03182f8f8018775dcf8724909f5063b7bf42515f2e7c7cda2e0445dfef2f22  test/results/test0022-aarch64-binary
-257b7fc047d3cf35ee6c92ad5396e015406859fb1bf7f8294706009f6b3043ff  test/results/test0022-amd64-binary
+bba27db075e3e401e845760b8df29646c12efa9bac44f7ed4d1cac6bffb3b88f  test/results/test0022-amd64-binary
 ebd67f240046c6b2c8e17785858b493bea38cec7bcca9d9da9c86fac2fd97b24  test/results/test0022-armv7l-binary
 0634ffd9401d8cb93135c54330f710a5f2f0ba8a1f6bf9cf21ab1f552bc1093e  test/results/test0022-knight-posix-binary
 8cf66e59d5405e090460c83a49a32279d8887c7c7999152786cb39e8ca18cb77  test/results/test0022-riscv32-binary
 c5505f1f88b92b159ce9bf0b2651795e40346c4d5bddac0b5836dbc7b98cc3e0  test/results/test0022-riscv64-binary
-6c87f7fbef8dd216a22a8bfc7ef67ff36818f765e223ed4a147266dd93defd30  test/results/test0022-x86-binary
+505e266c6afbf96b1e21c917fbdae793dac7eafcfcb6fb4333e31bc6430cec51  test/results/test0022-x86-binary
 e3369f12623bbf2cb876bde382e253e0ec75b7b0b8a91408dfb80f7f85543309  test/results/test0023-aarch64-binary
 290d8fed51bc2fcd4fbfa61845d7c4a6a7bba76d1d20074a3e1473e67176c5cd  test/results/test0023-amd64-binary
 bff807456b43213e0d3b2cd737d71e52e9b99dd06d2333c497c4835f73d10791  test/results/test0023-armv7l-binary
@@ -233,12 +233,12 @@ a757165996f314400ad5456c307b6f797b44a20f80da2e3a86b3b421954c83c7  test/results/t
 af8682b8250efb2d23341cde5c4336052dba8268b4748cf553329fe2d7427e51  test/results/test0030-riscv64-binary
 0c7201db6db89ebc0a2ea5b54897df2389f8ae44f136e590005a1cda9b1b40d0  test/results/test0030-x86-binary
 574399a66c1154cf72688bb12da6737336a112267b5bce381a69ca3e84f161e3  test/results/test0031-aarch64-binary
-14b1e784576d0c9fb3d3e8412906e8c40b9bca637d44263cf584692073653503  test/results/test0031-amd64-binary
+9beb849bc5048291566c6dae370476bd56f20f92a92c3643880e0a745c350443  test/results/test0031-amd64-binary
 226d6616e16f615be48d84049379d0144f3cbd0c90caeb677daa2daf165f4e93  test/results/test0031-armv7l-binary
 4d4f0498ae8aef927babfe62a9890508b0e77a97fd75a41f4d3924e047428d2e  test/results/test0031-knight-posix-binary
 5626dbef194a3b536d03014e8b81a87f9db6174818a055485b5f7a9b08684a6c  test/results/test0031-riscv32-binary
 c2a26cd8e37b093e0426556163893b10bb63414418e95161c079e89e0e6e95e3  test/results/test0031-riscv64-binary
-87f81a70234bd7ee39d26186487c75a08fed6c558bc931e185c6587fdfc4c141  test/results/test0031-x86-binary
+969dc2706046643c22bdf149a2a9683c6e975d1d1aaa98a8d39264158e8be8e8  test/results/test0031-x86-binary
 ae8eded71d77522cdb50bab88b7cb4e66152366039f54d35b363c5905850c62e  test/results/test0032-aarch64-binary
 5fc6532bf90acac0585b798449ef91e90cbfd6972bebe9d8672e2d9d41103ecc  test/results/test0032-amd64-binary
 fbc7ec844a8db354056545e786375be87f704e09191f3809b7b888874496c47f  test/results/test0032-armv7l-binary
@@ -331,19 +331,19 @@ e1c1642505d0a419db7b6fd5643b8dd74032e95768127976e345ce4238b8272c  test/results/t
 876b8a2e9456ce84e955ae48572dae9386c601666782ca12e83dc446aa6cda2f  test/results/test0100-riscv64-binary
 9785db540ba3e4b5eae5e6aeefb74f492ad9078aa62f8b726a49ade6e346ca41  test/results/test0100-x86-binary
 0f70075c8a384735b1532a7ac223040896d85f79464a1fa129e617c654c51c9c  test/results/test0101-aarch64-binary
-8ec27631974aa40661c273637bb8c44aa5a862745643a89ceeb83609439d2a4e  test/results/test0101-amd64-binary
+9c68c1a41b039c14ccbc20c80c379cc8b97a59c396711ceb3252fbdf4b550568  test/results/test0101-amd64-binary
 9b10fa4fe6495ae76277c6f1be63c84503452ac293316927cc05bb5f882af125  test/results/test0101-armv7l-binary
 4d23959f4d28e1e391446717cca6120ad6af9cc4e43bcadf8ee6924ee96692ab  test/results/test0101-knight-posix-binary
 c84ae0e0a25b084a215fcb73a427322e310313cd9ece6851f1b45acb5dfe4866  test/results/test0101-riscv32-binary
 9cbdac7f6703b92d695d4b72ea58ac290dbe1d1b2a25b84e4219cbb1648e04b0  test/results/test0101-riscv64-binary
-fcdd0bbbd27c1d9dd6188ed4d90b0b8b484dabcaa42dd1f071c8d7856a0103f4  test/results/test0101-x86-binary
+99a7dab467bb43cc49897c4b4fc8910c791e2aabaa21ac972b7337eed73446aa  test/results/test0101-x86-binary
 df0a678f624fdfec5a7a162ea899341a5f46be22dec1724208959a6afb5d8be5  test/results/test0102-aarch64-binary
-b29a0656e6b05d84a2898a6edee1770036a5cf6862540e010cd1c1bffa5e4ba1  test/results/test0102-amd64-binary
+5061c61e3124a1721202e1b35be2372af496eb5652b1b75b17618c70bbe2b09c  test/results/test0102-amd64-binary
 28f527706c40f53fb9617c931cf3a43f3bba6261f3d38c74cda4620cb6ea7925  test/results/test0102-armv7l-binary
 a5a1f95dbb0a71bc004cf35ddfeeda728768fe4f13467c8f3fb757e828cb6a94  test/results/test0102-knight-posix-binary
 82821411c4e9e142409f6011210681bb2659422ba5e2c82fe637720d4810a2fc  test/results/test0102-riscv32-binary
 08725aa9b5801f5afe0051e7a71f155f7cb8f618c40a6333ac9561edb279917e  test/results/test0102-riscv64-binary
-24743e87ddd0533ac9d68914bd9c1c131fd00c8532a46ca225e5f5044a058f25  test/results/test0102-x86-binary
+4a8e3223aded8ac695b432c23c4d06fb286462d3ba21f146401696919705e3d6  test/results/test0102-x86-binary
 f9c474fe5ae30793cb6a8da14fed5996bab81283898f69d17daa696a63eab8ef  test/results/test0103-aarch64-binary
 1d619c2331a01d6946f541e7ae01219e83fdd9a57d9d31c75e7ee412f80a7409  test/results/test0103-amd64-binary
 f98b3dc1542b5ea8d718ec9a39d27bc00900f1158ff202ac6a52c6e646430ff5  test/results/test0103-armv7l-binary
@@ -352,17 +352,17 @@ f98b3dc1542b5ea8d718ec9a39d27bc00900f1158ff202ac6a52c6e646430ff5  test/results/t
 d287d272327c4941d6b7d4b399a23d0ad23a464d4c6a024214633c57436911f2  test/results/test0103-riscv64-binary
 de4a74ad3f56ec1d7d23792abc8f44286927fbd1d71de88b48a08d8083ec8be0  test/results/test0103-x86-binary
 5c4c7e4b8dfb01958be405bf505e9d473a7bdfc3d1286e296fea87cd787d4b93  test/results/test0104-aarch64-binary
-6b4968617d006bf419c26ecd46ff84d217f23b50d6329237beff5734b36d345c  test/results/test0104-amd64-binary
+590975754d7431518ba3cc72a80bd048a5c1d0bc13a442befc543812be281ddb  test/results/test0104-amd64-binary
 fad55c4bda369902600f2f0abc9fa069f6c43bb70dd9e5a0da7ac0fe24669ace  test/results/test0104-armv7l-binary
 98ff0eeb2c9231212e6f62aa6c19887959fa2ea1504715f15035a2a16ed3ad1a  test/results/test0104-riscv32-binary
 ee7e4c0c211ced25cb47e77c344fe0b30f51634addf4cb01e2db904ecc22f441  test/results/test0104-riscv64-binary
-c43bec7d4b403a174d30c63c21a387bfcacbec7726a255553ccd138ed64382ff  test/results/test0104-x86-binary
+5fcd4bae9fea22e4b35e112eaf5c505de27b53bfcbbbe9c276c30e34abcbe0d4  test/results/test0104-x86-binary
 8973bea936deec7e5291932dc65840484507d4e7eea436e9afae6f3d52b2c618  test/results/test0105-aarch64-binary
-20f38e9d01457a41b011e7eae722b5f2150212a5ab80088757e6a4bf8ec988c4  test/results/test0105-amd64-binary
+6639b6cb30fe7ac5aa795f3c69c3699782b4b0937c0b7a996358206c46672800  test/results/test0105-amd64-binary
 3f52cf1adc1fb9348175f249ce8875170fe864d4f4785ddfb137e7a3d3528f05  test/results/test0105-armv7l-binary
 9bd9dad0e50e37495301476399b880ae03385442c94422c7646e41e7a16e0b14  test/results/test0105-riscv32-binary
-c05a936df9f35f13c0ef3db9afaf78ef899d72690df5803312e8c5d305ac174f  test/results/test0105-riscv64-binary
-515c05190877fbcb694bea5870b3acf42c5121acfed03fd83c223b0f421771d0  test/results/test0105-x86-binary
+c6fb9bef6aa54b3bb8e52a9377af9d12390af08d5715fbef3977356e8c257ff2  test/results/test0105-riscv64-binary
+70d962c15c723de970ed60d84fdf262e0534add42eeaed177b7a6ccffe111d4d  test/results/test0105-x86-binary
 9143b5676421cea6e31158850c1701df6c190adddc5b64769a691272e1560442  test/results/test0106-aarch64-binary
 dcae09a0e5b02cf248facff8e2547a2bbfee24b70a3ac6243a559bbe7d417c9e  test/results/test0106-amd64-binary
 a5fa9451726a243c746b1f718a135db17435425bdd475c5e8cafa893a2c10ab2  test/results/test0106-armv7l-binary
@@ -371,10 +371,10 @@ b7b1964de162ae6c3a371291dc3992f1581282abf9eff3b9c373eadfc3b862ea  test/results/t
 c9c1cbf8cccf87a3638d4551b28df147384be3dcb70c0bea8d114a6cdbe54ad4  test/results/test0106-riscv32-binary
 e2feb05120acfb41ee28f42efc95d1e58268ba6ff25ba19932a40747fb636739  test/results/test0106-riscv64-binary
 30ab073b31ab8d4901bb69fafe2817e6b98d97ca5d1e912594c70fcacaf93fd1  test/results/test0106-x86-binary
-9c909660d1fbcc8ab516d288815967bb1f608403d79b028afce951b984670190  test/results/test1000-aarch64-binary
-5fdeef8a8cdb0e4fc4ec6d81060149d0fbc02fe575bc968b382ea989d2ec5080  test/results/test1000-amd64-binary
-313373f81dfed3b10b5a60975489546c1f1b1d052159358352871462bdfecf47  test/results/test1000-armv7l-binary
-5d1cb253242ead2d1b738f89f50fa652d9b4c311c4fa56964316fd78d8ff7b3e  test/results/test1000-knight-posix-binary
-192a3535e60e27cb32cc588b61d5373b434d8b9f588b25037f65e8bcc44f40e4  test/results/test1000-riscv32-binary
-30f724f7c35757d21e880e51eeebcef7c17fddebae2aa2c7dd696b81023dc97c  test/results/test1000-riscv64-binary
-edcfde44d420908a90650b3e133993a58a25e524a2e8b7a88f8e9e320f867eb3  test/results/test1000-x86-binary
+e83a8932758921946cbd56aa7db95a94c7753c8519977a9b7ed24d0547863fe1  test/results/test1000-aarch64-binary
+528f00e27b5e0325ea105a9e58b372ddd5491e82b13442fc523f95545c0f5ced  test/results/test1000-amd64-binary
+10ebe1d31bd58839b46c184d872734f7b181a1de906db77d99b093ffe83d37b7  test/results/test1000-armv7l-binary
+0bcbb7bb63bb66882e0a1fcae80e2a4972a13d985d2e44a5bf38461642d2cfac  test/results/test1000-knight-posix-binary
+4761392335ce2e708e527fdaa699485971016eaeb2c1a7b9a3feb8ef7c8f759d  test/results/test1000-riscv32-binary
+dca631cff4d4aa831abb3d71bd02985d69a2e2ac9321933d21fa9b8da1cce6c9  test/results/test1000-riscv64-binary
+4a0eac814fbdb10623f088ab84741bdc7a582ca5d7420f1cca025282334021f1  test/results/test1000-x86-binary
diff --git a/test/test1000/proof.answer b/test/test1000/proof.answer
index f183c7c2..d806be71 100644
--- a/test/test1000/proof.answer
+++ b/test/test1000/proof.answer
@@ -1 +1 @@
-31663fad39273ee75dda3a8c93577724d2691c1e6c58611860cd57f66accb84f  test/test1000/proof
+eab8910fe52997de0110a8a83fc26e3843fcd0b4712ff11e683d2f427f768107  test/test1000/proof


Reviews:
user: login: stikonas
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-06T19:48:30Z
user: login: gtker
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-06T20:03:01Z
user: login: stikonas
state: COMMENTED
author_association: COLLABORATOR
submitted_at: 2025-03-06T20:07:24Z

Review comments:
diff_hunk: @@ -1174,24 +1185,12 @@ void primary_expr_string(void)
 {
 	char* number_string = int2str(current_count, 10, TRUE);
 	current_count = current_count + 1;
-	if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&STRING_");
-	else if(X86 == Architecture) emit_out("mov_eax, &STRING_");
-	else if(AMD64 == Architecture) emit_out("lea_rax,[rip+DWORD] %STRING_");
-	else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&STRING_");
-	else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&STRING_");
-	else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 ~STRING_");
-	uniqueID_out(function->s, number_string);
-	if((RISCV32 == Architecture) || (RISCV64 == Architecture))
-	{
-		emit_out("auipc\n");
-		emit_out("rd_a0 rs1_a0 !STRING_");
-		uniqueID_out(function->s, number_string);
-		emit_out("addi\n");
-	}
+	char* unique_id = create_unique_id("STRING_", function->s, number_string);
+
+	emit_load_named_immediate(REGISTER_ZERO, "", unique_id, NULL);
 
 	/* The target */
-	strings_list = emit(":STRING_", strings_list);
-	strings_list = uniqueID(function->s, strings_list, number_string);
+	strings_list = emit("\n", emit(unique_id, emit(":", strings_list)));
path: cc_core.c
user: login: stikonas
body: Could probably add `free(unique_id)` here...
created_at: 2025-03-06T19:48:29Z
author_association: COLLABORATOR
diff_hunk: @@ -1174,24 +1185,12 @@ void primary_expr_string(void)
 {
 	char* number_string = int2str(current_count, 10, TRUE);
 	current_count = current_count + 1;
-	if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&STRING_");
-	else if(X86 == Architecture) emit_out("mov_eax, &STRING_");
-	else if(AMD64 == Architecture) emit_out("lea_rax,[rip+DWORD] %STRING_");
-	else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&STRING_");
-	else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&STRING_");
-	else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 ~STRING_");
-	uniqueID_out(function->s, number_string);
-	if((RISCV32 == Architecture) || (RISCV64 == Architecture))
-	{
-		emit_out("auipc\n");
-		emit_out("rd_a0 rs1_a0 !STRING_");
-		uniqueID_out(function->s, number_string);
-		emit_out("addi\n");
-	}
+	char* unique_id = create_unique_id("STRING_", function->s, number_string);
+
+	emit_load_named_immediate(REGISTER_ZERO, "", unique_id, NULL);
 
 	/* The target */
-	strings_list = emit(":STRING_", strings_list);
-	strings_list = uniqueID(function->s, strings_list, number_string);
+	strings_list = emit("\n", emit(unique_id, emit(":", strings_list)));
path: cc_core.c
user: login: gtker
body: Wouldn't that lead to use after free? I'm guessing `strings_list` would need to use it to print it?
created_at: 2025-03-06T20:03:01Z
author_association: COLLABORATOR
diff_hunk: @@ -1174,24 +1185,12 @@ void primary_expr_string(void)
 {
 	char* number_string = int2str(current_count, 10, TRUE);
 	current_count = current_count + 1;
-	if((KNIGHT_NATIVE == Architecture) || (KNIGHT_POSIX == Architecture)) emit_out("LOADR R0 4\nJUMP 4\n&STRING_");
-	else if(X86 == Architecture) emit_out("mov_eax, &STRING_");
-	else if(AMD64 == Architecture) emit_out("lea_rax,[rip+DWORD] %STRING_");
-	else if(ARMV7L == Architecture) emit_out("!0 R0 LOAD32 R15 MEMORY\n~0 JUMP_ALWAYS\n&STRING_");
-	else if(AARCH64 == Architecture) emit_out("LOAD_W0_AHEAD\nSKIP_32_DATA\n&STRING_");
-	else if((RISCV32 == Architecture) || (RISCV64 == Architecture)) emit_out("rd_a0 ~STRING_");
-	uniqueID_out(function->s, number_string);
-	if((RISCV32 == Architecture) || (RISCV64 == Architecture))
-	{
-		emit_out("auipc\n");
-		emit_out("rd_a0 rs1_a0 !STRING_");
-		uniqueID_out(function->s, number_string);
-		emit_out("addi\n");
-	}
+	char* unique_id = create_unique_id("STRING_", function->s, number_string);
+
+	emit_load_named_immediate(REGISTER_ZERO, "", unique_id, NULL);
 
 	/* The target */
-	strings_list = emit(":STRING_", strings_list);
-	strings_list = uniqueID(function->s, strings_list, number_string);
+	strings_list = emit("\n", emit(unique_id, emit(":", strings_list)));
path: cc_core.c
user: login: stikonas
body: argh, yes, this is emit, not emit_out....
created_at: 2025-03-06T20:07:23Z
author_association: COLLABORATOR

PR comments:
user: login: stikonas
created_at: 2025-03-06T19:31:43Z
body: `auipc` should be sufficient for labels.
author_association: COLLABORATOR
user: login: stikonas
created_at: 2025-03-06T19:36:13Z
body: >  I'm also unsure if x86 should use mov or lea like is done for x86 variables

How can you even use lea there? I don't think we have lea relative to `rip` on x86 at all. Hence why only AMD64 binaries built by M2-Planet are relocatable and x86 aren't.
author_association: COLLABORATOR