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.
32 struct forwrefinfo
{ /* info held on forward refs. */
37 static int get_bits(char *value
);
38 static uint32_t get_cpu(char *cpu_str
);
39 static void parse_cmdline(int, char **);
40 static void assemble_file(char *);
41 static void register_output_formats(void);
42 static void report_error_gnu(int severity
, const char *fmt
, ...);
43 static void report_error_vc(int severity
, const char *fmt
, ...);
44 static void report_error_common(int severity
, const char *fmt
,
46 static int is_suppressed_warning(int severity
);
47 static void usage(void);
48 static efunc report_error
;
50 static int using_debug_info
, opt_verbose_info
;
51 bool tasm_compatible_mode
= false;
56 static char inname
[FILENAME_MAX
];
57 static char outname
[FILENAME_MAX
];
58 static char listname
[FILENAME_MAX
];
59 static char errname
[FILENAME_MAX
];
60 static int globallineno
; /* for forward-reference tracking */
61 /* static int pass = 0; */
62 static struct ofmt
*ofmt
= NULL
;
64 static FILE *error_file
; /* Where to write error messages */
66 static FILE *ofile
= NULL
;
67 int optimizing
= -1; /* number of optimization passes to take */
68 static int sb
, cmd_sb
= 16; /* by default */
69 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
70 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
71 bool global_offset_changed
; /* referenced in labels.c */
73 static struct location location
;
74 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
75 int32_t abs_seg
; /* ABSOLUTE segment basis */
76 int32_t abs_offset
; /* ABSOLUTE offset */
78 static struct RAA
*offsets
;
80 static struct SAA
*forwrefs
; /* keep track of forward references */
81 static const struct forwrefinfo
*forwref
;
83 static Preproc
*preproc
;
85 op_normal
, /* Preprocess and assemble */
86 op_preprocess
, /* Preprocess only */
87 op_depend
, /* Generate dependencies */
88 op_depend_missing_ok
, /* Generate dependencies, missing OK */
90 static enum op_type operating_mode
;
93 * Which of the suppressible warnings are suppressed. Entry zero
94 * isn't an actual warning, but it used for -w+error/-Werror.
96 static bool suppressed
[ERR_WARN_MAX
+1] = {
97 true, false, true, false, false, true, false, true, true, false
101 * The option names for the suppressible warnings. As before, entry
104 static const char *suppressed_names
[ERR_WARN_MAX
+1] = {
105 "error", "macro-params", "macro-selfref", "orphan-labels",
106 "number-overflow", "gnu-elf-extensions", "float-overflow",
107 "float-denorm", "float-underflow", "float-toolong"
111 * The explanations for the suppressible warnings. As before, entry
114 static const char *suppressed_what
[ERR_WARN_MAX
+1] = {
115 "treat warnings as errors",
116 "macro calls with wrong parameter count",
117 "cyclic macro references",
118 "labels alone on lines without trailing `:'",
119 "numeric constants does not fit in 64 bits",
120 "using 8- or 16-bit relocation in ELF32, a GNU extension",
121 "floating point overflow",
122 "floating point denormal",
123 "floating point underflow",
124 "too many digits in floating-point number"
128 * This is a null preprocessor which just copies lines from input
129 * to output. It's used when someone explicitly requests that NASM
130 * not preprocess their source file.
133 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*);
134 static char *no_pp_getline(void);
135 static void no_pp_cleanup(int);
136 static Preproc no_pp
= {
143 * get/set current offset...
145 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
146 raa_read(offsets,location.segment))
147 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
148 (void)(offsets=raa_write(offsets,location.segment,(x))))
150 static int want_usage
;
151 static int terminate_after_phase
;
152 int user_nolist
= 0; /* fbk 9/2/00 */
154 static void nasm_fputs(const char *line
, FILE * outfile
)
157 fputs(line
, outfile
);
163 int main(int argc
, char **argv
)
166 want_usage
= terminate_after_phase
= false;
167 report_error
= report_error_gnu
;
171 nasm_set_malloc_error(report_error
);
172 offsets
= raa_init();
173 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
176 operating_mode
= op_normal
;
180 register_output_formats();
182 parse_cmdline(argc
, argv
);
184 if (terminate_after_phase
) {
190 /* If debugging info is disabled, suppress any debug calls */
191 if (!using_debug_info
)
192 ofmt
->current_dfmt
= &null_debug_form
;
195 pp_extra_stdmac(ofmt
->stdmac
);
196 parser_global_info(ofmt
, &location
);
197 eval_global_info(ofmt
, lookup_label
, &location
);
199 /* define some macros dependent of command-line */
202 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
207 switch (operating_mode
) {
208 case op_depend_missing_ok
:
209 pp_include_path(NULL
); /* "assume generated" */
214 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
215 if (outname
[0] == '\0')
216 ofmt
->filename(inname
, outname
, report_error
);
218 fprintf(stdout
, "%s: %s", outname
, inname
);
219 while ((line
= preproc
->getline()))
229 char *file_name
= NULL
;
230 int32_t prior_linnum
= 0;
234 ofile
= fopen(outname
, "w");
236 report_error(ERR_FATAL
| ERR_NOFILE
,
237 "unable to open output file `%s'",
242 location
.known
= false;
245 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
246 while ((line
= preproc
->getline())) {
248 * We generate %line directives if needed for later programs
250 int32_t linnum
= prior_linnum
+= lineinc
;
251 int altline
= src_get(&linnum
, &file_name
);
253 if (altline
== 1 && lineinc
== 1)
254 nasm_fputs("", ofile
);
256 lineinc
= (altline
!= -1 || lineinc
!= 1);
257 fprintf(ofile
? ofile
: stdout
,
258 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
261 prior_linnum
= linnum
;
263 nasm_fputs(line
, ofile
);
266 nasm_free(file_name
);
270 if (ofile
&& terminate_after_phase
)
278 * We must call ofmt->filename _anyway_, even if the user
279 * has specified their own output file, because some
280 * formats (eg OBJ and COFF) use ofmt->filename to find out
281 * the name of the input file and then put that inside the
284 ofmt
->filename(inname
, outname
, report_error
);
286 ofile
= fopen(outname
, "wb");
288 report_error(ERR_FATAL
| ERR_NOFILE
,
289 "unable to open output file `%s'", outname
);
293 * We must call init_labels() before ofmt->init() since
294 * some object formats will want to define labels in their
295 * init routines. (eg OS/2 defines the FLAT group)
299 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
301 assemble_file(inname
);
303 if (!terminate_after_phase
) {
304 ofmt
->cleanup(using_debug_info
);
308 * We had an fclose on the output file here, but we
309 * actually do that in all the object file drivers as well,
310 * so we're leaving out the one here.
329 if (terminate_after_phase
)
336 * Get a parameter for a command line option.
337 * First arg must be in the form of e.g. -f...
339 static char *get_param(char *p
, char *q
, int *advance
)
342 if (p
[2]) { /* the parameter's in the option */
352 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
353 "option `-%c' requires an argument", p
[1]);
363 #define OPT_POSTFIX 1
364 struct textargs textopts
[] = {
365 {"prefix", OPT_PREFIX
},
366 {"postfix", OPT_POSTFIX
},
371 static int process_arg(char *p
, char *q
)
380 if (p
[0] == '-' && !stopoptions
) {
385 case 'o': /* these parameters take values */
400 if (!(param
= get_param(p
, q
, &advance
)))
402 if (p
[1] == 'o') { /* output file */
403 strcpy(outname
, param
);
404 } else if (p
[1] == 'f') { /* output format */
405 ofmt
= ofmt_find(param
);
407 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
408 "unrecognised output format `%s' - "
409 "use -hf for a list", param
);
411 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
412 } else if (p
[1] == 'O') { /* Optimization level */
416 /* Naked -O == -Ox */
417 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
421 case '0': case '1': case '2': case '3': case '4':
422 case '5': case '6': case '7': case '8': case '9':
423 opt
= strtoul(param
, ¶m
, 10);
425 /* -O0 -> optimizing == -1, 0.98 behaviour */
426 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
428 optimizing
= opt
- 1;
430 /* The optimizer seems to have problems with
431 < 5 passes? Hidden bug? */
432 optimizing
= 5; /* 5 passes */
434 optimizing
= opt
; /* More than 5 passes */
440 opt_verbose_info
= true;
445 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
449 report_error(ERR_FATAL
,
450 "unknown optimization option -O%c\n",
456 } else if (p
[1] == 'P' || p
[1] == 'p') { /* pre-include */
457 pp_pre_include(param
);
458 } else if (p
[1] == 'D' || p
[1] == 'd') { /* pre-define */
459 pp_pre_define(param
);
460 } else if (p
[1] == 'U' || p
[1] == 'u') { /* un-define */
461 pp_pre_undefine(param
);
462 } else if (p
[1] == 'I' || p
[1] == 'i') { /* include search path */
463 pp_include_path(param
);
464 } else if (p
[1] == 'l') { /* listing file */
465 strcpy(listname
, param
);
466 } else if (p
[1] == 'Z') { /* error messages file */
467 strcpy(errname
, param
);
468 } else if (p
[1] == 'F') { /* specify debug format */
469 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
470 if (!ofmt
->current_dfmt
) {
471 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
472 "unrecognized debug format `%s' for"
473 " output format `%s'",
474 param
, ofmt
->shortname
);
476 } else if (p
[1] == 'X') { /* specify error reporting format */
477 if (nasm_stricmp("vc", param
) == 0)
478 report_error
= report_error_vc
;
479 else if (nasm_stricmp("gnu", param
) == 0)
480 report_error
= report_error_gnu
;
482 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
483 "unrecognized error reporting format `%s'",
488 using_debug_info
= true;
492 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
494 " [options...] [--] filename\n"
495 " or nasm -v for version info\n\n"
496 " -t assemble in SciTech TASM compatible mode\n"
497 " -g generate debug information in selected format.\n");
499 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
500 " -a don't preprocess (assemble only)\n"
501 " -M generate Makefile dependencies on stdout\n"
502 " -MG d:o, missing files assumed generated\n\n"
503 " -Z<file> redirect error messages to file\n"
504 " -s redirect error messages to stdout\n\n"
505 " -F format select a debugging format\n\n"
506 " -I<path> adds a pathname to the include file path\n");
508 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
509 " -P<file> pre-includes a file\n"
510 " -D<macro>[=<value>] pre-defines a macro\n"
511 " -U<macro> undefines a macro\n"
512 " -X<format> specifies error reporting format (gnu or vc)\n"
513 " -w+foo enables warning foo (equiv. -Wfoo)\n"
514 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
516 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
517 printf(" %-23s %s (default %s)\n",
518 suppressed_names
[i
], suppressed_what
[i
],
519 suppressed
[i
] ? "off" : "on");
521 ("\nresponse files should contain command line parameters"
522 ", one per line.\n");
524 printf("\nvalid output formats for -f are"
525 " (`*' denotes default):\n");
526 ofmt_list(ofmt
, stdout
);
528 printf("\nFor a list of valid output formats, use -hf.\n");
529 printf("For a list of debug formats, use -f <form> -y.\n");
531 exit(0); /* never need usage message here */
534 printf("\nvalid debug formats for '%s' output format are"
535 " ('*' denotes default):\n", ofmt
->shortname
);
536 dfmt_list(ofmt
, stdout
);
540 tasm_compatible_mode
= true;
544 const char *nasm_version_string
=
545 "NASM version " NASM_VER
" compiled on " __DATE__
550 puts(nasm_version_string
);
551 exit(0); /* never need usage message here */
554 case 'e': /* preprocess only */
556 operating_mode
= op_preprocess
;
558 case 'a': /* assemble only - don't preprocess */
562 if (p
[2] == 'n' && p
[3] == 'o' && p
[4] == '-') {
571 if (p
[2] != '+' && p
[2] != '-') {
572 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
573 "invalid option to `-w'");
576 suppress
= (p
[2] == '-');
580 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
581 if (!nasm_stricmp(p
, suppressed_names
[i
]))
583 if (i
<= ERR_WARN_MAX
)
584 suppressed
[i
] = suppress
;
585 else if (!nasm_stricmp(p
, "all"))
586 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
587 suppressed
[i
] = suppress
;
589 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
590 "invalid warning `%s'", p
);
593 operating_mode
= p
[2] == 'G' ? op_depend_missing_ok
: op_depend
;
600 if (p
[2] == 0) { /* -- => stop processing options */
604 for (s
= 0; textopts
[s
].label
; s
++) {
605 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
616 report_error(ERR_NONFATAL
| ERR_NOFILE
|
618 "option `--%s' requires an argument",
622 advance
= 1, param
= q
;
625 if (s
== OPT_PREFIX
) {
626 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
627 lprefix
[PREFIX_MAX
- 1] = 0;
630 if (s
== OPT_POSTFIX
) {
631 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
632 lpostfix
[POSTFIX_MAX
- 1] = 0;
639 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
640 "unrecognised option `--%s'", p
+ 2);
648 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
649 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
650 "unrecognised option `-%c'", p
[1]);
655 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
656 "more than one input file specified");
664 #define ARG_BUF_DELTA 128
666 static void process_respfile(FILE * rfile
)
668 char *buffer
, *p
, *q
, *prevarg
;
669 int bufsize
, prevargsize
;
671 bufsize
= prevargsize
= ARG_BUF_DELTA
;
672 buffer
= nasm_malloc(ARG_BUF_DELTA
);
673 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
676 while (1) { /* Loop to handle all lines in file */
679 while (1) { /* Loop to handle long lines */
680 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
684 if (p
> buffer
&& p
[-1] == '\n')
686 if (p
- buffer
> bufsize
- 10) {
689 bufsize
+= ARG_BUF_DELTA
;
690 buffer
= nasm_realloc(buffer
, bufsize
);
695 if (!q
&& p
== buffer
) {
697 process_arg(prevarg
, NULL
);
704 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
705 * them are present at the end of the line.
707 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
709 while (p
> buffer
&& isspace(p
[-1]))
716 if (process_arg(prevarg
, p
))
719 if ((int) strlen(p
) > prevargsize
- 10) {
720 prevargsize
+= ARG_BUF_DELTA
;
721 prevarg
= nasm_realloc(prevarg
, prevargsize
);
727 /* Function to process args from a string of args, rather than the
728 * argv array. Used by the environment variable and response file
731 static void process_args(char *args
)
733 char *p
, *q
, *arg
, *prevarg
;
734 char separator
= ' ';
742 while (*p
&& *p
!= separator
)
744 while (*p
== separator
)
748 if (process_arg(prevarg
, arg
))
752 process_arg(arg
, NULL
);
755 static void parse_cmdline(int argc
, char **argv
)
758 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
760 *inname
= *outname
= *listname
= *errname
= '\0';
763 * First, process the NASMENV environment variable.
765 envreal
= getenv("NASMENV");
768 envcopy
= nasm_strdup(envreal
);
769 process_args(envcopy
);
774 * Now process the actual command line.
779 if (argv
[0][0] == '@') {
780 /* We have a response file, so process this as a set of
781 * arguments like the environment variable. This allows us
782 * to have multiple arguments on a single line, which is
783 * different to the -@resp file processing below for regular
786 char *str
= malloc(2048);
787 FILE *f
= fopen(&argv
[0][1], "r");
789 printf("out of memory");
793 while (fgets(str
, 2048, f
)) {
802 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
803 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
);
805 rfile
= fopen(p
, "r");
807 process_respfile(rfile
);
810 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
811 "unable to open response file `%s'", p
);
814 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
815 argv
+= i
, argc
-= i
;
819 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
820 "no input file specified");
822 /* Look for basic command line typos. This definitely doesn't
823 catch all errors, but it might help cases of fumbled fingers. */
824 if (!strcmp(inname
, errname
) || !strcmp(inname
, outname
) ||
825 !strcmp(inname
, listname
))
826 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
827 "file `%s' is both input and output file",
831 error_file
= fopen(errname
, "w");
833 error_file
= stderr
; /* Revert to default! */
834 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
835 "cannot open file `%s' for error messages",
841 /* List of directives */
843 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
844 D_EXTERN
, D_FLOAT
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
846 static const char *directives
[] = {
847 "", "absolute", "bits", "common", "cpu", "debug", "default",
848 "extern", "float", "global", "list", "section", "segment", "warning"
850 static enum directives
getkw(char **directive
, char **value
);
852 static void assemble_file(char *fname
)
854 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
860 struct tokenval tokval
;
863 int pass_cnt
= 0; /* count actual passes */
865 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
866 report_error(ERR_FATAL
, "command line: "
867 "32-bit segment size requires a higher cpu");
869 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
870 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
871 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
875 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
876 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
877 /* pass0 seq is 0, 0, 0,..., 1, 2 */
879 def_label
= pass
> 1 ? redefine_label
: define_label
;
881 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
885 nasmlist
.init(listname
, report_error
);
888 global_offset_changed
= false; /* set by redefine_label */
889 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
892 saa_rewind(forwrefs
);
893 forwref
= saa_rstruct(forwrefs
);
895 offsets
= raa_init();
897 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
900 location
.known
= true;
901 location
.offset
= offs
= GET_CURR_OFFS
;
903 while ((line
= preproc
->getline())) {
907 /* here we parse our directives; this is not handled by the 'real'
910 d
= getkw(&directive
, &value
);
915 case D_SEGMENT
: /* [SEGMENT n] */
917 seg
= ofmt
->section(value
, pass2
, &sb
);
919 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
920 "segment name `%s' not recognized",
924 location
.segment
= seg
;
927 case D_EXTERN
: /* [EXTERN label:special] */
929 value
++; /* skip initial $ if present */
932 while (*q
&& *q
!= ':')
936 ofmt
->symdef(value
, 0L, 0L, 3, q
);
938 } else if (pass
== 1) { /* pass == 1 */
943 while (*q
&& *q
!= ':') {
949 report_error(ERR_NONFATAL
,
950 "identifier expected after EXTERN");
958 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
960 pass0
= 1; /* fake pass 1 in labels.c */
961 declare_as_global(value
, special
,
963 define_label(value
, seg_alloc(), 0L, NULL
,
964 false, true, ofmt
, report_error
);
967 } /* else pass0 == 1 */
969 case D_BITS
: /* [BITS bits] */
970 globalbits
= sb
= get_bits(value
);
972 case D_GLOBAL
: /* [GLOBAL symbol:special] */
974 value
++; /* skip initial $ if present */
975 if (pass0
== 2) { /* pass 2 */
977 while (*q
&& *q
!= ':')
981 ofmt
->symdef(value
, 0L, 0L, 3, q
);
983 } else if (pass2
== 1) { /* pass == 1 */
988 while (*q
&& *q
!= ':') {
994 report_error(ERR_NONFATAL
,
995 "identifier expected after GLOBAL");
1003 declare_as_global(value
, special
, report_error
);
1006 case D_COMMON
: /* [COMMON symbol size:special] */
1008 value
++; /* skip initial $ if present */
1014 while (*p
&& !isspace(*p
)) {
1020 report_error(ERR_NONFATAL
,
1021 "identifier expected after COMMON");
1027 while (*p
&& isspace(*p
))
1030 while (*q
&& *q
!= ':')
1037 size
= readnum(p
, &rn_error
);
1039 report_error(ERR_NONFATAL
,
1040 "invalid size specified"
1041 " in COMMON declaration");
1043 define_common(value
, seg_alloc(), size
,
1044 special
, ofmt
, report_error
);
1046 report_error(ERR_NONFATAL
,
1047 "no size specified in"
1048 " COMMON declaration");
1049 } else if (pass0
== 2) { /* pass == 2 */
1051 while (*q
&& *q
!= ':') {
1058 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1062 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1064 stdscan_bufptr
= value
;
1065 tokval
.t_type
= TOKEN_INVALID
;
1066 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1067 report_error
, NULL
);
1070 report_error(pass0
==
1071 1 ? ERR_NONFATAL
: ERR_PANIC
,
1072 "cannot use non-relocatable expression as "
1073 "ABSOLUTE address");
1075 abs_seg
= reloc_seg(e
);
1076 abs_offset
= reloc_value(e
);
1078 } else if (pass
== 1)
1079 abs_offset
= 0x100; /* don't go near zero in case of / */
1081 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1084 location
.segment
= NO_SEG
;
1086 case D_DEBUG
: /* [DEBUG] */
1092 while (*p
&& !isspace(*p
)) {
1099 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1100 "identifier expected after DEBUG");
1103 while (*p
&& isspace(*p
))
1105 if (pass
== pass_max
)
1106 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1108 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1110 while (*value
&& isspace(*value
))
1113 if (*value
== '+' || *value
== '-') {
1114 validid
= (*value
== '-') ? true : false;
1119 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1120 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1122 if (i
<= ERR_WARN_MAX
)
1123 suppressed
[i
] = validid
;
1125 report_error(ERR_NONFATAL
,
1126 "invalid warning id in WARNING directive");
1129 case D_CPU
: /* [CPU] */
1130 cpu
= get_cpu(value
);
1132 case D_LIST
: /* [LIST {+|-}] */
1133 while (*value
&& isspace(*value
))
1136 if (*value
== '+') {
1139 if (*value
== '-') {
1146 case D_DEFAULT
: /* [DEFAULT] */
1148 stdscan_bufptr
= value
;
1149 tokval
.t_type
= TOKEN_INVALID
;
1150 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1151 switch ((int)tokval
.t_integer
) {
1167 if (float_option(value
)) {
1168 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1169 "unknown 'float' directive: %s",
1174 if (!ofmt
->directive(directive
, value
, pass2
))
1175 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1176 "unrecognised directive [%s]",
1180 report_error(ERR_NONFATAL
,
1181 "invalid parameter to [%s] directive",
1184 } else { /* it isn't a directive */
1186 parse_line(pass1
, line
, &output_ins
,
1187 report_error
, evaluate
, def_label
);
1189 if (!(optimizing
> 0) && pass
== 2) {
1190 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1191 output_ins
.forw_ref
= true;
1193 output_ins
.oprs
[forwref
->operand
].opflags
|=
1195 forwref
= saa_rstruct(forwrefs
);
1196 } while (forwref
!= NULL
1197 && forwref
->lineno
== globallineno
);
1199 output_ins
.forw_ref
= false;
1202 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1204 for (i
= 0; i
< output_ins
.operands
; i
++) {
1205 if (output_ins
.oprs
[i
].
1206 opflags
& OPFLAG_FORWARD
) {
1207 struct forwrefinfo
*fwinf
=
1208 (struct forwrefinfo
*)
1209 saa_wstruct(forwrefs
);
1210 fwinf
->lineno
= globallineno
;
1214 } else { /* pass == 2 */
1216 * Hack to prevent phase error in the code
1220 * If the second operand is a forward reference,
1221 * the UNITY property of the number 1 in that
1222 * operand is cancelled. Otherwise the above
1223 * sequence will cause a phase error.
1225 * This hack means that the above code will
1226 * generate 286+ code.
1228 * The forward reference will mean that the
1229 * operand will not have the UNITY property on
1230 * the first pass, so the pass behaviours will
1234 if (output_ins
.operands
>= 2 &&
1235 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1236 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1238 /* Remove special properties bits */
1239 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1247 if (output_ins
.opcode
== I_EQU
) {
1250 * Special `..' EQUs get processed in pass two,
1251 * except `..@' macro-processor EQUs which are done
1252 * in the normal place.
1254 if (!output_ins
.label
)
1255 report_error(ERR_NONFATAL
,
1256 "EQU not preceded by label");
1258 else if (output_ins
.label
[0] != '.' ||
1259 output_ins
.label
[1] != '.' ||
1260 output_ins
.label
[2] == '@') {
1261 if (output_ins
.operands
== 1 &&
1262 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1263 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1266 opflags
& OPFLAG_EXTERN
;
1267 def_label(output_ins
.label
,
1268 output_ins
.oprs
[0].segment
,
1269 output_ins
.oprs
[0].offset
, NULL
,
1272 } else if (output_ins
.operands
== 2
1273 && (output_ins
.oprs
[0].
1275 && (output_ins
.oprs
[0].type
& COLON
)
1276 && output_ins
.oprs
[0].segment
==
1278 && output_ins
.oprs
[0].wrt
== NO_SEG
1279 && (output_ins
.oprs
[1].
1281 && output_ins
.oprs
[1].segment
==
1283 && output_ins
.oprs
[1].wrt
==
1285 def_label(output_ins
.label
,
1288 output_ins
.oprs
[1].offset
, NULL
,
1292 report_error(ERR_NONFATAL
,
1293 "bad syntax for EQU");
1295 } else { /* pass == 2 */
1297 * Special `..' EQUs get processed here, except
1298 * `..@' macro processor EQUs which are done above.
1300 if (output_ins
.label
[0] == '.' &&
1301 output_ins
.label
[1] == '.' &&
1302 output_ins
.label
[2] != '@') {
1303 if (output_ins
.operands
== 1 &&
1304 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1305 define_label(output_ins
.label
,
1306 output_ins
.oprs
[0].segment
,
1307 output_ins
.oprs
[0].offset
,
1308 NULL
, false, false, ofmt
,
1310 } else if (output_ins
.operands
== 2
1311 && (output_ins
.oprs
[0].
1313 && (output_ins
.oprs
[0].type
& COLON
)
1314 && output_ins
.oprs
[0].segment
==
1316 && (output_ins
.oprs
[1].
1318 && output_ins
.oprs
[1].segment
==
1320 define_label(output_ins
.label
,
1323 output_ins
.oprs
[1].offset
,
1324 NULL
, false, false, ofmt
,
1327 report_error(ERR_NONFATAL
,
1328 "bad syntax for EQU");
1331 } else { /* instruction isn't an EQU */
1335 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1336 &output_ins
, report_error
);
1338 /* if (using_debug_info) && output_ins.opcode != -1) */
1339 if (using_debug_info
)
1340 { /* fbk 03/25/01 */
1341 /* this is done here so we can do debug type info */
1343 TYS_ELEMENTS(output_ins
.operands
);
1344 switch (output_ins
.opcode
) {
1347 TYS_ELEMENTS(output_ins
.oprs
[0].
1352 TYS_ELEMENTS(output_ins
.oprs
[0].
1357 TYS_ELEMENTS(output_ins
.oprs
[0].
1362 TYS_ELEMENTS(output_ins
.oprs
[0].
1367 TYS_ELEMENTS(output_ins
.oprs
[0].
1371 typeinfo
|= TY_BYTE
;
1374 typeinfo
|= TY_WORD
;
1377 if (output_ins
.eops_float
)
1378 typeinfo
|= TY_FLOAT
;
1380 typeinfo
|= TY_DWORD
;
1383 typeinfo
|= TY_QWORD
;
1386 typeinfo
|= TY_TBYTE
;
1389 typeinfo
|= TY_OWORD
;
1392 typeinfo
= TY_LABEL
;
1396 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1401 SET_CURR_OFFS(offs
);
1404 * else l == -1 => invalid instruction, which will be
1405 * flagged as an error on pass 2
1408 } else { /* pass == 2 */
1409 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1410 &output_ins
, ofmt
, report_error
,
1412 SET_CURR_OFFS(offs
);
1416 cleanup_insn(&output_ins
);
1419 location
.offset
= offs
= GET_CURR_OFFS
;
1420 } /* end while (line = preproc->getline... */
1422 if (pass1
== 2 && global_offset_changed
)
1423 report_error(ERR_NONFATAL
,
1424 "phase error detected at end of assembly.");
1427 preproc
->cleanup(1);
1429 if (pass1
== 1 && terminate_after_phase
) {
1437 if (pass
> 1 && !global_offset_changed
) {
1440 pass
= pass_max
- 1;
1441 } else if (!(optimizing
> 0))
1444 } /* for (pass=1; pass<=2; pass++) */
1446 preproc
->cleanup(0);
1449 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1451 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1453 } /* exit from assemble_file (...) */
1455 static enum directives
getkw(char **directive
, char **value
)
1461 /* allow leading spaces or tabs */
1462 while (*buf
== ' ' || *buf
== '\t')
1470 while (*p
&& *p
!= ']')
1478 while (*p
&& *p
!= ';') {
1485 *directive
= p
= buf
+ 1;
1486 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1493 while (isspace(*buf
))
1494 buf
++; /* beppu - skip leading whitespace */
1501 return bsii(*directive
, directives
, elements(directives
));
1505 * gnu style error reporting
1506 * This function prints an error message to error_file in the
1507 * style used by GNU. An example would be:
1508 * file.asm:50: error: blah blah blah
1509 * where file.asm is the name of the file, 50 is the line number on
1510 * which the error occurs (or is detected) and "error:" is one of
1511 * the possible optional diagnostics -- it can be "error" or "warning"
1512 * or something else. Finally the line terminates with the actual
1515 * @param severity the severity of the warning or error
1516 * @param fmt the printf style format string
1518 static void report_error_gnu(int severity
, const char *fmt
, ...)
1522 if (is_suppressed_warning(severity
))
1525 if (severity
& ERR_NOFILE
)
1526 fputs("nasm: ", error_file
);
1528 char *currentfile
= NULL
;
1530 src_get(&lineno
, ¤tfile
);
1531 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1532 nasm_free(currentfile
);
1535 report_error_common(severity
, fmt
, ap
);
1540 * MS style error reporting
1541 * This function prints an error message to error_file in the
1542 * style used by Visual C and some other Microsoft tools. An example
1544 * file.asm(50) : error: blah blah blah
1545 * where file.asm is the name of the file, 50 is the line number on
1546 * which the error occurs (or is detected) and "error:" is one of
1547 * the possible optional diagnostics -- it can be "error" or "warning"
1548 * or something else. Finally the line terminates with the actual
1551 * @param severity the severity of the warning or error
1552 * @param fmt the printf style format string
1554 static void report_error_vc(int severity
, const char *fmt
, ...)
1558 if (is_suppressed_warning(severity
))
1561 if (severity
& ERR_NOFILE
)
1562 fputs("nasm: ", error_file
);
1564 char *currentfile
= NULL
;
1566 src_get(&lineno
, ¤tfile
);
1567 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1568 nasm_free(currentfile
);
1571 report_error_common(severity
, fmt
, ap
);
1576 * check for supressed warning
1577 * checks for suppressed warning or pass one only warning and we're
1580 * @param severity the severity of the warning or error
1581 * @return true if we should abort error/warning printing
1583 static int is_suppressed_warning(int severity
)
1586 * See if it's a suppressed warning.
1588 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1589 (severity
& ERR_WARN_MASK
) != 0 &&
1590 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1592 * See if it's a pass-one only warning and we're not in pass one.
1594 ((severity
& ERR_PASS1
) && pass0
== 2);
1598 * common error reporting
1599 * This is the common back end of the error reporting schemes currently
1600 * implemented. It prints the nature of the warning and then the
1601 * specific error message to error_file and may or may not return. It
1602 * doesn't return if the error severity is a "panic" or "debug" type.
1604 * @param severity the severity of the warning or error
1605 * @param fmt the printf style format string
1607 static void report_error_common(int severity
, const char *fmt
,
1610 switch (severity
& ERR_MASK
) {
1612 fputs("warning: ", error_file
);
1615 fputs("error: ", error_file
);
1618 fputs("fatal: ", error_file
);
1621 fputs("panic: ", error_file
);
1624 fputs("debug: ", error_file
);
1628 vfprintf(error_file
, fmt
, args
);
1629 putc('\n', error_file
);
1631 if (severity
& ERR_USAGE
)
1634 switch (severity
& ERR_MASK
) {
1636 /* no further action, by definition */
1639 if (!suppressed
[0]) /* Treat warnings as errors */
1640 terminate_after_phase
= true;
1643 terminate_after_phase
= true;
1652 exit(1); /* instantly die */
1653 break; /* placate silly compilers */
1656 /* abort(); *//* halt, catch fire, and dump core */
1662 static void usage(void)
1664 fputs("type `nasm -h' for help\n", error_file
);
1667 static void register_output_formats(void)
1669 ofmt
= ofmt_register(report_error
);
1672 #define BUF_DELTA 512
1674 static FILE *no_pp_fp
;
1675 static efunc no_pp_err
;
1676 static ListGen
*no_pp_list
;
1677 static int32_t no_pp_lineinc
;
1679 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1682 src_set_fname(nasm_strdup(file
));
1686 no_pp_fp
= fopen(file
, "r");
1688 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1689 "unable to open input file `%s'", file
);
1690 no_pp_list
= listgen
;
1691 (void)pass
; /* placate compilers */
1692 (void)eval
; /* placate compilers */
1695 static char *no_pp_getline(void)
1697 char *buffer
, *p
, *q
;
1700 bufsize
= BUF_DELTA
;
1701 buffer
= nasm_malloc(BUF_DELTA
);
1702 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1704 while (1) { /* Loop to handle %line */
1707 while (1) { /* Loop to handle long lines */
1708 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1712 if (p
> buffer
&& p
[-1] == '\n')
1714 if (p
- buffer
> bufsize
- 10) {
1716 offset
= p
- buffer
;
1717 bufsize
+= BUF_DELTA
;
1718 buffer
= nasm_realloc(buffer
, bufsize
);
1719 p
= buffer
+ offset
;
1723 if (!q
&& p
== buffer
) {
1729 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1730 * them are present at the end of the line.
1732 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1734 if (!nasm_strnicmp(buffer
, "%line", 5)) {
1737 char *nm
= nasm_malloc(strlen(buffer
));
1738 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1739 nasm_free(src_set_fname(nm
));
1749 no_pp_list
->line(LIST_READ
, buffer
);
1754 static void no_pp_cleanup(int pass
)
1756 (void)pass
; /* placate GCC */
1760 static uint32_t get_cpu(char *value
)
1762 if (!strcmp(value
, "8086"))
1764 if (!strcmp(value
, "186"))
1766 if (!strcmp(value
, "286"))
1768 if (!strcmp(value
, "386"))
1770 if (!strcmp(value
, "486"))
1772 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1774 if (!strcmp(value
, "686") ||
1775 !nasm_stricmp(value
, "ppro") ||
1776 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1778 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1780 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1781 !nasm_stricmp(value
, "willamette"))
1782 return IF_WILLAMETTE
;
1783 if (!nasm_stricmp(value
, "prescott"))
1785 if (!nasm_stricmp(value
, "x64") ||
1786 !nasm_stricmp(value
, "x86-64"))
1788 if (!nasm_stricmp(value
, "ia64") ||
1789 !nasm_stricmp(value
, "ia-64") ||
1790 !nasm_stricmp(value
, "itanium") ||
1791 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1794 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1795 "unknown 'cpu' type");
1797 return IF_PLEVEL
; /* the maximum level */
1800 static int get_bits(char *value
)
1804 if ((i
= atoi(value
)) == 16)
1805 return i
; /* set for a 16-bit segment */
1808 report_error(ERR_NONFATAL
,
1809 "cannot specify 32-bit segment on processor below a 386");
1812 } else if (i
== 64) {
1813 if (cpu
< IF_X86_64
) {
1814 report_error(ERR_NONFATAL
,
1815 "cannot specify 64-bit segment on processor below an x86-64");
1819 report_error(ERR_NONFATAL
,
1820 "%s output format does not support 64-bit code",
1825 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1826 "`%s' is not a valid segment size; must be 16, 32 or 64",