]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added bitset data structure for code generation usage
authorMichael D. Lowis <mike.lowis@gentex.com>
Mon, 29 Nov 2021 21:48:01 +0000 (16:48 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Mon, 29 Nov 2021 21:48:01 +0000 (16:48 -0500)
cerise/inc/cerise.h
cerise/src/bitmap.c [new file with mode: 0644]
cerise/src/grammar.c
cerise/src/ssa.c

index 91d1f4fd8b6cee375fec301f302a0fc171fe12c9..427ba21fe766dd031bed1fa6f1b041f6752adc1a 100644 (file)
@@ -244,6 +244,13 @@ void compile(char* fname);
 long align_item(long offset, long size);
 long size_of(Type* type);
 
+// src/bitmap.c
+typedef struct Bitset Bitset;
+
+Bitset* bitset_new(size_t max);
+void bitset_add(Bitset* set, size_t val);
+bool bitset_has(Bitset* set, size_t val);
+
 // src/ssa.c
 bool ssa_asbool(SsaNode* node);
 long long ssa_asint(SsaNode* node);
diff --git a/cerise/src/bitmap.c b/cerise/src/bitmap.c
new file mode 100644 (file)
index 0000000..0303a6b
--- /dev/null
@@ -0,0 +1,42 @@
+#include <cerise.h>
+
+#define CELL_BITS (sizeof(size_t) * 8u)
+
+struct Bitset {
+    size_t maxval;
+    size_t bits[];
+};
+
+Bitset* bitset_new(size_t max)
+{
+    size_t ncells = (max / CELL_BITS) + ((max & (CELL_BITS-1)) ? 1 : 0);
+    Bitset* set = calloc(1, sizeof(Bitset) + (ncells * sizeof(size_t)));
+    set->maxval = max;
+    return set;
+}
+
+void bitset_add(Bitset* set, size_t val)
+{
+    if (val <= set->maxval)
+    {
+        size_t cell_index = (val / CELL_BITS);
+        size_t bit_index  = val - (cell_index * CELL_BITS);
+        set->bits[cell_index] |= (1 << bit_index);
+    }
+    else
+    {
+        fatal("tried to set a bit greater than max for set");
+    }
+}
+
+bool bitset_has(Bitset* set, size_t val)
+{
+    bool ret = false;
+    if (val <= set->maxval)
+    {
+        size_t cell_index = (val / CELL_BITS);
+        size_t bit_index  = val - (cell_index * CELL_BITS);
+        ret = ((set->bits[cell_index] & (1 << bit_index)) != 0);
+    }
+    return ret;
+}
index 12076c163e12aa055ddf7ce7797be8327d761161..cfc8e59f6199e7527edc5d9c8e49284da34de7b0 100644 (file)
@@ -436,6 +436,11 @@ static SsaBlock* statement_seq(Parser* p)
             p->curr_join = p->curr_join->next;
             expect(p, END);
         }
+//        else if (accept(p, END))
+//        {
+//            /* this is an empty block?? */
+//            break;
+//        }
         else /* assignments/expressions */
         {
             SsaNode* expr = expression(p);
index 106802126c8a6bda49389293e64ccc41e7eaa2ff..51f258c3bdc6cd500b7d51b42090454eb6765451 100644 (file)
@@ -532,29 +532,29 @@ void ssa_print_block(Parser* p, SsaBlock* block)
 static void print_block_graph(Parser* p, SsaBlock* block)
 {
     /* print the phis */
-    for (SsaPhi* phi = block->phis; phi; phi = phi->next)
-    {
-        Symbol* s = symbol_getbyid(p, phi->symid);
-        s->version++;
-        printf("block%lu: %s.%lu = phi(", block->id, s->name, s->version);
-        for (SsaPhiVar* var = phi->vars; var; var = var->next)
-        {
-            printf("%s.%lu", s->name, var->version);
-            if (var->next)
-            {
-                printf(", ");
-            }
-        }
-        puts(")");
-    }
-    /* print the instructions */
-    for (SsaNode* node = block->head; node; node = node->next)
-    {
-        printf("block%lu : ", block->id);
-        print_dest(p, node);
-        ssa_print(p, node);
-        puts("");
-    }
+//    for (SsaPhi* phi = block->phis; phi; phi = phi->next)
+//    {
+//        Symbol* s = symbol_getbyid(p, phi->symid);
+//        s->version++;
+//        printf("block%lu: %s.%lu = phi(", block->id, s->name, s->version);
+//        for (SsaPhiVar* var = phi->vars; var; var = var->next)
+//        {
+//            printf("%s.%lu", s->name, var->version);
+//            if (var->next)
+//            {
+//                printf(", ");
+//            }
+//        }
+//        puts(")");
+//    }
+//    /* print the instructions */
+//    for (SsaNode* node = block->head; node; node = node->next)
+//    {
+//        printf("block%lu : ", block->id);
+//        print_dest(p, node);
+//        ssa_print(p, node);
+//        puts("");
+//    }
 
     /* print the next nodes */
     if (block->links[0])