14 * quick and dirty assembler for dcpu16
16 * Justin Wind <justin.wind@gmail.com>
17 * 2012 04 07 - implementation started
18 * 2012 04 10 - functional
19 * 2012 04 16 - support dat statements
22 * needs ability to specify location for code or data
23 * needs ability to specify label as relative to another label
24 * short labels not correctly computed
27 static const char * const src_id_
= "$Id$";
29 const char const out_filename_default_
[] = "a.out";
31 /* global invocation options */
40 #define DEBUG_PRINTF(...) do { if (opt_.verbose > 2) { printf("DEBUG: "); printf(__VA_ARGS__); } } while (0)
41 #define DEBUG_PRINTFQ(...) do { if (opt_.verbose > 2) printf(__VA_ARGS__); } while (0)
42 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
45 void usage_(char *prog
, unsigned int full
) {
46 FILE *f
= full
? stdout
: stderr
;
47 char *x
= strrchr(prog
, '/');
53 fprintf(f
, "%s -- \n\n",
56 fprintf(f
, "Usage: %s [-h] [-v] [-s] [-o file] file [file [...]]\n",
60 fprintf(f
, "\nOptions:\n"
61 "\t-h -- this screen\n"
62 "\t-o <file> -- output to <file> [default: %s]\n"
63 "\t-s -- allow short labels in instruction words\n"
64 "\t-d -- dry run, print results, do not write to file\n"
65 "\t-v -- verbose output\n",
66 out_filename_default_
);
68 fprintf(f
, "\n%78s\n",
74 /* instructions have operands */
76 struct operand_
*next
;
77 char *operand
; /* tokenized operand text */
80 /* keep an array of instructions as we read them in */
83 char *label
; /* set if a label points here */
84 char *opcode
; /* tokenized instruction text */
85 struct operand_
*operands
; /* list of operands */
86 unsigned int ready
: 1; /* bytecode computed? */
87 unsigned int length
; /* number of words of bytecode */
88 DCPU16_WORD instr_words
[];
91 /* keep an array of labels, indexed back to their instruction locations */
93 char *label
; /* name of label */
94 struct instruction_
**instr
; /* pointer into array of instructions */
95 unsigned int ready
: 1; /* do we know where this label is yet? */
100 /* locate and return the label entry matching name */
102 struct label_
*label_find_(struct dynamic_array
*labels
, char *name
) {
105 for (x
= 0; x
< labels
->entries
; x
++) {
106 struct label_
*l
= (struct label_
*)DYNARRAY_ITEM(*labels
, x
);
107 if (strcmp(l
->label
, name
) == 0)
114 /* if a label has a validly-calculated address, fetch it */
116 int label_addr_(struct dynamic_array
*labels
, char *name
, DCPU16_WORD
*addr
) {
119 if ( (l
= label_find_(labels
, name
)) == NULL
)
128 /* attempt to determine the addresses of all labels */
130 void label_addr_calculate_(struct dynamic_array
*instructionps
, struct dynamic_array
*labels
) {
133 /* idea: label1:label2 - calculated as offset between labels */
135 /* for each label.. */
136 for (i
= 0; i
< labels
->entries
; i
++) {
138 struct instruction_
**instr
;
139 unsigned int word_count
= 0;
141 l
= (struct label_
*)DYNARRAY_ITEM(*labels
, i
);
143 /* if it's already calculated, great. */
148 * starting at the instruction for this label,
149 * walk backwards through the list of instructions
150 * until we get to the start or a known prior label address.
151 * update our label with the freshly calculated addr
153 for (instr
= ((struct label_
*)DYNARRAY_ITEM(*labels
, i
))->instr
;
154 instr
>= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, 0);
158 DEBUG_PRINTF("%s: instr not ready\n", __func__
);
159 word_count
+= (*instr
)->length
;
161 /* have we come across an instruction which a label points to?
162 it should already be calculated, so just add that on and be done */
164 && strcmp((*instr
)->label
, l
->label
)) {
167 if (label_addr_(labels
, (*instr
)->label
, &addr
)) {
168 fprintf(stderr
, "internal error: incomplete prior address for '%s' while calculating '%s'\n",
178 l
->addr
= word_count
;
180 DEBUG_PRINTF("label '%s' now has addr of 0x%04x\n", l
->label
, word_count
);
185 /* generate the nibble for a given basic opcode */
187 int opcode_bits_(char *opcode
) {
191 } opcodes_lower_nibble
[] = {
193 /* { "future nbi instruction", 0x00 }, */
212 for (o
= opcodes_lower_nibble
; o
->op
[0]; o
++) {
213 if (strcasecmp(o
->op
, opcode
) == 0)
217 if (o
->op
[0] == '\0') {
218 fprintf(stderr
, "unknown instruction '%s'\n", opcode
);
225 /* generate the six bits for a given nbi opcode (aka first operand to opcode 0x00) */
227 int nbi_opcode_bits_(char *nbi_opcode
) {
231 } nbi_opcodes_bits
[] = {
232 { " ", 0x00 }, /* reserved for future */
237 for (o
= nbi_opcodes_bits
; o
->op
[0]; o
++) {
238 if (strcasecmp(o
->op
, nbi_opcode
) == 0)
242 if (o
->op
[0] == '\0') {
243 fprintf(stderr
, "unknown nbi instruction '%s'\n", o
->op
);
250 /* convert register character like 'x' to value like 0x03 */
252 unsigned int register_enumerate_(char r
) {
253 const char regs
[] = "AaBbCcXxYyZzIiJj";
254 const char *x
= strchr(regs
, r
);
259 fprintf(stderr
, "internal error, unknown register character 0x%02x\n", r
);
263 /* removes all occurences of chars from buf */
265 void buf_strip_chars_(char *buf
, char *chars
) {
268 for (s
= d
= buf
; *s
; s
++, d
++) {
269 while (*s
&& strchr(chars
, *s
)) {
281 * generate the six bits for a given operand string
282 * returns -1 if it could not parse the operand
283 * returns -2 if it could not parse the operand due to an unresolved label
284 * notes: nextword may be overwritten even if it's not used in final instruction
287 int value_bits_(struct dynamic_array
*labels
, const char *operand_orig
, DCPU16_WORD
*nextword
, unsigned int *nextwordused
, unsigned int allow_short_labels
) {
288 static char *operand
= NULL
;
289 static size_t operand_sz
= 0;
295 Our operand working buffer shouldn't ever need to be too big,
296 but DAT might blow that assumption.
298 if (operand_sz
<= strlen(operand_orig
)) {
300 size_t new_sz
= strlen(operand_orig
);
306 DEBUG_PRINTF("%s: allocating buffer of size %zu\n", __func__
, new_sz
);
307 tmp_ptr
= realloc(operand
, new_sz
);
308 if (tmp_ptr
== NULL
) {
309 fprintf(stderr
, "%s(%zu):%s\n", "realloc", new_sz
, strerror(errno
));
316 o
= strcpy(operand
, operand_orig
);
318 DEBUG_PRINTF("%s: operand '%s' ", __func__
, operand
); /* completed later */
320 /* this is a very stupid parser */
322 /* first, let's trim all whitespace out of string at once to make parsing easier */
323 buf_strip_chars_(operand
, " \t\n");
325 /* single character might match a register */
326 if (strlen(operand
) == 1
327 && strchr("AaBbCcXxYyZzIiJj", *operand
)) {
328 DEBUG_PRINTFQ("is register %c\n", *operand
);
329 return register_enumerate_(*operand
);
333 if (strcasecmp(operand
, "POP") == 0) {
334 DEBUG_PRINTFQ("is POP\n");
337 if (strcasecmp(operand
, "PUSH") == 0) {
338 DEBUG_PRINTFQ("is PUSH\n");
341 if (strcasecmp(operand
, "PEEK") == 0) {
342 DEBUG_PRINTFQ("is PEEK\n");
345 if (strcasecmp(operand
, "SP") == 0) {
346 DEBUG_PRINTFQ("is register SP\n");
349 if (strcasecmp(operand
, "PC") == 0) {
350 DEBUG_PRINTFQ("is register PC\n");
353 if (strcasecmp(operand
, "O") == 0) {
354 DEBUG_PRINTFQ("is register O\n");
358 /* is the operand [bracketed]? */
359 if (operand
[0] == '[' && operand
[strlen(operand
) - 1] == ']') {
360 /* eat the brackets */
361 operand
[strlen(operand
) - 1] = '\0';
364 /* is it [register]? */
365 if (strlen(operand
) == 1
366 && strchr("AaBbCcXxYyZzIiJj", *operand
)) {
367 DEBUG_PRINTFQ("is dereferenced register %c\n", *operand
);
368 return 0x08 | register_enumerate_(*operand
);
371 /* is it [register+something]? */
372 if ( (ep
= strchr(operand
, '+')) ) {
380 /* figure out which one is which */
382 && strchr("AaBbCcXxYyZzIiJj", *ep
)) {
385 } else if (strlen(operand
) == 1
386 && strchr("AaBbCcXxYyZzIiJj", *operand
) ) {
390 DEBUG_PRINTFQ("is unparsable\n");
391 fprintf(stderr
, "couldn't parse operand '%s'\n", operand_orig
);
395 /* check if something is understandable as a value */
397 l
= strtoul(constant
, &ep
, 0);
399 && (*constant
&& (*ep
== '\0')) ) {
400 /* string conversion went without issue */
401 /* validate it will fit in a word */
403 DEBUG_PRINTFQ("is out of range\n");
404 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
409 *nextword
= l
& 0xffff;
411 DEBUG_PRINTFQ("is a dereferenced register (%c) + constant (%hu)\n", *reg
, *nextword
);
412 return 0x10 | register_enumerate_(*reg
);
414 DEBUG_PRINTFQ("is out of range\n");
415 fprintf(stderr
, "trouble with operand '%s': %s\n", operand_orig
, strerror(errno
));
419 /* what? still here? assume it's a label, I guess */
420 /* try to populate nextword with label address */
421 if (label_addr_(labels
, operand
, nextword
)) {
422 DEBUG_PRINTFQ("(deferred label resolution)\n");
426 DEBUG_PRINTFQ("is a dereferenced register (%c) + label\n", *reg
);
428 return 0x10 | register_enumerate_(*reg
);
431 /* it must just be a dereferenced literal then */
434 l
= strtoul(operand
, &ep
, 0);
436 && (*operand
&& (*ep
== '\0')) ) {
437 /* string conversion went without issue */
438 /* validate it will fit in a word */
440 DEBUG_PRINTFQ("is out of range\n");
441 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
445 DEBUG_PRINTFQ("is a dereferenced literal value (%hu)\n", *nextword
);
446 *nextword
= l
& 0xffff;
450 DEBUG_PRINTFQ("is out of range\n");
451 fprintf(stderr
, "trouble with operand '%s': %s\n", operand_orig
, strerror(errno
));
454 /* not a number? try a label */
455 if (label_addr_(labels
, operand
, nextword
)) {
456 DEBUG_PRINTFQ("(deferred label resolution)\n");
460 DEBUG_PRINTFQ("is a dereferenced label\n");
465 /* left with a literal or a label, then */
468 l
= strtoul(operand
, &ep
, 0);
470 || (*operand
&& (*ep
== '\0')) ) {
472 DEBUG_PRINTFQ("is out of range\n");
473 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
477 DEBUG_PRINTFQ("is literal value (%lu)\n", l
);
482 *nextword
= l
& 0xffff;
487 /* try to populate nextword with label address */
488 if (label_addr_(labels
, operand
, nextword
)) {
489 DEBUG_PRINTFQ("(deferred label resolution)\n");
490 /* assume non-small literal value */
495 DEBUG_PRINTFQ("is label '%s' (0x%02hx)\n", operand
, *nextword
);
496 if (*nextword
< 0x20 && allow_short_labels
) {
497 DEBUG_PRINTF("small value label win\n");
498 return (0x20 + *nextword
) & 0x3f;
505 /* prints an instruction's assembly */
507 int instruction_print_(struct instruction_
*i
, unsigned int with_label
) {
512 r
= printf("%-16s %3s", i
->label
? i
->label
: "", i
->opcode
);
514 r
= printf("%3s", i
->opcode
);
516 for (o
= i
->operands
; o
; o
= o
->next
)
517 r
+= printf(" %s%s", o
->operand
, o
->next
? "," : "");
523 * Parses a zero-terminated line of input into a newly-allocated struct instruction_.
524 * [label] instruction [operand[,operand[,...]]]
525 * Does no validation of contents of any of these tokens, as of yet.
528 int tokenize_line_(char *line
, struct instruction_
**next_instr
) {
529 const char const *whitespace
= " \t\n";
530 const char const *quotes
= "\"'`";
531 struct instruction_
*instr
= NULL
;
533 char *label
, *opcode
;
534 struct operand_
*operand_list
= NULL
;
535 struct operand_
**operand_tail
= &operand_list
;
536 size_t instr_words_needed
= 0;
543 /* strip leading whitespace */
544 line
+= strspn(line
, whitespace
);
548 /* set first bare ';' to '\0', thus isolating any comments */
549 /* here we only care about the side-effect of truncating the first separator character */
550 (void)strqtok_r(line
, ";", '\\', quotes
, &qt
, &st
);
551 /* we don't care if there was an unmatched quote at this point, let's see what happens */
555 /* carve off the first token, determine if it is a label */
556 x
= strqtok_r(line
, whitespace
, '\\', quotes
, &qt
, &st
);
557 if (x
== NULL
|| *x
== '\0')
560 /* labels could contain an unmatched quote character, I guess? */
564 /* we have something, try to make sense of what it is */
566 #ifdef NON_SPEC_LABELS
567 /* I want my labels like 'label:' */
568 if ( *(x
+ strlen(line
) - 1) == ':' ) {
569 *(x
+ strlen(line
) - 1) = '\0';
570 DEBUG_PRINTF("label: %s\n", x
);
574 opcode
= strqtok_r(NULL
, whitespace
, '\\', quotes
, &qt
, &st
);
579 #endif /* NON_SPEC_LABELS */
581 /* spec gives example of labels as ':label' */
586 opcode
= strqtok_r(NULL
, whitespace
, '\\', quotes
, &qt
, &st
);
591 /* opcodes shouldn't have quotes, so we'll ignore any unmatched quotes again */
593 if (opcode
&& *opcode
) {
594 /* if we have an opcode, we'll need at least one word to compile instruction */
595 instr_words_needed
++;
597 while ( (x
= strqtok_r(NULL
, ",", '\\', quotes
, &qt
, &st
)) ) {
598 struct operand_
*new_operand
;
601 /* trim whitespaces */
602 x
+= strspn(x
, whitespace
);
605 for (y
= x
+ strlen(x
) - 1; *y
; y
--) {
606 if (strchr(whitespace
, *y
)) {
613 fprintf(stderr
, "null operand encountered\n");
617 DEBUG_PRINTF("tokenized operand '%s'\n", x
);
619 new_operand
= malloc(sizeof *new_operand
);
620 if (new_operand
== NULL
) {
621 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
625 new_operand
->operand
= strdup(x
);
626 if (new_operand
->operand
== NULL
) {
627 fprintf(stderr
, "%s():%s\n", "strdup", strerror(errno
));
631 new_operand
->next
= NULL
;
633 if (strchr(quotes
, x
[0])) {
634 /* if this is a quoted operand, assuming we are in a DAT statement, it will take up slightly less room than it is long */
635 instr_words_needed
+= strlen(x
) - 1;
637 instr_words_needed
++;
639 *operand_tail
= new_operand
;
640 operand_tail
= &(*operand_tail
)->next
;
644 DEBUG_PRINTF("allocating new instruction with room for %zu bytes\n", instr_words_needed
);
646 instr
= calloc(1, (instr_words_needed
* sizeof *instr
->instr_words
) + sizeof *instr
);
648 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
653 instr
->label
= strdup(label
);
654 if (instr
->label
== NULL
) {
655 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
663 instr
->opcode
= strdup(opcode
);
664 if (instr
->opcode
== NULL
) {
665 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
672 instr
->operands
= operand_list
;
679 /* try to generate bytecode for an instruction */
680 /* returns -1 on unrecoverable error */
682 int instr_assemble_(struct dynamic_array
*labels
, struct instruction_
*i
, unsigned int allow_short_labels
) {
683 unsigned int nwu
= 0; /* number of words used */
684 unsigned int incomplete
= 0;
686 struct operand_
*o
= i
->operands
;
688 if (opt_
.verbose
> 2) {
689 printf("%s: assembling %p ", __func__
, i
);
690 instruction_print_(i
, 1);
691 printf("(line :%zu)\n", i
->src_line
);
695 /* already assembled, nothing to do */
699 /* special case DAT */
700 if (strncasecmp(i
->opcode
, "DAT", 3) == 0) {
701 DEBUG_PRINTF("processing DAT...\n");
705 for ( /* */ ; o
; o
= o
->next
) {
710 DEBUG_PRINTF("DAT operand:'%s' next:%p\n", o
->operand
, o
->next
);
712 /* is this a string? */
713 if ( (x
= strchr("\"'`", o
->operand
[0])) ) {
714 dat_len
= strlen(o
->operand
) - 1;
715 if (o
->operand
[dat_len
] == *x
) {
717 DEBUG_PRINTF("DAT string operand: %s\n", o
->operand
);
719 for (j
= 0, x
= o
->operand
+ 1;
722 i
->instr_words
[i
->length
] = *x
;
725 /* Note that strings in DAT do not include their zero-terminators */
726 /* specify as 'DAT "string", 0' */
731 /* is this a number? */
734 l
= strtoul(o
->operand
, &ep
, 0);
736 && (*o
->operand
&& (*ep
== '\0')) ) {
737 /* conversion succeeded */
739 fprintf(stderr
, "value '%lu' out of range\n", l
);
742 i
->instr_words
[i
->length
] = l
;
747 /* otherwise assume it's a label, even if we don't know what it is */
748 if (label_addr_(labels
, o
->operand
, &i
->instr_words
[i
->length
])) {
749 DEBUG_PRINTF("(deferred label resolution)\n");
756 DEBUG_PRINTF("pending label address\n");
764 /* start with opcode bits */
765 bits
= opcode_bits_(i
->opcode
);
767 fprintf(stderr
, "unrecognized instruction '%s%s", i
->opcode
, i
->operands
? " " : "");
768 for (o
= i
->operands
; o
; o
= o
->next
)
769 fprintf(stderr
, " %s%s", o
->operand
, o
->next
? "," : "");
770 fprintf(stderr
, "'\n");
773 i
->instr_words
[0] |= 0x0f & bits
;
775 /* in rendered bytecode, all instructions have two operands; nbi instructions take 'first operand' bits. */
776 if ((bits
& 0x0f) == 0) {
777 bits
= nbi_opcode_bits_(i
->opcode
);
779 fprintf(stderr
, "INTERNAL ERROR: missing instruction in nbi opcode table\n");
784 fprintf(stderr
, "'%s' requires more operands\n", i
->opcode
);
787 bits
= value_bits_(labels
, o
->operand
, i
->instr_words
+ 1, &nwu
, allow_short_labels
);
789 fprintf(stderr
, "couldn't assemble instruction\n");
791 } else if (bits
== -2) {
792 DEBUG_PRINTF("%s: assembly deferred: unresolved label\n", __func__
);
793 /* keep going, but don't finalize until we can calculate label address */
799 i
->instr_words
[0] |= (bits
& 0x3f) << 4;
802 fprintf(stderr
, "'%s' requires more operands\n", i
->opcode
);
806 bits
= value_bits_(labels
, o
->operand
, i
->instr_words
+ 1 + nwu
, &nwu
, allow_short_labels
);
808 fprintf(stderr
, "couldn't assemble instruction\n");
810 } else if (bits
== -2) {
811 DEBUG_PRINTF("%s: assembly deferred: unresolved label\n", __func__
);
812 /* keep going, but don't finalize until we can calculate label address */
817 i
->instr_words
[0] |= (bits
& 0x3f) << 10;
820 fprintf(stderr
, "too many operands\n");
824 /* counting labels as words, we now know at least the maximum instruction length */
828 DEBUG_PRINTF("instruction words: [%u]", i
->length
);
829 for (bits
= 0; bits
<= (int)nwu
; bits
++)
830 DEBUG_PRINTFQ(" %04x", i
->instr_words
[bits
]);
833 DEBUG_PRINTFQ(" (preliminary)");
844 * read lines from stream f
845 * break each line into parts, populate parts into structures
848 int parse_stream_(FILE *f
, const char *src
, struct dynamic_array
*instructionps
, struct dynamic_array
*labels
, unsigned int allow_short_labels
) {
849 struct instruction_
*instr
, **instr_list_entry
;
850 unsigned int line
= 0;
854 buf
[sizeof buf
- 1] = '\0';
856 while (fgets(buf
, sizeof buf
, f
)) {
859 if (buf
[sizeof buf
- 1] != '\0') {
860 fprintf(stderr
, "%s:%u:%s", src
, line
, "input line too long\n");
865 if (tokenize_line_(buf
, &instr
)) {
866 fprintf(stderr
, "%s:%u:%s", src
, line
, "trouble tokenizing input\n");
872 instr
->src_line
= line
;
873 /* add to list of instructions */
874 instr_list_entry
= dynarray_add(instructionps
, &instr
);
875 if (instr_list_entry
== NULL
) {
876 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not populate instruction list\n");
881 struct label_ new_label
= {
882 .label
= instr
->label
,
883 .instr
= instr_list_entry
,
887 if (label_find_(labels
, instr
->label
)) {
888 fprintf(stderr
, "%s:%u:%s", src
, line
, "duplicate label\n");
892 if (dynarray_add(labels
, &new_label
) == NULL
) {
893 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not populate label list\n");
896 label_addr_calculate_(instructionps
, labels
);
899 if (instr_assemble_(labels
, instr
, allow_short_labels
)) {
900 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not assemble instruction\n");
906 fprintf(stderr
, "%s():%s\n", "fgets", strerror(errno
));
910 fprintf(stderr
, "parsing aborted\n");
918 * make a full pass over instruction list to resolve labels
921 int assemble_check_(struct dynamic_array
*instructionps
, struct dynamic_array
*labels
, unsigned int allow_short_labels
) {
925 /* fixing short labels .... */
926 /* by here we have our list of instructions and their maximum instruction lengths */
927 /* and we have a list of addresses, based on those maximum lengths */
928 /* So, if doing short labels, all label addresses are now suspect, so recompute them all... */
929 /* and reassemble.. */
930 /* uh.. what else am I forgetting.. this method won't work for labels approaching the limit */
931 /* of short form addresses, when there are more than the difference number of short form labels used previous to those addresses */
933 /* try this? keep another list of locations a label address is used */
934 /* as we step forward, and recompute an address, back up to first occurence of address, make sure nothing else has changed */
936 DEBUG_PRINTF(" final pass of assembler...\n");
937 for (x
= 0; x
< instructionps
->entries
; x
++) {
938 struct instruction_
**instrp
= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, x
);
939 retval
= instr_assemble_(labels
, *instrp
, allow_short_labels
);
941 fprintf(stderr
, "instruction %zu failed to assemble\n", x
);
944 if (! (*instrp
)->ready
) {
945 fprintf(stderr
, "instruction not resolvable\n");
950 VERBOSE_PRINTF("%3s %6s %-32s %-4s\n", "", "_addr_", "_label_", "_instruction_");
951 for (x
= 0; x
< labels
->entries
; x
++) {
952 struct label_
*l
= (struct label_
*)DYNARRAY_ITEM(*labels
, x
);
956 printf("%3s0x%04x %-32s ",
960 instruction_print_(*(l
->instr
), 0);
965 VERBOSE_PRINTF("\n");
968 fprintf(stderr
, "some labels could not be resolved\n");
974 * write assembled words to named file
977 int output_(struct dynamic_array
*instructionps
, const char *filename
) {
979 struct instruction_
**instrp
;
980 size_t i
, r
, total_words
= 0;
984 of
= fopen(filename
, "w");
986 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
991 for (i
= 0; i
< instructionps
->entries
; i
++) {
992 instrp
= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, i
);
996 s
= instruction_print_(*instrp
, 1);
997 printf("%*s;", (44 - s
) > 0 ? (44 - s
) : 0, "");
998 for (x
= 0; x
< (*instrp
)->length
; x
++) {
999 printf(" %04x", (*instrp
)->instr_words
[x
]);
1005 r
= fwrite((*instrp
)->instr_words
, sizeof(DCPU16_WORD
), (*instrp
)->length
, of
);
1006 if (r
< (*instrp
)->length
) {
1007 fprintf(stderr
, "%s():%s\n", "fwrite", strerror(errno
));
1011 total_words
+= (*instrp
)->length
;
1014 fprintf(stderr
, "%s 0x%04zx instructions as 0x%04zx words\n",
1015 opt_
.dryrun
? "assembled" : "wrote",
1022 static struct dynamic_array
*instructionps_
;
1023 static struct dynamic_array
*labels_
;
1025 int main(int argc
, char *argv
[]) {
1026 const char *out_filename
= NULL
;
1027 unsigned int allow_short_labels
= 0;
1030 while ( (c
= getopt(argc
, argv
, "dhsvo:")) != EOF
) {
1037 allow_short_labels
++;
1042 fprintf(stderr
, "Sorry, I can only write one file at a time.\n");
1045 out_filename
= optarg
;
1065 if (out_filename
== NULL
)
1066 out_filename
= out_filename_default_
;
1069 instructionps_
= dynarray_new(sizeof (struct instruction_
*), 1024);
1070 labels_
= dynarray_new(sizeof(struct label_
), 256);
1071 if (instructionps_
== NULL
1072 || labels_
== NULL
) {
1073 fprintf(stderr
, "failed to initialize\n");
1077 /* if filenames were specified, parse them instead of stdin */
1080 char *filename
= *argv
;
1081 FILE *f
= fopen(filename
, "r");
1086 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
1090 VERBOSE_PRINTF("assembling '%s'...\n", filename
);
1091 c
= parse_stream_(f
, filename
, instructionps_
, labels_
, allow_short_labels
);
1097 VERBOSE_PRINTF("assembling '%s'...\n", "stdin");
1098 c
= parse_stream_(stdin
, "-", instructionps_
, labels_
, allow_short_labels
);
1101 fprintf(stderr
, "could not parse input, aborting\n");
1105 if (assemble_check_(instructionps_
, labels_
, allow_short_labels
)) {
1106 fprintf(stderr
, "errors prevented assembly\n");
1110 if (output_(instructionps_
, out_filename
)) {
1111 fprintf(stderr
, "failed to create output\n");