+/* maintain an array of the instructions we have parsed */
+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;
+ }
+ 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;
+ }
+
+ (*il)->instr[(*il)->entries] = i;
+ (*il)->entries += 1;
+ return 0;
+}
+
+/* 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) {
+ fprintf(stderr, "%s():%s\n", "malloc", strerror(errno));
+ return NULL;
+ }
+ ll->allocated = init_size;
+ ll->entries = 0;
+ return ll;
+}
+
+/* instr here is index into instruction list */
+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));
+ if (tmp_ptr == NULL) {
+ fprintf(stderr, "%s():%s\n", "realloc", strerror(errno));
+ return -1;
+ }
+ *ll = tmp_ptr;
+ (*ll)->allocated = new_allocated;
+ }
+
+ DEBUG_NOTIFY("TRACE: adding label '%s'\n", (*instr)->label);
+
+ (*ll)->label[(*ll)->entries].label = (*instr)->label;
+ (*ll)->label[(*ll)->entries].instr = instr;
+ (*ll)->entries += 1;
+ return 0;
+}
+
+/* locate the index of a labelled instruction within the instruction list */
+static
+struct instruction_ **label_list_find_instr(struct label_list_ *ll, char *label) {
+ size_t x;
+
+ for (x = 0; x < ll->entries; x++) {
+ if (strcmp(ll->label[x].label, label) == 0)
+ return ll->label[x].instr;
+ }
+ return NULL;
+}
+
+/* look up the address of a calculated address */
+static
+int label_list_find_addr(struct label_list_ *ll, char *label, DCPU16_WORD *addr) {
+ size_t x;
+
+ 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;
+}
+
+/* attempt to determine the addresses of labels */
+static
+void label_addr_calculate_(struct instruction_list_ *il, struct label_list_ *ll) {
+ size_t i;
+
+ /* walk through labels */
+ for (i = 0; i < ll->entries; i++) {
+ struct instruction_ **instr;
+ unsigned int word_count = 0;
+
+ if (ll->label[i].ready)
+ continue;
+
+ /*
+ * walk backwards through the list of instructions
+ * until we get to the start or a known prior label address
+ * update our label
+ */
+ for (instr = ll->label[i].instr; instr >= il->instr; instr--) {
+
+ word_count += (*instr)->length;
+
+ if ((*instr)->label
+ && strcmp((*instr)->label, ll->label[i].label)) {
+ DCPU16_WORD addr;
+
+ if (label_list_find_addr(ll, (*instr)->label, &addr)) {
+ fprintf(stderr, "internal error: incomplete prior address for '%s' while calculating '%s'\n",
+ (*instr)->label,
+ ll->label[i].label);
+ continue;
+ }
+ word_count += addr;
+ break;
+ }
+ }
+ ll->label[i].addr = word_count;
+ ll->label[i].ready = 1;
+ DEBUG_NOTIFY("label '%s' has addr of 0x%04x\n", ll->label[i].label, word_count);
+ }
+}
+
+static
+void instr_free_(struct instruction_ *i) {
+ if (i->label)
+ free(i->label);
+ if (i->opcode)
+ free(i->opcode);
+ while (i->operands) {
+ struct operand_ *o = i->operands;
+
+ i->operands = o->next;
+ free(o);
+ }
+
+ free(i);
+}
+
+/* generate the nibble for a given basic opcode */
+static
+int opcode_bits_(char *opcode) {
+ static struct {
+ char op[4];
+ char value;
+ } opcodes_lower_nibble[] = {
+ { "JSR", 0x00 },
+ /* { "future nbi instruction", 0x00 }, */
+ { "SET", 0x01 },
+ { "ADD", 0x02 },
+ { "SUB", 0x03 },
+ { "MUL", 0x04 },
+ { "DIV", 0x05 },
+ { "MOD", 0x06 },
+ { "SHL", 0x07 },
+ { "SHR", 0x08 },
+ { "AND", 0x09 },
+ { "BOR", 0x0a },
+ { "XOR", 0x0b },
+ { "IFE", 0x0c },
+ { "IFN", 0x0d },
+ { "IFG", 0x0e },
+ { "IFB", 0x0f },
+ { "", 0x00 }
+ }, *o;
+
+ for (o = opcodes_lower_nibble; o->op[0]; o++) {
+ if (strcasecmp(o->op, opcode) == 0)
+ break;
+ }
+
+ if (o->op[0] == '\0') {
+ fprintf(stderr, "unknown instruction '%s'\n", opcode);
+ return -1;
+ }
+
+ return o->value;
+}
+
+/* generate the six bits for a given nbi opcode (aka first operand to opcode 0x00) */
+static
+int nbi_opcode_bits_(char *nbi_opcode) {
+ static struct {
+ char op[4];
+ char value;
+ } nbi_opcodes_bits[] = {
+ { " ", 0x00 }, /* reserved for future */
+ { "JSR", 0x01 },
+ { "", 0x00 }
+ }, *o;
+
+ for (o = nbi_opcodes_bits; o->op[0]; o++) {
+ if (strcasecmp(o->op, nbi_opcode) == 0)
+ break;
+ }
+
+ if (o->op[0] == '\0') {
+ fprintf(stderr, "unknown nbi instruction '%s'\n", o->op);
+ return -1;
+ }
+
+ return o->value;
+}
+
+/* convert register character like 'x' to value like 0x03 */
+static inline
+unsigned int register_enumerate_(char r) {
+ const char regs[] = "AaBbCcXxYyZzIiJj";
+ const char *x = strchr(regs, r);
+
+ if (x)
+ return (x - regs)/2;