From: Justin Wind Date: Sat, 14 Apr 2012 19:49:40 +0000 (-0700) Subject: generalized list handling of instructions and labels X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=503abee3b3bf76015786f5a99e31117fbfe2d310;p=dcpu16 generalized list handling of instructions and labels Instructions and labels are now kept in growable arrays, using common interface. --- diff --git a/as-dcpu16.c b/as-dcpu16.c index 798bf7e..0131ba4 100644 --- a/as-dcpu16.c +++ b/as-dcpu16.c @@ -6,7 +6,7 @@ #include #include -#include "as-dcpu16.h" +#include "dcpu16.h" /* * quick and dirty assembler for dcpu16 @@ -23,13 +23,19 @@ static const char * const src_id_ = "$Id$"; const char const out_filename_default_[] = "a.out"; -unsigned int verbose_ = 0; -unsigned int dryrun_ = 0; +/* global invocation options */ +struct options { + unsigned int verbose; + unsigned int dryrun; +} opt_ = { + .verbose = 0, + .dryrun = 0, +}; -#define DEBUG_PRINTF(...) do { if (verbose_ > 2) printf(__VA_ARGS__); } while (0) -#define VERBOSE_PRINTF(...) do { if (verbose_) printf(__VA_ARGS__); } while (0) - +#define DEBUG_PRINTF(...) do { if (opt_.verbose > 2) { printf("DEBUG: "); printf(__VA_ARGS__); } } while (0) +#define DEBUG_PRINTFQ(...) do { if (opt_.verbose > 2) printf(__VA_ARGS__); } while (0) +#define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0) static void usage_(char *prog, unsigned int full) { @@ -61,145 +67,181 @@ void usage_(char *prog, unsigned int full) { } -/* maintain an array of the instructions we have parsed */ +/* instructions have operands */ +struct operand_ { + struct operand_ *next; + char *operand; /* tokenized operand text */ +}; + +/* keep an array of instructions as we read them in */ +struct instruction_ { + char *label; /* set if a label points here */ + char *opcode; /* tokenized instruction text */ + struct operand_ *operands; /* list of operands */ + unsigned int ready : 1; /* bytecode computed? */ + unsigned int length; /* number of words of bytecode */ + DCPU16_WORD instr_words[]; +}; + +/* keep an array of labels, indexed back to their instruction locations */ +struct label_ { + char *label; /* name of label */ + struct instruction_ **instr; /* pointer into array of instructions */ + unsigned int ready : 1; /* do we know where this label is yet? */ + DCPU16_WORD addr; +}; + + +/* routines to support generic grow-able arrays */ + +struct dynamic_array_ { + size_t entry_size; + size_t grow_size; + size_t allocated; + size_t entries; + void *a; +}; + +#define DYNARRAY_ITEM(da, index) ( (char *)(da).a + ( (da).entry_size * index ) ) + +/* allocate and initialize a new generic dynamic array */ static -struct instruction_list_ *instr_list_new(void) { - size_t init_size = 1024; - struct instruction_list_ *il = malloc(IL_SIZE(init_size)); - if (il == NULL) { - fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); - return NULL; +struct dynamic_array_ *dynarray_new_(size_t entry_size, size_t grow_size) { + struct dynamic_array_ *da; + + if (entry_size == 0 || grow_size == 0) { + fprintf(stderr, "%s: internal error: sizes cannot be zero\n", __func__); + exit(EX_SOFTWARE); } - il->allocated = init_size; - il->entries = 0; - return il; -} -static -int instr_list_insert(struct instruction_list_ **il, struct instruction_ *i) { - /* make room make room */ - if ((*il)->entries - 1 == (*il)->allocated) { - size_t new_allocated = (*il)->allocated + 1024; - void *tmp_ptr = realloc(*il, IL_SIZE(new_allocated)); - if (tmp_ptr == NULL) { - fprintf(stderr, "%s():%s\n", "realloc", strerror(errno)); - return -1; - } - *il = tmp_ptr; - (*il)->allocated = new_allocated; + da = calloc(1, sizeof *da); + if (da == NULL) { + fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); + return NULL; } - (*il)->instr[(*il)->entries] = i; - (*il)->entries += 1; - return 0; -} + da->entry_size = entry_size; + da->grow_size = grow_size; -/* also maintain a list of the labels we've seen, indexed back to their instructions. */ -/* FIXME: ugh, this could all stand to be rewritten cleaner */ -/* these lists could be rearranged to be a lot easier to wrangle and/or maybe use common interfaces */ -/* they were thrown together on the fly */ -static -struct label_list_ *label_list_new(void) { - size_t init_size = 256; - struct label_list_ *ll = malloc(LL_SIZE(init_size)); - if (ll == NULL) { + da->a = malloc(da->entry_size * da->grow_size); + if (da->a == NULL) { fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); - return NULL; } - ll->allocated = init_size; - ll->entries = 0; - return ll; + + da->allocated = grow_size; + + DEBUG_PRINTF("allocated new dynarray:%p a:%p entry_size:%zu\n", da, da->a, da->entry_size); + + return da; } -/* instr here is index into instruction list */ +/* copy item onto end of array */ static -int label_list_insert(struct label_list_ **ll, struct instruction_ **instr) { - if ((*ll)->entries - 1 == (*ll)->allocated) { - size_t new_allocated = (*ll)->allocated + 256; - void *tmp_ptr = realloc(*ll, IL_SIZE(new_allocated)); +void *dynarray_add_(struct dynamic_array_ *da, void *item) { + void *dst; + + /* make room, make room */ + if (da->entries == da->allocated) { + size_t new_allocated = da->allocated + da->grow_size; + void *tmp_ptr = realloc(da->a, new_allocated * da->entry_size); if (tmp_ptr == NULL) { fprintf(stderr, "%s():%s\n", "realloc", strerror(errno)); - return -1; + return NULL; } - *ll = tmp_ptr; - (*ll)->allocated = new_allocated; + da->a = tmp_ptr; + da->allocated = new_allocated; + + DEBUG_PRINTF("grew dynarray:%p\n", da); } - DEBUG_PRINTF("TRACE: adding label '%s'\n", (*instr)->label); + dst = DYNARRAY_ITEM(*da, da->entries); + memcpy(dst, item, da->entry_size); - (*ll)->label[(*ll)->entries].label = (*instr)->label; - (*ll)->label[(*ll)->entries].instr = instr; - (*ll)->entries += 1; - return 0; + da->entries++; + + DEBUG_PRINTF("added dynarray:%p entry:%zu item:%p\n", da, da->entries, item); + + return dst; } -/* locate the index of a labelled instruction within the instruction list */ + +/* locate and return the label entry matching name */ static -struct instruction_ **label_list_find_instr(struct label_list_ *ll, char *label) { +struct label_ *label_find_(struct dynamic_array_ *labels, char *name) { size_t x; - for (x = 0; x < ll->entries; x++) { - if (strcmp(ll->label[x].label, label) == 0) - return ll->label[x].instr; + for (x = 0; x < labels->entries; x++) { + struct label_ *l = (struct label_ *)DYNARRAY_ITEM(*labels, x); + if (strcmp(l->label, name) == 0) + return l; } return NULL; } -/* look up the address of a calculated address */ + +/* if a label has a validly-calculated address, fetch it */ static -int label_list_find_addr(struct label_list_ *ll, char *label, DCPU16_WORD *addr) { - size_t x; +int label_addr_(struct dynamic_array_ *labels, char *name, DCPU16_WORD *addr) { + struct label_ *l; - for (x = 0; x < ll->entries; x++) { - if (strcmp(ll->label[x].label, label) == 0) { - if (ll->label[x].ready == 1) { - *addr = ll->label[x].addr; - return 0; - } - } - } - return -1; + if ( (l = label_find_(labels, name)) == NULL ) + return -1; + if (! l->ready) + return -2; + *addr = l->addr; + return 0; } -/* attempt to determine the addresses of labels */ + +/* attempt to determine the addresses of all labels */ static -void label_addr_calculate_(struct instruction_list_ *il, struct label_list_ *ll) { +void label_addr_calculate_(struct dynamic_array_ *instructionps, struct dynamic_array_ *labels) { size_t i; - /* walk through labels */ - for (i = 0; i < ll->entries; i++) { + /* for each label.. */ + for (i = 0; i < labels->entries; i++) { + struct label_ *l; struct instruction_ **instr; unsigned int word_count = 0; - if (ll->label[i].ready) + l = (struct label_ *)DYNARRAY_ITEM(*labels, i); + + /* if it's already calculated, great. */ + if (l->ready) continue; /* + * starting at the instruction for this label, * walk backwards through the list of instructions - * until we get to the start or a known prior label address - * update our label + * until we get to the start or a known prior label address. + * update our label with the freshly calculated addr */ - for (instr = ll->label[i].instr; instr >= il->instr; instr--) { + for (instr = ((struct label_ *)DYNARRAY_ITEM(*labels, i))->instr; + instr >= (struct instruction_ **)DYNARRAY_ITEM(*instructionps, 0); + instr--) { word_count += (*instr)->length; + /* have we come across an instruction which a label points to? + it should already be calculated, so just add that on and be done */ if ((*instr)->label - && strcmp((*instr)->label, ll->label[i].label)) { + && strcmp((*instr)->label, l->label)) { DCPU16_WORD addr; - if (label_list_find_addr(ll, (*instr)->label, &addr)) { + if (label_addr_(labels, (*instr)->label, &addr)) { fprintf(stderr, "internal error: incomplete prior address for '%s' while calculating '%s'\n", (*instr)->label, - ll->label[i].label); + l->label); continue; } + word_count += addr; break; } } - ll->label[i].addr = word_count; - ll->label[i].ready = 1; - DEBUG_PRINTF("label '%s' has addr of 0x%04x\n", ll->label[i].label, word_count); + l->addr = word_count; + l->ready = 1; + DEBUG_PRINTF("label '%s' now has addr of 0x%04x\n", l->label, word_count); } } @@ -320,7 +362,7 @@ void buf_strip_chars_(char *buf, char *chars) { * notes: nextword may be overwritten even if it's not used in final instruction */ static -int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextword, unsigned int *nextwordused, unsigned int allow_short_labels) { +int value_bits_(struct dynamic_array_ *labels, char *operand_orig, DCPU16_WORD *nextword, unsigned int *nextwordused, unsigned int allow_short_labels) { static char *operand = NULL; static size_t operand_sz = 0; @@ -361,33 +403,33 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor /* single character might match a register */ if (strlen(operand) == 1 && strchr("AaBbCcXxYyZzIiJj", *operand)) { - DEBUG_PRINTF("is register %c\n", *operand); + DEBUG_PRINTFQ("is register %c\n", *operand); return register_enumerate_(*operand); } /* easy matches */ if (strcasecmp(operand, "POP") == 0) { - DEBUG_PRINTF("is POP\n"); + DEBUG_PRINTFQ("is POP\n"); return 0x18; } if (strcasecmp(operand, "PUSH") == 0) { - DEBUG_PRINTF("is PUSH\n"); + DEBUG_PRINTFQ("is PUSH\n"); return 0x19; } if (strcasecmp(operand, "PEEK") == 0) { - DEBUG_PRINTF("is PEEK\n"); + DEBUG_PRINTFQ("is PEEK\n"); return 0x1a; } if (strcasecmp(operand, "SP") == 0) { - DEBUG_PRINTF("is register SP\n"); + DEBUG_PRINTFQ("is register SP\n"); return 0x1b; } if (strcasecmp(operand, "PC") == 0) { - DEBUG_PRINTF("is register PC\n"); + DEBUG_PRINTFQ("is register PC\n"); return 0x1c; } if (strcasecmp(operand, "O") == 0) { - DEBUG_PRINTF("is register O\n"); + DEBUG_PRINTFQ("is register O\n"); return 0x1d; } @@ -400,7 +442,7 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor /* is it [register]? */ if (strlen(operand) == 1 && strchr("AaBbCcXxYyZzIiJj", *operand)) { - DEBUG_PRINTF("is dereferenced register %c\n", *operand); + DEBUG_PRINTFQ("is dereferenced register %c\n", *operand); return 0x08 | register_enumerate_(*operand); } @@ -423,7 +465,7 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor reg = operand; constant = ep; } else { - DEBUG_PRINTF("is unparsable\n"); + DEBUG_PRINTFQ("is unparsable\n"); fprintf(stderr, "couldn't parse operand '%s'\n", operand_orig); return -1; } @@ -436,7 +478,7 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor /* string conversion went without issue */ /* validate it will fit in a word */ if (l > 0xffff) { - DEBUG_PRINTF("is out of range\n"); + DEBUG_PRINTFQ("is out of range\n"); fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig); return -1; } @@ -444,22 +486,22 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor /* seems fine */ *nextword = l & 0xffff; *nextwordused += 1; - DEBUG_PRINTF("is a dereferenced register (%c) + constant (%hu)\n", *reg, *nextword); + DEBUG_PRINTFQ("is a dereferenced register (%c) + constant (%hu)\n", *reg, *nextword); return 0x10 | register_enumerate_(*reg); } else if (errno) { - DEBUG_PRINTF("is out of range\n"); + DEBUG_PRINTFQ("is out of range\n"); fprintf(stderr, "trouble with operand '%s': %s\n", operand_orig, strerror(errno)); return -1; } /* what? still here? assume it's a label, I guess */ /* try to populate nextword with label address */ - if (label_list_find_addr(ll, operand, nextword)) { - DEBUG_PRINTF("(deferred label resolution)\n"); + if (label_addr_(labels, operand, nextword)) { + DEBUG_PRINTFQ("(deferred label resolution)\n"); *nextwordused += 1; return -2; } - DEBUG_PRINTF("is a dereferenced register (%c) + label\n", *reg); + DEBUG_PRINTFQ("is a dereferenced register (%c) + label\n", *reg); *nextwordused += 1; return 0x10 | register_enumerate_(*reg); } @@ -473,27 +515,27 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor /* string conversion went without issue */ /* validate it will fit in a word */ if (l > 0xffff) { - DEBUG_PRINTF("is out of range\n"); + DEBUG_PRINTFQ("is out of range\n"); fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig); return -1; } - DEBUG_PRINTF("is a dereferenced literal value (%hu)\n", *nextword); + DEBUG_PRINTFQ("is a dereferenced literal value (%hu)\n", *nextword); *nextword = l & 0xffff; *nextwordused += 1; return 0x1e; } else if (errno) { - DEBUG_PRINTF("is out of range\n"); + DEBUG_PRINTFQ("is out of range\n"); fprintf(stderr, "trouble with operand '%s': %s\n", operand_orig, strerror(errno)); } /* not a number? try a label */ - if (label_list_find_addr(ll, operand, nextword)) { - DEBUG_PRINTF("(deferred label resolution)\n"); + if (label_addr_(labels, operand, nextword)) { + DEBUG_PRINTFQ("(deferred label resolution)\n"); *nextwordused += 1; return -2; } - DEBUG_PRINTF("is a dereferenced label\n"); + DEBUG_PRINTFQ("is a dereferenced label\n"); *nextwordused += 1; return 0x1e; } @@ -505,12 +547,12 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor if (errno == 0 || (*operand && (*ep == '\0')) ) { if (l > 0xffff) { - DEBUG_PRINTF("is out of range\n"); + DEBUG_PRINTFQ("is out of range\n"); fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig); return -1; } - DEBUG_PRINTF("is literal value (%lu)\n", l); + DEBUG_PRINTFQ("is literal value (%lu)\n", l); if (l < 0x20) { return l + 0x20; } @@ -521,14 +563,14 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor } /* try to populate nextword with label address */ - if (label_list_find_addr(ll, operand, nextword)) { - DEBUG_PRINTF("(deferred label resolution)\n"); + if (label_addr_(labels, operand, nextword)) { + DEBUG_PRINTFQ("(deferred label resolution)\n"); /* assume non-small literal value */ *nextwordused += 1; return -2; } - DEBUG_PRINTF("is label '%s' (0x%02hx)\n", operand, *nextword); + DEBUG_PRINTFQ("is label '%s' (0x%02hx)\n", operand, *nextword); if (*nextword < 0x20 && allow_short_labels) { DEBUG_PRINTF("small value label win\n"); return (0x20 + *nextword) & 0x3f; @@ -538,6 +580,7 @@ int value_bits_(struct label_list_ *ll, char *operand_orig, DCPU16_WORD *nextwor return 0x1f; } +/* prints an instruction's assembly */ static inline int instruction_print_(struct instruction_ *i, unsigned int with_label) { struct operand_ *o; @@ -669,15 +712,15 @@ int buf_tokenize_(char *buf, struct instruction_ **next_instr) { /* try to generate bytecode for an instruction */ static -int instr_assemble_(struct label_list_ *ll, struct instruction_ *i, unsigned int allow_short_labels) { +int instr_assemble_(struct dynamic_array_ *labels, struct instruction_ *i, unsigned int allow_short_labels) { unsigned int nwu = 0; /* number of words used */ unsigned int incomplete = 0; int bits; struct operand_ *o = i->operands; - if (verbose_ > 2) { - printf("%s: assembling ", __func__); - instruction_print_(i,1); + if (opt_.verbose > 2) { + printf("%s: assembling %p ", __func__, i); + instruction_print_(i, 1); printf("\n"); } @@ -719,7 +762,7 @@ int instr_assemble_(struct label_list_ *ll, struct instruction_ *i, unsigned int fprintf(stderr, "'%s' requires more operands\n", i->opcode); return -1; } - bits = value_bits_(ll, o->operand, i->instr_words + 1, &nwu, allow_short_labels); + bits = value_bits_(labels, o->operand, i->instr_words + 1, &nwu, allow_short_labels); if (bits == -1) { fprintf(stderr, "couldn't assemble instruction\n"); return -1; @@ -738,7 +781,7 @@ int instr_assemble_(struct label_list_ *ll, struct instruction_ *i, unsigned int return -1; } - bits = value_bits_(ll, o->operand, i->instr_words + nwu + 1, &nwu, allow_short_labels); + bits = value_bits_(labels, o->operand, i->instr_words + nwu + 1, &nwu, allow_short_labels); if (bits == -1) { fprintf(stderr, "couldn't assemble instruction\n"); return -1; @@ -762,15 +805,15 @@ int instr_assemble_(struct label_list_ *ll, struct instruction_ *i, unsigned int DEBUG_PRINTF("instruction words: [%u]", i->length); for (bits = 0; bits <= (int)nwu; bits++) - DEBUG_PRINTF(" %04x", i->instr_words[bits]); + DEBUG_PRINTFQ(" %04x", i->instr_words[bits]); if (incomplete) { - DEBUG_PRINTF(" (preliminary)"); + DEBUG_PRINTFQ(" (preliminary)"); } else { i->ready = 1; } - DEBUG_PRINTF("\n"); + DEBUG_PRINTFQ("\n"); return 0; } @@ -780,7 +823,7 @@ int instr_assemble_(struct label_list_ *ll, struct instruction_ *i, unsigned int * break each line into parts, populate parts into structures */ static -int parse_stream_(FILE *f, const char *src, struct instruction_list_ **il, struct label_list_ **ll, unsigned int allow_short_labels) { +int parse_stream_(FILE *f, const char *src, struct dynamic_array_ *instructionps, struct dynamic_array_ *labels, unsigned int allow_short_labels) { struct instruction_ *instr, **instr_list_entry; unsigned int line = 0; int retval = 0; @@ -805,23 +848,32 @@ int parse_stream_(FILE *f, const char *src, struct instruction_list_ **il, struc if (instr) { /* add to list of instructions */ - if (instr_list_insert(il, instr)) { + instr_list_entry = dynarray_add_(instructionps, &instr); + if (instr_list_entry == NULL) { fprintf(stderr, "%s:%u:%s", src, line, "could not populate instruction list\n"); + break; } - instr_list_entry = (*il)->instr + (*il)->entries - 1; if (instr->label) { - if (label_list_find_instr(*ll, instr->label)) { + struct label_ new_label = { + .label = instr->label, + .instr = instr_list_entry, + .ready = 0, + .addr = 0, + }; + if (label_find_(labels, instr->label)) { fprintf(stderr, "%s:%u:%s", src, line, "duplicate label\n"); break; } - if (label_list_insert(ll, instr_list_entry)) { + + if (dynarray_add_(labels, &new_label) == NULL) { fprintf(stderr, "%s:%u:%s", src, line, "could not populate label list\n"); + break; } - label_addr_calculate_(*il, *ll); + label_addr_calculate_(instructionps, labels); } - instr_assemble_(*ll, instr, allow_short_labels); + instr_assemble_(labels, instr, allow_short_labels); } } if (ferror(f)) { @@ -840,28 +892,30 @@ int parse_stream_(FILE *f, const char *src, struct instruction_list_ **il, struc * make a full pass over instruction list to resolve labels */ static -int assemble_check_(struct instruction_list_ *il, struct label_list_ *ll, unsigned int allow_short_labels) { +int assemble_check_(struct dynamic_array_ *instructionps, struct dynamic_array_ *labels, unsigned int allow_short_labels) { int retval = 0; size_t x; DEBUG_PRINTF(" final pass of assembler...\n"); - for (x = 0; x < il->entries; x++) { - retval |= instr_assemble_(ll, il->instr[x], allow_short_labels); + for (x = 0; x < instructionps->entries; x++) { + struct instruction_ **instrp = (struct instruction_ **)DYNARRAY_ITEM(*instructionps, x); + retval |= instr_assemble_(labels, *instrp, allow_short_labels); if (retval) { - fprintf(stderr, "instruction failed to assemble\n"); + fprintf(stderr, "instruction %zu failed to assemble\n", x); } } VERBOSE_PRINTF("%3s %6s %-32s %-4s\n", "", "_addr_", "_label_", "_instruction_"); - for (x = 0; x < ll->entries; x++) { - if (! ll->label[x].ready) + for (x = 0; x < labels->entries; x++) { + struct label_ *l = (struct label_ *)DYNARRAY_ITEM(*labels, x); + if (! l->ready) retval |= -1; - if (verbose_) { + if (opt_.verbose) { printf("%3s0x%04x %-32s ", - ll->label[x].ready ? "" : "*", - ll->label[x].addr, - ll->label[x].label); - instruction_print_(*(ll->label[x].instr), 0); + l->ready ? "" : "*", + l->addr, + l->label); + instruction_print_(*(l->instr), 0); printf("\n"); } } @@ -875,13 +929,13 @@ int assemble_check_(struct instruction_list_ *il, struct label_list_ *ll, unsign } static -int output_(struct instruction_list_ *il, const char *filename) { +int output_(struct dynamic_array_ *instructionps, const char *filename) { FILE *of = NULL; - struct instruction_ *instr; + struct instruction_ **instrp; size_t i, r, total_words = 0; size_t x; - if (! dryrun_) { + if (! opt_.dryrun) { of = fopen(filename, "w"); if (of == NULL) { fprintf(stderr, "%s('%s'):%s\n", "fopen", filename, strerror(errno)); @@ -889,39 +943,39 @@ int output_(struct instruction_list_ *il, const char *filename) { } } - for (i = 0; i < il->entries; i++) { - instr = il->instr[i]; + for (i = 0; i < instructionps->entries; i++) { + instrp = (struct instruction_ **)DYNARRAY_ITEM(*instructionps, i); - if (verbose_) { + if (opt_.verbose) { int s; - s = instruction_print_(instr, 1); + s = instruction_print_(*instrp, 1); printf("%*s;", (44 - s) > 0 ? (44 - s) : 0, ""); - for (x = 0; x < instr->length; x++) { - printf(" %04x", instr->instr_words[x]); + for (x = 0; x < (*instrp)->length; x++) { + printf(" %04x", (*instrp)->instr_words[x]); } printf("\n"); } if (of) { - r = fwrite(instr->instr_words, sizeof(DCPU16_WORD), instr->length, of); - if (r < instr->length) { + r = fwrite((*instrp)->instr_words, sizeof(DCPU16_WORD), (*instrp)->length, of); + if (r < (*instrp)->length) { fprintf(stderr, "%s():%s\n", "fwrite", strerror(errno)); return -1; } } - total_words += instr->length; + total_words += (*instrp)->length; } fprintf(stderr, "%s 0x%04zx instructions as 0x%04zx words\n", - dryrun_ ? "assembled" : "wrote", + opt_.dryrun ? "assembled" : "wrote", i, total_words); return 0; } -static struct instruction_list_ *il_; -static struct label_list_ *ll_; +static struct dynamic_array_ *instructionps_; +static struct dynamic_array_ *labels_; int main(int argc, char *argv[]) { const char *out_filename = NULL; @@ -931,7 +985,7 @@ int main(int argc, char *argv[]) { while ( (c = getopt(argc, argv, "dhsvo:")) != EOF ) { switch (c) { case 'd': - dryrun_++; + opt_.dryrun++; break; case 's': @@ -947,7 +1001,7 @@ int main(int argc, char *argv[]) { break; case 'v': - verbose_++; + opt_.verbose++; break; case 'h': @@ -967,8 +1021,13 @@ int main(int argc, char *argv[]) { out_filename = out_filename_default_; /* init tables */ - il_ = instr_list_new(); - ll_ = label_list_new(); + instructionps_ = dynarray_new_(sizeof (struct instruction_ *), 1024); + labels_ = dynarray_new_(sizeof(struct label_), 256); + if (instructionps_ == NULL + || labels_ == NULL) { + fprintf(stderr, "failed to initialize\n"); + exit(EX_OSERR); + } /* if filenames were specified, parse them instead of stdin */ if (argc) { @@ -984,21 +1043,21 @@ int main(int argc, char *argv[]) { } VERBOSE_PRINTF("assembling '%s'...\n", filename); - parse_stream_(f, filename, &il_, &ll_, allow_short_labels); + parse_stream_(f, filename, instructionps_, labels_, allow_short_labels); fclose(f); } } else { VERBOSE_PRINTF("assembling '%s'...\n", "stdin"); - parse_stream_(stdin, "-", &il_, &ll_, allow_short_labels); + parse_stream_(stdin, "-", instructionps_, labels_, allow_short_labels); } - if (assemble_check_(il_, ll_, allow_short_labels)) { + if (assemble_check_(instructionps_, labels_, allow_short_labels)) { fprintf(stderr, "errors prevented assembly\n"); exit(EX_DATAERR); } - if (output_(il_, out_filename)) { + if (output_(instructionps_, out_filename)) { fprintf(stderr, "failed to create output\n"); exit(EX_OSERR); } diff --git a/as-dcpu16.h b/as-dcpu16.h deleted file mode 100644 index 3b11d85..0000000 --- a/as-dcpu16.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef AS_DCPU16_H_PTFNJB09 -#define AS_DCPU16_H_PTFNJB09 - -#include "dcpu16.h" - -struct instruction_ { - char *label; /* set if a label points here */ - char *opcode; /* tokenized instruction text */ - struct operand_ *operands; /* list of operands */ - unsigned int ready : 1; /* bytecode computed? */ - unsigned int length; /* number of words of bytecode */ - DCPU16_WORD instr_words[]; -}; - - -struct operand_ { - struct operand_ *next; - char *operand; /* tokenized operand text */ -}; - - -#define IL_SIZE(entries) (((entries) * sizeof(struct instruction_ *)) + sizeof(struct instruction_list_)) - -struct instruction_list_ { - size_t allocated; - size_t entries; - struct instruction_ *instr[]; -}; - - -/* note label table holds its own structs, not pointers like instruction list */ -struct label_ { - char *label; /* name of label */ - struct instruction_ **instr; /* pointer into instruction list table */ - unsigned int ready : 1; /* do we know where this label is yet? */ - DCPU16_WORD addr; -}; - -#define LL_SIZE(entries) (((entries) * sizeof(struct label_ *)) + sizeof(struct label_list_)) - -struct label_list_ { - size_t allocated; - size_t entries; - struct label_ label[]; -}; - -#endif /* AS_DCPU16_H_PTFNJB09 */