X-Git-Url: http://git.squeep.com/?p=dcpu16;a=blobdiff_plain;f=as-dcpu16.c;h=37954aaf20b76fce9bb5410485c507f3e531f3fe;hp=4ed6050adf2ea078f219a709cae5ddfda8127c80;hb=HEAD;hpb=4706199a81dc631b6969927e1a6ad27591852b20 diff --git a/as-dcpu16.c b/as-dcpu16.c index 4ed6050..37954aa 100644 --- a/as-dcpu16.c +++ b/as-dcpu16.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -16,10 +17,14 @@ * 2012 04 07 - implementation started * 2012 04 10 - functional * 2012 04 16 - support dat statements + * 2012 05 05 - v1.7 revision started + * 2012 05 08 - v1.7 revision implemented * * TODO * needs ability to specify location for code or data + * needs ability to specify label as relative to another label * short labels not correctly computed + * in label struct, store index of instruction rather than ptr, ptrs for iteration in addr calculation are ugly */ static const char * const src_id_ = "$Id$"; @@ -68,6 +73,12 @@ void usage_(char *prog, unsigned int full) { } } +/* LSB-0 aaaaaabbbbbooooo */ +#define OPCODE_BITS 5 +#define OPERAND_B_BITS 5 +#define OPERAND_A_BITS 6 +#define N_BIT_MASK(__x__) ((1 << (__x__)) - 1) + /* instructions have operands */ struct operand_ { @@ -77,6 +88,7 @@ struct operand_ { /* keep an array of instructions as we read them in */ struct instruction_ { + size_t src_line; char *label; /* set if a label points here */ char *opcode; /* tokenized instruction text */ struct operand_ *operands; /* list of operands */ @@ -127,6 +139,8 @@ static void label_addr_calculate_(struct dynamic_array *instructionps, struct dynamic_array *labels) { size_t i; + /* idea: label1:label2 - calculated as offset between labels */ + /* for each label.. */ for (i = 0; i < labels->entries; i++) { struct label_ *l; @@ -135,9 +149,14 @@ void label_addr_calculate_(struct dynamic_array *instructionps, struct dynamic_a l = (struct label_ *)DYNARRAY_ITEM(*labels, i); + DEBUG_PRINTFQ("%s: calculating address of label '%s'\n", __func__, l->label); + +#if 0 +force full resolution while debugging /* if it's already calculated, great. */ if (l->ready) continue; +#endif /* * starting at the instruction for this label, @@ -145,14 +164,24 @@ void label_addr_calculate_(struct dynamic_array *instructionps, struct dynamic_a * 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--) { - if ((*instr)->ready) - DEBUG_PRINTF("%s: instr not ready\n", __func__); + /* first fetch the instruction associated with the label we want to know about.. */ + /* the addr of this instruction will be whatever follows all the preceding instructions */ + /* so back up one before counting instruction lengths... */ + instr = ((struct label_ *)DYNARRAY_ITEM(*labels, i))->instr; + /* is it the first one? */ + if (instr == (struct instruction_ **)DYNARRAY_ITEM(*instructionps, 0)) + break; + + instr--; + + while (instr >= (struct instruction_ **)DYNARRAY_ITEM(*instructionps, 0)) { + if ((*instr)->ready == 0) + DEBUG_PRINTF("%s: instr '%s' not ready\n", __func__, (*instr)->opcode); word_count += (*instr)->length; + DEBUG_PRINTF("%s: instr '%s' takes '%u' bytes\n", __func__, (*instr)->opcode, (*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 @@ -169,6 +198,7 @@ void label_addr_calculate_(struct dynamic_array *instructionps, struct dynamic_a word_count += addr; break; } + instr--; } l->addr = word_count; l->ready = 1; @@ -185,22 +215,41 @@ int opcode_bits_(char *opcode) { char value; } opcodes_lower_nibble[] = { { "JSR", 0x00 }, - /* { "future nbi instruction", 0x00 }, */ + { "INT", 0x00 }, + { "IAG", 0x00 }, + { "IAS", 0x00 }, + { "RFI", 0x00 }, + { "IAQ", 0x00 }, + { "HWN", 0x00 }, + { "HWQ", 0x00 }, + { "HWI", 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 }, + { "MLI", 0x05 }, + { "DIV", 0x06 }, + { "DVI", 0x07 }, + { "MOD", 0x08 }, + { "MDI", 0x09 }, + { "AND", 0x0a }, + { "BOR", 0x0b }, + { "XOR", 0x0c }, + { "SHR", 0x0d }, + { "ASR", 0x0e }, + { "SHL", 0x0f }, + { "IFB", 0x10 }, + { "IFC", 0x11 }, + { "IFE", 0x12 }, + { "IFN", 0x13 }, + { "IFG", 0x14 }, + { "IFA", 0x15 }, + { "IFL", 0x16 }, + { "IFU", 0x17 }, + { "ADX", 0x1a }, + { "SBX", 0x1b }, + { "STI", 0x1e }, + { "SDI", 0x1f }, { "", 0x00 } }, *o; @@ -226,6 +275,14 @@ int nbi_opcode_bits_(char *nbi_opcode) { } nbi_opcodes_bits[] = { { " ", 0x00 }, /* reserved for future */ { "JSR", 0x01 }, + { "INT", 0x08 }, + { "IAG", 0x09 }, + { "IAS", 0x0a }, + { "RFI", 0x0b }, + { "IAQ", 0x0c }, + { "HWN", 0x10 }, + { "HWQ", 0x11 }, + { "HWI", 0x12 }, { "", 0x00 } }, *o; @@ -277,6 +334,7 @@ void buf_strip_chars_(char *buf, char *chars) { * 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, const char *operand_orig, DCPU16_WORD *nextword, unsigned int *nextwordused, unsigned int allow_short_labels) { @@ -325,18 +383,49 @@ int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_W } /* easy matches */ - if (strcasecmp(operand, "POP") == 0) { + + /* push and pop now share the same operand value */ + if (strcasecmp(operand, "POP") == 0 + || strcasecmp(operand, "[SP++]") == 0) { DEBUG_PRINTFQ("is POP\n"); return 0x18; } - if (strcasecmp(operand, "PUSH") == 0) { + if (strcasecmp(operand, "PUSH") == 0 + || strcasecmp(operand, "[--SP]") == 0) { DEBUG_PRINTFQ("is PUSH\n"); - return 0x19; + return 0x18; } - if (strcasecmp(operand, "PEEK") == 0) { + + if (strcasecmp(operand, "PEEK") == 0 + || strcasecmp(operand, "[SP]") == 0) { DEBUG_PRINTFQ("is PEEK\n"); + return 0x19; + } + + /* this could be better, if we had a real token tree */ + if (strncasecmp(operand, "PICK", 4) == 0) { + DEBUG_PRINTFQ("is PICK "); + + errno = 0; + l = strtoul(operand + 4, &ep, 0); + if (errno == 0 + && (*(operand + 4) && (*ep == '\0')) ) { + if (l > 0xffff) { + DEBUG_PRINTFQ("(out of range)\n"); + fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig); + return -1; + } + } else if (errno == ERANGE) { + DEBUG_PRINTFQ("(out of range)\n"); + fprintf(stderr, "constant invalid in operand '%s'\n", operand_orig); + return -1; + } + *nextword = l & 0xffff; + *nextwordused += 1; + DEBUG_PRINTFQ("0x%04x\n", *nextword); return 0x1a; } + if (strcasecmp(operand, "SP") == 0) { DEBUG_PRINTFQ("is register SP\n"); return 0x1b; @@ -345,8 +434,8 @@ int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_W DEBUG_PRINTFQ("is register PC\n"); return 0x1c; } - if (strcasecmp(operand, "O") == 0) { - DEBUG_PRINTFQ("is register O\n"); + if (strcasecmp(operand, "EX") == 0) { + DEBUG_PRINTFQ("is register EX\n"); return 0x1d; } @@ -368,17 +457,19 @@ int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_W char *reg; char *constant; + DEBUG_PRINTFQ("is multipart.. "); + /* eat the plus */ *ep = '\0'; ep++; /* figure out which one is which */ - if (strlen(ep) == 1 - && strchr("AaBbCcXxYyZzIiJj", *ep)) { + if ((strlen(ep) == 1 && strchr("AaBbCcXxYyZzIiJj", *ep)) + || (strlen(ep) == 2 && strcasecmp(ep, "SP")) ) { reg = ep; constant = operand; - } else if (strlen(operand) == 1 - && strchr("AaBbCcXxYyZzIiJj", *operand) ) { + } else if ((strlen(operand) == 1 && strchr("AaBbCcXxYyZzIiJj", *operand)) + || (strlen(operand) == 2 && strcasecmp(operand, "SP")) ) { reg = operand; constant = ep; } else { @@ -403,12 +494,17 @@ int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_W /* seems fine */ *nextword = l & 0xffff; *nextwordused += 1; + + /* special case [SP+n]/PICK n */ + if (strlen(reg) == 2) { + DEBUG_PRINTFQ("is PICK 0x%04x\n", *nextword); + return 0x1a; + } + 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; + } else if (errno == ERANGE) { + fprintf(stderr, "%s('%s'):%s\n", "strtoul", constant, strerror(errno)); } /* what? still here? assume it's a label, I guess */ @@ -442,8 +538,7 @@ int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_W *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)); + /* if number wasn't parsable, just fall through and assume it's a label */ } /* not a number? try a label */ @@ -470,8 +565,11 @@ int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_W } DEBUG_PRINTFQ("is literal value (%lu)\n", l); - if (l < 0x20) { - return l + 0x20; + if (l < 0x1f) { + return l + 0x21; + } + if (l == 0xffff) { + return 0x20; } *nextword = l & 0xffff; @@ -488,9 +586,15 @@ int value_bits_(struct dynamic_array *labels, const char *operand_orig, DCPU16_W } DEBUG_PRINTFQ("is label '%s' (0x%02hx)\n", operand, *nextword); - if (*nextword < 0x20 && allow_short_labels) { + if (allow_short_labels + && (*nextword < 0x1f) ) { + DEBUG_PRINTF("small value label win\n"); + return (0x21 + *nextword) & N_BIT_MASK(OPERAND_A_BITS); + } + if (allow_short_labels + && (*nextword == 0xffff) ) { DEBUG_PRINTF("small value label win\n"); - return (0x20 + *nextword) & 0x3f; + return 0x20; } *nextwordused += 1; @@ -504,153 +608,177 @@ int instruction_print_(struct instruction_ *i, unsigned int with_label) { int r; if (with_label) - r = printf("%-16s %3s", i->label ? i->label : "", i->opcode); - else - r = printf("%3s", i->opcode); + r = printf("%-16s ", i->label ? i->label : ""); + + r = printf("%3s", i->opcode ? i->opcode : ""); for (o = i->operands; o; o = o->next) r += printf(" %s%s", o->operand, o->next ? "," : ""); + if (i->ready) { + DCPU16_WORD l; + printf(" ["); + l = dcpu16_mnemonify_buf(i->instr_words); + printf("]"); + + if (i->length != l) + DEBUG_PRINTF("!!internal inconsistency!! i->length:%u l:%hu should match\n", i->length, l); + } return r; } -/* buf_tokenize_ - * Parses the zero-terminated line of input 'buf' into a newly-allocated struct instruction_. - * [label] opcode [operand[,operand[,...]]] - * Does not yet validate if labels, opcodes, or operands are valid... +/* tokenize_line_ + * Parses a zero-terminated line of input into a newly-allocated struct instruction_. + * [label] instruction [operand[,operand[,...]]] + * Does no validation of contents of any of these tokens, as of yet. + * does not clean up after itself if a malloc fails */ static -int buf_tokenize_(char *buf, struct instruction_ **next_instr) { - const char const *sep = " \t\n"; - const char const *quot = "'`\""; +int tokenize_line_(char *line, struct instruction_ **next_instr) { + const char const *whitespace = " \t\n"; + const char const *quotes = "\"'`"; struct instruction_ *instr = NULL; + char *x, *st, *qt; + char *label, *opcode; struct operand_ *operand_list = NULL; - char *label = NULL, - *opcode = NULL, - *operand = NULL; - char *x, - *y, - *st, *qt; - size_t instr_words_needed = 1; + struct operand_ **operand_tail = &operand_list; + size_t instr_words_needed = 0; - assert(buf != NULL); - assert(next_instr != NULL); + assert(line); + assert(next_instr); *next_instr = NULL; - /* kill leading whitespace */ - buf += strspn(buf, " \t\n"); - - /* kill trailing whitespace */ - for (x = buf + strlen(buf); *x && strchr(sep, *x); x--) - *x = '\0'; + /* strip leading whitespace */ + line += strspn(line, whitespace); + if (*line == '\0') + return 0; - /* split on first non-quoted ';', ignore following comment */ - x = strqtok_r(buf, ";", '\\', quot, &qt, &st); - if (x == NULL) + /* set first bare ';' to '\0', thus isolating any comments */ + /* here we only care about the side-effect of truncating the first separator character */ + (void)strqtok_r(line, ";", '\\', quotes, &qt, &st); + /* we don't care if there was an unmatched quote at this point, let's see what happens */ + if (*line == '\0') return 0; - if (qt) { - fprintf(stderr, "unmatched %c-quote\n", *qt); - return -1; - } - /* determine if first token is label, opcode, or we just have a blank line to ignore */ - x = strqtok_r(x, sep, '\\', quot, &qt, &st); - if (x == NULL) + /* carve off the first token, determine if it is a label */ + x = strqtok_r(line, whitespace, '\\', quotes, &qt, &st); + if (x == NULL || *x == '\0') return 0; if (qt) { - fprintf(stderr, "unmatched %c-quote\n", *qt); - return -1; + /* labels could contain an unmatched quote character, I guess? */ + qt = NULL; } -/* I want c-style labels in my asm, but example in spec uses : in prefix rather than postfix */ + /* we have something, try to make sense of what it is */ + #ifdef NON_SPEC_LABELS - /* labels end with :, otherwise its an opcode */ - y = x + strlen(x) - 1; - if (*y == ':') { - *y = '\0'; + /* I want my labels like 'label:' */ + if ( *(x + strlen(line) - 1) == ':' ) { + *(x + strlen(line) - 1) = '\0'; + DEBUG_PRINTF("label: %s\n", x); + label = x; - opcode = strqtok_r(NULL, sep, '\\', quot, &qt, &st); + + opcode = strqtok_r(NULL, whitespace, '\\', quotes, &qt, &st); } else { label = NULL; opcode = x; } -#else /* NON_SPEC_LABELS */ - /* labels.. begin? with ':' ? okay, I guess. Whatever. */ - /* otherwise, it's an opcode */ +#endif /* NON_SPEC_LABELS */ + + /* spec gives example of labels as ':label' */ if (*x == ':') { - label = x + 1; - opcode = strqtok_r(NULL, sep, '\\', quot, &qt, &st); + *x = '\0'; + x++; + label = x; + opcode = strqtok_r(NULL, whitespace, '\\', quotes, &qt, &st); } else { label = NULL; opcode = x; } -#endif /* NON_SPEC_LABELS */ + /* opcodes shouldn't have quotes, so we'll ignore any unmatched quotes again */ - if (opcode) { - operand = st; - } + if (opcode && *opcode) { + /* if we have an opcode, we'll need at least one word to compile instruction */ + instr_words_needed++; - /* - While normal instructions just have comma-separated operands, - DAT can be followed by comma-separated list of: - label, to be resolved to address - value, like 0xffff - string, "quoted", characters to be rendered into low-byte of words - */ - - if (operand) { - struct operand_ **o_next = &operand_list; + /* build a list of operands to hang off this instruction */ + while ( (x = strqtok_r(NULL, ",", '\\', quotes, &qt, &st)) ) { + struct operand_ *new_operand; + char *y; - for (x = strqtok_r(operand, ",", '\\', quot, &qt, &st); - x; - x = strqtok_r(NULL, ",", '\\', quot, &qt, &st) ) { + /* trim whitespaces */ + x += strspn(x, whitespace); - /* trim leading whitespace */ - x += strspn(x, " \t\n"); + if (*x) { + for (y = x + strlen(x) - 1; *y; y--) { + if (strchr(whitespace, *y)) { + *y = '\0'; + } + } + } + /* nothing left? */ if (*x == '\0') { - fprintf(stderr, "encountered empty operand\n"); + fprintf(stderr, "null operand encountered\n"); return -1; } - /* trim trailing whitespace */ - y = x + strlen(x) - 1; - while (strchr(" \t\n", *y)) { - *y = '\0'; - y--; - } + DEBUG_PRINTF("tokenized operand '%s'\n", x); - /* new operand to append to list */ - *o_next = malloc(sizeof **o_next); - if (*o_next == NULL) { - fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); + new_operand = malloc(sizeof *new_operand); + if (new_operand == NULL) { + fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); return -1; } - /* assume an operand uses one word, unless it's a string */ - instr_words_needed += (*x == '"') ? strlen(x) : 1; - - (*o_next)->operand = strdup(x); - if ((*o_next)->operand == NULL) { + new_operand->operand = strdup(x); + if (new_operand->operand == NULL) { fprintf(stderr, "%s():%s\n", "strdup", strerror(errno)); return -1; } - (*o_next)->next = NULL; - o_next = &((*o_next)->next); + + new_operand->next = NULL; + + if (strchr(quotes, x[0])) { + /* if this is a quoted operand, assuming we are in a DAT statement, it will take up slightly less room than it is long */ + instr_words_needed += strlen(x) - 1; + } + instr_words_needed++; + + *operand_tail = new_operand; + operand_tail = &(*operand_tail)->next; } } - DEBUG_PRINTF("allocating instr with room for %zu words\n", instr_words_needed); + DEBUG_PRINTF("allocating new instruction with room for %zu bytes\n", instr_words_needed); - /* extra room for assembled words */ instr = calloc(1, (instr_words_needed * sizeof *instr->instr_words) + sizeof *instr); if (instr == NULL) { - fprintf(stderr, "%s():%s\n", "calloc", strerror(errno)); + fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); return -1; } - instr->label = label ? strdup(label) : NULL; - instr->opcode = opcode ? strdup(opcode) : NULL; + if (label) { + instr->label = strdup(label); + if (instr->label == NULL) { + fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); + return -1; + } + } else { + label = NULL; + } + + if (opcode) { + instr->opcode = strdup(opcode); + if (instr->opcode == NULL) { + fprintf(stderr, "%s():%s\n", "malloc", strerror(errno)); + return -1; + } + } else { + opcode = NULL; + } + instr->operands = operand_list; *next_instr = instr; @@ -659,6 +787,7 @@ int buf_tokenize_(char *buf, struct instruction_ **next_instr) { } /* try to generate bytecode for an instruction */ +/* returns -1 on unrecoverable error */ static int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsigned int allow_short_labels) { unsigned int nwu = 0; /* number of words used */ @@ -667,13 +796,17 @@ int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsign struct operand_ *o = i->operands; if (opt_.verbose > 2) { - printf("%s: assembling %p ", __func__, i); + printf("%s: assembling %p ", __func__, (void *)i); instruction_print_(i, 1); - printf("\n"); + printf("(line %zu)\n", i->src_line); } - if (i->ready) { - /* already assembled, nothing to do */ + if (i->opcode == NULL) { + assert(i->label); + assert(i->operands == NULL); + /* just a label, move along */ + i->length = 0; + i->ready = 1; return 0; } @@ -683,14 +816,15 @@ int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsign i->length = 0; - while (o) { + for ( /* */ ; o; o = o->next) { size_t j, dat_len; char *x; unsigned long l; - DEBUG_PRINTF("DAT operand:'%s' next:%p\n", o->operand, o->next); + DEBUG_PRINTF("DAT operand:'%s' next:%p\n", o->operand, (void *)o->next); /* is this a string? */ + /* does it start with a quote, and end with the same quote? */ if ( (x = strchr("\"'`", o->operand[0])) ) { dat_len = strlen(o->operand) - 1; if (o->operand[dat_len] == *x) { @@ -700,14 +834,16 @@ int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsign for (j = 0, x = o->operand + 1; j < dat_len - 1; j++, x++) { - i->instr_words[i->length] = (DCPU16_WORD)*x; + i->instr_words[i->length] = *x; i->length++; } + /* Note that strings in DAT do not include their zero-terminators */ + /* specify as 'DAT "string", 0' */ } - o = o->next; continue; } + /* is this a number? */ char *ep; errno = 0; l = strtoul(o->operand, &ep, 0); @@ -718,20 +854,27 @@ int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsign fprintf(stderr, "value '%lu' out of range\n", l); return -1; } + i->instr_words[i->length] = l; + i->length++; + continue; } - fprintf(stderr, "FIXME finish implementing DAT\n"); - /* check if it's a parsable number */ - - /* otherwise assume it's a label */ - - + /* otherwise assume it's a label, even if we don't know what it is */ + if (label_addr_(labels, o->operand, &i->instr_words[i->length])) { + DEBUG_PRINTF("(deferred label '%s' resolution)\n", o->operand); + incomplete = 1; + } + i->length++; + } - o = o->next; + if (incomplete) { + DEBUG_PRINTF("pending label address\n"); + } else { + i->ready = 1; } return 0; - } + } /* end of DAT */ /* start with opcode bits */ bits = opcode_bits_(i->opcode); @@ -742,10 +885,10 @@ int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsign fprintf(stderr, "'\n"); return -1; } - i->instr_words[0] |= 0x0f & bits; + i->instr_words[0] |= bits & N_BIT_MASK(OPCODE_BITS); - /* in rendered bytecode, all instructions have two operands; nbi instructions take 'first operand' bits. */ - if ((bits & 0x0f) == 0) { + /* in rendered bytecode, all instructions have a and b operands; nbi instructions occupy 'b operand' bits. */ + if ((bits & N_BIT_MASK(OPCODE_BITS)) == 0) { bits = nbi_opcode_bits_(i->opcode); if (bits < 0) { fprintf(stderr, "INTERNAL ERROR: missing instruction in nbi opcode table\n"); @@ -768,7 +911,11 @@ int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsign } o = o->next; } - i->instr_words[0] |= (bits & 0x3f) << 4; + if (bits > N_BIT_MASK(OPERAND_B_BITS)) { + fprintf(stderr, "%s: internal error: operand '%s' generated out of range\n", __func__, "b"); + return -1; + } + i->instr_words[0] |= (bits & N_BIT_MASK(OPERAND_B_BITS)) << OPCODE_BITS; if (o == NULL) { fprintf(stderr, "'%s' requires more operands\n", i->opcode); @@ -786,7 +933,10 @@ int instr_assemble_(struct dynamic_array *labels, struct instruction_ *i, unsign bits = 0; } o = o->next; - i->instr_words[0] |= (bits & 0x3f) << 10; + if (bits > N_BIT_MASK(OPERAND_A_BITS)) { + fprintf(stderr, "%s: internal error: operand '%s' generated out of range\n", __func__, "a"); + } + i->instr_words[0] |= (bits & N_BIT_MASK(OPERAND_A_BITS)) << (OPCODE_BITS + OPERAND_B_BITS); if (o != NULL) { fprintf(stderr, "too many operands\n"); @@ -834,13 +984,14 @@ int parse_stream_(FILE *f, const char *src, struct dynamic_array *instructionps, break; } - if (buf_tokenize_(buf, &instr)) { + if (tokenize_line_(buf, &instr)) { fprintf(stderr, "%s:%u:%s", src, line, "trouble tokenizing input\n"); retval = -1; break; } if (instr) { + instr->src_line = line; /* add to list of instructions */ instr_list_entry = dynarray_add(instructionps, &instr); if (instr_list_entry == NULL) { @@ -867,7 +1018,10 @@ int parse_stream_(FILE *f, const char *src, struct dynamic_array *instructionps, label_addr_calculate_(instructionps, labels); } - instr_assemble_(labels, instr, allow_short_labels); + if (instr_assemble_(labels, instr, allow_short_labels)) { + fprintf(stderr, "%s:%u:%s", src, line, "could not assemble instruction\n"); + break; + } } } if (ferror(f)) { @@ -904,9 +1058,14 @@ int assemble_check_(struct dynamic_array *instructionps, struct dynamic_array *l DEBUG_PRINTF(" final pass of assembler...\n"); for (x = 0; x < instructionps->entries; x++) { struct instruction_ **instrp = (struct instruction_ **)DYNARRAY_ITEM(*instructionps, x); - retval |= instr_assemble_(labels, *instrp, allow_short_labels); + retval = instr_assemble_(labels, *instrp, allow_short_labels); if (retval) { fprintf(stderr, "instruction %zu failed to assemble\n", x); + return retval; + } + if (! (*instrp)->ready) { + fprintf(stderr, "instruction not resolvable at line %lu\n", (*instrp)->src_line); + return -1; } } @@ -933,6 +1092,9 @@ int assemble_check_(struct dynamic_array *instructionps, struct dynamic_array *l return retval; } +/* output_ + * write assembled words to named file + */ static int output_(struct dynamic_array *instructionps, const char *filename) { FILE *of = NULL; @@ -1048,13 +1210,18 @@ int main(int argc, char *argv[]) { } VERBOSE_PRINTF("assembling '%s'...\n", filename); - parse_stream_(f, filename, instructionps_, labels_, allow_short_labels); - + c = parse_stream_(f, filename, instructionps_, labels_, allow_short_labels); fclose(f); + if (c) + break; } } else { VERBOSE_PRINTF("assembling '%s'...\n", "stdin"); - parse_stream_(stdin, "-", instructionps_, labels_, allow_short_labels); + c = parse_stream_(stdin, "-", instructionps_, labels_, allow_short_labels); + } + if (c) { + fprintf(stderr, "could not parse input, aborting\n"); + exit(EX_DATAERR); } if (assemble_check_(instructionps_, labels_, allow_short_labels)) {