From: Michael D. Lowis Date: Tue, 25 Jan 2022 21:44:12 +0000 (-0500) Subject: added recursive phi function handling. Still some bugs to work out... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=0f9ff4bb5ebfd48fae66e03acf75c7908e2e10d2;p=proto%2Fobnc.git added recursive phi function handling. Still some bugs to work out... --- diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index b989b57..c46d0ff 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -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(); } diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index bddfa48..50bf064 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -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"); }