+/* 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 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);
+ }
+
+ da = calloc(1, sizeof *da);
+ if (da == NULL) {
+ fprintf(stderr, "%s():%s\n", "calloc", strerror(errno));
+ return NULL;
+ }
+
+ da->entry_size = entry_size;
+ da->grow_size = grow_size;
+
+ da->a = malloc(da->entry_size * da->grow_size);
+ if (da->a == NULL) {
+ fprintf(stderr, "%s():%s\n", "malloc", strerror(errno));
+ }
+
+ da->allocated = grow_size;
+
+ DEBUG_PRINTF("allocated new dynarray:%p a:%p entry_size:%zu\n", da, da->a, da->entry_size);
+
+ return da;
+}
+
+/* copy item onto end of array */
+static
+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 NULL;
+ }
+ da->a = tmp_ptr;
+ da->allocated = new_allocated;
+
+ DEBUG_PRINTF("grew dynarray:%p\n", da);
+ }
+
+ dst = DYNARRAY_ITEM(*da, da->entries);
+ memcpy(dst, item, da->entry_size);
+
+ da->entries++;
+
+ DEBUG_PRINTF("added dynarray:%p entry:%zu item:%p\n", da, da->entries, item);
+
+ return dst;
+}
+
+
+/* locate and return the label entry matching name */
+static
+struct label_ *label_find_(struct dynamic_array_ *labels, char *name) {
+ size_t x;
+
+ 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;
+}
+
+
+/* if a label has a validly-calculated address, fetch it */
+static
+int label_addr_(struct dynamic_array_ *labels, char *name, DCPU16_WORD *addr) {
+ struct label_ *l;
+
+ 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 all labels */
+static
+void label_addr_calculate_(struct dynamic_array_ *instructionps, struct dynamic_array_ *labels) {
+ size_t i;
+
+ /* for each label.. */
+ for (i = 0; i < labels->entries; i++) {
+ struct label_ *l;
+ struct instruction_ **instr;
+ unsigned int word_count = 0;
+
+ 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 with the freshly calculated addr
+ */
+ 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, l->label)) {
+ DCPU16_WORD addr;
+
+ if (label_addr_(labels, (*instr)->label, &addr)) {
+ fprintf(stderr, "internal error: incomplete prior address for '%s' while calculating '%s'\n",
+ (*instr)->label,
+ l->label);
+ continue;
+ }
+
+ word_count += addr;
+ break;
+ }
+ }
+ l->addr = word_count;
+ l->ready = 1;
+ DEBUG_PRINTF("label '%s' now has addr of 0x%04x\n", l->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;
+
+ fprintf(stderr, "internal error, unknown register character 0x%02x\n", r);
+ return -1;
+}
+
+/* removes all occurences of chars from buf */
+static inline
+void buf_strip_chars_(char *buf, char *chars) {
+ char *s, *d;
+
+ for (s = d = buf; *s; s++, d++) {
+ while (*s && strchr(chars, *s)) {
+ s++;
+ }
+ if (!*s)
+ break;
+ *d = *s;
+ }
+ *d = *s;
+}
+
+/* value_bits_
+ * generate the six bits for a given operand string
+ * returns -1 if it could not parse the operand
+ * returns -2 if it could not parse the operand due to an unresolved label
+ * notes: nextword may be overwritten even if it's not used in final instruction
+ */
+static
+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;
+
+ unsigned long l;
+ char *o, *ep;
+
+ /*
+ Our operand working buffer shouldn't ever need to be too big,
+ but DAT might blow that assumption.
+ */
+ if (operand_sz <= strlen(operand_orig)) {
+ void *tmp_ptr;
+ size_t new_sz = strlen(operand_orig);
+
+ if (new_sz < 256)
+ new_sz = 256;
+ new_sz += 256;
+
+ DEBUG_PRINTF("%s: allocating buffer of size %zu\n", __func__, new_sz);
+ tmp_ptr = realloc(operand, new_sz);
+ if (tmp_ptr == NULL) {
+ fprintf(stderr, "%s(%zu):%s\n", "realloc", new_sz, strerror(errno));
+ return -1;
+ }
+ operand = tmp_ptr;
+ operand_sz = new_sz;
+ }
+
+ o = strcpy(operand, operand_orig);
+
+ DEBUG_PRINTF("%s: operand '%s' ", __func__, operand); /* completed later */
+
+ /* this is a very stupid parser */
+
+ /* first, let's trim all whitespace out of string at once to make parsing easier */
+ buf_strip_chars_(operand, " \t\n");
+
+ /* single character might match a register */
+ if (strlen(operand) == 1
+ && strchr("AaBbCcXxYyZzIiJj", *operand)) {
+ DEBUG_PRINTFQ("is register %c\n", *operand);
+ return register_enumerate_(*operand);
+ }
+
+ /* easy matches */
+ if (strcasecmp(operand, "POP") == 0) {
+ DEBUG_PRINTFQ("is POP\n");
+ return 0x18;
+ }
+ if (strcasecmp(operand, "PUSH") == 0) {
+ DEBUG_PRINTFQ("is PUSH\n");
+ return 0x19;
+ }
+ if (strcasecmp(operand, "PEEK") == 0) {
+ DEBUG_PRINTFQ("is PEEK\n");
+ return 0x1a;
+ }
+ if (strcasecmp(operand, "SP") == 0) {
+ DEBUG_PRINTFQ("is register SP\n");
+ return 0x1b;
+ }
+ if (strcasecmp(operand, "PC") == 0) {
+ DEBUG_PRINTFQ("is register PC\n");
+ return 0x1c;
+ }
+ if (strcasecmp(operand, "O") == 0) {
+ DEBUG_PRINTFQ("is register O\n");
+ return 0x1d;
+ }
+
+ /* is the operand [bracketed]? */
+ if (operand[0] == '[' && operand[strlen(operand) - 1] == ']') {
+ /* eat the brackets */
+ operand[strlen(operand) - 1] = '\0';
+ operand++;
+
+ /* is it [register]? */
+ if (strlen(operand) == 1
+ && strchr("AaBbCcXxYyZzIiJj", *operand)) {
+ DEBUG_PRINTFQ("is dereferenced register %c\n", *operand);
+ return 0x08 | register_enumerate_(*operand);
+ }
+
+ /* is it [register+something]? */
+ if ( (ep = strchr(operand, '+')) ) {
+ char *reg;
+ char *constant;
+
+ /* eat the plus */
+ *ep = '\0';
+ ep++;
+
+ /* figure out which one is which */
+ if (strlen(ep) == 1
+ && strchr("AaBbCcXxYyZzIiJj", *ep)) {
+ reg = ep;
+ constant = operand;
+ } else if (strlen(operand) == 1
+ && strchr("AaBbCcXxYyZzIiJj", *operand) ) {
+ reg = operand;
+ constant = ep;
+ } else {
+ DEBUG_PRINTFQ("is unparsable\n");
+ fprintf(stderr, "couldn't parse operand '%s'\n", operand_orig);
+ return -1;
+ }
+
+ /* check if something is understandable as a value */
+ errno = 0;
+ l = strtoul(constant, &ep, 0);
+ if (errno == 0
+ && (*constant && (*ep == '\0')) ) {
+ /* string conversion went without issue */
+ /* validate it will fit in a word */
+ if (l > 0xffff) {
+ DEBUG_PRINTFQ("is out of range\n");
+ fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig);
+ return -1;
+ }
+
+ /* seems fine */
+ *nextword = l & 0xffff;
+ *nextwordused += 1;
+ DEBUG_PRINTFQ("is a dereferenced register (%c) + constant (%hu)\n", *reg, *nextword);
+ return 0x10 | register_enumerate_(*reg);
+ } else if (errno) {
+ 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_addr_(labels, operand, nextword)) {
+ DEBUG_PRINTFQ("(deferred label resolution)\n");
+ *nextwordused += 1;
+ return -2;
+ }
+ DEBUG_PRINTFQ("is a dereferenced register (%c) + label\n", *reg);
+ *nextwordused += 1;
+ return 0x10 | register_enumerate_(*reg);
+ }
+
+ /* it must just be a dereferenced literal then */
+
+ errno = 0;
+ l = strtoul(operand, &ep, 0);
+ if (errno == 0
+ && (*operand && (*ep == '\0')) ) {
+ /* string conversion went without issue */
+ /* validate it will fit in a word */
+ if (l > 0xffff) {
+ DEBUG_PRINTFQ("is out of range\n");
+ fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig);
+ return -1;
+ }
+
+ DEBUG_PRINTFQ("is a dereferenced literal value (%hu)\n", *nextword);
+ *nextword = l & 0xffff;
+ *nextwordused += 1;
+ return 0x1e;
+ } else if (errno) {
+ 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_addr_(labels, operand, nextword)) {
+ DEBUG_PRINTFQ("(deferred label resolution)\n");
+ *nextwordused += 1;
+ return -2;
+ }
+ DEBUG_PRINTFQ("is a dereferenced label\n");
+ *nextwordused += 1;
+ return 0x1e;
+ }
+
+ /* left with a literal or a label, then */
+
+ errno = 0;
+ l = strtoul(operand, &ep, 0);
+ if (errno == 0
+ || (*operand && (*ep == '\0')) ) {
+ if (l > 0xffff) {
+ DEBUG_PRINTFQ("is out of range\n");
+ fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig);
+ return -1;
+ }
+
+ DEBUG_PRINTFQ("is literal value (%lu)\n", l);
+ if (l < 0x20) {
+ return l + 0x20;
+ }
+
+ *nextword = l & 0xffff;
+ *nextwordused += 1;
+ return 0x1f;
+ }
+
+ /* try to populate nextword with label address */
+ if (label_addr_(labels, operand, nextword)) {
+ DEBUG_PRINTFQ("(deferred label resolution)\n");
+ /* assume non-small literal value */
+ *nextwordused += 1;
+ return -2;
+ }
+
+ 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;
+ }
+
+ *nextwordused += 1;
+ return 0x1f;
+}
+
+/* prints an instruction's assembly */
+static inline
+int instruction_print_(struct instruction_ *i, unsigned int with_label) {
+ struct operand_ *o;
+ int r;
+
+ if (with_label)
+ r = printf("%-16s %3s", i->label ? i->label : "", i->opcode);
+ else
+ r = printf("%3s", i->opcode);
+
+ for (o = i->operands; o; o = o->next)
+ r += printf(" %s%s", o->operand, o->next ? "," : "");
+
+ return r;
+}
+
+/* parse an instruction out of buf, create new instruction struct if seemingly valid */
+/* does not actually check if instruction is valid yet */