063741d51a8698330c3a8af13d12c33a03114d11
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 * needs ability to specify label as relative to another label
23 * short labels not correctly computed
26 static const char * const src_id_
= "$Id$";
28 const char const out_filename_default_
[] = "a.out";
30 /* global invocation options */
39 #define DEBUG_PRINTF(...) do { if (opt_.verbose > 2) { printf("DEBUG: "); printf(__VA_ARGS__); } } while (0)
40 #define DEBUG_PRINTFQ(...) do { if (opt_.verbose > 2) printf(__VA_ARGS__); } while (0)
41 #define VERBOSE_PRINTF(...) do { if (opt_.verbose) printf(__VA_ARGS__); } while (0)
44 void usage_(char *prog
, unsigned int full
) {
45 FILE *f
= full
? stdout
: stderr
;
46 char *x
= strrchr(prog
, '/');
52 fprintf(f
, "%s -- \n\n",
55 fprintf(f
, "Usage: %s [-h] [-v] [-s] [-o file] file [file [...]]\n",
59 fprintf(f
, "\nOptions:\n"
60 "\t-h -- this screen\n"
61 "\t-o <file> -- output to <file> [default: %s]\n"
62 "\t-s -- allow short labels in instruction words\n"
63 "\t-d -- dry run, print results, do not write to file\n"
64 "\t-v -- verbose output\n",
65 out_filename_default_
);
67 fprintf(f
, "\n%78s\n",
73 /* instructions have operands */
75 struct operand_
*next
;
76 char *operand
; /* tokenized operand text */
79 /* keep an array of instructions as we read them in */
82 char *label
; /* set if a label points here */
83 char *opcode
; /* tokenized instruction text */
84 struct operand_
*operands
; /* list of operands */
85 unsigned int ready
: 1; /* bytecode computed? */
86 unsigned int length
; /* number of words of bytecode */
87 DCPU16_WORD instr_words
[];
90 /* keep an array of labels, indexed back to their instruction locations */
92 char *label
; /* name of label */
93 struct instruction_
**instr
; /* pointer into array of instructions */
94 unsigned int ready
: 1; /* do we know where this label is yet? */
99 /* locate and return the label entry matching name */
101 struct label_
*label_find_(struct dynamic_array
*labels
, char *name
) {
104 for (x
= 0; x
< labels
->entries
; x
++) {
105 struct label_
*l
= (struct label_
*)DYNARRAY_ITEM(*labels
, x
);
106 if (strcmp(l
->label
, name
) == 0)
113 /* if a label has a validly-calculated address, fetch it */
115 int label_addr_(struct dynamic_array
*labels
, char *name
, DCPU16_WORD
*addr
) {
118 if ( (l
= label_find_(labels
, name
)) == NULL
)
127 /* attempt to determine the addresses of all labels */
129 void label_addr_calculate_(struct dynamic_array
*instructionps
, struct dynamic_array
*labels
) {
132 /* idea: label1:label2 - calculated as offset between labels */
134 /* for each label.. */
135 for (i
= 0; i
< labels
->entries
; i
++) {
137 struct instruction_
**instr
;
138 unsigned int word_count
= 0;
140 l
= (struct label_
*)DYNARRAY_ITEM(*labels
, i
);
142 /* if it's already calculated, great. */
147 * starting at the instruction for this label,
148 * walk backwards through the list of instructions
149 * until we get to the start or a known prior label address.
150 * update our label with the freshly calculated addr
152 for (instr
= ((struct label_
*)DYNARRAY_ITEM(*labels
, i
))->instr
;
153 instr
>= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, 0);
157 DEBUG_PRINTF("%s: instr not ready\n", __func__
);
158 word_count
+= (*instr
)->length
;
160 /* have we come across an instruction which a label points to?
161 it should already be calculated, so just add that on and be done */
163 && strcmp((*instr
)->label
, l
->label
)) {
166 if (label_addr_(labels
, (*instr
)->label
, &addr
)) {
167 fprintf(stderr
, "internal error: incomplete prior address for '%s' while calculating '%s'\n",
177 l
->addr
= word_count
;
179 DEBUG_PRINTF("label '%s' now has addr of 0x%04x\n", l
->label
, word_count
);
184 /* generate the nibble for a given basic opcode */
186 int opcode_bits_(char *opcode
) {
190 } opcodes_lower_nibble
[] = {
192 /* { "future nbi instruction", 0x00 }, */
211 for (o
= opcodes_lower_nibble
; o
->op
[0]; o
++) {
212 if (strcasecmp(o
->op
, opcode
) == 0)
216 if (o
->op
[0] == '\0') {
217 fprintf(stderr
, "unknown instruction '%s'\n", opcode
);
224 /* generate the six bits for a given nbi opcode (aka first operand to opcode 0x00) */
226 int nbi_opcode_bits_(char *nbi_opcode
) {
230 } nbi_opcodes_bits
[] = {
231 { " ", 0x00 }, /* reserved for future */
236 for (o
= nbi_opcodes_bits
; o
->op
[0]; o
++) {
237 if (strcasecmp(o
->op
, nbi_opcode
) == 0)
241 if (o
->op
[0] == '\0') {
242 fprintf(stderr
, "unknown nbi instruction '%s'\n", o
->op
);
249 /* convert register character like 'x' to value like 0x03 */
251 unsigned int register_enumerate_(char r
) {
252 const char regs
[] = "AaBbCcXxYyZzIiJj";
253 const char *x
= strchr(regs
, r
);
258 fprintf(stderr
, "internal error, unknown register character 0x%02x\n", r
);
262 /* removes all occurences of chars from buf */
264 void buf_strip_chars_(char *buf
, char *chars
) {
267 for (s
= d
= buf
; *s
; s
++, d
++) {
268 while (*s
&& strchr(chars
, *s
)) {
280 * generate the six bits for a given operand string
281 * returns -1 if it could not parse the operand
282 * returns -2 if it could not parse the operand due to an unresolved label
283 * notes: nextword may be overwritten even if it's not used in final instruction
286 int value_bits_(struct dynamic_array
*labels
, const char *operand_orig
, DCPU16_WORD
*nextword
, unsigned int *nextwordused
, unsigned int allow_short_labels
) {
287 static char *operand
= NULL
;
288 static size_t operand_sz
= 0;
294 Our operand working buffer shouldn't ever need to be too big,
295 but DAT might blow that assumption.
297 if (operand_sz
<= strlen(operand_orig
)) {
299 size_t new_sz
= strlen(operand_orig
);
305 DEBUG_PRINTF("%s: allocating buffer of size %zu\n", __func__
, new_sz
);
306 tmp_ptr
= realloc(operand
, new_sz
);
307 if (tmp_ptr
== NULL
) {
308 fprintf(stderr
, "%s(%zu):%s\n", "realloc", new_sz
, strerror(errno
));
315 o
= strcpy(operand
, operand_orig
);
317 DEBUG_PRINTF("%s: operand '%s' ", __func__
, operand
); /* completed later */
319 /* this is a very stupid parser */
321 /* first, let's trim all whitespace out of string at once to make parsing easier */
322 buf_strip_chars_(operand
, " \t\n");
324 /* single character might match a register */
325 if (strlen(operand
) == 1
326 && strchr("AaBbCcXxYyZzIiJj", *operand
)) {
327 DEBUG_PRINTFQ("is register %c\n", *operand
);
328 return register_enumerate_(*operand
);
332 if (strcasecmp(operand
, "POP") == 0) {
333 DEBUG_PRINTFQ("is POP\n");
336 if (strcasecmp(operand
, "PUSH") == 0) {
337 DEBUG_PRINTFQ("is PUSH\n");
340 if (strcasecmp(operand
, "PEEK") == 0) {
341 DEBUG_PRINTFQ("is PEEK\n");
344 if (strcasecmp(operand
, "SP") == 0) {
345 DEBUG_PRINTFQ("is register SP\n");
348 if (strcasecmp(operand
, "PC") == 0) {
349 DEBUG_PRINTFQ("is register PC\n");
352 if (strcasecmp(operand
, "O") == 0) {
353 DEBUG_PRINTFQ("is register O\n");
357 /* is the operand [bracketed]? */
358 if (operand
[0] == '[' && operand
[strlen(operand
) - 1] == ']') {
359 /* eat the brackets */
360 operand
[strlen(operand
) - 1] = '\0';
363 /* is it [register]? */
364 if (strlen(operand
) == 1
365 && strchr("AaBbCcXxYyZzIiJj", *operand
)) {
366 DEBUG_PRINTFQ("is dereferenced register %c\n", *operand
);
367 return 0x08 | register_enumerate_(*operand
);
370 /* is it [register+something]? */
371 if ( (ep
= strchr(operand
, '+')) ) {
379 /* figure out which one is which */
381 && strchr("AaBbCcXxYyZzIiJj", *ep
)) {
384 } else if (strlen(operand
) == 1
385 && strchr("AaBbCcXxYyZzIiJj", *operand
) ) {
389 DEBUG_PRINTFQ("is unparsable\n");
390 fprintf(stderr
, "couldn't parse operand '%s'\n", operand_orig
);
394 /* check if something is understandable as a value */
396 l
= strtoul(constant
, &ep
, 0);
398 && (*constant
&& (*ep
== '\0')) ) {
399 /* string conversion went without issue */
400 /* validate it will fit in a word */
402 DEBUG_PRINTFQ("is out of range\n");
403 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
408 *nextword
= l
& 0xffff;
410 DEBUG_PRINTFQ("is a dereferenced register (%c) + constant (%hu)\n", *reg
, *nextword
);
411 return 0x10 | register_enumerate_(*reg
);
413 DEBUG_PRINTFQ("is out of range\n");
414 fprintf(stderr
, "trouble with operand '%s': %s\n", operand_orig
, strerror(errno
));
418 /* what? still here? assume it's a label, I guess */
419 /* try to populate nextword with label address */
420 if (label_addr_(labels
, operand
, nextword
)) {
421 DEBUG_PRINTFQ("(deferred label resolution)\n");
425 DEBUG_PRINTFQ("is a dereferenced register (%c) + label\n", *reg
);
427 return 0x10 | register_enumerate_(*reg
);
430 /* it must just be a dereferenced literal then */
433 l
= strtoul(operand
, &ep
, 0);
435 && (*operand
&& (*ep
== '\0')) ) {
436 /* string conversion went without issue */
437 /* validate it will fit in a word */
439 DEBUG_PRINTFQ("is out of range\n");
440 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
444 DEBUG_PRINTFQ("is a dereferenced literal value (%hu)\n", *nextword
);
445 *nextword
= l
& 0xffff;
449 DEBUG_PRINTFQ("is out of range\n");
450 fprintf(stderr
, "trouble with operand '%s': %s\n", operand_orig
, strerror(errno
));
453 /* not a number? try a label */
454 if (label_addr_(labels
, operand
, nextword
)) {
455 DEBUG_PRINTFQ("(deferred label resolution)\n");
459 DEBUG_PRINTFQ("is a dereferenced label\n");
464 /* left with a literal or a label, then */
467 l
= strtoul(operand
, &ep
, 0);
469 || (*operand
&& (*ep
== '\0')) ) {
471 DEBUG_PRINTFQ("is out of range\n");
472 fprintf(stderr
, "constant invalid in operand '%s'\n", operand_orig
);
476 DEBUG_PRINTFQ("is literal value (%lu)\n", l
);
481 *nextword
= l
& 0xffff;
486 /* try to populate nextword with label address */
487 if (label_addr_(labels
, operand
, nextword
)) {
488 DEBUG_PRINTFQ("(deferred label resolution)\n");
489 /* assume non-small literal value */
494 DEBUG_PRINTFQ("is label '%s' (0x%02hx)\n", operand
, *nextword
);
495 if (*nextword
< 0x20 && allow_short_labels
) {
496 DEBUG_PRINTF("small value label win\n");
497 return (0x20 + *nextword
) & 0x3f;
504 /* prints an instruction's assembly */
506 int instruction_print_(struct instruction_
*i
, unsigned int with_label
) {
511 r
= printf("%-16s %3s", i
->label
? i
->label
: "", i
->opcode
);
513 r
= printf("%3s", i
->opcode
);
515 for (o
= i
->operands
; o
; o
= o
->next
)
516 r
+= printf(" %s%s", o
->operand
, o
->next
? "," : "");
522 * Parses a zero-terminated line of input into a newly-allocated struct instruction_.
523 * [label] instruction [operand[,operand[,...]]]
524 * Does no validation of contents of any of these tokens, as of yet.
527 int tokenize_line_(char *line
, struct instruction_
**next_instr
) {
528 const char const *whitespace
= " \t\n";
529 const char const *quotes
= "\"'`";
530 struct instruction_
*instr
= NULL
;
532 char *label
, *opcode
;
533 struct operand_
*operand_list
= NULL
;
534 struct operand_
**operand_tail
= &operand_list
;
535 size_t instr_words_needed
= 0;
542 /* strip leading whitespace */
543 line
+= strspn(line
, whitespace
);
547 /* set first bare ';' to '\0', thus isolating any comments */
548 /* here we only care about the side-effect of truncating the first separator character */
549 (void)strqtok_r(line
, ";", '\\', quotes
, &qt
, &st
);
550 /* we don't care if there was an unmatched quote at this point, let's see what happens */
554 /* carve off the first token, determine if it is a label */
555 x
= strqtok_r(line
, whitespace
, '\\', quotes
, &qt
, &st
);
556 if (x
== NULL
|| *x
== '\0')
559 /* labels could contain an unmatched quote character, I guess? */
563 /* we have something, try to make sense of what it is */
565 #ifdef NON_SPEC_LABELS
566 /* I want my labels like 'label:' */
567 if ( *(x
+ strlen(line
) - 1) == ':' ) {
568 *(x
+ strlen(line
) - 1) = '\0';
569 DEBUG_PRINTF("label: %s\n", x
);
573 opcode
= strqtok_r(NULL
, whitespace
, '\\', quotes
, &qt
, &st
);
578 #endif /* NON_SPEC_LABELS */
580 /* spec gives example of labels as ':label' */
585 opcode
= strqtok_r(NULL
, whitespace
, '\\', quotes
, &qt
, &st
);
590 /* opcodes shouldn't have quotes, so we'll ignore any unmatched quotes again */
592 if (opcode
&& *opcode
) {
593 /* if we have an opcode, we'll need at least one word to compile instruction */
594 instr_words_needed
++;
596 while ( (x
= strqtok_r(NULL
, ",", '\\', quotes
, &qt
, &st
)) ) {
597 struct operand_
*new_operand
;
600 /* trim whitespaces */
601 x
+= strspn(x
, whitespace
);
604 for (y
= x
+ strlen(x
) - 1; *y
; y
--) {
605 if (strchr(whitespace
, *y
)) {
612 fprintf(stderr
, "null operand encountered\n");
616 DEBUG_PRINTF("tokenized operand '%s'\n", x
);
618 new_operand
= malloc(sizeof *new_operand
);
619 if (new_operand
== NULL
) {
620 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
624 new_operand
->operand
= strdup(x
);
625 if (new_operand
->operand
== NULL
) {
626 fprintf(stderr
, "%s():%s\n", "strdup", strerror(errno
));
630 new_operand
->next
= NULL
;
632 if (strchr(quotes
, x
[0])) {
633 /* if this is a quoted operand, assuming we are in a DAT statement, it will take up slightly less room than it is long */
634 instr_words_needed
+= strlen(x
) - 1;
636 instr_words_needed
++;
638 *operand_tail
= new_operand
;
639 operand_tail
= &(*operand_tail
)->next
;
643 DEBUG_PRINTF("allocating new instruction with room for %zu bytes\n", instr_words_needed
);
645 instr
= calloc(1, (instr_words_needed
* sizeof *instr
->instr_words
) + sizeof *instr
);
647 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
652 instr
->label
= strdup(label
);
653 if (instr
->label
== NULL
) {
654 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
662 instr
->opcode
= strdup(opcode
);
663 if (instr
->opcode
== NULL
) {
664 fprintf(stderr
, "%s():%s\n", "malloc", strerror(errno
));
671 instr
->operands
= operand_list
;
678 /* try to generate bytecode for an instruction */
679 /* returns -1 on unrecoverable error */
681 int instr_assemble_(struct dynamic_array
*labels
, struct instruction_
*i
, unsigned int allow_short_labels
) {
682 unsigned int nwu
= 0; /* number of words used */
683 unsigned int incomplete
= 0;
685 struct operand_
*o
= i
->operands
;
687 if (opt_
.verbose
> 2) {
688 printf("%s: assembling %p ", __func__
, i
);
689 instruction_print_(i
, 1);
690 printf("(line :%zu)\n", i
->src_line
);
694 /* already assembled, nothing to do */
698 /* special case DAT */
699 if (strncasecmp(i
->opcode
, "DAT", 3) == 0) {
700 DEBUG_PRINTF("processing DAT...\n");
704 for ( /* */ ; o
; o
= o
->next
) {
709 DEBUG_PRINTF("DAT operand:'%s' next:%p\n", o
->operand
, o
->next
);
711 /* is this a string? */
712 if ( (x
= strchr("\"'`", o
->operand
[0])) ) {
713 dat_len
= strlen(o
->operand
) - 1;
714 if (o
->operand
[dat_len
] == *x
) {
716 DEBUG_PRINTF("DAT string operand: %s\n", o
->operand
);
718 for (j
= 0, x
= o
->operand
+ 1;
721 i
->instr_words
[i
->length
] = *x
;
724 /* Note that strings in DAT do not include their zero-terminators */
725 /* specify as 'DAT "string", 0' */
730 /* is this a number? */
733 l
= strtoul(o
->operand
, &ep
, 0);
735 && (*o
->operand
&& (*ep
== '\0')) ) {
736 /* conversion succeeded */
738 fprintf(stderr
, "value '%lu' out of range\n", l
);
741 i
->instr_words
[i
->length
] = l
;
746 /* otherwise assume it's a label, even if we don't know what it is */
747 if (label_addr_(labels
, o
->operand
, &i
->instr_words
[i
->length
])) {
748 DEBUG_PRINTF("(deferred label resolution)\n");
755 DEBUG_PRINTF("pending label address\n");
763 /* start with opcode bits */
764 bits
= opcode_bits_(i
->opcode
);
766 fprintf(stderr
, "unrecognized instruction '%s%s", i
->opcode
, i
->operands
? " " : "");
767 for (o
= i
->operands
; o
; o
= o
->next
)
768 fprintf(stderr
, " %s%s", o
->operand
, o
->next
? "," : "");
769 fprintf(stderr
, "'\n");
772 i
->instr_words
[0] |= 0x0f & bits
;
774 /* in rendered bytecode, all instructions have two operands; nbi instructions take 'first operand' bits. */
775 if ((bits
& 0x0f) == 0) {
776 bits
= nbi_opcode_bits_(i
->opcode
);
778 fprintf(stderr
, "INTERNAL ERROR: missing instruction in nbi opcode table\n");
783 fprintf(stderr
, "'%s' requires more operands\n", i
->opcode
);
786 bits
= value_bits_(labels
, o
->operand
, i
->instr_words
+ 1, &nwu
, allow_short_labels
);
788 fprintf(stderr
, "couldn't assemble instruction\n");
790 } else if (bits
== -2) {
791 DEBUG_PRINTF("%s: assembly deferred: unresolved label\n", __func__
);
792 /* keep going, but don't finalize until we can calculate label address */
798 i
->instr_words
[0] |= (bits
& 0x3f) << 4;
801 fprintf(stderr
, "'%s' requires more operands\n", i
->opcode
);
805 bits
= value_bits_(labels
, o
->operand
, i
->instr_words
+ 1 + nwu
, &nwu
, allow_short_labels
);
807 fprintf(stderr
, "couldn't assemble instruction\n");
809 } else if (bits
== -2) {
810 DEBUG_PRINTF("%s: assembly deferred: unresolved label\n", __func__
);
811 /* keep going, but don't finalize until we can calculate label address */
816 i
->instr_words
[0] |= (bits
& 0x3f) << 10;
819 fprintf(stderr
, "too many operands\n");
823 /* counting labels as words, we now know at least the maximum instruction length */
827 DEBUG_PRINTF("instruction words: [%u]", i
->length
);
828 for (bits
= 0; bits
<= (int)nwu
; bits
++)
829 DEBUG_PRINTFQ(" %04x", i
->instr_words
[bits
]);
832 DEBUG_PRINTFQ(" (preliminary)");
843 * read lines from stream f
844 * break each line into parts, populate parts into structures
847 int parse_stream_(FILE *f
, const char *src
, struct dynamic_array
*instructionps
, struct dynamic_array
*labels
, unsigned int allow_short_labels
) {
848 struct instruction_
*instr
, **instr_list_entry
;
849 unsigned int line
= 0;
853 buf
[sizeof buf
- 1] = '\0';
855 while (fgets(buf
, sizeof buf
, f
)) {
858 if (buf
[sizeof buf
- 1] != '\0') {
859 fprintf(stderr
, "%s:%u:%s", src
, line
, "input line too long\n");
864 if (tokenize_line_(buf
, &instr
)) {
865 fprintf(stderr
, "%s:%u:%s", src
, line
, "trouble tokenizing input\n");
871 instr
->src_line
= line
;
872 /* add to list of instructions */
873 instr_list_entry
= dynarray_add(instructionps
, &instr
);
874 if (instr_list_entry
== NULL
) {
875 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not populate instruction list\n");
880 struct label_ new_label
= {
881 .label
= instr
->label
,
882 .instr
= instr_list_entry
,
886 if (label_find_(labels
, instr
->label
)) {
887 fprintf(stderr
, "%s:%u:%s", src
, line
, "duplicate label\n");
891 if (dynarray_add(labels
, &new_label
) == NULL
) {
892 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not populate label list\n");
895 label_addr_calculate_(instructionps
, labels
);
898 if (instr_assemble_(labels
, instr
, allow_short_labels
)) {
899 fprintf(stderr
, "%s:%u:%s", src
, line
, "could not assemble instruction\n");
905 fprintf(stderr
, "%s():%s\n", "fgets", strerror(errno
));
909 fprintf(stderr
, "parsing aborted\n");
917 * make a full pass over instruction list to resolve labels
920 int assemble_check_(struct dynamic_array
*instructionps
, struct dynamic_array
*labels
, unsigned int allow_short_labels
) {
924 /* fixing short labels .... */
925 /* by here we have our list of instructions and their maximum instruction lengths */
926 /* and we have a list of addresses, based on those maximum lengths */
927 /* So, if doing short labels, all label addresses are now suspect, so recompute them all... */
928 /* and reassemble.. */
929 /* uh.. what else am I forgetting.. this method won't work for labels approaching the limit */
930 /* of short form addresses, when there are more than the difference number of short form labels used previous to those addresses */
932 /* try this? keep another list of locations a label address is used */
933 /* as we step forward, and recompute an address, back up to first occurence of address, make sure nothing else has changed */
935 DEBUG_PRINTF(" final pass of assembler...\n");
936 for (x
= 0; x
< instructionps
->entries
; x
++) {
937 struct instruction_
**instrp
= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, x
);
938 retval
= instr_assemble_(labels
, *instrp
, allow_short_labels
);
940 fprintf(stderr
, "instruction %zu failed to assemble\n", x
);
943 if (! (*instrp
)->ready
) {
944 fprintf(stderr
, "instruction not resolvable\n");
949 VERBOSE_PRINTF("%3s %6s %-32s %-4s\n", "", "_addr_", "_label_", "_instruction_");
950 for (x
= 0; x
< labels
->entries
; x
++) {
951 struct label_
*l
= (struct label_
*)DYNARRAY_ITEM(*labels
, x
);
955 printf("%3s0x%04x %-32s ",
959 instruction_print_(*(l
->instr
), 0);
964 VERBOSE_PRINTF("\n");
967 fprintf(stderr
, "some labels could not be resolved\n");
973 * write assembled words to named file
976 int output_(struct dynamic_array
*instructionps
, const char *filename
) {
978 struct instruction_
**instrp
;
979 size_t i
, r
, total_words
= 0;
983 of
= fopen(filename
, "w");
985 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
990 for (i
= 0; i
< instructionps
->entries
; i
++) {
991 instrp
= (struct instruction_
**)DYNARRAY_ITEM(*instructionps
, i
);
995 s
= instruction_print_(*instrp
, 1);
996 printf("%*s;", (44 - s
) > 0 ? (44 - s
) : 0, "");
997 for (x
= 0; x
< (*instrp
)->length
; x
++) {
998 printf(" %04x", (*instrp
)->instr_words
[x
]);
1004 r
= fwrite((*instrp
)->instr_words
, sizeof(DCPU16_WORD
), (*instrp
)->length
, of
);
1005 if (r
< (*instrp
)->length
) {
1006 fprintf(stderr
, "%s():%s\n", "fwrite", strerror(errno
));
1010 total_words
+= (*instrp
)->length
;
1013 fprintf(stderr
, "%s 0x%04zx instructions as 0x%04zx words\n",
1014 opt_
.dryrun
? "assembled" : "wrote",
1021 static struct dynamic_array
*instructionps_
;
1022 static struct dynamic_array
*labels_
;
1024 int main(int argc
, char *argv
[]) {
1025 const char *out_filename
= NULL
;
1026 unsigned int allow_short_labels
= 0;
1029 while ( (c
= getopt(argc
, argv
, "dhsvo:")) != EOF
) {
1036 allow_short_labels
++;
1041 fprintf(stderr
, "Sorry, I can only write one file at a time.\n");
1044 out_filename
= optarg
;
1064 if (out_filename
== NULL
)
1065 out_filename
= out_filename_default_
;
1068 instructionps_
= dynarray_new(sizeof (struct instruction_
*), 1024);
1069 labels_
= dynarray_new(sizeof(struct label_
), 256);
1070 if (instructionps_
== NULL
1071 || labels_
== NULL
) {
1072 fprintf(stderr
, "failed to initialize\n");
1076 /* if filenames were specified, parse them instead of stdin */
1079 char *filename
= *argv
;
1080 FILE *f
= fopen(filename
, "r");
1085 fprintf(stderr
, "%s('%s'):%s\n", "fopen", filename
, strerror(errno
));
1089 VERBOSE_PRINTF("assembling '%s'...\n", filename
);
1090 c
= parse_stream_(f
, filename
, instructionps_
, labels_
, allow_short_labels
);
1096 VERBOSE_PRINTF("assembling '%s'...\n", "stdin");
1097 c
= parse_stream_(stdin
, "-", instructionps_
, labels_
, allow_short_labels
);
1100 fprintf(stderr
, "could not parse input, aborting\n");
1104 if (assemble_check_(instructionps_
, labels_
, allow_short_labels
)) {
1105 fprintf(stderr
, "errors prevented assembly\n");
1109 if (output_(instructionps_
, out_filename
)) {
1110 fprintf(stderr
, "failed to create output\n");