From 8e1237686fb9e03bf3cb9a4289809f2179ff18c7 Mon Sep 17 00:00:00 2001
From: Kyle Gunger <kgunger12@gmail.com>
Date: Wed, 14 Feb 2024 01:35:18 -0500
Subject: mul

---
 compiler.c | 42 +++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 41 insertions(+), 1 deletion(-)

diff --git a/compiler.c b/compiler.c
index 115f445..1e72cd4 100644
--- a/compiler.c
+++ b/compiler.c
@@ -1057,7 +1057,7 @@ void _var_op_set_ptr(CompData *out, Variable *store, Variable *from) {
 char *_var_get_store(CompData *out, Variable *store) {
 	if (_var_ptr_type(store) == PTYPE_REF){
 		vect_push_string(&out->text, "\tmov rdi, ");
-		vect_push_free_string(&out->text, _op_get_location(store));
+		addvect_push_free_string(&out->text, _op_get_location(store));
 		vect_push_string(&out->text, " ; pre-deref for inbuilt mov (store)\n");
 
 		for(size_t i = store->ptr_chain.count - 1; i > 0; i--){
@@ -1489,8 +1489,48 @@ void var_op_mul(CompData *out, Variable *base, Variable *mul) {
 
 	if(base->type->name[0] == 'i') {
 		// Integer multiplication
+		if (base->location > 0 && mul->location != LOC_LITL) {
+			vect_push_string(&out->text, "\timul ");
+			vect_push_free_string(&out->text, _var_get_store(out, base));
+			vect_push_string(&out->text, ", ");
+			vect_push_free_string(&out->text, _var_get_from(out, base, mul));
+			vect_push_string(&out->text, "; complete mul\n\n");
+		} else {
+			// Mov to rax for the multiplication, move back after.
+			vect_push_string(&out->text, "\tmov ");
+			vect_push_free_string(&out->text, _op_get_register(1, _var_size(base)));
+			vect_push_string(&out->text, ", ");
+			vect_push_free_string(&out->text, _var_get_store(out, base));
+			vect_push_string(&out->text, "; pre-mul mov\n");
+
+			vect_push_string(&out->text, "\timul ");
+			vect_push_free_string(&out->text, _var_get_from(out, base, mul));
+			vect_push_string(&out->text, "; mul\n");
+			
+			// move back after mul
+			vect_push_string(&out->text, "\tmov ");
+			vect_push_free_string(&out->text, _var_get_store(out, base));
+			vect_push_string(&out->text, ", ");
+			vect_push_free_string(&out->text, _op_get_register(1, _var_size(base)));
+			vect_push_string(&out->text, "; post-mul mov\n");
+		}
 	} else {
+		vect_push_string(&out->text, "\tmov ");
+		vect_push_free_string(&out->text, _op_get_register(1, _var_size(base)));
+		vect_push_string(&out->text, ", ");
+		vect_push_free_string(&out->text, _var_get_store(out, base));
+		vect_push_string(&out->text, "; pre-mul mov\n");
+
 		vect_push_string(&out->text, "\tmul ");
+		vect_push_free_string(&out->text, _var_get_from(out, base, mul));
+		vect_push_string(&out->text, "; mul\n");
+		
+		// move back after mul
+		vect_push_string(&out->text, "\tmov ");
+		vect_push_free_string(&out->text, _var_get_store(out, base));
+		vect_push_string(&out->text, ", ");
+		vect_push_free_string(&out->text, _op_get_register(1, _var_size(base)));
+		vect_push_string(&out->text, "; post-mul mov\n");
 	}
 }
 
-- 
cgit v1.2.3