#include <unistd.h>
#include <stdio.h>
#include <string.h>
+#include <strings.h>
#include <errno.h>
#include <sysexits.h>
#include <assert.h>
*
* 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$";
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;
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,
* 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
word_count += addr;
break;
}
+ instr--;
}
l->addr = word_count;
l->ready = 1;
char *reg;
char *constant;
+ DEBUG_PRINTFQ("is multipart.. ");
+
/* eat the plus */
*ep = '\0';
ep++;
*nextwordused += 1;
DEBUG_PRINTFQ("is a dereferenced register (%c) + constant (%hu)\n", *reg, *nextword);
return 0x10 | register_enumerate_(*reg);
- } else if (errno) {
+ } else if (errno == ERANGE) {
+#if 0
+oh, right, labels fall through
DEBUG_PRINTFQ("is out of range\n");
fprintf(stderr, "trouble with operand '%s': %s\n", operand_orig, strerror(errno));
return -1;
+#endif
}
/* what? still here? assume it's a label, I guess */
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 ? "," : "");
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;
- struct operand_ **o_next = &operand_list;
- char *label = NULL,
- *opcode = 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, sep);
-
- /* locate first non-quoted ';', ignore anything following it */
- x = strqtok_r(buf, ";", '\\', quot, &qt, &st);
- if (x == NULL)
- return 0;
- if (qt) {
- fprintf(stderr, "unmatched %c-quote\n", *qt);
- return -1;
- }
- if (*buf == '\0')
+ /* strip leading whitespace */
+ line += strspn(line, whitespace);
+ if (*line == '\0')
return 0;
- /* kill trailing whitespace */
- for (x = buf + strlen(buf) - 1; *x && strchr(sep, *x); x--)
- *x = '\0';
- if (*buf == '\0')
+ /* 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;
- DEBUG_PRINTF("trimmed buf: '%s'\n", buf);
-
- /* determine if first token is label, opcode, or we just have a blank line to ignore */
- x = strqtok_r(buf, sep, '\\', quot, &qt, &st);
+ /* 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 '%s'\n", *qt, 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 == ':') {
- DEBUG_PRINTF("found label '%s'\n", 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);
- if (qt) {
- fprintf(stderr, "unmatched %c-quote '%s'\n", *qt, qt);
- return -1;
- }
+
+ 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 == ':') {
- DEBUG_PRINTF("found label '%s'\n", x);
- label = x + 1;
- opcode = strqtok_r(NULL, sep, '\\', quot, &qt, &st);
- if (qt) {
- fprintf(stderr, "unmatched %c-quote '%s'\n", *qt, qt);
- return -1;
- }
+ *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 ( !label && (!opcode || !*opcode) ) {
- DEBUG_PRINTF("no label nor instruction?\n");
- return 0;
- }
+ if (opcode && *opcode) {
+ /* if we have an opcode, we'll need at least one word to compile instruction */
+ instr_words_needed++;
- DEBUG_PRINTF("label:'%s' opcode:'%s' operands:'%s'\n", label, opcode, st);
+ /* build a list of operands to hang off this instruction */
+ while ( (x = strqtok_r(NULL, ",", '\\', quotes, &qt, &st)) ) {
+ struct operand_ *new_operand;
+ char *y;
- /*
- 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
- */
+ /* trim whitespaces */
+ x += strspn(x, whitespace);
- while ( (x = strqtok_r(NULL, ",", '\\', quot, &qt, &st)) ) {
- DEBUG_PRINTF("\tx:'%s' qt:'%s' st:'%s'\n", x, qt, st);
+ if (*x) {
+ for (y = x + strlen(x) - 1; *y; y--) {
+ if (strchr(whitespace, *y)) {
+ *y = '\0';
+ }
+ }
+ }
+ /* nothing left? */
+ if (*x == '\0') {
+ fprintf(stderr, "null operand encountered\n");
+ return -1;
+ }
- if (qt) {
- fprintf(stderr, "unmatched %c-quote '%s'\n", *qt, qt);
- return -1;
- }
+ DEBUG_PRINTF("tokenized operand '%s'\n", x);
- /* trim trailing whitespace */
- y = x + strlen(x) - 1;
- while (strchr(sep, *y)) {
- *y = '\0';
- y--;
- }
+ new_operand = malloc(sizeof *new_operand);
+ if (new_operand == NULL) {
+ fprintf(stderr, "%s():%s\n", "malloc", strerror(errno));
+ return -1;
+ }
- /* new operand to append to list */
- *o_next = malloc(sizeof **o_next);
- if (*o_next == NULL) {
- fprintf(stderr, "%s():%s\n", "calloc", strerror(errno));
- return -1;
- }
+ new_operand->operand = strdup(x);
+ if (new_operand->operand == NULL) {
+ fprintf(stderr, "%s():%s\n", "strdup", strerror(errno));
+ return -1;
+ }
- /* assume an operand takes up one word, unless it's a string */
- /* if it's a string, it comes with quotes, which will get stripped, but will include trailing zero */
- instr_words_needed += (*x == '"') ? strlen(x) - 1 : 1;
+ new_operand->next = NULL;
- (*o_next)->operand = strdup(x);
- if ((*o_next)->operand == NULL) {
- fprintf(stderr, "%s():%s\n", "strdup", strerror(errno));
- return -1;
+ 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;
}
- (*o_next)->next = NULL;
- o_next = &((*o_next)->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;
if (opt_.verbose > 2) {
printf("%s: assembling %p ", __func__, i);
instruction_print_(i, 1);
- printf("(line :%zu)\n", i->src_line);
+ printf("(line %zu)\n", i->src_line);
}
+#if 0
+while debugging, always reassemble
if (i->ready) {
/* already assembled, nothing to do */
return 0;
}
+#endif
+
+ if (i->opcode == NULL) {
+ assert(i->label);
+ assert(i->operands == NULL);
+ /* just a label, move along */
+ i->length = 0;
+ i->ready = 1;
+ return 0;
+ }
/* special case DAT */
if (strncasecmp(i->opcode, "DAT", 3) == 0) {
DEBUG_PRINTF("DAT operand:'%s' next:%p\n", o->operand, 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) {
/* 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 resolution)\n");
+ DEBUG_PRINTF("(deferred label '%s' resolution)\n", o->operand);
incomplete = 1;
}
i->length++;
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;
VERBOSE_PRINTF("assembling '%s'...\n", filename);
c = parse_stream_(f, filename, instructionps_, labels_, allow_short_labels);
+ fclose(f);
if (c)
break;
- fclose(f);
}
} else {
VERBOSE_PRINTF("assembling '%s'...\n", "stdin");