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
, bool *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
},
370 static bool stopoptions
= false;
371 static bool process_arg(char *p
, char *q
)
375 bool advance
= false;
381 if (p
[0] == '-' && !stopoptions
) {
382 if (strchr("oOfpPdDiIlFXuUZwW", p
[1])) {
383 /* These parameters take values */
384 if (!(param
= get_param(p
, q
, &advance
)))
393 case 'o': /* output file */
394 strcpy(outname
, param
);
397 case 'f': /* output format */
398 ofmt
= ofmt_find(param
);
400 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
401 "unrecognised output format `%s' - "
402 "use -hf for a list", param
);
404 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
408 case 'O': /* Optimization level */
413 /* Naked -O == -Ox */
414 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
418 case '0': case '1': case '2': case '3': case '4':
419 case '5': case '6': case '7': case '8': case '9':
420 opt
= strtoul(param
, ¶m
, 10);
422 /* -O0 -> optimizing == -1, 0.98 behaviour */
423 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
425 optimizing
= opt
- 1;
427 /* The optimizer seems to have problems with
428 < 5 passes? Hidden bug? */
429 optimizing
= 5; /* 5 passes */
431 optimizing
= opt
; /* More than 5 passes */
437 opt_verbose_info
= true;
442 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
446 report_error(ERR_FATAL
,
447 "unknown optimization option -O%c\n",
456 case 'p': /* pre-include */
458 pp_pre_include(param
);
461 case 'd': /* pre-define */
463 pp_pre_define(param
);
466 case 'u': /* un-define */
468 pp_pre_undefine(param
);
471 case 'i': /* include search path */
473 pp_include_path(param
);
476 case 'l': /* listing file */
477 strcpy(listname
, param
);
480 case 'Z': /* error messages file */
481 strcpy(errname
, param
);
484 case 'F': /* specify debug format */
485 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
486 if (!ofmt
->current_dfmt
) {
487 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
488 "unrecognized debug format `%s' for"
489 " output format `%s'",
490 param
, ofmt
->shortname
);
494 case 'X': /* specify error reporting format */
495 if (nasm_stricmp("vc", param
) == 0)
496 report_error
= report_error_vc
;
497 else if (nasm_stricmp("gnu", param
) == 0)
498 report_error
= report_error_gnu
;
500 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
501 "unrecognized error reporting format `%s'",
506 using_debug_info
= true;
511 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
513 " [options...] [--] filename\n"
514 " or nasm -v for version info\n\n"
515 " -t assemble in SciTech TASM compatible mode\n"
516 " -g generate debug information in selected format.\n");
518 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
519 " -a don't preprocess (assemble only)\n"
520 " -M generate Makefile dependencies on stdout\n"
521 " -MG d:o, missing files assumed generated\n\n"
522 " -Z<file> redirect error messages to file\n"
523 " -s redirect error messages to stdout\n\n"
524 " -F format select a debugging format\n\n"
525 " -I<path> adds a pathname to the include file path\n");
527 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
528 " -P<file> pre-includes a file\n"
529 " -D<macro>[=<value>] pre-defines a macro\n"
530 " -U<macro> undefines a macro\n"
531 " -X<format> specifies error reporting format (gnu or vc)\n"
532 " -w+foo enables warning foo (equiv. -Wfoo)\n"
533 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
535 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
536 printf(" %-23s %s (default %s)\n",
537 suppressed_names
[i
], suppressed_what
[i
],
538 suppressed
[i
] ? "off" : "on");
540 ("\nresponse files should contain command line parameters"
541 ", one per line.\n");
543 printf("\nvalid output formats for -f are"
544 " (`*' denotes default):\n");
545 ofmt_list(ofmt
, stdout
);
547 printf("\nFor a list of valid output formats, use -hf.\n");
548 printf("For a list of debug formats, use -f <form> -y.\n");
550 exit(0); /* never need usage message here */
554 printf("\nvalid debug formats for '%s' output format are"
555 " ('*' denotes default):\n", ofmt
->shortname
);
556 dfmt_list(ofmt
, stdout
);
561 tasm_compatible_mode
= true;
566 const char *nasm_version_string
=
567 "NASM version " NASM_VER
" compiled on " __DATE__
572 puts(nasm_version_string
);
573 exit(0); /* never need usage message here */
577 case 'e': /* preprocess only */
579 operating_mode
= op_preprocess
;
582 case 'a': /* assemble only - don't preprocess */
587 if (param
[0] == 'n' && param
[1] == 'o' && param
[2] == '-') {
596 if (param
[0] != '+' && param
[0] != '-') {
597 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
598 "invalid option to `-w'");
601 suppress
= (param
[0] == '-');
605 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
606 if (!nasm_stricmp(param
, suppressed_names
[i
]))
608 if (i
<= ERR_WARN_MAX
)
609 suppressed
[i
] = suppress
;
610 else if (!nasm_stricmp(param
, "all"))
611 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
612 suppressed
[i
] = suppress
;
613 else if (!nasm_stricmp(param
, "none"))
614 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
615 suppressed
[i
] = !suppress
;
617 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
618 "invalid warning `%s'", param
);
622 operating_mode
= p
[2] == 'G' ? op_depend_missing_ok
: op_depend
;
629 if (p
[2] == 0) { /* -- => stop processing options */
633 for (s
= 0; textopts
[s
].label
; s
++) {
634 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
645 report_error(ERR_NONFATAL
| ERR_NOFILE
|
647 "option `--%s' requires an argument",
651 advance
= 1, param
= q
;
654 if (s
== OPT_PREFIX
) {
655 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
656 lprefix
[PREFIX_MAX
- 1] = 0;
659 if (s
== OPT_POSTFIX
) {
660 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
661 lpostfix
[POSTFIX_MAX
- 1] = 0;
668 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
669 "unrecognised option `--%s'", p
+ 2);
677 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
678 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
679 "unrecognised option `-%c'", p
[1]);
684 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
685 "more than one input file specified");
693 #define ARG_BUF_DELTA 128
695 static void process_respfile(FILE * rfile
)
697 char *buffer
, *p
, *q
, *prevarg
;
698 int bufsize
, prevargsize
;
700 bufsize
= prevargsize
= ARG_BUF_DELTA
;
701 buffer
= nasm_malloc(ARG_BUF_DELTA
);
702 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
705 while (1) { /* Loop to handle all lines in file */
708 while (1) { /* Loop to handle long lines */
709 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
713 if (p
> buffer
&& p
[-1] == '\n')
715 if (p
- buffer
> bufsize
- 10) {
718 bufsize
+= ARG_BUF_DELTA
;
719 buffer
= nasm_realloc(buffer
, bufsize
);
724 if (!q
&& p
== buffer
) {
726 process_arg(prevarg
, NULL
);
733 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
734 * them are present at the end of the line.
736 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
738 while (p
> buffer
&& isspace(p
[-1]))
745 if (process_arg(prevarg
, p
))
748 if ((int) strlen(p
) > prevargsize
- 10) {
749 prevargsize
+= ARG_BUF_DELTA
;
750 prevarg
= nasm_realloc(prevarg
, prevargsize
);
756 /* Function to process args from a string of args, rather than the
757 * argv array. Used by the environment variable and response file
760 static void process_args(char *args
)
762 char *p
, *q
, *arg
, *prevarg
;
763 char separator
= ' ';
771 while (*p
&& *p
!= separator
)
773 while (*p
== separator
)
777 if (process_arg(prevarg
, arg
))
781 process_arg(arg
, NULL
);
784 static void parse_cmdline(int argc
, char **argv
)
787 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
789 *inname
= *outname
= *listname
= *errname
= '\0';
792 * First, process the NASMENV environment variable.
794 envreal
= getenv("NASMENV");
797 envcopy
= nasm_strdup(envreal
);
798 process_args(envcopy
);
803 * Now process the actual command line.
808 if (argv
[0][0] == '@') {
809 /* We have a response file, so process this as a set of
810 * arguments like the environment variable. This allows us
811 * to have multiple arguments on a single line, which is
812 * different to the -@resp file processing below for regular
815 char *str
= malloc(2048);
816 FILE *f
= fopen(&argv
[0][1], "r");
818 printf("out of memory");
822 while (fgets(str
, 2048, f
)) {
831 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
832 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &advance
);
834 rfile
= fopen(p
, "r");
836 process_respfile(rfile
);
839 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
840 "unable to open response file `%s'", p
);
843 advance
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
844 argv
+= advance
, argc
-= advance
;
848 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
849 "no input file specified");
851 /* Look for basic command line typos. This definitely doesn't
852 catch all errors, but it might help cases of fumbled fingers. */
853 if (!strcmp(inname
, errname
) || !strcmp(inname
, outname
) ||
854 !strcmp(inname
, listname
))
855 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
856 "file `%s' is both input and output file",
860 error_file
= fopen(errname
, "w");
862 error_file
= stderr
; /* Revert to default! */
863 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
864 "cannot open file `%s' for error messages",
870 /* List of directives */
872 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
873 D_EXTERN
, D_FLOAT
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
875 static const char *directives
[] = {
876 "", "absolute", "bits", "common", "cpu", "debug", "default",
877 "extern", "float", "global", "list", "section", "segment", "warning"
879 static enum directives
getkw(char **directive
, char **value
);
881 static void assemble_file(char *fname
)
883 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
889 struct tokenval tokval
;
892 int pass_cnt
= 0; /* count actual passes */
894 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
895 report_error(ERR_FATAL
, "command line: "
896 "32-bit segment size requires a higher cpu");
898 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
899 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
900 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
904 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
905 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
906 /* pass0 seq is 0, 0, 0,..., 1, 2 */
908 def_label
= pass
> 1 ? redefine_label
: define_label
;
910 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
914 nasmlist
.init(listname
, report_error
);
917 global_offset_changed
= false; /* set by redefine_label */
918 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
921 saa_rewind(forwrefs
);
922 forwref
= saa_rstruct(forwrefs
);
924 offsets
= raa_init();
926 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
929 location
.known
= true;
930 location
.offset
= offs
= GET_CURR_OFFS
;
932 while ((line
= preproc
->getline())) {
936 /* here we parse our directives; this is not handled by the 'real'
939 d
= getkw(&directive
, &value
);
944 case D_SEGMENT
: /* [SEGMENT n] */
946 seg
= ofmt
->section(value
, pass2
, &sb
);
948 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
949 "segment name `%s' not recognized",
953 location
.segment
= seg
;
956 case D_EXTERN
: /* [EXTERN label:special] */
958 value
++; /* skip initial $ if present */
961 while (*q
&& *q
!= ':')
965 ofmt
->symdef(value
, 0L, 0L, 3, q
);
967 } else if (pass
== 1) { /* pass == 1 */
972 while (*q
&& *q
!= ':') {
978 report_error(ERR_NONFATAL
,
979 "identifier expected after EXTERN");
987 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
989 pass0
= 1; /* fake pass 1 in labels.c */
990 declare_as_global(value
, special
,
992 define_label(value
, seg_alloc(), 0L, NULL
,
993 false, true, ofmt
, report_error
);
996 } /* else pass0 == 1 */
998 case D_BITS
: /* [BITS bits] */
999 globalbits
= sb
= get_bits(value
);
1001 case D_GLOBAL
: /* [GLOBAL symbol:special] */
1003 value
++; /* skip initial $ if present */
1004 if (pass0
== 2) { /* pass 2 */
1006 while (*q
&& *q
!= ':')
1010 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1012 } else if (pass2
== 1) { /* pass == 1 */
1017 while (*q
&& *q
!= ':') {
1023 report_error(ERR_NONFATAL
,
1024 "identifier expected after GLOBAL");
1032 declare_as_global(value
, special
, report_error
);
1035 case D_COMMON
: /* [COMMON symbol size:special] */
1037 value
++; /* skip initial $ if present */
1043 while (*p
&& !isspace(*p
)) {
1049 report_error(ERR_NONFATAL
,
1050 "identifier expected after COMMON");
1056 while (*p
&& isspace(*p
))
1059 while (*q
&& *q
!= ':')
1066 size
= readnum(p
, &rn_error
);
1068 report_error(ERR_NONFATAL
,
1069 "invalid size specified"
1070 " in COMMON declaration");
1072 define_common(value
, seg_alloc(), size
,
1073 special
, ofmt
, report_error
);
1075 report_error(ERR_NONFATAL
,
1076 "no size specified in"
1077 " COMMON declaration");
1078 } else if (pass0
== 2) { /* pass == 2 */
1080 while (*q
&& *q
!= ':') {
1087 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1091 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1093 stdscan_bufptr
= value
;
1094 tokval
.t_type
= TOKEN_INVALID
;
1095 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1096 report_error
, NULL
);
1099 report_error(pass0
==
1100 1 ? ERR_NONFATAL
: ERR_PANIC
,
1101 "cannot use non-relocatable expression as "
1102 "ABSOLUTE address");
1104 abs_seg
= reloc_seg(e
);
1105 abs_offset
= reloc_value(e
);
1107 } else if (pass
== 1)
1108 abs_offset
= 0x100; /* don't go near zero in case of / */
1110 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1113 location
.segment
= NO_SEG
;
1115 case D_DEBUG
: /* [DEBUG] */
1121 while (*p
&& !isspace(*p
)) {
1128 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1129 "identifier expected after DEBUG");
1132 while (*p
&& isspace(*p
))
1134 if (pass
== pass_max
)
1135 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1137 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1139 while (*value
&& isspace(*value
))
1142 if (*value
== '+' || *value
== '-') {
1143 validid
= (*value
== '-') ? true : false;
1148 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1149 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1151 if (i
<= ERR_WARN_MAX
)
1152 suppressed
[i
] = validid
;
1154 report_error(ERR_NONFATAL
,
1155 "invalid warning id in WARNING directive");
1158 case D_CPU
: /* [CPU] */
1159 cpu
= get_cpu(value
);
1161 case D_LIST
: /* [LIST {+|-}] */
1162 while (*value
&& isspace(*value
))
1165 if (*value
== '+') {
1168 if (*value
== '-') {
1175 case D_DEFAULT
: /* [DEFAULT] */
1177 stdscan_bufptr
= value
;
1178 tokval
.t_type
= TOKEN_INVALID
;
1179 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1180 switch ((int)tokval
.t_integer
) {
1196 if (float_option(value
)) {
1197 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1198 "unknown 'float' directive: %s",
1203 if (!ofmt
->directive(directive
, value
, pass2
))
1204 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1205 "unrecognised directive [%s]",
1209 report_error(ERR_NONFATAL
,
1210 "invalid parameter to [%s] directive",
1213 } else { /* it isn't a directive */
1215 parse_line(pass1
, line
, &output_ins
,
1216 report_error
, evaluate
, def_label
);
1218 if (!(optimizing
> 0) && pass
== 2) {
1219 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1220 output_ins
.forw_ref
= true;
1222 output_ins
.oprs
[forwref
->operand
].opflags
|=
1224 forwref
= saa_rstruct(forwrefs
);
1225 } while (forwref
!= NULL
1226 && forwref
->lineno
== globallineno
);
1228 output_ins
.forw_ref
= false;
1231 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1233 for (i
= 0; i
< output_ins
.operands
; i
++) {
1234 if (output_ins
.oprs
[i
].
1235 opflags
& OPFLAG_FORWARD
) {
1236 struct forwrefinfo
*fwinf
=
1237 (struct forwrefinfo
*)
1238 saa_wstruct(forwrefs
);
1239 fwinf
->lineno
= globallineno
;
1243 } else { /* pass == 2 */
1245 * Hack to prevent phase error in the code
1249 * If the second operand is a forward reference,
1250 * the UNITY property of the number 1 in that
1251 * operand is cancelled. Otherwise the above
1252 * sequence will cause a phase error.
1254 * This hack means that the above code will
1255 * generate 286+ code.
1257 * The forward reference will mean that the
1258 * operand will not have the UNITY property on
1259 * the first pass, so the pass behaviours will
1263 if (output_ins
.operands
>= 2 &&
1264 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1265 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1267 /* Remove special properties bits */
1268 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1276 if (output_ins
.opcode
== I_EQU
) {
1279 * Special `..' EQUs get processed in pass two,
1280 * except `..@' macro-processor EQUs which are done
1281 * in the normal place.
1283 if (!output_ins
.label
)
1284 report_error(ERR_NONFATAL
,
1285 "EQU not preceded by label");
1287 else if (output_ins
.label
[0] != '.' ||
1288 output_ins
.label
[1] != '.' ||
1289 output_ins
.label
[2] == '@') {
1290 if (output_ins
.operands
== 1 &&
1291 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1292 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1295 opflags
& OPFLAG_EXTERN
;
1296 def_label(output_ins
.label
,
1297 output_ins
.oprs
[0].segment
,
1298 output_ins
.oprs
[0].offset
, NULL
,
1301 } else if (output_ins
.operands
== 2
1302 && (output_ins
.oprs
[0].
1304 && (output_ins
.oprs
[0].type
& COLON
)
1305 && output_ins
.oprs
[0].segment
==
1307 && output_ins
.oprs
[0].wrt
== NO_SEG
1308 && (output_ins
.oprs
[1].
1310 && output_ins
.oprs
[1].segment
==
1312 && output_ins
.oprs
[1].wrt
==
1314 def_label(output_ins
.label
,
1317 output_ins
.oprs
[1].offset
, NULL
,
1321 report_error(ERR_NONFATAL
,
1322 "bad syntax for EQU");
1324 } else { /* pass == 2 */
1326 * Special `..' EQUs get processed here, except
1327 * `..@' macro processor EQUs which are done above.
1329 if (output_ins
.label
[0] == '.' &&
1330 output_ins
.label
[1] == '.' &&
1331 output_ins
.label
[2] != '@') {
1332 if (output_ins
.operands
== 1 &&
1333 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1334 define_label(output_ins
.label
,
1335 output_ins
.oprs
[0].segment
,
1336 output_ins
.oprs
[0].offset
,
1337 NULL
, false, false, ofmt
,
1339 } else if (output_ins
.operands
== 2
1340 && (output_ins
.oprs
[0].
1342 && (output_ins
.oprs
[0].type
& COLON
)
1343 && output_ins
.oprs
[0].segment
==
1345 && (output_ins
.oprs
[1].
1347 && output_ins
.oprs
[1].segment
==
1349 define_label(output_ins
.label
,
1352 output_ins
.oprs
[1].offset
,
1353 NULL
, false, false, ofmt
,
1356 report_error(ERR_NONFATAL
,
1357 "bad syntax for EQU");
1360 } else { /* instruction isn't an EQU */
1364 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1365 &output_ins
, report_error
);
1367 /* if (using_debug_info) && output_ins.opcode != -1) */
1368 if (using_debug_info
)
1369 { /* fbk 03/25/01 */
1370 /* this is done here so we can do debug type info */
1372 TYS_ELEMENTS(output_ins
.operands
);
1373 switch (output_ins
.opcode
) {
1376 TYS_ELEMENTS(output_ins
.oprs
[0].
1381 TYS_ELEMENTS(output_ins
.oprs
[0].
1386 TYS_ELEMENTS(output_ins
.oprs
[0].
1391 TYS_ELEMENTS(output_ins
.oprs
[0].
1396 TYS_ELEMENTS(output_ins
.oprs
[0].
1400 typeinfo
|= TY_BYTE
;
1403 typeinfo
|= TY_WORD
;
1406 if (output_ins
.eops_float
)
1407 typeinfo
|= TY_FLOAT
;
1409 typeinfo
|= TY_DWORD
;
1412 typeinfo
|= TY_QWORD
;
1415 typeinfo
|= TY_TBYTE
;
1418 typeinfo
|= TY_OWORD
;
1421 typeinfo
= TY_LABEL
;
1425 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1430 SET_CURR_OFFS(offs
);
1433 * else l == -1 => invalid instruction, which will be
1434 * flagged as an error on pass 2
1437 } else { /* pass == 2 */
1438 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1439 &output_ins
, ofmt
, report_error
,
1441 SET_CURR_OFFS(offs
);
1445 cleanup_insn(&output_ins
);
1448 location
.offset
= offs
= GET_CURR_OFFS
;
1449 } /* end while (line = preproc->getline... */
1451 if (pass1
== 2 && global_offset_changed
)
1452 report_error(ERR_NONFATAL
,
1453 "phase error detected at end of assembly.");
1456 preproc
->cleanup(1);
1458 if (pass1
== 1 && terminate_after_phase
) {
1466 if (pass
> 1 && !global_offset_changed
) {
1469 pass
= pass_max
- 1;
1470 } else if (!(optimizing
> 0))
1473 } /* for (pass=1; pass<=2; pass++) */
1475 preproc
->cleanup(0);
1478 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1480 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1482 } /* exit from assemble_file (...) */
1484 static enum directives
getkw(char **directive
, char **value
)
1490 /* allow leading spaces or tabs */
1491 while (*buf
== ' ' || *buf
== '\t')
1499 while (*p
&& *p
!= ']')
1507 while (*p
&& *p
!= ';') {
1514 *directive
= p
= buf
+ 1;
1515 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1522 while (isspace(*buf
))
1523 buf
++; /* beppu - skip leading whitespace */
1530 return bsii(*directive
, directives
, elements(directives
));
1534 * gnu style error reporting
1535 * This function prints an error message to error_file in the
1536 * style used by GNU. An example would be:
1537 * file.asm:50: error: blah blah blah
1538 * where file.asm is the name of the file, 50 is the line number on
1539 * which the error occurs (or is detected) and "error:" is one of
1540 * the possible optional diagnostics -- it can be "error" or "warning"
1541 * or something else. Finally the line terminates with the actual
1544 * @param severity the severity of the warning or error
1545 * @param fmt the printf style format string
1547 static void report_error_gnu(int severity
, const char *fmt
, ...)
1551 if (is_suppressed_warning(severity
))
1554 if (severity
& ERR_NOFILE
)
1555 fputs("nasm: ", error_file
);
1557 char *currentfile
= NULL
;
1559 src_get(&lineno
, ¤tfile
);
1560 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1561 nasm_free(currentfile
);
1564 report_error_common(severity
, fmt
, ap
);
1569 * MS style error reporting
1570 * This function prints an error message to error_file in the
1571 * style used by Visual C and some other Microsoft tools. An example
1573 * file.asm(50) : error: blah blah blah
1574 * where file.asm is the name of the file, 50 is the line number on
1575 * which the error occurs (or is detected) and "error:" is one of
1576 * the possible optional diagnostics -- it can be "error" or "warning"
1577 * or something else. Finally the line terminates with the actual
1580 * @param severity the severity of the warning or error
1581 * @param fmt the printf style format string
1583 static void report_error_vc(int severity
, const char *fmt
, ...)
1587 if (is_suppressed_warning(severity
))
1590 if (severity
& ERR_NOFILE
)
1591 fputs("nasm: ", error_file
);
1593 char *currentfile
= NULL
;
1595 src_get(&lineno
, ¤tfile
);
1596 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1597 nasm_free(currentfile
);
1600 report_error_common(severity
, fmt
, ap
);
1605 * check for supressed warning
1606 * checks for suppressed warning or pass one only warning and we're
1609 * @param severity the severity of the warning or error
1610 * @return true if we should abort error/warning printing
1612 static int is_suppressed_warning(int severity
)
1615 * See if it's a suppressed warning.
1617 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1618 (severity
& ERR_WARN_MASK
) != 0 &&
1619 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1621 * See if it's a pass-one only warning and we're not in pass one.
1623 ((severity
& ERR_PASS1
) && pass0
== 2);
1627 * common error reporting
1628 * This is the common back end of the error reporting schemes currently
1629 * implemented. It prints the nature of the warning and then the
1630 * specific error message to error_file and may or may not return. It
1631 * doesn't return if the error severity is a "panic" or "debug" type.
1633 * @param severity the severity of the warning or error
1634 * @param fmt the printf style format string
1636 static void report_error_common(int severity
, const char *fmt
,
1639 switch (severity
& ERR_MASK
) {
1641 fputs("warning: ", error_file
);
1644 fputs("error: ", error_file
);
1647 fputs("fatal: ", error_file
);
1650 fputs("panic: ", error_file
);
1653 fputs("debug: ", error_file
);
1657 vfprintf(error_file
, fmt
, args
);
1658 putc('\n', error_file
);
1660 if (severity
& ERR_USAGE
)
1663 switch (severity
& ERR_MASK
) {
1665 /* no further action, by definition */
1668 if (!suppressed
[0]) /* Treat warnings as errors */
1669 terminate_after_phase
= true;
1672 terminate_after_phase
= true;
1681 exit(1); /* instantly die */
1682 break; /* placate silly compilers */
1685 /* abort(); *//* halt, catch fire, and dump core */
1691 static void usage(void)
1693 fputs("type `nasm -h' for help\n", error_file
);
1696 static void register_output_formats(void)
1698 ofmt
= ofmt_register(report_error
);
1701 #define BUF_DELTA 512
1703 static FILE *no_pp_fp
;
1704 static efunc no_pp_err
;
1705 static ListGen
*no_pp_list
;
1706 static int32_t no_pp_lineinc
;
1708 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1711 src_set_fname(nasm_strdup(file
));
1715 no_pp_fp
= fopen(file
, "r");
1717 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1718 "unable to open input file `%s'", file
);
1719 no_pp_list
= listgen
;
1720 (void)pass
; /* placate compilers */
1721 (void)eval
; /* placate compilers */
1724 static char *no_pp_getline(void)
1726 char *buffer
, *p
, *q
;
1729 bufsize
= BUF_DELTA
;
1730 buffer
= nasm_malloc(BUF_DELTA
);
1731 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1733 while (1) { /* Loop to handle %line */
1736 while (1) { /* Loop to handle long lines */
1737 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1741 if (p
> buffer
&& p
[-1] == '\n')
1743 if (p
- buffer
> bufsize
- 10) {
1745 offset
= p
- buffer
;
1746 bufsize
+= BUF_DELTA
;
1747 buffer
= nasm_realloc(buffer
, bufsize
);
1748 p
= buffer
+ offset
;
1752 if (!q
&& p
== buffer
) {
1758 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1759 * them are present at the end of the line.
1761 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1763 if (!nasm_strnicmp(buffer
, "%line", 5)) {
1766 char *nm
= nasm_malloc(strlen(buffer
));
1767 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1768 nasm_free(src_set_fname(nm
));
1778 no_pp_list
->line(LIST_READ
, buffer
);
1783 static void no_pp_cleanup(int pass
)
1785 (void)pass
; /* placate GCC */
1789 static uint32_t get_cpu(char *value
)
1791 if (!strcmp(value
, "8086"))
1793 if (!strcmp(value
, "186"))
1795 if (!strcmp(value
, "286"))
1797 if (!strcmp(value
, "386"))
1799 if (!strcmp(value
, "486"))
1801 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1803 if (!strcmp(value
, "686") ||
1804 !nasm_stricmp(value
, "ppro") ||
1805 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1807 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1809 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1810 !nasm_stricmp(value
, "willamette"))
1811 return IF_WILLAMETTE
;
1812 if (!nasm_stricmp(value
, "prescott"))
1814 if (!nasm_stricmp(value
, "x64") ||
1815 !nasm_stricmp(value
, "x86-64"))
1817 if (!nasm_stricmp(value
, "ia64") ||
1818 !nasm_stricmp(value
, "ia-64") ||
1819 !nasm_stricmp(value
, "itanium") ||
1820 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1823 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1824 "unknown 'cpu' type");
1826 return IF_PLEVEL
; /* the maximum level */
1829 static int get_bits(char *value
)
1833 if ((i
= atoi(value
)) == 16)
1834 return i
; /* set for a 16-bit segment */
1837 report_error(ERR_NONFATAL
,
1838 "cannot specify 32-bit segment on processor below a 386");
1841 } else if (i
== 64) {
1842 if (cpu
< IF_X86_64
) {
1843 report_error(ERR_NONFATAL
,
1844 "cannot specify 64-bit segment on processor below an x86-64");
1848 report_error(ERR_NONFATAL
,
1849 "%s output format does not support 64-bit code",
1854 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1855 "`%s' is not a valid segment size; must be 16, 32 or 64",