]> git.mdlowis.com Git - proto/obnc.git/commitdiff
fleshed out more kinds of operations
authormike lowis <mike@mdlowis.com>
Tue, 27 Apr 2021 03:55:04 +0000 (23:55 -0400)
committermike lowis <mike@mdlowis.com>
Tue, 27 Apr 2021 03:55:04 +0000 (23:55 -0400)
cerise/codegen.c
cerise/parser.c
cerise/tests/Module.m
cerise/tests/Module.s

index c3b1636ceee76fe59bfbc1b33edf6499ea0af9cd..8e2f452f7eac8fffb28b4bdcf92fd9b8f9407436 100644 (file)
@@ -238,12 +238,11 @@ static void load_var(Parser* p, Item* item)
     switch (item->mode)
     {
         case ITEM_CONST:
-            puts("BAR");
             /* TODO: range check the constant */
-            // emit instruction
-            item->reg = p->curr_reg;
+//            // emit instruction
+//            item->reg = p->curr_reg;
 
-            printf("movq %s, $%lld\n", curr_regname(p), item->imm.i);
+            printf("    movq    %s, $%lld\n", curr_regname(p), item->imm.i);
             item->reg = p->curr_reg;
             next_reg(p);
             break;
@@ -350,19 +349,27 @@ void codegen_unop(Parser* p, int op, Item* a)
     }
 }
 
+static char* ImmOps[] = {
+    ['+'] = "addq",
+};
+
 static void imm_binop(Parser* p, int op, Item* a, Item* b)
 {
     if (a->type->form == FORM_INT || a->type->form == FORM_BOOL)
     {
-        switch (op)
+        char* opstring = ImmOps[op];
+        if (a->mode == ITEM_CONST)
         {
-            case '+':
-                printf("    addq    $%lld, %s\n", b->imm.i, regname(a->reg));
-                break;
-
-            default:
-                assert(!"not implemented");
-                break;
+//            printf("    %s    $%lld, %s\n",
+//                opstring, regname(b->reg), a->imm.i, regname(b->reg));
+            printf("    %s    $%lld, %s\n",
+                opstring, a->imm.i, regname(b->reg));
+            a->reg = b->reg;
+        }
+        else
+        {
+            printf("    %s    $%lld, %s\n",
+                opstring, b->imm.i, regname(a->reg));
         }
     }
     else
@@ -371,6 +378,15 @@ static void imm_binop(Parser* p, int op, Item* a, Item* b)
     }
 }
 
+static void imm_regop(Parser* p, int op, Item* a, Item* b)
+{
+    if (a->type->form == FORM_INT || a->type->form == FORM_BOOL)
+    {
+        printf("    addq    %s, %s, %s\n",
+            regname(a->reg), regname(b->reg), regname(b->reg));
+    }
+}
+
 void codegen_binop(Parser* p, int op, Item* a, Item* b)
 {
     if (items_const(a, b))
@@ -381,17 +397,38 @@ void codegen_binop(Parser* p, int op, Item* a, Item* b)
     {
         load_var(p, a);
         imm_binop(p, op, a, b);
+        p->curr_reg--;
+    }
+    else if (a->mode == ITEM_CONST)
+    {
+        load_var(p, b);
+        imm_binop(p, op, a, b);
+        p->curr_reg--;
     }
     else
     {
         load_var(p, a);
         load_var(p, b);
+        item_dump(a);
+        item_dump(b);
+
+        imm_regop(p, op, a, b);
+
         // emit instruction
         // p->topreg--;
         // a->reg = p->topreg-1;
     }
 }
 
+//  PROCEDURE Switch*(VAR x: Item);
+//  BEGIN Put1(Mov, RH, 0, -60); Put2(Ldw, RH, RH, 0);
+//    x.mode := Reg;
+//    x.type := intType;
+//    x.r := RH;
+//    INC(RH)
+//  END Switch;
+
+
 void codegen_store(Parser* p, Item* a, Item* b)
 {
     if (a->mode == ITEM_MVAR && b->reg == NOREG)
index a76adf4364889414ae05b5118f9de9c021283049..fed4f0306e90182a7abd4bd13c6fd450977a7b53 100644 (file)
@@ -450,7 +450,7 @@ RULE(expression)
 {
     int ops[] = { EQ, NEQ, '<', LTEQ, '>', GTEQ, IS, 0 };
     simple_expr(p, item);
-    item_dump(item);
+
     if (matches_oneof(p, ops))
     {
         Item right = { 0 };
@@ -597,6 +597,7 @@ RULE(statement_seq)
                 init_item(item, sym);
                 item->imm.s = sym->name;
 
+                printf("\n//  %s = ...\n", sym->name);
                 expression(p, &right);
                 check_types(p, item, &right);
                 codegen_store(p, item, &right);
index 21b89db7914b19b09a17822cffdeaf98e222ad4b..de352730dfd99f46988b6240a7d6c2302a1d71de 100644 (file)
@@ -14,11 +14,11 @@ var
     c : Int
 
 begin
-    a = true;
-    a = A;
-    b = 24;
-    b = B;
+#    a = true;
+#    a = A;
+#    b = 24;
+#    b = B;
     c = b + 1;
-#    c = 1 + b;
-#    c = a + b;
+    c = 1 + b;
+#    c = b + b;
 end
index 1f9c33192c482bc9cf54837e40123c49f43562cb..8ef8b9e28f29bf49be02a81b4797a8d9f1ae18b1 100644 (file)
@@ -1,5 +1,3 @@
-// Item M:0 R:0 
-// Item M:0 R:0 42
     .data
 Module_a:
     .zero 1
@@ -18,23 +16,16 @@ Module:
     pushq   %rbp
     movq    %rsp, %rbp
 
-// Item M:0 R:0 
-    movq    $1, Module_a(%rip)
-// Item M:0 R:0 
-    movq    $1, Module_a(%rip)
-// Item M:0 R:0 24
-    movq    $24, Module_b(%rip)
-// Item M:0 R:0 42
-    movq    $42, Module_b(%rip)
+
+//  c = ...
+    movq    Module_b(%rip), %rdi
+    addq    $1, %rdi
+    movq    %rdi, Module_c(%rip)
+
+//  c = ...
     movq    Module_b(%rip), %rdi
     addq    $1, %rdi
-// Item M:2 R:1  b
     movq    %rdi, Module_c(%rip)
-BAR
-movq %rsi, $1
-    movq    Module_b(%rip), %rdx
-// Item M:0 R:2 1
-    movq    %rsi, Module_c(%rip)
 
     pop     %rbp
     ret