]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added code to define variables
authorMichael D. Lowis <mike.lowis@gentex.com>
Thu, 22 Apr 2021 20:45:18 +0000 (16:45 -0400)
committerMichael D. Lowis <mike.lowis@gentex.com>
Thu, 22 Apr 2021 20:45:18 +0000 (16:45 -0400)
cerise/cerise.h
cerise/codegen.c
cerise/parser.c
cerise/tests/Module.s

index 98bfe079f0c95729e6c101cb105fec3667e40615..b110dbc619db8dff517dec5ddeb78363e2cb5107 100644 (file)
@@ -152,6 +152,7 @@ void codegen_setint(Item* item, Type* type, long long val);
 void codegen_setreal(Item* item, double val);
 void codegen_setstr(Item* item, char* val);
 
+void codegen_var(char* name, Type* type);
 void codegen_main(char* modname);
 void codegen_startproc(char* name, long long localsz);
 void codegen_endproc(void);
index 08848eff86ae6669e9f28379f2b5fd51000268a5..18f8c4fa8054afd54e673f052f146e56ddcfd3be 100644 (file)
@@ -4,12 +4,12 @@
 
 Type BoolType = {
     .form = FORM_BOOL,
-    .size = sizeof(int)
+    .size = sizeof(bool)
 };
 
 Type IntType = {
     .form = FORM_INT,
-    .size = sizeof(int)
+    .size = sizeof(long)
 };
 
 Type RealType = {
@@ -186,11 +186,29 @@ static void const_unop(int op, Item* a)
     }
 }
 
+void codegen_var(char* name, Type* type)
+{
+    if (type->form == FORM_BOOL || type->form == FORM_INT)
+    {
+        printf(".data\n");
+        printf("%s:\n", name);
+        printf("    .zero %d\n\n", type->size);
+    }
+    else if (type->form == FORM_REAL)
+    {
+    }
+    else
+    {
+        assert(!"not supported");
+    }
+}
+
 void codegen_main(char* modname)
 {
     codegen_startproc("main", 0);
 //    op_l  (OP_CALL, modname);
     printf("    call     %s\n", modname);
+    printf("    xor      %%rax, %%rax\n");
     codegen_endproc();
 }
 
index c315d8d5f57e4f7a14f024a875de21a74967765e..58e01afd2eb9fd832513551c41e214e70be32171 100644 (file)
@@ -480,6 +480,7 @@ RULE(var_decl)
         for (int i = 0; i < nsyms; i++)
         {
             sym->type = type->type;
+            codegen_var(sym->name, sym->type);
             sym = sym->next;
         }
 
index 91f16b4099dab4ba492949a62de7f972cfd107d4..72c4981fce30e54120108b0fbb9713136f070de4 100644 (file)
@@ -1,3 +1,11 @@
+.data
+a:
+    .zero 1
+
+.data
+b:
+    .zero 8
+
     .text
     .globl Module
 Module:
@@ -19,6 +27,7 @@ main:
     movq     %rsp, %rbp
 
     call     Module
+    xor      %rax, %rax
 
     pop      %rbp
     ret