From: Michael D. Lowis Date: Mon, 29 Nov 2021 21:48:01 +0000 (-0500) Subject: added bitset data structure for code generation usage X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=746244221d459d4fab9220f3ecaa247b35846f45;p=proto%2Fobnc.git added bitset data structure for code generation usage --- diff --git a/cerise/inc/cerise.h b/cerise/inc/cerise.h index 91d1f4f..427ba21 100644 --- a/cerise/inc/cerise.h +++ b/cerise/inc/cerise.h @@ -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 index 0000000..0303a6b --- /dev/null +++ b/cerise/src/bitmap.c @@ -0,0 +1,42 @@ +#include + +#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; +} diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 12076c1..cfc8e59 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -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); diff --git a/cerise/src/ssa.c b/cerise/src/ssa.c index 1068021..51f258c 100644 --- a/cerise/src/ssa.c +++ b/cerise/src/ssa.c @@ -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])