]> git.mdlowis.com Git - proto/obnc.git/commitdiff
checkpoint commit
authormike lowis <mike@mdlowis.com>
Tue, 1 Jun 2021 04:04:57 +0000 (00:04 -0400)
committermike lowis <mike@mdlowis.com>
Tue, 1 Jun 2021 04:04:57 +0000 (00:04 -0400)
cerise/backend/c99/codegen.c
cerise/inc/cerise.h
cerise/src/align.c [new file with mode: 0644]
cerise/src/grammar.c
cerise/ssa.md [new file with mode: 0644]
cerise/tests/Module.m

index b16c4899e44de3f253f18e836ce2ae6b2ad4a3f6..ebffc4841e541b35b19d0423cc22bce605611c65 100644 (file)
@@ -146,7 +146,7 @@ static char* temp(Item* a)
     char name[32];
     if (a->mode == ITEM_INDEX)
     {
-        snprintf(name, sizeof(name), "(*_T%d)", a->reg);
+        snprintf(name, sizeof(name), "(*_T%d + _T%lld)", a->reg, a->imm.i);
     }
     else if (a->mode == ITEM_FIELD)
     {
@@ -459,19 +459,27 @@ void codegen_index(Parser* p, Item* array, Item* index)
         {
             error(p, "negative array index");
         }
+        index->imm.i = (index->imm.i * size_of(array->type->base));
     }
     else
     {
         printf("    __CHECK_RANGE(%s < %d);\n", temp(index), array->type->size);
+        int dest_reg = declare_temp(p, index->type, 0);
+        printf(" = %s * %ld;\n", temp(index), size_of(array->type->base));
+        index->reg = dest_reg;
+    }
+
+    if (array->mode == ITEM_INDEX)
+    {
+        int dest_reg = declare_temp(p, index->type, 0);
+        printf(" = _T%lld + %s;\n", array->imm.i, temp(index));
+        index->reg = dest_reg;
     }
 
     /* emit the operation to calculate offset */
     array->type = array->type->base;
     array->mode = ITEM_INDEX;
-
-//    int dest_reg = declare_temp(p, array->type, 1);
-//    printf(" = &%s[%s];\n", temp(array), temp(index));
-//    array->reg = dest_reg;
+    array->imm.i = index->reg;
 }
 
 Field* get_field(Parser* p, Type* type, char* name)
index ffc7439f92eae92b4d6efd0f25eae7f05bc88305..bf5d62a9dfe96a1fc3891cbafb8bcf2c4acf1f01 100644 (file)
@@ -198,6 +198,10 @@ void dump_item(Item* a);
 void dump_symbol(Symbol* a);
 void dump_type(Type* a);
 
+// src/align.c
+long align_item(long offset, long size);
+long size_of(Type* type);
+
 /* Backend Code Generation
  *****************************************************************************/
 extern Type BoolType, IntType, RealType, StringType;
diff --git a/cerise/src/align.c b/cerise/src/align.c
new file mode 100644 (file)
index 0000000..42d7e76
--- /dev/null
@@ -0,0 +1,29 @@
+#include "cerise.h"
+
+long align_item(long offset, long size)
+{
+    offset--;
+    if (size >= 8u)
+    {
+        offset += (8 - (offset & (8 - 1)));
+    }
+    else if (size >= 4u)
+    {
+        offset += (4 - (offset & (4 - 1)));
+    }
+    else if (size >= 2u)
+    {
+        offset += (2 - (offset & (2 - 1)));
+    }
+    return offset;
+}
+
+long size_of(Type* type)
+{
+    long size = 1;
+    for (; type->form == FORM_ARRAY; type = type->base)
+    {
+        size = (size * type->size);
+    }
+    return (size * type->size);
+}
index e17d87327ab10801db6ecd4abde76228e674e592..d82edbbb35fde5f1756b025aebc3f71822edbdcd 100644 (file)
@@ -63,34 +63,6 @@ static void init_item(Item* item, Symbol* sym)
     }
 }
 
-static long align_item(long offset, long size)
-{
-    offset--;
-    if (size >= 8u)
-    {
-        offset += (8 - (offset & (8 - 1)));
-    }
-    else if (size >= 4u)
-    {
-        offset += (4 - (offset & (4 - 1)));
-    }
-    else if (size >= 2u)
-    {
-        offset += (2 - (offset & (2 - 1)));
-    }
-    return offset;
-}
-
-static long size_of(Type* type)
-{
-    long size = 1;
-    for (; type->form == FORM_ARRAY; type = type->base)
-    {
-        size = (size * type->size);
-    }
-    return (size * type->size);
-}
-
 Field* add_field(Parser* p, Type* type, char* name, bool export)
 {
     Field* prev = NULL;
diff --git a/cerise/ssa.md b/cerise/ssa.md
new file mode 100644 (file)
index 0000000..a842482
--- /dev/null
@@ -0,0 +1,43 @@
+type symbol = string
+
+type expr =
+    | Var    of symbol
+    | Num    of int
+    | Plus   of expr * expr
+    | Assign of symbol * expr
+
+type mem =
+      Var  of symbol
+    | Temp of symbol
+    | Addr of symbol
+
+type operand =
+      Const of int
+    | Mem   of mem
+
+type cond =
+      Bool of operand
+    | Not  of operand
+    | Eq   of operand * operand
+    | Leq  of operand * operand
+    | Le   of operand * operand
+
+type rhs =
+      Plus  of operand * operand
+    | Times of operand * operand
+    | Id    of operand
+
+type instr=
+      Read       of symbol
+    | Write      of symbol
+    | Lab        of symbol
+    | Assign     of symbol * rhs
+    | AssignRI   of operand * operand * operand
+    | AssignLI   of operand * operand * operand
+    | BranchComp of cond * label
+    | Halt
+    | Nop
+
+type tree =
+      Oneline of instr
+    | Seq     of tree * tree
\ No newline at end of file
index ebf4627d7b60638c1f8cf37c2c3c8c5dedee36eb..e23d994a0386b5af72f2c0aaebaa5ac701ea3b29 100644 (file)
@@ -117,5 +117,7 @@ begin
 #  f.dim.h = 0;
 #  f.label[0] = 42;
 
-    g[1] = 42;
+    c = 4;
+    g[c] = 42;
+#    e[0][9] = 42;
 end