From c4b8e00ae21d138dd1415b3033acc9ea43d34e53 Mon Sep 17 00:00:00 2001 From: David Given Date: Mon, 26 Sep 2016 22:48:58 +0200 Subject: [PATCH] Revamp the array module not to use nasty macros any more. Slightly more verbose to use, but definitely cleaner. --- mach/proto/mcg/mcg.h | 8 ++-- mach/proto/mcg/parse_em.c | 16 +++---- mach/proto/mcg/pass_convertstackops.c | 44 +++++++++---------- mach/proto/mcg/pass_eliminatetrivialblocks.c | 16 +++---- mach/proto/mcg/pass_instructionselection.c | 8 ++-- mach/proto/mcg/pass_removedeadblocks.c | 20 ++++----- mach/proto/mcg/pass_typeinference.c | 8 ++-- mach/proto/mcg/procedure.c | 8 ++-- mach/proto/mcg/treebuilder.c | 14 +++--- modules/src/data/array.c | 46 +++++++++++--------- modules/src/data/array.h | 40 ++++++++--------- 11 files changed, 113 insertions(+), 115 deletions(-) diff --git a/mach/proto/mcg/mcg.h b/mach/proto/mcg/mcg.h index e40bbcbf5..8cf16f952 100644 --- a/mach/proto/mcg/mcg.h +++ b/mach/proto/mcg/mcg.h @@ -74,15 +74,15 @@ struct procedure const char* name; struct basicblock* root_bb; size_t nlocals; - ARRAY(struct basicblock, blocks); + ARRAYOF(struct basicblock) blocks; }; struct basicblock { const char* name; - ARRAY(struct em, ems); - ARRAY(struct ir, irs); - ARRAY(struct hop, hops); + ARRAYOF(struct em) ems; + ARRAYOF(struct ir) irs; + ARRAYOF(struct hop) hops; bool is_fake : 1; bool is_root : 1; bool is_terminated : 1; diff --git a/mach/proto/mcg/parse_em.c b/mach/proto/mcg/parse_em.c index 705bc1587..7c0015184 100644 --- a/mach/proto/mcg/parse_em.c +++ b/mach/proto/mcg/parse_em.c @@ -77,7 +77,7 @@ static void queue_insn_simple(int opcode) { struct em* em = new_insn(opcode); em->paramtype = PARAM_NONE; - APPEND(code_bb->ems, em); + array_append(&code_bb->ems, em); switch (opcode) { @@ -92,7 +92,7 @@ static void queue_insn_value(int opcode, arith value) struct em* em = new_insn(opcode); em->paramtype = PARAM_IVALUE; em->u.ivalue = value; - APPEND(code_bb->ems, em); + array_append(&code_bb->ems, em); switch (opcode) { @@ -110,7 +110,7 @@ static void queue_insn_label(int opcode, const char* label, arith offset) em->paramtype = PARAM_LVALUE; em->u.lvalue.label = label; em->u.lvalue.offset = offset; - APPEND(code_bb->ems, em); + array_append(&code_bb->ems, em); switch (opcode) { @@ -126,14 +126,14 @@ static void queue_insn_block(int opcode, struct basicblock* left, struct basicbl em->paramtype = PARAM_BVALUE; em->u.bvalue.left = left; em->u.bvalue.right = right; - APPEND(code_bb->ems, em); + array_append(&code_bb->ems, em); terminate_block(); } static void change_basicblock(struct basicblock* newbb) { - APPENDU(current_proc->blocks, newbb); + array_appendu(¤t_proc->blocks, newbb); if (code_bb && !code_bb->is_terminated) queue_insn_block(op_bra, newbb, NULL); @@ -252,7 +252,7 @@ static void parse_pseu(void) struct em* em = new_insn(op_bra); em->paramtype = PARAM_BVALUE; em->u.bvalue.left = bb_get(label); - APPEND(data_bb->ems, em); + array_append(&data_bb->ems, em); } data_offset(label, 0, ro); @@ -289,7 +289,7 @@ static void parse_pseu(void) current_proc->nlocals = em.em_nlocals; code_bb = current_proc->root_bb; code_bb->is_root = true; - APPEND(current_proc->blocks, code_bb); + array_append(¤t_proc->blocks, code_bb); symbol = symbol_get(current_proc->name); symbol->section = SECTION_TEXT; @@ -351,7 +351,7 @@ static void create_data_label(const char* label) { data_bb = bb_get(label); data_bb->is_fake = true; - APPEND(current_proc->blocks, data_bb); + array_append(¤t_proc->blocks, data_bb); } } diff --git a/mach/proto/mcg/pass_convertstackops.c b/mach/proto/mcg/pass_convertstackops.c index 77cdf39d9..fd18ab67b 100644 --- a/mach/proto/mcg/pass_convertstackops.c +++ b/mach/proto/mcg/pass_convertstackops.c @@ -1,16 +1,16 @@ #include "mcg.h" STATICMAP(graph); -STATICARRAY(struct ir, pops); -STATICARRAY(struct ir, pushes); +static ARRAYOF(struct ir) pops; +static ARRAYOF(struct ir) pushes; static struct ir* get_last_push(struct basicblock* bb) { int i; - for (i=bb->irs_count-1; i>=0; i--) + for (i=bb->irs.count-1; i>=0; i--) { - struct ir* ir = bb->irs[i]; + struct ir* ir = bb->irs.item[i]; if (ir->opcode == IR_PUSH) return ir; @@ -25,10 +25,10 @@ static struct ir* get_first_pop(struct basicblock* bb) { int i; - for (i=0; iirs_count; i++) + for (i=0; iirs.count; i++) { struct ir* irr; - struct ir* ir = bb->irs[i]; + struct ir* ir = bb->irs.item[i]; if (ir->opcode == IR_PUSH) return NULL; @@ -55,11 +55,11 @@ static void make_bb_graph(struct procedure* proc) int i, j; graph_count = 0; - for (i=0; iblocks_count; i++) + for (i=0; iblocks.count; i++) { - struct basicblock* bb = proc->blocks[i]; - for (j=0; jirs_count; j++) - ir_walk(bb->irs[j], collect_outputs_cb, bb); + struct basicblock* bb = proc->blocks.item[i]; + for (j=0; jirs.count; j++) + ir_walk(bb->irs.item[j], collect_outputs_cb, bb); } } @@ -68,7 +68,7 @@ static void convert_block(struct procedure* proc, struct basicblock* bb) int i, j; struct ir* ir; - pushes_count = pops_count = 0; + pushes.count = pops.count = 0; for (;;) { struct ir* lastpush = get_last_push(bb); @@ -87,7 +87,7 @@ static void convert_block(struct procedure* proc, struct basicblock* bb) ir = get_first_pop(outbb); if (!ir || (ir->size != lastpush->size)) return; - APPENDU(pops, ir); + array_appendu(&pops, ir); /* Also abort unless *every* predecessor block of the one we've * just found *also* ends in a push of the same size. */ @@ -101,7 +101,7 @@ static void convert_block(struct procedure* proc, struct basicblock* bb) ir = get_last_push(inbb); if (!ir || (ir->size != lastpush->size)) return; - APPENDU(pushes, ir); + array_appendu(&pushes, ir); } } } @@ -109,22 +109,22 @@ static void convert_block(struct procedure* proc, struct basicblock* bb) /* Okay, now we can wire them all up. */ - for (i=0; iis_sequence); *ir = *ir->left; ir->is_sequence = true; } - for (i=0; isize, pushir); - for (j=1; jsize, phi, pushes[j]); + for (j=1; jsize, phi, pushes.item[j]); phi->is_sequence = ir->is_sequence; *ir = *phi; @@ -138,8 +138,8 @@ void pass_convert_stack_ops(struct procedure* proc) make_bb_graph(proc); - for (i=0; iblocks_count; i++) - convert_block(proc, proc->blocks[i]); + for (i=0; iblocks.count; i++) + convert_block(proc, proc->blocks.item[i]); } /* vim: set sw=4 ts=4 expandtab : */ diff --git a/mach/proto/mcg/pass_eliminatetrivialblocks.c b/mach/proto/mcg/pass_eliminatetrivialblocks.c index 3b7280466..a152d7c97 100644 --- a/mach/proto/mcg/pass_eliminatetrivialblocks.c +++ b/mach/proto/mcg/pass_eliminatetrivialblocks.c @@ -6,11 +6,11 @@ static bool rewrite_jumps_cb(struct ir* ir, void* user) { struct basicblock* bb = ir->u.bvalue; if (!bb->is_fake - && (bb->irs_count > 0) - && (bb->irs[0]->opcode == IR_JUMP) - && (bb->irs[0]->left->opcode == IR_BLOCK)) + && (bb->irs.count > 0) + && (bb->irs.item[0]->opcode == IR_JUMP) + && (bb->irs.item[0]->left->opcode == IR_BLOCK)) { - ir->u.bvalue = bb->irs[0]->left->u.bvalue; + ir->u.bvalue = bb->irs.item[0]->left->u.bvalue; } } @@ -21,9 +21,9 @@ static void rewrite_jumps(struct basicblock* bb) { int i; - for (i=0; iirs_count; i++) + for (i=0; iirs.count; i++) { - struct ir* ir = bb->irs[i]; + struct ir* ir = bb->irs.item[i]; ir_walk(ir, rewrite_jumps_cb, NULL); } } @@ -32,9 +32,9 @@ void pass_eliminate_trivial_blocks(struct procedure* proc) { int i; - for (i=0; iblocks_count; i++) + for (i=0; iblocks.count; i++) { - struct basicblock* bb = proc->blocks[i]; + struct basicblock* bb = proc->blocks.item[i]; rewrite_jumps(bb); } } diff --git a/mach/proto/mcg/pass_instructionselection.c b/mach/proto/mcg/pass_instructionselection.c index aebfd9160..9d17c06e3 100644 --- a/mach/proto/mcg/pass_instructionselection.c +++ b/mach/proto/mcg/pass_instructionselection.c @@ -125,10 +125,10 @@ static void select_instructions(struct basicblock* bb) tracef('I', "I: BLOCK: %s\n", bb->name); - for (i=0; iirs_count; i++) + for (i=0; iirs.count; i++) { int insnno; - struct ir* ir = bb->irs[i]; + struct ir* ir = bb->irs.item[i]; burm_label(ir); insnno = burm_rule(ir->state_label, 1); @@ -145,9 +145,9 @@ void pass_instruction_selector(struct procedure* proc) vregcount = 1; - for (i=0; iblocks_count; i++) + for (i=0; iblocks.count; i++) { - struct basicblock* bb = proc->blocks[i]; + struct basicblock* bb = proc->blocks.item[i]; select_instructions(bb); } } diff --git a/mach/proto/mcg/pass_removedeadblocks.c b/mach/proto/mcg/pass_removedeadblocks.c index af2d54d70..24cb41889 100644 --- a/mach/proto/mcg/pass_removedeadblocks.c +++ b/mach/proto/mcg/pass_removedeadblocks.c @@ -1,6 +1,6 @@ #include "mcg.h" -STATICARRAY(struct basicblock, used); +static ARRAYOF(struct basicblock) used; static void walk_blocks(struct basicblock* bb); @@ -15,12 +15,12 @@ static void walk_blocks(struct basicblock* bb) { int i; - if (!CONTAINS(used, bb)) + if (!array_contains(&used, bb)) { - APPENDU(used, bb); + array_append(&used, bb); - for (i=0; iirs_count; i++) - ir_walk(bb->irs[i], walk_blocks_cb, NULL); + for (i=0; iirs.count; i++) + ir_walk(bb->irs.item[i], walk_blocks_cb, NULL); } } @@ -28,12 +28,12 @@ void pass_remove_dead_blocks(struct procedure* proc) { int i, j; - used_count = 0; - walk_blocks(proc->blocks[0]); + used.count = 0; + walk_blocks(proc->blocks.item[0]); - proc->blocks_count = 0; - for (i=0; iblocks, used[i]); + proc->blocks.count = 0; + for (i=0; iblocks, used.item[i]); } /* vim: set sw=4 ts=4 expandtab : */ diff --git a/mach/proto/mcg/pass_typeinference.c b/mach/proto/mcg/pass_typeinference.c index 4d57e3212..a3a8335ee 100644 --- a/mach/proto/mcg/pass_typeinference.c +++ b/mach/proto/mcg/pass_typeinference.c @@ -43,9 +43,9 @@ static void push_types_up(struct basicblock* bb) { int i; - for (i=0; iirs_count; i++) + for (i=0; iirs.count; i++) { - struct ir* ir = bb->irs[i]; + struct ir* ir = bb->irs.item[i]; ir->type = search_for_type(ir, ir->type); } } @@ -54,8 +54,8 @@ void pass_type_inference(struct procedure* proc) { int i; - for (i=0; iblocks_count; i++) - push_types_up(proc->blocks[i]); + for (i=0; iblocks.count; i++) + push_types_up(proc->blocks.item[i]); } /* vim: set sw=4 ts=4 expandtab : */ diff --git a/mach/proto/mcg/procedure.c b/mach/proto/mcg/procedure.c index 78d8f3311..2adc74c0f 100644 --- a/mach/proto/mcg/procedure.c +++ b/mach/proto/mcg/procedure.c @@ -5,9 +5,9 @@ static void print_blocks(char k, struct procedure* proc) int i; tracef(k, "%c: procedure %s\n", k, proc->name); - for (int i=0; iblocks_count; i++) + for (int i=0; iblocks.count; i++) { - struct basicblock* bb = proc->blocks[i]; + struct basicblock* bb = proc->blocks.item[i]; int j; tracef(k, "%c:\n", k); @@ -15,8 +15,8 @@ static void print_blocks(char k, struct procedure* proc) bb->is_fake ? "FAKE " : "", bb->name); - for (int j=0; jirs_count; j++) - ir_print(k, bb->irs[j]); + for (int j=0; jirs.count; j++) + ir_print(k, bb->irs.item[j]); } } diff --git a/mach/proto/mcg/treebuilder.c b/mach/proto/mcg/treebuilder.c index 92c7bcf3f..a0ebe0e3f 100644 --- a/mach/proto/mcg/treebuilder.c +++ b/mach/proto/mcg/treebuilder.c @@ -77,7 +77,7 @@ static struct ir* appendir(struct ir* ir) assert(current_bb != NULL); ir->is_sequence = true; - APPEND(current_bb->irs, ir); + array_append(¤t_bb->irs, ir); ir_print('0', ir); return ir; @@ -323,9 +323,9 @@ static struct ir* extract_block_refs(struct basicblock* bb) struct ir* outir = NULL; int i; - for (i=0; iems_count; i++) + for (i=0; iems.count; i++) { - struct em* em = bb->ems[i]; + struct em* em = bb->ems.item[i]; assert(em->opcode == op_bra); assert(em->paramtype == PARAM_BVALUE); @@ -651,9 +651,9 @@ static void generate_tree(struct basicblock* bb) current_bb = bb; reset_stack(); - for (i=0; iems_count; i++) + for (i=0; iems.count; i++) { - struct em* em = bb->ems[i]; + struct em* em = bb->ems.item[i]; tracef('E', "E: read %s ", em_mnem[em->opcode - sp_fmnem]); switch (em->paramtype) { @@ -696,8 +696,8 @@ void tb_procedure(struct procedure* current_proc) { int i; - for (i=0; iblocks_count; i++) - generate_tree(current_proc->blocks[i]); + for (i=0; iblocks.count; i++) + generate_tree(current_proc->blocks.item[i]); } diff --git a/modules/src/data/array.c b/modules/src/data/array.c index 9f61d2805..68fad1ff5 100644 --- a/modules/src/data/array.c +++ b/modules/src/data/array.c @@ -2,52 +2,56 @@ #include #include "array.h" -void array_append(void*** array, int* count, int* max, void* value) +void array_append(void* arrayp, void* value) { - if (*count == *max) - { - int newmax = (*max == 0) ? 8 : (*max * 2); - void** newarray = realloc(*array, newmax * sizeof(void*)); + struct array* array = arrayp; - *max = newmax; - *array = newarray; + if (array->count == array->max) + { + int newmax = (array->max == 0) ? 8 : (array->max * 2); + void** newarray = realloc(array->item, newmax * sizeof(void*)); + + array->max = newmax; + array->item = newarray; } - (*array)[*count] = value; - (*count)++; + array->item[array->count] = value; + array->count++; } -bool array_contains(void** array, int count, void* value) +bool array_contains(void* arrayp, void* value) { + struct array* array = arrayp; int i; - for (i=0; icount; i++) + if (array->item[i] == value) return true; return false; } -void array_appendu(void*** array, int* count, int* max, void* value) +void array_appendu(void* arrayp, void* value) { - if (!array_contains(*array, *count, value)) - array_append(array, count, max, value); + if (!array_contains(arrayp, value)) + array_append(arrayp, value); } -void array_remove(void** array, int* count, void* value) +void array_remove(void* arrayp, void* value) { + struct array* array = arrayp; int i; - for (i=0; i<*count; i++) + for (i=0; icount; i++) { - if (array[i] == value) + if (array->item[i] == value) { - while (i < (*count-1)) + while (i < (array->count-1)) { - array[i] = array[i+1]; + array->item[i] = array->item[i+1]; i++; } - (*count)--; + array->count--; return; } } diff --git a/modules/src/data/array.h b/modules/src/data/array.h index b3c9a03e5..156bad1e2 100644 --- a/modules/src/data/array.h +++ b/modules/src/data/array.h @@ -1,32 +1,26 @@ #ifndef ARRAY_H #define ARRAY_H -#define ARRAY(TYPE, NAME) \ - TYPE** NAME; \ - int NAME##_count; \ - int NAME##_max +/* Danger, Will Robinson! The type and the macro must be compatible. */ -#define STATICARRAY(TYPE, NAME) \ - static TYPE** NAME; \ - static int NAME##_count; \ - static int NAME##_max +struct array +{ + void** item; + int count; + int max; +}; -#define APPEND(ARRAY, VALUE) \ - array_append((void***) &ARRAY, &ARRAY##_count, &ARRAY##_max, VALUE) +#define ARRAYOF(TYPE) \ + struct { \ + TYPE** item; \ + int count; \ + int max; \ + } -#define CONTAINS(ARRAY, VALUE) \ - array_contains((void**) ARRAY, ARRAY##_count, VALUE) - -#define APPENDU(ARRAY, VALUE) \ - array_appendu((void***) &ARRAY, &ARRAY##_count, &ARRAY##_max, VALUE) - -#define REMOVE(ARRAY, VALUE) \ - array_remove((void**) ARRAY, &ARRAY##_count, VALUE) - -extern void array_append(void*** array, int* count, int* max, void* value); -extern bool array_contains(void** array, int count, void* value); -extern void array_appendu(void*** array, int* count, int* max, void* value); -extern void array_remove(void** array, int* count, void* value); +extern void array_append(void* array, void* value); +extern void array_appendu(void* array, void* value); +extern void array_remove(void* array, void* value); +extern bool array_contains(void* array, void* value); #endif