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.
27 struct forwrefinfo
{ /* info held on forward refs. */
32 static int get_bits(char *value
);
33 static uint32_t get_cpu(char *cpu_str
);
34 static void parse_cmdline(int, char **);
35 static void assemble_file(char *);
36 static int getkw(char **directive
, char **value
);
37 static void register_output_formats(void);
38 static void report_error_gnu(int severity
, const char *fmt
, ...);
39 static void report_error_vc(int severity
, const char *fmt
, ...);
40 static void report_error_common(int severity
, const char *fmt
,
42 static int is_suppressed_warning(int severity
);
43 static void usage(void);
44 static efunc report_error
;
46 static int using_debug_info
, opt_verbose_info
;
47 int tasm_compatible_mode
= FALSE
;
52 static char inname
[FILENAME_MAX
];
53 static char outname
[FILENAME_MAX
];
54 static char listname
[FILENAME_MAX
];
55 static int globallineno
; /* for forward-reference tracking */
56 /* static int pass = 0; */
57 static struct ofmt
*ofmt
= NULL
;
59 static FILE *error_file
; /* Where to write error messages */
61 static FILE *ofile
= NULL
;
62 int optimizing
= -1; /* number of optimization passes to take */
63 static int sb
, cmd_sb
= 16; /* by default */
64 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
65 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
66 int global_offset_changed
; /* referenced in labels.c */
68 static loc_t location
;
69 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
70 int32_t abs_seg
; /* ABSOLUTE segment basis */
71 int32_t abs_offset
; /* ABSOLUTE offset */
73 static struct RAA
*offsets
;
75 static struct SAA
*forwrefs
; /* keep track of forward references */
76 static struct forwrefinfo
*forwref
;
78 static Preproc
*preproc
;
80 op_normal
, /* Preprocess and assemble */
81 op_preprocess
, /* Preprocess only */
82 op_depend
/* Generate dependencies */
84 static enum op_type operating_mode
;
87 * Which of the suppressible warnings are suppressed. Entry zero
88 * doesn't do anything. Initial defaults are given here.
90 static char suppressed
[1 + ERR_WARN_MAX
] = {
91 0, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
95 * The option names for the suppressible warnings. As before, entry
98 static const char *suppressed_names
[1 + ERR_WARN_MAX
] = {
99 NULL
, "macro-params", "macro-selfref", "orphan-labels",
105 * The explanations for the suppressible warnings. As before, entry
108 static const char *suppressed_what
[1 + ERR_WARN_MAX
] = {
110 "macro calls with wrong no. of params",
111 "cyclic macro self-references",
112 "labels alone on lines without trailing `:'",
113 "numeric constants greater than 0xFFFFFFFF",
114 "using 8- or 16-bit relocation in ELF, a GNU extension"
118 * This is a null preprocessor which just copies lines from input
119 * to output. It's used when someone explicitly requests that NASM
120 * not preprocess their source file.
123 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*);
124 static char *no_pp_getline(void);
125 static void no_pp_cleanup(int);
126 static Preproc no_pp
= {
133 * get/set current offset...
135 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
136 raa_read(offsets,location.segment))
137 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
138 (void)(offsets=raa_write(offsets,location.segment,(x))))
140 static int want_usage
;
141 static int terminate_after_phase
;
142 int user_nolist
= 0; /* fbk 9/2/00 */
144 static void nasm_fputs(const char *line
, FILE * outfile
)
147 fputs(line
, outfile
);
148 fputc('\n', outfile
);
153 int main(int argc
, char **argv
)
156 want_usage
= terminate_after_phase
= FALSE
;
157 report_error
= report_error_gnu
;
159 nasm_set_malloc_error(report_error
);
160 offsets
= raa_init();
161 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
164 operating_mode
= op_normal
;
170 register_output_formats();
172 parse_cmdline(argc
, argv
);
174 if (terminate_after_phase
) {
180 /* If debugging info is disabled, suppress any debug calls */
181 if (!using_debug_info
)
182 ofmt
->current_dfmt
= &null_debug_form
;
185 pp_extra_stdmac(ofmt
->stdmac
);
186 parser_global_info(ofmt
, &location
);
187 eval_global_info(ofmt
, lookup_label
, &location
);
189 /* define some macros dependent of command-line */
192 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
197 switch (operating_mode
) {
201 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
202 if (outname
[0] == '\0')
203 ofmt
->filename(inname
, outname
, report_error
);
205 fprintf(stdout
, "%s: %s", outname
, inname
);
206 while ((line
= preproc
->getline()))
216 char *file_name
= NULL
;
217 int32_t prior_linnum
= 0;
221 ofile
= fopen(outname
, "w");
223 report_error(ERR_FATAL
| ERR_NOFILE
,
224 "unable to open output file `%s'",
229 location
.known
= FALSE
;
232 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
233 while ((line
= preproc
->getline())) {
235 * We generate %line directives if needed for later programs
237 int32_t linnum
= prior_linnum
+= lineinc
;
238 int altline
= src_get(&linnum
, &file_name
);
240 if (altline
== 1 && lineinc
== 1)
241 nasm_fputs("", ofile
);
243 lineinc
= (altline
!= -1 || lineinc
!= 1);
244 fprintf(ofile
? ofile
: stdout
,
245 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
248 prior_linnum
= linnum
;
250 nasm_fputs(line
, ofile
);
253 nasm_free(file_name
);
257 if (ofile
&& terminate_after_phase
)
265 * We must call ofmt->filename _anyway_, even if the user
266 * has specified their own output file, because some
267 * formats (eg OBJ and COFF) use ofmt->filename to find out
268 * the name of the input file and then put that inside the
271 ofmt
->filename(inname
, outname
, report_error
);
273 ofile
= fopen(outname
, "wb");
275 report_error(ERR_FATAL
| ERR_NOFILE
,
276 "unable to open output file `%s'", outname
);
280 * We must call init_labels() before ofmt->init() since
281 * some object formats will want to define labels in their
282 * init routines. (eg OS/2 defines the FLAT group)
286 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
288 assemble_file(inname
);
290 if (!terminate_after_phase
) {
291 ofmt
->cleanup(using_debug_info
);
295 * We had an fclose on the output file here, but we
296 * actually do that in all the object file drivers as well,
297 * so we're leaving out the one here.
316 if (terminate_after_phase
)
323 * Get a parameter for a command line option.
324 * First arg must be in the form of e.g. -f...
326 static char *get_param(char *p
, char *q
, int *advance
)
329 if (p
[2]) { /* the parameter's in the option */
339 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
340 "option `-%c' requires an argument", p
[1]);
350 #define OPT_POSTFIX 1
351 struct textargs textopts
[] = {
352 {"prefix", OPT_PREFIX
},
353 {"postfix", OPT_POSTFIX
},
358 static int process_arg(char *p
, char *q
)
366 if (p
[0] == '-' && !stopoptions
) {
371 case 'o': /* these parameters take values */
386 if (!(param
= get_param(p
, q
, &advance
)))
388 if (p
[1] == 'o') { /* output file */
389 strcpy(outname
, param
);
390 } else if (p
[1] == 'f') { /* output format */
391 ofmt
= ofmt_find(param
);
393 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
394 "unrecognised output format `%s' - "
395 "use -hf for a list", param
);
397 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
398 } else if (p
[1] == 'O') { /* Optimization level */
402 if (isdigit(*param
)) {
404 while (isdigit(*++param
)) ;
406 optimizing
= -1; /* 0.98 behaviour */
408 optimizing
= 0; /* Two passes, 0.98.09 behavior */
410 optimizing
= opt
; /* Multiple passes */
412 if (*param
== 'v' || *param
== '+') {
414 opt_verbose_info
= TRUE
;
416 } else { /* garbage */
421 } /* while (*param) */
423 report_error(ERR_FATAL
,
424 "command line optimization level must be 'v', 0..3 or <nn>");
425 } else if (p
[1] == 'P' || p
[1] == 'p') { /* pre-include */
426 pp_pre_include(param
);
427 } else if (p
[1] == 'D' || p
[1] == 'd') { /* pre-define */
428 pp_pre_define(param
);
429 } else if (p
[1] == 'U' || p
[1] == 'u') { /* un-define */
430 pp_pre_undefine(param
);
431 } else if (p
[1] == 'I' || p
[1] == 'i') { /* include search path */
432 pp_include_path(param
);
433 } else if (p
[1] == 'l') { /* listing file */
434 strcpy(listname
, param
);
435 } else if (p
[1] == 'E') { /* error messages file */
436 error_file
= fopen(param
, "w");
438 error_file
= stderr
; /* Revert to default! */
439 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
440 "cannot open file `%s' for error messages",
443 } else if (p
[1] == 'F') { /* specify debug format */
444 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
445 if (!ofmt
->current_dfmt
) {
446 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
447 "unrecognized debug format `%s' for"
448 " output format `%s'",
449 param
, ofmt
->shortname
);
451 } else if (p
[1] == 'X') { /* specify error reporting format */
452 if (nasm_stricmp("vc", param
) == 0)
453 report_error
= report_error_vc
;
454 else if (nasm_stricmp("gnu", param
) == 0)
455 report_error
= report_error_gnu
;
457 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
458 "unrecognized error reporting format `%s'",
463 using_debug_info
= TRUE
;
467 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
469 " [options...] [--] filename\n"
470 " or nasm -r for version info (obsolete)\n"
471 " or nasm -v for version info (preferred)\n\n"
472 " -t assemble in SciTech TASM compatible mode\n"
473 " -g generate debug information in selected format.\n");
475 (" -e preprocess only (writes output to stdout by default)\n"
476 " -a don't preprocess (assemble only)\n"
477 " -M generate Makefile dependencies on stdout\n\n"
478 " -E<file> redirect error messages to file\n"
479 " -s redirect error messages to stdout\n\n"
480 " -F format select a debugging format\n\n"
481 " -I<path> adds a pathname to the include file path\n");
483 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
484 " -P<file> pre-includes a file\n"
485 " -D<macro>[=<value>] pre-defines a macro\n"
486 " -U<macro> undefines a macro\n"
487 " -X<format> specifies error reporting format (gnu or vc)\n"
488 " -w+foo enables warnings about foo; -w-foo disables them\n"
489 "where foo can be:\n");
490 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
491 printf(" %-23s %s (default %s)\n",
492 suppressed_names
[i
], suppressed_what
[i
],
493 suppressed
[i
] ? "off" : "on");
495 ("\nresponse files should contain command line parameters"
496 ", one per line.\n");
498 printf("\nvalid output formats for -f are"
499 " (`*' denotes default):\n");
500 ofmt_list(ofmt
, stdout
);
502 printf("\nFor a list of valid output formats, use -hf.\n");
503 printf("For a list of debug formats, use -f <form> -y.\n");
505 exit(0); /* never need usage message here */
508 printf("\nvalid debug formats for '%s' output format are"
509 " ('*' denotes default):\n", ofmt
->shortname
);
510 dfmt_list(ofmt
, stdout
);
514 tasm_compatible_mode
= TRUE
;
519 const char *nasm_version_string
=
520 "NASM version " NASM_VER
" compiled on " __DATE__
525 puts(nasm_version_string
);
526 exit(0); /* never need usage message here */
529 case 'e': /* preprocess only */
530 operating_mode
= op_preprocess
;
532 case 'a': /* assemble only - don't preprocess */
536 if (p
[2] != '+' && p
[2] != '-') {
537 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
538 "invalid option to `-w'");
540 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
541 if (!nasm_stricmp(p
+ 3, suppressed_names
[i
]))
543 if (i
<= ERR_WARN_MAX
)
544 suppressed
[i
] = (p
[2] == '-');
546 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
547 "invalid option to `-w'");
551 operating_mode
= op_depend
;
558 if (p
[2] == 0) { /* -- => stop processing options */
562 for (s
= 0; textopts
[s
].label
; s
++) {
563 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
574 report_error(ERR_NONFATAL
| ERR_NOFILE
|
576 "option `--%s' requires an argument",
580 advance
= 1, param
= q
;
583 if (s
== OPT_PREFIX
) {
584 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
585 lprefix
[PREFIX_MAX
- 1] = 0;
588 if (s
== OPT_POSTFIX
) {
589 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
590 lpostfix
[POSTFIX_MAX
- 1] = 0;
597 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
598 "unrecognised option `--%s'", p
+ 2);
606 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
607 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
608 "unrecognised option `-%c'", p
[1]);
613 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
614 "more than one input file specified");
622 #define ARG_BUF_DELTA 128
624 static void process_respfile(FILE * rfile
)
626 char *buffer
, *p
, *q
, *prevarg
;
627 int bufsize
, prevargsize
;
629 bufsize
= prevargsize
= ARG_BUF_DELTA
;
630 buffer
= nasm_malloc(ARG_BUF_DELTA
);
631 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
634 while (1) { /* Loop to handle all lines in file */
637 while (1) { /* Loop to handle long lines */
638 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
642 if (p
> buffer
&& p
[-1] == '\n')
644 if (p
- buffer
> bufsize
- 10) {
647 bufsize
+= ARG_BUF_DELTA
;
648 buffer
= nasm_realloc(buffer
, bufsize
);
653 if (!q
&& p
== buffer
) {
655 process_arg(prevarg
, NULL
);
662 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
663 * them are present at the end of the line.
665 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
667 while (p
> buffer
&& isspace(p
[-1]))
674 if (process_arg(prevarg
, p
))
677 if (strlen(p
) > prevargsize
- 10) {
678 prevargsize
+= ARG_BUF_DELTA
;
679 prevarg
= nasm_realloc(prevarg
, prevargsize
);
685 /* Function to process args from a string of args, rather than the
686 * argv array. Used by the environment variable and response file
689 static void process_args(char *args
)
691 char *p
, *q
, *arg
, *prevarg
;
692 char separator
= ' ';
700 while (*p
&& *p
!= separator
)
702 while (*p
== separator
)
706 if (process_arg(prevarg
, arg
))
710 process_arg(arg
, NULL
);
713 static void parse_cmdline(int argc
, char **argv
)
716 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
718 *inname
= *outname
= *listname
= '\0';
721 * First, process the NASMENV environment variable.
723 envreal
= getenv("NASMENV");
726 envcopy
= nasm_strdup(envreal
);
727 process_args(envcopy
);
732 * Now process the actual command line.
737 if (argv
[0][0] == '@') {
738 /* We have a response file, so process this as a set of
739 * arguments like the environment variable. This allows us
740 * to have multiple arguments on a single line, which is
741 * different to the -@resp file processing below for regular
744 char *str
= malloc(2048);
745 FILE *f
= fopen(&argv
[0][1], "r");
747 printf("out of memory");
751 while (fgets(str
, 2048, f
)) {
760 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
761 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
);
763 rfile
= fopen(p
, "r");
765 process_respfile(rfile
);
768 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
769 "unable to open response file `%s'", p
);
772 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
773 argv
+= i
, argc
-= i
;
777 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
778 "no input file specified");
781 static void assemble_file(char *fname
)
783 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
785 int i
, rn_error
, validid
;
787 struct tokenval tokval
;
790 int pass_cnt
= 0; /* count actual passes */
792 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
793 report_error(ERR_FATAL
, "command line: "
794 "32-bit segment size requires a higher cpu");
796 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
797 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
798 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
802 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
803 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
804 /* pass0 seq is 0, 0, 0,..., 1, 2 */
806 def_label
= pass
> 1 ? redefine_label
: define_label
;
808 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
812 nasmlist
.init(listname
, report_error
);
815 global_offset_changed
= FALSE
; /* set by redefine_label */
816 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
819 saa_rewind(forwrefs
);
820 forwref
= saa_rstruct(forwrefs
);
822 offsets
= raa_init();
824 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
827 location
.known
= TRUE
;
828 location
.offset
= offs
= GET_CURR_OFFS
;
830 while ((line
= preproc
->getline())) {
833 /* here we parse our directives; this is not handled by the 'real'
836 i
= getkw(&directive
, &value
);
839 case 1: /* [SEGMENT n] */
840 seg
= ofmt
->section(value
, pass2
, &sb
);
842 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
843 "segment name `%s' not recognized",
847 location
.segment
= seg
;
850 case 2: /* [EXTERN label:special] */
852 value
++; /* skip initial $ if present */
855 while (*q
&& *q
!= ':')
859 ofmt
->symdef(value
, 0L, 0L, 3, q
);
861 } else if (pass
== 1) { /* pass == 1 */
866 while (*q
&& *q
!= ':') {
872 report_error(ERR_NONFATAL
,
873 "identifier expected after EXTERN");
881 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
883 pass0
= 1; /* fake pass 1 in labels.c */
884 declare_as_global(value
, special
,
886 define_label(value
, seg_alloc(), 0L, NULL
,
887 FALSE
, TRUE
, ofmt
, report_error
);
890 } /* else pass0 == 1 */
892 case 3: /* [BITS bits] */
893 globalbits
= sb
= get_bits(value
);
895 case 4: /* [GLOBAL symbol:special] */
897 value
++; /* skip initial $ if present */
898 if (pass0
== 2) { /* pass 2 */
900 while (*q
&& *q
!= ':')
904 ofmt
->symdef(value
, 0L, 0L, 3, q
);
906 } else if (pass2
== 1) { /* pass == 1 */
911 while (*q
&& *q
!= ':') {
917 report_error(ERR_NONFATAL
,
918 "identifier expected after GLOBAL");
926 declare_as_global(value
, special
, report_error
);
929 case 5: /* [COMMON symbol size:special] */
931 value
++; /* skip initial $ if present */
937 while (*p
&& !isspace(*p
)) {
943 report_error(ERR_NONFATAL
,
944 "identifier expected after COMMON");
950 while (*p
&& isspace(*p
))
953 while (*q
&& *q
!= ':')
960 size
= readnum(p
, &rn_error
);
962 report_error(ERR_NONFATAL
,
963 "invalid size specified"
964 " in COMMON declaration");
966 define_common(value
, seg_alloc(), size
,
967 special
, ofmt
, report_error
);
969 report_error(ERR_NONFATAL
,
970 "no size specified in"
971 " COMMON declaration");
972 } else if (pass0
== 2) { /* pass == 2 */
974 while (*q
&& *q
!= ':') {
981 ofmt
->symdef(value
, 0L, 0L, 3, q
);
985 case 6: /* [ABSOLUTE address] */
987 stdscan_bufptr
= value
;
988 tokval
.t_type
= TOKEN_INVALID
;
989 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
993 report_error(pass0
==
994 1 ? ERR_NONFATAL
: ERR_PANIC
,
995 "cannot use non-relocatable expression as "
998 abs_seg
= reloc_seg(e
);
999 abs_offset
= reloc_value(e
);
1001 } else if (pass
== 1)
1002 abs_offset
= 0x100; /* don't go near zero in case of / */
1004 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1007 location
.segment
= NO_SEG
;
1015 while (*p
&& !isspace(*p
)) {
1022 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1023 "identifier expected after DEBUG");
1026 while (*p
&& isspace(*p
))
1028 if (pass
== pass_max
)
1029 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1031 case 8: /* [WARNING {+|-}warn-name] */
1033 while (*value
&& isspace(*value
))
1036 if (*value
== '+' || *value
== '-') {
1037 validid
= (*value
== '-') ? TRUE
: FALSE
;
1042 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1043 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1045 if (i
<= ERR_WARN_MAX
)
1046 suppressed
[i
] = validid
;
1048 report_error(ERR_NONFATAL
,
1049 "invalid warning id in WARNING directive");
1053 cpu
= get_cpu(value
);
1055 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1056 while (*value
&& isspace(*value
))
1059 if (*value
== '+') {
1062 if (*value
== '-') {
1065 report_error(ERR_NONFATAL
,
1066 "invalid parameter to \"list\" directive");
1071 if (!ofmt
->directive(directive
, value
, pass2
))
1072 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1073 "unrecognised directive [%s]",
1076 } else { /* it isn't a directive */
1078 parse_line(pass1
, line
, &output_ins
,
1079 report_error
, evaluate
, def_label
);
1081 if (!(optimizing
> 0) && pass
== 2) {
1082 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1083 output_ins
.forw_ref
= TRUE
;
1085 output_ins
.oprs
[forwref
->operand
].opflags
|=
1087 forwref
= saa_rstruct(forwrefs
);
1088 } while (forwref
!= NULL
1089 && forwref
->lineno
== globallineno
);
1091 output_ins
.forw_ref
= FALSE
;
1094 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1096 for (i
= 0; i
< output_ins
.operands
; i
++) {
1097 if (output_ins
.oprs
[i
].
1098 opflags
& OPFLAG_FORWARD
) {
1099 struct forwrefinfo
*fwinf
=
1100 (struct forwrefinfo
*)
1101 saa_wstruct(forwrefs
);
1102 fwinf
->lineno
= globallineno
;
1106 } else { /* pass == 2 */
1108 * Hack to prevent phase error in the code
1112 * If the second operand is a forward reference,
1113 * the UNITY property of the number 1 in that
1114 * operand is cancelled. Otherwise the above
1115 * sequence will cause a phase error.
1117 * This hack means that the above code will
1118 * generate 286+ code.
1120 * The forward reference will mean that the
1121 * operand will not have the UNITY property on
1122 * the first pass, so the pass behaviours will
1126 if (output_ins
.operands
>= 2 &&
1127 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1128 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1130 /* Remove special properties bits */
1131 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1139 if (output_ins
.opcode
== I_EQU
) {
1142 * Special `..' EQUs get processed in pass two,
1143 * except `..@' macro-processor EQUs which are done
1144 * in the normal place.
1146 if (!output_ins
.label
)
1147 report_error(ERR_NONFATAL
,
1148 "EQU not preceded by label");
1150 else if (output_ins
.label
[0] != '.' ||
1151 output_ins
.label
[1] != '.' ||
1152 output_ins
.label
[2] == '@') {
1153 if (output_ins
.operands
== 1 &&
1154 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1155 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1158 opflags
& OPFLAG_EXTERN
;
1159 def_label(output_ins
.label
,
1160 output_ins
.oprs
[0].segment
,
1161 output_ins
.oprs
[0].offset
, NULL
,
1164 } else if (output_ins
.operands
== 2
1165 && (output_ins
.oprs
[0].
1167 && (output_ins
.oprs
[0].type
& COLON
)
1168 && output_ins
.oprs
[0].segment
==
1170 && output_ins
.oprs
[0].wrt
== NO_SEG
1171 && (output_ins
.oprs
[1].
1173 && output_ins
.oprs
[1].segment
==
1175 && output_ins
.oprs
[1].wrt
==
1177 def_label(output_ins
.label
,
1180 output_ins
.oprs
[1].offset
, NULL
,
1184 report_error(ERR_NONFATAL
,
1185 "bad syntax for EQU");
1187 } else { /* pass == 2 */
1189 * Special `..' EQUs get processed here, except
1190 * `..@' macro processor EQUs which are done above.
1192 if (output_ins
.label
[0] == '.' &&
1193 output_ins
.label
[1] == '.' &&
1194 output_ins
.label
[2] != '@') {
1195 if (output_ins
.operands
== 1 &&
1196 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1197 define_label(output_ins
.label
,
1198 output_ins
.oprs
[0].segment
,
1199 output_ins
.oprs
[0].offset
,
1200 NULL
, FALSE
, FALSE
, ofmt
,
1202 } else if (output_ins
.operands
== 2
1203 && (output_ins
.oprs
[0].
1205 && (output_ins
.oprs
[0].type
& COLON
)
1206 && output_ins
.oprs
[0].segment
==
1208 && (output_ins
.oprs
[1].
1210 && output_ins
.oprs
[1].segment
==
1212 define_label(output_ins
.label
,
1215 output_ins
.oprs
[1].offset
,
1216 NULL
, FALSE
, FALSE
, ofmt
,
1219 report_error(ERR_NONFATAL
,
1220 "bad syntax for EQU");
1223 } else { /* instruction isn't an EQU */
1227 int32_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1228 &output_ins
, report_error
);
1230 /* if (using_debug_info) && output_ins.opcode != -1) */
1231 if (using_debug_info
)
1232 { /* fbk 03/25/01 */
1233 /* this is done here so we can do debug type info */
1235 TYS_ELEMENTS(output_ins
.operands
);
1236 switch (output_ins
.opcode
) {
1239 TYS_ELEMENTS(output_ins
.oprs
[0].
1244 TYS_ELEMENTS(output_ins
.oprs
[0].
1249 TYS_ELEMENTS(output_ins
.oprs
[0].
1254 TYS_ELEMENTS(output_ins
.oprs
[0].
1259 TYS_ELEMENTS(output_ins
.oprs
[0].
1263 typeinfo
|= TY_BYTE
;
1266 typeinfo
|= TY_WORD
;
1269 if (output_ins
.eops_float
)
1270 typeinfo
|= TY_FLOAT
;
1272 typeinfo
|= TY_DWORD
;
1275 typeinfo
|= TY_QWORD
;
1278 typeinfo
|= TY_TBYTE
;
1281 typeinfo
= TY_LABEL
;
1285 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1290 SET_CURR_OFFS(offs
);
1293 * else l == -1 => invalid instruction, which will be
1294 * flagged as an error on pass 2
1297 } else { /* pass == 2 */
1298 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1299 &output_ins
, ofmt
, report_error
,
1301 SET_CURR_OFFS(offs
);
1305 cleanup_insn(&output_ins
);
1308 location
.offset
= offs
= GET_CURR_OFFS
;
1309 } /* end while (line = preproc->getline... */
1311 if (pass1
== 2 && global_offset_changed
)
1312 report_error(ERR_NONFATAL
,
1313 "phase error detected at end of assembly.");
1316 preproc
->cleanup(1);
1318 if (pass1
== 1 && terminate_after_phase
) {
1326 if (pass
> 1 && !global_offset_changed
) {
1329 pass
= pass_max
- 1;
1330 } else if (!(optimizing
> 0))
1333 } /* for (pass=1; pass<=2; pass++) */
1335 preproc
->cleanup(0);
1338 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1340 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1342 } /* exit from assemble_file (...) */
1344 static int getkw(char **directive
, char **value
)
1350 /* allow leading spaces or tabs */
1351 while (*buf
== ' ' || *buf
== '\t')
1359 while (*p
&& *p
!= ']')
1367 while (*p
&& *p
!= ';') {
1374 *directive
= p
= buf
+ 1;
1375 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1382 while (isspace(*buf
))
1383 buf
++; /* beppu - skip leading whitespace */
1390 for (q
= p
; *q
; q
++)
1393 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1395 if (!nasm_stricmp(p
, "extern"))
1397 if (!nasm_stricmp(p
, "bits"))
1399 if (!nasm_stricmp(p
, "global"))
1401 if (!nasm_stricmp(p
, "common"))
1403 if (!nasm_stricmp(p
, "absolute"))
1405 if (!nasm_stricmp(p
, "debug"))
1407 if (!nasm_stricmp(p
, "warning"))
1409 if (!nasm_stricmp(p
, "cpu"))
1411 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1417 * gnu style error reporting
1418 * This function prints an error message to error_file in the
1419 * style used by GNU. An example would be:
1420 * file.asm:50: error: blah blah blah
1421 * where file.asm is the name of the file, 50 is the line number on
1422 * which the error occurs (or is detected) and "error:" is one of
1423 * the possible optional diagnostics -- it can be "error" or "warning"
1424 * or something else. Finally the line terminates with the actual
1427 * @param severity the severity of the warning or error
1428 * @param fmt the printf style format string
1430 static void report_error_gnu(int severity
, const char *fmt
, ...)
1434 if (is_suppressed_warning(severity
))
1437 if (severity
& ERR_NOFILE
)
1438 fputs("nasm: ", error_file
);
1440 char *currentfile
= NULL
;
1442 src_get(&lineno
, ¤tfile
);
1443 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1444 nasm_free(currentfile
);
1447 report_error_common(severity
, fmt
, ap
);
1452 * MS style error reporting
1453 * This function prints an error message to error_file in the
1454 * style used by Visual C and some other Microsoft tools. An example
1456 * file.asm(50) : error: blah blah blah
1457 * where file.asm is the name of the file, 50 is the line number on
1458 * which the error occurs (or is detected) and "error:" is one of
1459 * the possible optional diagnostics -- it can be "error" or "warning"
1460 * or something else. Finally the line terminates with the actual
1463 * @param severity the severity of the warning or error
1464 * @param fmt the printf style format string
1466 static void report_error_vc(int severity
, const char *fmt
, ...)
1470 if (is_suppressed_warning(severity
))
1473 if (severity
& ERR_NOFILE
)
1474 fputs("nasm: ", error_file
);
1476 char *currentfile
= NULL
;
1478 src_get(&lineno
, ¤tfile
);
1479 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1480 nasm_free(currentfile
);
1483 report_error_common(severity
, fmt
, ap
);
1488 * check for supressed warning
1489 * checks for suppressed warning or pass one only warning and we're
1492 * @param severity the severity of the warning or error
1493 * @return true if we should abort error/warning printing
1495 static int is_suppressed_warning(int severity
)
1498 * See if it's a suppressed warning.
1500 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1501 (severity
& ERR_WARN_MASK
) != 0 &&
1502 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1504 * See if it's a pass-one only warning and we're not in pass one.
1506 ((severity
& ERR_PASS1
) && pass0
== 2);
1510 * common error reporting
1511 * This is the common back end of the error reporting schemes currently
1512 * implemented. It prints the nature of the warning and then the
1513 * specific error message to error_file and may or may not return. It
1514 * doesn't return if the error severity is a "panic" or "debug" type.
1516 * @param severity the severity of the warning or error
1517 * @param fmt the printf style format string
1519 static void report_error_common(int severity
, const char *fmt
,
1522 switch (severity
& ERR_MASK
) {
1524 fputs("warning: ", error_file
);
1527 fputs("error: ", error_file
);
1530 fputs("fatal: ", error_file
);
1533 fputs("panic: ", error_file
);
1536 fputs("debug: ", error_file
);
1540 vfprintf(error_file
, fmt
, args
);
1541 fputc('\n', error_file
);
1543 if (severity
& ERR_USAGE
)
1546 switch (severity
& ERR_MASK
) {
1549 /* no further action, by definition */
1552 /* hack enables listing(!) on errors */
1553 terminate_after_phase
= TRUE
;
1562 exit(1); /* instantly die */
1563 break; /* placate silly compilers */
1566 /* abort(); *//* halt, catch fire, and dump core */
1572 static void usage(void)
1574 fputs("type `nasm -h' for help\n", error_file
);
1577 static void register_output_formats(void)
1579 ofmt
= ofmt_register(report_error
);
1582 #define BUF_DELTA 512
1584 static FILE *no_pp_fp
;
1585 static efunc no_pp_err
;
1586 static ListGen
*no_pp_list
;
1587 static int32_t no_pp_lineinc
;
1589 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1592 src_set_fname(nasm_strdup(file
));
1596 no_pp_fp
= fopen(file
, "r");
1598 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1599 "unable to open input file `%s'", file
);
1600 no_pp_list
= listgen
;
1601 (void)pass
; /* placate compilers */
1602 (void)eval
; /* placate compilers */
1605 static char *no_pp_getline(void)
1607 char *buffer
, *p
, *q
;
1610 bufsize
= BUF_DELTA
;
1611 buffer
= nasm_malloc(BUF_DELTA
);
1612 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1614 while (1) { /* Loop to handle %line */
1617 while (1) { /* Loop to handle long lines */
1618 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1622 if (p
> buffer
&& p
[-1] == '\n')
1624 if (p
- buffer
> bufsize
- 10) {
1626 offset
= p
- buffer
;
1627 bufsize
+= BUF_DELTA
;
1628 buffer
= nasm_realloc(buffer
, bufsize
);
1629 p
= buffer
+ offset
;
1633 if (!q
&& p
== buffer
) {
1639 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1640 * them are present at the end of the line.
1642 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1644 if (!strncmp(buffer
, "%line", 5)) {
1647 char *nm
= nasm_malloc(strlen(buffer
));
1648 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1649 nasm_free(src_set_fname(nm
));
1659 no_pp_list
->line(LIST_READ
, buffer
);
1664 static void no_pp_cleanup(int pass
)
1666 (void)pass
; /* placate GCC */
1670 static uint32_t get_cpu(char *value
)
1673 if (!strcmp(value
, "8086"))
1675 if (!strcmp(value
, "186"))
1677 if (!strcmp(value
, "286"))
1679 if (!strcmp(value
, "386"))
1681 if (!strcmp(value
, "486"))
1683 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1685 if (!strcmp(value
, "686") ||
1686 !nasm_stricmp(value
, "ppro") ||
1687 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1689 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1691 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1692 !nasm_stricmp(value
, "willamette"))
1693 return IF_WILLAMETTE
;
1694 if (!nasm_stricmp(value
, "prescott"))
1696 if (!nasm_stricmp(value
, "x64") ||
1697 !nasm_stricmp(value
, "x86-64"))
1699 if (!nasm_stricmp(value
, "ia64") ||
1700 !nasm_stricmp(value
, "ia-64") ||
1701 !nasm_stricmp(value
, "itanium") ||
1702 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1705 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1706 "unknown 'cpu' type");
1708 return IF_PLEVEL
; /* the maximum level */
1711 static int get_bits(char *value
)
1715 if ((i
= atoi(value
)) == 16)
1716 return i
; /* set for a 16-bit segment */
1719 report_error(ERR_NONFATAL
,
1720 "cannot specify 32-bit segment on processor below a 386");
1723 } else if (i
== 64) {
1724 if (cpu
< IF_X86_64
) {
1725 report_error(ERR_NONFATAL
,
1726 "cannot specify 64-bit segment on processor below an x86-64");
1730 report_error(ERR_NONFATAL
,
1731 "%s output format does not support 64-bit code",
1736 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1737 "`%s' is not a valid segment size; must be 16, 32 or 64",