NASM 0.91
[nasm/sigaren-mirror.git] / nasm.c
blobf4c75c43a58118a14743e98baca88c4444b39f33
1 /* The Netwide Assembler main program module
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
7 */
9 #include <stdio.h>
10 #include <stdarg.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
15 #include "nasm.h"
16 #include "nasmlib.h"
17 #include "parser.h"
18 #include "assemble.h"
19 #include "labels.h"
20 #include "outform.h"
22 static void report_error (int, char *, ...);
23 static void parse_cmdline (int, char **);
24 static void assemble_file (char *);
25 static int getkw (char *buf, char **value);
26 static void register_output_formats(void);
27 static void usage(void);
29 static char *obuf;
30 static char inname[FILENAME_MAX];
31 static char outname[FILENAME_MAX];
32 static char realout[FILENAME_MAX];
33 static int lineno; /* for error reporting */
34 static int pass;
35 static struct ofmt *ofmt = NULL;
37 static FILE *ofile = NULL;
38 static int sb = 16; /* by default */
40 static long current_seg;
41 static struct RAA *offsets;
42 static long abs_offset;
43 #define OFFSET_DELTA 256
46 * get/set current offset...
48 #define get_curr_ofs (current_seg==NO_SEG?abs_offset:\
49 raa_read(offsets,current_seg))
50 #define set_curr_ofs(x) (current_seg==NO_SEG?(void)(abs_offset=(x)):\
51 (void)(offsets=raa_write(offsets,current_seg,(x))))
53 static int want_usage;
54 static int terminate_after_phase;
56 int main(int argc, char **argv) {
57 want_usage = terminate_after_phase = FALSE;
59 nasm_set_malloc_error (report_error);
60 offsets = raa_init();
62 seg_init();
64 register_output_formats();
66 parse_cmdline(argc, argv);
68 if (terminate_after_phase) {
69 if (want_usage)
70 usage();
71 return 1;
74 if (!*outname) {
75 ofmt->filename (inname, realout, report_error);
76 strcpy(outname, realout);
79 ofile = fopen(outname, "wb");
80 if (!ofile) {
81 report_error (ERR_FATAL | ERR_NOFILE,
82 "unable to open output file `%s'", outname);
84 ofmt->init (ofile, report_error, define_label);
85 assemble_file (inname);
86 if (!terminate_after_phase) {
87 ofmt->cleanup ();
88 cleanup_labels ();
90 fclose (ofile);
91 if (terminate_after_phase)
92 remove(outname);
94 if (want_usage)
95 usage();
97 return 0;
100 static void parse_cmdline(int argc, char **argv) {
101 char *param;
103 *inname = *outname = '\0';
104 while (--argc) {
105 char *p = *++argv;
106 if (p[0]=='-') {
107 switch (p[1]) {
108 case 'o': /* these parameters take values */
109 case 'f':
110 if (p[2]) /* the parameter's in the option */
111 param = p+2;
112 else if (!argv[1]) {
113 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
114 "option `-%c' requires an argument",
115 p[1]);
116 break;
117 } else
118 --argc, param = *++argv;
119 if (p[1]=='o') { /* output file */
120 strcpy (outname, param);
121 } else if (p[1]=='f') { /* output format */
122 ofmt = ofmt_find(param);
123 if (!ofmt) {
124 report_error (ERR_FATAL | ERR_NOFILE | ERR_USAGE,
125 "unrecognised output format `%s'",
126 param);
129 break;
130 case 'h':
131 fprintf(stderr,
132 "usage: nasm [-o outfile] [-f format] filename\n");
133 fprintf(stderr,
134 " or nasm -r for version info\n\n");
135 fprintf(stderr,
136 "valid output formats for -f are"
137 " (`*' denotes default):\n");
138 ofmt_list(ofmt);
139 exit (0); /* never need usage message here */
140 break;
141 case 'r':
142 fprintf(stderr, "NASM version %s\n", NASM_VER);
143 exit (0); /* never need usage message here */
144 break;
145 default:
146 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
147 "unrecognised option `-%c'",
148 p[1]);
149 break;
151 } else {
152 if (*inname) {
153 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
154 "more than one input file specified");
155 } else
156 strcpy(inname, p);
159 if (!*inname)
160 report_error (ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
161 "no input file specified");
164 /* used by error function to report location */
165 static char currentfile[FILENAME_MAX];
167 static void assemble_file (char *fname) {
168 FILE *fp = fopen (fname, "r");
169 FILE *oldfile = NULL; /* jrh - used when processing include files */
170 int oldfileline = 0;
171 char *value, *p, buffer[1024+2]; /* maximum line length defined here */
172 insn output_ins;
173 int i, seg, rn_error;
175 if (!fp) { /* couldn't open file */
176 report_error (ERR_FATAL | ERR_NOFILE,
177 "unable to open input file `%s'", fname);
178 return;
181 init_labels ();
182 strcpy(currentfile,fname);
184 /* pass one */
185 pass = 1;
186 current_seg = ofmt->section(NULL, pass, &sb);
187 lineno = 0;
188 while (1) {
189 if (! fgets(buffer, sizeof(buffer), fp)) { /* EOF on current file */
190 if (oldfile) {
191 fclose(fp);
192 fp = oldfile;
193 lineno = oldfileline;
194 strcpy(currentfile,fname);
195 oldfile = NULL;
196 continue;
198 else
199 break;
201 lineno++;
202 if (buffer[strlen(buffer)-1] == '\n') {
203 buffer[strlen(buffer)-1] = '\0';
204 } else {
206 * We have a line that's too long. Throw an error, read
207 * to EOL, and ignore the line for assembly purposes.
209 report_error (ERR_NONFATAL, "line is longer than %d characters",
210 sizeof(buffer)-2);
211 while (fgets(buffer, sizeof(buffer), fp) &&
212 buffer[strlen(buffer)-1] != '\n');
213 continue; /* read another line */
216 /* here we parse our directives; this is not handled by the 'real'
217 * parser. */
219 if ( (i = getkw (buffer, &value)) ) {
220 switch (i) {
221 case 1: /* [SEGMENT n] */
222 seg = ofmt->section (value, pass, &sb);
223 if (seg == NO_SEG) {
224 report_error (ERR_NONFATAL,
225 "segment name `%s' not recognised",
226 value);
227 } else {
228 current_seg = seg;
230 break;
231 case 2: /* [EXTERN label] */
232 if (*value == '$')
233 value++; /* skip initial $ if present */
234 declare_as_global (value, report_error);
235 define_label (value, seg_alloc(), 0L, ofmt, report_error);
236 break;
237 case 3: /* [BITS bits] */
238 switch (atoi(value)) {
239 case 16:
240 case 32:
241 sb = atoi(value);
242 break;
243 default:
244 report_error(ERR_NONFATAL,
245 "`%s' is not a valid argument to [BITS]",
246 value);
247 break;
249 break;
250 case 4: /* [INC file] */
251 oldfile = fp;
252 oldfileline = lineno;
253 lineno = 0;
254 strcpy(currentfile,value);
255 fp = fopen(value,"r");
256 if (!fp) {
257 lineno = oldfileline;
258 fp = oldfile;
259 strcpy(currentfile,fname);
260 report_error (ERR_FATAL,
261 "unable to open include file `%s'\n",
262 value);
264 break;
265 case 5: /* [GLOBAL symbol] */
266 if (*value == '$')
267 value++; /* skip initial $ if present */
268 declare_as_global (value, report_error);
269 break;
270 case 6: /* [COMMON symbol size] */
271 p = value;
272 while (*p && !isspace(*p))
273 p++;
274 if (*p) {
275 long size;
277 while (*p && isspace(*p))
278 *p++ = '\0';
279 size = readnum (p, &rn_error);
280 if (rn_error)
281 report_error (ERR_NONFATAL, "invalid size specified"
282 " in COMMON declaration");
283 else
284 define_common (value, seg_alloc(), size,
285 ofmt, report_error);
286 } else
287 report_error (ERR_NONFATAL, "no size specified in"
288 " COMMON declaration");
289 break;
290 case 7: /* [ABSOLUTE address] */
291 current_seg = NO_SEG;
292 abs_offset = readnum(value, &rn_error);
293 if (rn_error) {
294 report_error (ERR_NONFATAL, "invalid address specified"
295 " for ABSOLUTE directive");
296 abs_offset = 0x100;/* don't go near zero in case of / */
298 break;
299 default:
300 if (!ofmt->directive (buffer+1, value, 1))
301 report_error (ERR_NONFATAL, "unrecognised directive [%s]",
302 buffer+1);
303 break;
305 } else {
306 long offs = get_curr_ofs;
307 parse_line (current_seg, offs, lookup_label,
308 1, buffer, &output_ins, ofmt, report_error);
309 if (output_ins.opcode == I_EQU) {
311 * Special `..' EQUs get processed in pass two.
313 if (!output_ins.label)
314 report_error (ERR_NONFATAL,
315 "EQU not preceded by label");
316 else if (output_ins.label[0] != '.' ||
317 output_ins.label[1] != '.') {
318 if (output_ins.operands == 1 &&
319 (output_ins.oprs[0].type & IMMEDIATE)) {
320 define_label (output_ins.label,
321 output_ins.oprs[0].segment,
322 output_ins.oprs[0].offset,
323 ofmt, report_error);
324 } else if (output_ins.operands == 2 &&
325 (output_ins.oprs[0].type & IMMEDIATE) &&
326 (output_ins.oprs[0].type & COLON) &&
327 output_ins.oprs[0].segment == NO_SEG &&
328 (output_ins.oprs[1].type & IMMEDIATE) &&
329 output_ins.oprs[1].segment == NO_SEG) {
330 define_label (output_ins.label,
331 output_ins.oprs[0].offset | SEG_ABS,
332 output_ins.oprs[1].offset,
333 ofmt, report_error);
334 } else
335 report_error(ERR_NONFATAL, "bad syntax for EQU");
337 } else {
338 if (output_ins.label)
339 define_label (output_ins.label,
340 current_seg, offs,
341 ofmt, report_error);
342 offs += insn_size (current_seg, offs, sb,
343 &output_ins, report_error);
344 set_curr_ofs (offs);
346 cleanup_insn (&output_ins);
350 if (terminate_after_phase) {
351 fclose(ofile);
352 remove(outname);
353 if (want_usage)
354 usage();
355 exit (1);
358 /* pass two */
359 pass = 2;
360 rewind (fp);
361 current_seg = ofmt->section(NULL, pass, &sb);
362 raa_free (offsets);
363 offsets = raa_init();
364 lineno = 0;
365 while (1) {
366 if (!fgets(buffer, sizeof(buffer), fp)) {
367 if (oldfile) {
368 fclose(fp);
369 fp = oldfile;
370 lineno = oldfileline;
371 strcpy(currentfile,fname);
372 oldfile = NULL;
373 continue;
374 } else
375 break;
377 lineno++;
378 if (buffer[strlen(buffer)-1] == '\n')
379 buffer[strlen(buffer)-1] = '\0';
380 else
381 report_error (ERR_PANIC,
382 "too-long line got through from pass one");
384 /* here we parse our directives; this is not handled by
385 * the 'real' parser. */
387 if ( (i = getkw (buffer, &value)) ) {
388 switch (i) {
389 case 1: /* [SEGMENT n] */
390 seg = ofmt->section (value, pass, &sb);
391 if (seg == NO_SEG) {
392 report_error (ERR_PANIC,
393 "invalid segment name on pass two");
394 } else
395 current_seg = seg;
396 break;
397 case 2: /* [EXTERN label] */
398 break;
399 case 3: /* [BITS bits] */
400 switch (atoi(value)) {
401 case 16:
402 case 32:
403 sb = atoi(value);
404 break;
405 default:
406 report_error(ERR_PANIC,
407 "invalid [BITS] value on pass two",
408 value);
409 break;
411 break;
412 case 4:
413 oldfile = fp;
414 oldfileline = lineno;
415 lineno = 0;
416 strcpy(currentfile,value);
417 fp = fopen(value,"r");
418 if (!fp) {
419 lineno = oldfileline;
420 fp = oldfile;
421 strcpy(currentfile,fname);
423 * We don't report this error in the PANIC
424 * class, even though we might expect to have
425 * already picked it up during pass one,
426 * because of the tiny chance that some other
427 * process may have removed the include file
428 * between the passes.
430 report_error (ERR_FATAL,
431 "unable to open include file `%s'\n",
432 value);
434 break;
435 case 5: /* [GLOBAL symbol] */
436 break;
437 case 6: /* [COMMON symbol size] */
438 break;
439 case 7: /* [ABSOLUTE addr] */
440 current_seg = NO_SEG;
441 abs_offset = readnum(value, &rn_error);
442 if (rn_error)
443 report_error (ERR_PANIC, "invalid ABSOLUTE address "
444 "in pass two");
445 break;
446 default:
447 if (!ofmt->directive (buffer+1, value, 2))
448 report_error (ERR_PANIC, "invalid directive on pass two");
449 break;
451 } else {
452 long offs = get_curr_ofs;
453 parse_line (current_seg, offs, lookup_label, 2,
454 buffer, &output_ins, ofmt, report_error);
455 obuf = buffer;
456 if (output_ins.label)
457 define_label_stub (output_ins.label, report_error);
458 if (output_ins.opcode == I_EQU) {
460 * Special `..' EQUs get processed here.
462 if (output_ins.label[0] == '.' &&
463 output_ins.label[1] == '.') {
464 if (output_ins.operands == 1 &&
465 (output_ins.oprs[0].type & IMMEDIATE)) {
466 define_label (output_ins.label,
467 output_ins.oprs[0].segment,
468 output_ins.oprs[0].offset,
469 ofmt, report_error);
470 } else if (output_ins.operands == 2 &&
471 (output_ins.oprs[0].type & IMMEDIATE) &&
472 (output_ins.oprs[0].type & COLON) &&
473 output_ins.oprs[0].segment == NO_SEG &&
474 (output_ins.oprs[1].type & IMMEDIATE) &&
475 output_ins.oprs[1].segment == NO_SEG) {
476 define_label (output_ins.label,
477 output_ins.oprs[0].offset | SEG_ABS,
478 output_ins.oprs[1].offset,
479 ofmt, report_error);
480 } else
481 report_error(ERR_NONFATAL, "bad syntax for EQU");
484 offs += assemble (current_seg, offs, sb,
485 &output_ins, ofmt, report_error);
486 cleanup_insn (&output_ins);
487 set_curr_ofs (offs);
492 static int getkw (char *buf, char **value) {
493 char *p, *q;
495 if (*buf!='[')
496 return 0;
497 p = buf;
498 while (*p && *p != ']') p++;
499 if (!*p)
500 return 0;
501 q = p++;
502 while (*p && *p != ';') {
503 if (!isspace(*p))
504 return 0;
505 p++;
507 q[1] = '\0';
509 p = buf+1;
510 while (*buf && *buf!=' ' && *buf!=']' && *buf!='\t')
511 buf++;
512 if (*buf==']') {
513 *buf = '\0';
514 *value = buf;
515 } else {
516 *buf++ = '\0';
517 *value = buf;
518 while (*buf!=']') buf++;
519 *buf++ = '\0';
521 for (q=p; *q; q++)
522 *q = tolower(*q);
523 if (!strcmp(p, "segment") || !strcmp(p, "section"))
524 return 1;
525 if (!strcmp(p, "extern"))
526 return 2;
527 if (!strcmp(p, "bits"))
528 return 3;
529 if (!strcmp(p, "inc") || !strcmp(p, "include"))
530 return 4;
531 if (!strcmp(p, "global"))
532 return 5;
533 if (!strcmp(p, "common"))
534 return 6;
535 if (!strcmp(p, "absolute"))
536 return 7;
537 return -1;
540 static void report_error (int severity, char *fmt, ...) {
541 va_list ap;
543 if (severity & ERR_NOFILE)
544 fputs ("nasm: ", stderr);
545 else
546 fprintf (stderr, "%s:%d: ", currentfile, lineno);
548 if ( (severity & ERR_MASK) == ERR_WARNING)
549 fputs ("warning: ", stderr);
550 else if ( (severity & ERR_MASK) == ERR_PANIC)
551 fputs ("panic: ", stderr);
553 va_start (ap, fmt);
554 vfprintf (stderr, fmt, ap);
555 fputc ('\n', stderr);
557 if (severity & ERR_USAGE)
558 want_usage = TRUE;
560 switch (severity & ERR_MASK) {
561 case ERR_WARNING:
562 /* no further action, by definition */
563 break;
564 case ERR_NONFATAL:
565 terminate_after_phase = TRUE;
566 break;
567 case ERR_FATAL:
568 fclose(ofile);
569 remove(outname);
570 if (want_usage)
571 usage();
572 exit(1); /* instantly die */
573 break; /* placate silly compilers */
574 case ERR_PANIC:
575 abort(); /* panic and dump core */
576 break;
580 static void usage(void) {
581 fputs("type `nasm -h' for help\n", stderr);
584 static void register_output_formats(void) {
585 /* Flat-form binary format */
586 #ifdef OF_BIN
587 extern struct ofmt of_bin;
588 #endif
589 /* Unix formats: a.out, COFF, ELF */
590 #ifdef OF_AOUT
591 extern struct ofmt of_aout;
592 #endif
593 #ifdef OF_COFF
594 extern struct ofmt of_coff;
595 #endif
596 #ifdef OF_ELF
597 extern struct ofmt of_elf;
598 #endif
599 /* Linux strange format: as86 */
600 #ifdef OF_AS86
601 extern struct ofmt of_as86;
602 #endif
603 /* DOS formats: OBJ, Win32 */
604 #ifdef OF_OBJ
605 extern struct ofmt of_obj;
606 #endif
607 #ifdef OF_WIN32
608 extern struct ofmt of_win32;
609 #endif
610 #ifdef OF_RDF
611 extern struct ofmt of_rdf;
612 #endif
613 #ifdef OF_DBG /* debug format must be included specifically */
614 extern struct ofmt of_dbg;
615 #endif
617 #ifdef OF_BIN
618 ofmt_register (&of_bin);
619 #endif
620 #ifdef OF_AOUT
621 ofmt_register (&of_aout);
622 #endif
623 #ifdef OF_COFF
624 ofmt_register (&of_coff);
625 #endif
626 #ifdef OF_ELF
627 ofmt_register (&of_elf);
628 #endif
629 #ifdef OF_AS86
630 ofmt_register (&of_as86);
631 #endif
632 #ifdef OF_OBJ
633 ofmt_register (&of_obj);
634 #endif
635 #ifdef OF_WIN32
636 ofmt_register (&of_win32);
637 #endif
638 #ifdef OF_RDF
639 ofmt_register (&of_rdf);
640 #endif
641 #ifdef OF_DBG
642 ofmt_register (&of_dbg);
643 #endif
645 * set the default format
647 ofmt = &OF_DEFAULT;