13 * quick and dirty assembler for dcpu16
15 * Justin Wind <justin.wind@gmail.com>
16 * 2012 04 07 - implementation started
17 * 2012 04 10 - functional
18 * 2012 04 16 - support dat statements
21 * needs ability to specify location for code or data
22 * short labels not correctly computed
25 static const char * const src_id_
= "$Id$";
27 const char const out_filename_default_
[] = "a.out";
29 /* global invocation options */
38 #define DEBUG_PRINTF(...) do { if (opt_.verbose > 2) { printf("DEBUG: "); printf(__VA_ARGS__); } } while (0)
39 #define DEBUG_PRINTFQ(...) do { if (opt_.verbose > 2) printf(__VA_ARGS__); } while (0)
40 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
43 void usage_(char *prog
, unsigned int full
) {
44 FILE *f
= full
? stdout
: stderr
;
45 char *x
= strrchr(prog
, '/');
51 fprintf(f
, "%s -- \n\n",
54 fprintf(f
, "Usage: %s [-h] [-v] [-s] [-o file] file [file [...]]\n",
58 fprintf(f
, "\nOptions:\n"
59 "\t-h -- this screen\n"
60 "\t-o <file> -- output to <file> [default: %s]\n"
61 "\t-s -- allow short labels in instruction words\n"
62 "\t-d -- dry run, print results, do not write to file\n"
63 "\t-v -- verbose output\n",
64 out_filename_default_
);
66 fprintf(f
, "\n%78s\n",
72 /* instructions have operands */
74 struct operand_
*next
;
75 char *operand
; /* tokenized operand text */
78 /* keep an array of instructions as we read them in */
80 char *label
; /* set if a label points here */
81 char *opcode
; /* tokenized instruction text */
82 struct operand_
*operands
; /* list of operands */
83 unsigned int ready
: 1; /* bytecode computed? */
84 unsigned int length
; /* number of words of bytecode */
85 DCPU16_WORD instr_words
[];
88 /* keep an array of labels, indexed back to their instruction locations */
90 char *label
; /* name of label */
91 struct instruction_
**instr
; /* pointer into array of instructions */
92 unsigned int ready
: 1; /* do we know where this label is yet? */
97 /* locate and return the label entry matching name */
99 struct label_
*label_find_(struct dynamic_array
*labels
, char *name
) {
102 for (x
= 0; x
< labels
->entries
; x
++) {
103 struct label_
*l
= (struct label_
*)DYNARRAY_ITEM(*labels
, x
);
104 if (strcmp(l
->label
, name
) == 0)
111 /* if a label has a validly-calculated address, fetch it */
113 int label_addr_(struct dynamic_array
*labels
, char *name
, DCPU16_WORD
*addr
) {
116 if ( (l
= label_find_(labels
, name
)) == NULL
)
125 /* attempt to determine the addresses of all labels */
127 void label_addr_calculate_(struct dynamic_array
*instructionps
, struct dynamic_array
*labels
) {
130 /* for each label.. */
131 for (i
= 0; i
< labels
->entries
; i
++) {
133 struct instruction_
**instr
;
134 unsigned int word_count
= 0;
136 l
= (struct label_
*)DYNARRAY_ITEM(*labels
, i
);
138 /* if it's already calculated, great. */
143 * starting at the instruction for this label,
144 * walk backwards through the list of instructions
145 * until we get to the start or a known prior label address.
146 * update our label with the freshly calculated addr
148 for (instr
= ((struct label_
*)DYNARRAY_ITEM(*labels
, i
))->instr
;
149 instr
>= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, 0);
153 DEBUG_PRINTF("%s: instr not ready\n", __func__
);
154 word_count
+= (*instr
)->length
;
156 /* have we come across an instruction which a label points to?
157 it should already be calculated, so just add that on and be done */
159 && strcmp((*instr
)->label
, l
->label
)) {
162 if (label_addr_(labels
, (*instr
)->label
, &addr
)) {
163 fprintf(stderr
, "internal error: incomplete prior address for '%s' while calculating '%s'\n",
173 l
->addr
= word_count
;
175 DEBUG_PRINTF("label '%s' now has addr of 0x%04x\n", l
->label
, word_count
);
180 /* generate the nibble for a given basic opcode */
182 int opcode_bits_(char *opcode
) {
186 } opcodes_lower_nibble
[] = {
188 /* { "future nbi instruction", 0x00 }, */
207 for (o
= opcodes_lower_nibble
; o
->op
[0]; o
++) {
208 if (strcasecmp(o
->op
, opcode
) == 0)
212 if (o
->op
[0] == '\0') {
213 fprintf(stderr
, "unknown instruction '%s'\n", opcode
);
220 /* generate the six bits for a given nbi opcode (aka first operand to opcode 0x00) */
222 int nbi_opcode_bits_(char *nbi_opcode
) {
226 } nbi_opcodes_bits
[] = {
227 { " ", 0x00 }, /* reserved for future */
232 for (o
= nbi_opcodes_bits
; o
->op
[0]; o
++) {
233 if (strcasecmp(o
->op
, nbi_opcode
) == 0)
237 if (o
->op
[0] == '\0') {
238 fprintf(stderr
, "unknown nbi instruction '%s'\n", o
->op
);
245 /* convert register character like 'x' to value like 0x03 */
247 unsigned int register_enumerate_(char r
) {
248 const char regs
[] = "AaBbCcXxYyZzIiJj";
249 const char *x
= strchr(regs
, r
);
254 fprintf(stderr
, "internal error, unknown register character 0x%02x\n", r
);
258 /* removes all occurences of chars from buf */
260 void buf_strip_chars_(char *buf
, char *chars
) {
263 for (s
= d
= buf
; *s
; s
++, d
++) {
264 while (*s
&& strchr(chars
, *s
)) {
276 * generate the six bits for a given operand string
277 * returns -1 if it could not parse the operand
278 * returns -2 if it could not parse the operand due to an unresolved label
279 * notes: nextword may be overwritten even if it's not used in final instruction
282 int value_bits_(struct dynamic_array
*labels
, const char *operand_orig
, DCPU16_WORD
*nextword
, unsigned int *nextwordused
, unsigned int allow_short_labels
) {
283 static char *operand
= NULL
;
284 static size_t operand_sz
= 0;
290 Our operand working buffer shouldn't ever need to be too big,
291 but DAT might blow that assumption.
293 if (operand_sz
<= strlen(operand_orig
)) {
295 size_t new_sz
= strlen(operand_orig
);
301 DEBUG_PRINTF("%s: allocating buffer of size %zu\n", __func__
, new_sz
);
302 tmp_ptr
= realloc(operand
, new_sz
);
303 if (tmp_ptr
== NULL
) {
304 fprintf(stderr
, "%s(%zu):%s\n", "realloc", new_sz
, strerror(errno
));
311 o
= strcpy(operand
, operand_orig
);
313 DEBUG_PRINTF("%s: operand '%s' ", __func__
, operand
); /* completed later */
315 /* this is a very stupid parser */
317 /* first, let's trim all whitespace out of string at once to make parsing easier */
318 buf_strip_chars_(operand
, " \t\n");
320 /* single character might match a register */
321 if (strlen(operand
) == 1
322 && strchr("AaBbCcXxYyZzIiJj", *operand
)) {
323 DEBUG_PRINTFQ("is register %c\n", *operand
);
324 return register_enumerate_(*operand
);
328 if (strcasecmp(operand
, "POP") == 0) {
329 DEBUG_PRINTFQ("is POP\n");
332 if (strcasecmp(operand
, "PUSH") == 0) {
333 DEBUG_PRINTFQ("is PUSH\n");
336 if (strcasecmp(operand
, "PEEK") == 0) {
337 DEBUG_PRINTFQ("is PEEK\n");
340 if (strcasecmp(operand
, "SP") == 0) {
341 DEBUG_PRINTFQ("is register SP\n");
344 if (strcasecmp(operand
, "PC") == 0) {
345 DEBUG_PRINTFQ("is register PC\n");
348 if (strcasecmp(operand
, "O") == 0) {
349 DEBUG_PRINTFQ("is register O\n");
353 /* is the operand [bracketed]? */
354 if (operand
[0] == '[' && operand
[strlen(operand
) - 1] == ']') {
355 /* eat the brackets */
356 operand
[strlen(operand
) - 1] = '\0';
359 /* is it [register]? */
360 if (strlen(operand
) == 1
361 && strchr("AaBbCcXxYyZzIiJj", *operand
)) {
362 DEBUG_PRINTFQ("is dereferenced register %c\n", *operand
);
363 return 0x08 | register_enumerate_(*operand
);
366 /* is it [register+something]? */
367 if ( (ep
= strchr(operand
, '+')) ) {
375 /* figure out which one is which */
377 && strchr("AaBbCcXxYyZzIiJj", *ep
)) {
380 } else if (strlen(operand
) == 1
381 && strchr("AaBbCcXxYyZzIiJj", *operand
) ) {
385 DEBUG_PRINTFQ("is unparsable\n");
386 fprintf(stderr
, "couldn't parse operand '%s'\n", operand_orig
);
390 /* check if something is understandable as a value */
392 l
= strtoul(constant
, &ep
, 0);
394 && (*constant
&& (*ep
== '\0')) ) {
395 /* string conversion went without issue */
396 /* validate it will fit in a word */
398 DEBUG_PRINTFQ("is out of range\n");
399 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
404 *nextword
= l
& 0xffff;
406 DEBUG_PRINTFQ("is a dereferenced register (%c) + constant (%hu)\n", *reg
, *nextword
);
407 return 0x10 | register_enumerate_(*reg
);
409 DEBUG_PRINTFQ("is out of range\n");
410 fprintf(stderr
, "trouble with operand '%s': %s\n", operand_orig
, strerror(errno
));
414 /* what? still here? assume it's a label, I guess */
415 /* try to populate nextword with label address */
416 if (label_addr_(labels
, operand
, nextword
)) {
417 DEBUG_PRINTFQ("(deferred label resolution)\n");
421 DEBUG_PRINTFQ("is a dereferenced register (%c) + label\n", *reg
);
423 return 0x10 | register_enumerate_(*reg
);
426 /* it must just be a dereferenced literal then */
429 l
= strtoul(operand
, &ep
, 0);
431 && (*operand
&& (*ep
== '\0')) ) {
432 /* string conversion went without issue */
433 /* validate it will fit in a word */
435 DEBUG_PRINTFQ("is out of range\n");
436 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
440 DEBUG_PRINTFQ("is a dereferenced literal value (%hu)\n", *nextword
);
441 *nextword
= l
& 0xffff;
445 DEBUG_PRINTFQ("is out of range\n");
446 fprintf(stderr
, "trouble with operand '%s': %s\n", operand_orig
, strerror(errno
));
449 /* not a number? try a label */
450 if (label_addr_(labels
, operand
, nextword
)) {
451 DEBUG_PRINTFQ("(deferred label resolution)\n");
455 DEBUG_PRINTFQ("is a dereferenced label\n");
460 /* left with a literal or a label, then */
463 l
= strtoul(operand
, &ep
, 0);
465 || (*operand
&& (*ep
== '\0')) ) {
467 DEBUG_PRINTFQ("is out of range\n");
468 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
472 DEBUG_PRINTFQ("is literal value (%lu)\n", l
);
477 *nextword
= l
& 0xffff;
482 /* try to populate nextword with label address */
483 if (label_addr_(labels
, operand
, nextword
)) {
484 DEBUG_PRINTFQ("(deferred label resolution)\n");
485 /* assume non-small literal value */
490 DEBUG_PRINTFQ("is label '%s' (0x%02hx)\n", operand
, *nextword
);
491 if (*nextword
< 0x20 && allow_short_labels
) {
492 DEBUG_PRINTF("small value label win\n");
493 return (0x20 + *nextword
) & 0x3f;
500 /* prints an instruction's assembly */
502 int instruction_print_(struct instruction_
*i
, unsigned int with_label
) {
507 r
= printf("%-16s %3s", i
->label
? i
->label
: "", i
->opcode
);
509 r
= printf("%3s", i
->opcode
);
511 for (o
= i
->operands
; o
; o
= o
->next
)
512 r
+= printf(" %s%s", o
->operand
, o
->next
? "," : "");
518 * Parses the zero-terminated line of input 'buf' into a newly-allocated struct instruction_.
519 * [label] opcode [operand[,operand[,...]]]
520 * Does not yet validate if labels, opcodes, or operands are valid...
523 int buf_tokenize_(char *buf
, struct instruction_
**next_instr
) {
524 const char const *sep
= " \t\n";
525 const char const *quot
= "'`\"";
526 struct instruction_
*instr
= NULL
;
527 struct operand_
*operand_list
= NULL
;
534 size_t instr_words_needed
= 1;
537 assert(next_instr
!= NULL
);
541 /* kill leading whitespace */
542 buf
+= strspn(buf
, " \t\n");
544 /* kill trailing whitespace */
545 for (x
= buf
+ strlen(buf
); *x
&& strchr(sep
, *x
); x
--)
548 /* split on first non-quoted ';', ignore following comment */
549 x
= strqtok_r(buf
, ";", '\\', quot
, &qt
, &st
);
553 fprintf(stderr
, "unmatched %c-quote\n", *qt
);
557 /* determine if first token is label, opcode, or we just have a blank line to ignore */
558 x
= strqtok_r(x
, sep
, '\\', quot
, &qt
, &st
);
562 fprintf(stderr
, "unmatched %c-quote\n", *qt
);
566 /* I want c-style labels in my asm, but example in spec uses : in prefix rather than postfix */
567 #ifdef NON_SPEC_LABELS
568 /* labels end with :, otherwise its an opcode */
569 y
= x
+ strlen(x
) - 1;
573 opcode
= strqtok_r(NULL
, sep
, '\\', quot
, &qt
, &st
);
578 #else /* NON_SPEC_LABELS */
579 /* labels.. begin? with ':' ? okay, I guess. Whatever. */
580 /* otherwise, it's an opcode */
583 opcode
= strqtok_r(NULL
, sep
, '\\', quot
, &qt
, &st
);
588 #endif /* NON_SPEC_LABELS */
595 While normal instructions just have comma-separated operands,
596 DAT can be followed by comma-separated list of:
597 label, to be resolved to address
599 string, "quoted", characters to be rendered into low-byte of words
603 struct operand_
**o_next
= &operand_list
;
605 for (x
= strqtok_r(operand
, ",", '\\', quot
, &qt
, &st
);
607 x
= strqtok_r(NULL
, ",", '\\', quot
, &qt
, &st
) ) {
609 /* trim leading whitespace */
610 x
+= strspn(x
, " \t\n");
612 fprintf(stderr
, "encountered empty operand\n");
616 /* trim trailing whitespace */
617 y
= x
+ strlen(x
) - 1;
618 while (strchr(" \t\n", *y
)) {
623 /* new operand to append to list */
624 *o_next
= malloc(sizeof **o_next
);
625 if (*o_next
== NULL
) {
626 fprintf(stderr
, "%s():%s\n", "calloc", strerror(errno
));
630 /* assume an operand uses one word, unless it's a string */
631 instr_words_needed
+= (*x
== '"') ? strlen(x
) : 1;
633 (*o_next
)->operand
= strdup(x
);
634 if ((*o_next
)->operand
== NULL
) {
635 fprintf(stderr
, "%s():%s\n", "strdup", strerror(errno
));
638 (*o_next
)->next
= NULL
;
639 o_next
= &((*o_next
)->next
);
643 DEBUG_PRINTF("allocating instr with room for %zu words\n", instr_words_needed
);
645 /* extra room for assembled words */
646 instr
= calloc(1, (instr_words_needed
* sizeof *instr
->instr_words
) + sizeof *instr
);
648 fprintf(stderr
, "%s():%s\n", "calloc", strerror(errno
));
652 instr
->label
= label
? strdup(label
) : NULL
;
653 instr
->opcode
= opcode
? strdup(opcode
) : NULL
;
654 instr
->operands
= operand_list
;
661 /* try to generate bytecode for an instruction */
663 int instr_assemble_(struct dynamic_array
*labels
, struct instruction_
*i
, unsigned int allow_short_labels
) {
664 unsigned int nwu
= 0; /* number of words used */
665 unsigned int incomplete
= 0;
667 struct operand_
*o
= i
->operands
;
669 if (opt_
.verbose
> 2) {
670 printf("%s: assembling %p ", __func__
, i
);
671 instruction_print_(i
, 1);
676 /* already assembled, nothing to do */
680 /* special case DAT */
681 if (strncasecmp(i
->opcode
, "DAT", 3) == 0) {
682 DEBUG_PRINTF("processing DAT...\n");
691 DEBUG_PRINTF("DAT operand:'%s' next:%p\n", o
->operand
, o
->next
);
693 /* is this a string? */
694 if ( (x
= strchr("\"'`", o
->operand
[0])) ) {
695 dat_len
= strlen(o
->operand
) - 1;
696 if (o
->operand
[dat_len
] == *x
) {
698 DEBUG_PRINTF("DAT string operand: %s\n", o
->operand
);
700 for (j
= 0, x
= o
->operand
+ 1;
703 i
->instr_words
[i
->length
] = (DCPU16_WORD
)*x
;
713 l
= strtoul(o
->operand
, &ep
, 0);
715 && (*o
->operand
&& (*ep
== '\0')) ) {
716 /* conversion succeeded */
718 fprintf(stderr
, "value '%lu' out of range\n", l
);
723 fprintf(stderr
, "FIXME finish implementing DAT\n");
724 /* check if it's a parsable number */
726 /* otherwise assume it's a label */
736 /* start with opcode bits */
737 bits
= opcode_bits_(i
->opcode
);
739 fprintf(stderr
, "unrecognized instruction '%s%s", i
->opcode
, i
->operands
? " " : "");
740 for (o
= i
->operands
; o
; o
= o
->next
)
741 fprintf(stderr
, " %s%s", o
->operand
, o
->next
? "," : "");
742 fprintf(stderr
, "'\n");
745 i
->instr_words
[0] |= 0x0f & bits
;
747 /* in rendered bytecode, all instructions have two operands; nbi instructions take 'first operand' bits. */
748 if ((bits
& 0x0f) == 0) {
749 bits
= nbi_opcode_bits_(i
->opcode
);
751 fprintf(stderr
, "INTERNAL ERROR: missing instruction in nbi opcode table\n");
756 fprintf(stderr
, "'%s' requires more operands\n", i
->opcode
);
759 bits
= value_bits_(labels
, o
->operand
, i
->instr_words
+ 1, &nwu
, allow_short_labels
);
761 fprintf(stderr
, "couldn't assemble instruction\n");
763 } else if (bits
== -2) {
764 DEBUG_PRINTF("%s: assembly deferred: unresolved label\n", __func__
);
765 /* keep going, but don't finalize until we can calculate label address */
771 i
->instr_words
[0] |= (bits
& 0x3f) << 4;
774 fprintf(stderr
, "'%s' requires more operands\n", i
->opcode
);
778 bits
= value_bits_(labels
, o
->operand
, i
->instr_words
+ 1 + nwu
, &nwu
, allow_short_labels
);
780 fprintf(stderr
, "couldn't assemble instruction\n");
782 } else if (bits
== -2) {
783 DEBUG_PRINTF("%s: assembly deferred: unresolved label\n", __func__
);
784 /* keep going, but don't finalize until we can calculate label address */
789 i
->instr_words
[0] |= (bits
& 0x3f) << 10;
792 fprintf(stderr
, "too many operands\n");
796 /* counting labels as words, we now know at least the maximum instruction length */
800 DEBUG_PRINTF("instruction words: [%u]", i
->length
);
801 for (bits
= 0; bits
<= (int)nwu
; bits
++)
802 DEBUG_PRINTFQ(" %04x", i
->instr_words
[bits
]);
805 DEBUG_PRINTFQ(" (preliminary)");
816 * read lines from stream f
817 * break each line into parts, populate parts into structures
820 int parse_stream_(FILE *f
, const char *src
, struct dynamic_array
*instructionps
, struct dynamic_array
*labels
, unsigned int allow_short_labels
) {
821 struct instruction_
*instr
, **instr_list_entry
;
822 unsigned int line
= 0;
826 buf
[sizeof buf
- 1] = '\0';
828 while (fgets(buf
, sizeof buf
, f
)) {
831 if (buf
[sizeof buf
- 1] != '\0') {
832 fprintf(stderr
, "%s:%u:%s", src
, line
, "input line too long\n");
837 if (buf_tokenize_(buf
, &instr
)) {
838 fprintf(stderr
, "%s:%u:%s", src
, line
, "trouble tokenizing input\n");
844 /* add to list of instructions */
845 instr_list_entry
= dynarray_add(instructionps
, &instr
);
846 if (instr_list_entry
== NULL
) {
847 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not populate instruction list\n");
852 struct label_ new_label
= {
853 .label
= instr
->label
,
854 .instr
= instr_list_entry
,
858 if (label_find_(labels
, instr
->label
)) {
859 fprintf(stderr
, "%s:%u:%s", src
, line
, "duplicate label\n");
863 if (dynarray_add(labels
, &new_label
) == NULL
) {
864 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not populate label list\n");
867 label_addr_calculate_(instructionps
, labels
);
870 instr_assemble_(labels
, instr
, allow_short_labels
);
874 fprintf(stderr
, "%s():%s\n", "fgets", strerror(errno
));
878 fprintf(stderr
, "parsing aborted\n");
886 * make a full pass over instruction list to resolve labels
889 int assemble_check_(struct dynamic_array
*instructionps
, struct dynamic_array
*labels
, unsigned int allow_short_labels
) {
893 /* fixing short labels .... */
894 /* by here we have our list of instructions and their maximum instruction lengths */
895 /* and we have a list of addresses, based on those maximum lengths */
896 /* So, if doing short labels, all label addresses are now suspect, so recompute them all... */
897 /* and reassemble.. */
898 /* uh.. what else am I forgetting.. this method won't work for labels approaching the limit */
899 /* of short form addresses, when there are more than the difference number of short form labels used previous to those addresses */
901 /* try this? keep another list of locations a label address is used */
902 /* as we step forward, and recompute an address, back up to first occurence of address, make sure nothing else has changed */
904 DEBUG_PRINTF(" final pass of assembler...\n");
905 for (x
= 0; x
< instructionps
->entries
; x
++) {
906 struct instruction_
**instrp
= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, x
);
907 retval
|= instr_assemble_(labels
, *instrp
, allow_short_labels
);
909 fprintf(stderr
, "instruction %zu failed to assemble\n", x
);
913 VERBOSE_PRINTF("%3s %6s %-32s %-4s\n", "", "_addr_", "_label_", "_instruction_");
914 for (x
= 0; x
< labels
->entries
; x
++) {
915 struct label_
*l
= (struct label_
*)DYNARRAY_ITEM(*labels
, x
);
919 printf("%3s0x%04x %-32s ",
923 instruction_print_(*(l
->instr
), 0);
928 VERBOSE_PRINTF("\n");
931 fprintf(stderr
, "some labels could not be resolved\n");
937 int output_(struct dynamic_array
*instructionps
, const char *filename
) {
939 struct instruction_
**instrp
;
940 size_t i
, r
, total_words
= 0;
944 of
= fopen(filename
, "w");
946 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
951 for (i
= 0; i
< instructionps
->entries
; i
++) {
952 instrp
= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, i
);
956 s
= instruction_print_(*instrp
, 1);
957 printf("%*s;", (44 - s
) > 0 ? (44 - s
) : 0, "");
958 for (x
= 0; x
< (*instrp
)->length
; x
++) {
959 printf(" %04x", (*instrp
)->instr_words
[x
]);
965 r
= fwrite((*instrp
)->instr_words
, sizeof(DCPU16_WORD
), (*instrp
)->length
, of
);
966 if (r
< (*instrp
)->length
) {
967 fprintf(stderr
, "%s():%s\n", "fwrite", strerror(errno
));
971 total_words
+= (*instrp
)->length
;
974 fprintf(stderr
, "%s 0x%04zx instructions as 0x%04zx words\n",
975 opt_
.dryrun
? "assembled" : "wrote",
982 static struct dynamic_array
*instructionps_
;
983 static struct dynamic_array
*labels_
;
985 int main(int argc
, char *argv
[]) {
986 const char *out_filename
= NULL
;
987 unsigned int allow_short_labels
= 0;
990 while ( (c
= getopt(argc
, argv
, "dhsvo:")) != EOF
) {
997 allow_short_labels
++;
1002 fprintf(stderr
, "Sorry, I can only write one file at a time.\n");
1005 out_filename
= optarg
;
1025 if (out_filename
== NULL
)
1026 out_filename
= out_filename_default_
;
1029 instructionps_
= dynarray_new(sizeof (struct instruction_
*), 1024);
1030 labels_
= dynarray_new(sizeof(struct label_
), 256);
1031 if (instructionps_
== NULL
1032 || labels_
== NULL
) {
1033 fprintf(stderr
, "failed to initialize\n");
1037 /* if filenames were specified, parse them instead of stdin */
1040 char *filename
= *argv
;
1041 FILE *f
= fopen(filename
, "r");
1046 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
1050 VERBOSE_PRINTF("assembling '%s'...\n", filename
);
1051 parse_stream_(f
, filename
, instructionps_
, labels_
, allow_short_labels
);
1056 VERBOSE_PRINTF("assembling '%s'...\n", "stdin");
1057 parse_stream_(stdin
, "-", instructionps_
, labels_
, allow_short_labels
);
1060 if (assemble_check_(instructionps_
, labels_
, allow_short_labels
)) {
1061 fprintf(stderr
, "errors prevented assembly\n");
1065 if (output_(instructionps_
, out_filename
)) {
1066 fprintf(stderr
, "failed to create output\n");