]> git.mdlowis.com Git - proto/obnc.git/commitdiff
added recursive phi function handling. Still some bugs to work out...
authorMichael D. Lowis <mike.lowis@gentex.com>
Tue, 25 Jan 2022 21:44:12 +0000 (16:44 -0500)
committerMichael D. Lowis <mike.lowis@gentex.com>
Tue, 25 Jan 2022 21:44:12 +0000 (16:44 -0500)
cerise/src/grammar.c
cerise/src/ssa.c

index b989b57f7e3d9c94b6db873d59020009841b1e70..c46d0ff269ec82ca1ac37c2a7b989904102d42d0 100644 (file)
@@ -663,6 +663,10 @@ static void module(Parser* p)
 
 //        ssa_print_block(p, block);
         ssa_print_graph(p, block);
+
+        extern void ssa_print_asm(Parser* p, SsaBlock* block);
+        ssa_print_asm(p, block);
+
         //proc_end();
     }
 
index bddfa48086e3a76e1da9a0296710c15b34c80f9b..50bf064315d526b5dead740ed444f37cf60b2f46 100644 (file)
@@ -94,37 +94,41 @@ SsaNode* ssa_op(Parser* p, int op, SsaNode* left, SsaNode* right)
 
 static void phi_add(Parser* p, SsaVar var)
 {
-    /* first, append the phi function to the list */
-    SsaPhi** phis = &(p->curr_join->phis);
-    for (; *phis; phis = &((*phis)->next))
+    /* for all enclosing join nodes */
+    for (SsaBlock* join = p->curr_join; join; join = join->next)
     {
-        if ((*phis)->symid == var.symid)
+        /* first, append the phi function to the list */
+        SsaPhi** phis = &(join->phis);
+        for (; *phis; phis = &((*phis)->next))
         {
-            break;
+            if ((*phis)->symid == var.symid)
+            {
+                break;
+            }
+        }
+        if (!*phis)
+        {
+            *phis = calloc(1, sizeof(SsaPhi));
+            (*phis)->symid = var.symid;
         }
-    }
-    if (!*phis)
-    {
-        *phis = calloc(1, sizeof(SsaPhi));
-        (*phis)->symid = var.symid;
-    }
 
-    /* now add or update the variable entry */
-    SsaPhiVar** vars = &((*phis)->vars);
-    for (; *vars; vars = &((*vars)->next))
-    {
-        if ((*vars)->block == p->curr_block->id)
+        /* now add or update the variable entry */
+        SsaPhiVar** vars = &((*phis)->vars);
+        for (; *vars; vars = &((*vars)->next))
+        {
+            if ((*vars)->block == p->curr_block->id)
+            {
+                (*vars)->version = var.symver;
+                break;
+            }
+        }
+        if (!*vars)
         {
+            *vars = calloc(1, sizeof(SsaPhiVar));
+            (*vars)->block = p->curr_block->id;
             (*vars)->version = var.symver;
-            break;
         }
     }
-    if (!*vars)
-    {
-        *vars = calloc(1, sizeof(SsaPhiVar));
-        (*vars)->block = p->curr_block->id;
-        (*vars)->version = var.symver;
-    }
 }
 
 SsaNode* ssa_store(Parser* p, SsaNode* dest, SsaNode* value)
@@ -624,5 +628,49 @@ void ssa_print_graph(Parser* p, SsaBlock* block)
 
         puts("");
     }
-    printf("@enduml\n");
+    printf("@enduml\n\n");
+}
+
+
+void ssa_print_asm(Parser* p, SsaBlock* block)
+{
+    /* perform a topological sort of the nodes */
+    SsaBlock* sorted = NULL;
+    Bitset* set = bitset_new(p->blockid);
+    topsort(set, &sorted, block);
+
+    /* now let's print the plantuml representation */
+    printf("@startasm\n");
+    for (SsaBlock* curr = sorted; curr; curr = curr->next)
+    {
+        printf("block%lu:\n", curr->id);
+
+        /* print the phis */
+        for (SsaPhi* phi = curr->phis; phi; phi = phi->next)
+        {
+            Symbol* s = symbol_getbyid(p, phi->symid);
+            s->version++;
+            printf("    %s.%lu = phi(", 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 = curr->head; node; node = node->next)
+        {
+//            printf("block%lu : ", curr->id);
+            printf("    ");
+            print_dest(p, node);
+            ssa_print(p, node);
+            puts("");
+        }
+    }
+    printf("@endasm\n\n");
 }