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]);
360 static void copy_filename(char *dst
, const char *src
)
362 size_t len
= strlen(src
);
364 if (len
>= (size_t)FILENAME_MAX
) {
365 report_error(ERR_FATAL
| ERR_NOFILE
, "file name too long");
368 strncpy(dst
, src
, FILENAME_MAX
);
377 #define OPT_POSTFIX 1
378 struct textargs textopts
[] = {
379 {"prefix", OPT_PREFIX
},
380 {"postfix", OPT_POSTFIX
},
384 static bool stopoptions
= false;
385 static bool process_arg(char *p
, char *q
)
389 bool advance
= false;
395 if (p
[0] == '-' && !stopoptions
) {
396 if (strchr("oOfpPdDiIlFXuUZwW", p
[1])) {
397 /* These parameters take values */
398 if (!(param
= get_param(p
, q
, &advance
)))
407 case 'o': /* output file */
408 copy_filename(outname
, param
);
411 case 'f': /* output format */
412 ofmt
= ofmt_find(param
);
414 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
415 "unrecognised output format `%s' - "
416 "use -hf for a list", param
);
418 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
422 case 'O': /* Optimization level */
427 /* Naked -O == -Ox */
428 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
432 case '0': case '1': case '2': case '3': case '4':
433 case '5': case '6': case '7': case '8': case '9':
434 opt
= strtoul(param
, ¶m
, 10);
436 /* -O0 -> optimizing == -1, 0.98 behaviour */
437 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
439 optimizing
= opt
- 1;
441 /* The optimizer seems to have problems with
442 < 5 passes? Hidden bug? */
443 optimizing
= 5; /* 5 passes */
445 optimizing
= opt
; /* More than 5 passes */
451 opt_verbose_info
= true;
456 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
460 report_error(ERR_FATAL
,
461 "unknown optimization option -O%c\n",
470 case 'p': /* pre-include */
472 pp_pre_include(param
);
475 case 'd': /* pre-define */
477 pp_pre_define(param
);
480 case 'u': /* un-define */
482 pp_pre_undefine(param
);
485 case 'i': /* include search path */
487 pp_include_path(param
);
490 case 'l': /* listing file */
491 copy_filename(listname
, param
);
494 case 'Z': /* error messages file */
495 strcpy(errname
, param
);
498 case 'F': /* specify debug format */
499 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
500 if (!ofmt
->current_dfmt
) {
501 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
502 "unrecognized debug format `%s' for"
503 " output format `%s'",
504 param
, ofmt
->shortname
);
508 case 'X': /* specify error reporting format */
509 if (nasm_stricmp("vc", param
) == 0)
510 report_error
= report_error_vc
;
511 else if (nasm_stricmp("gnu", param
) == 0)
512 report_error
= report_error_gnu
;
514 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
515 "unrecognized error reporting format `%s'",
520 using_debug_info
= true;
525 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
527 " [options...] [--] filename\n"
528 " or nasm -v for version info\n\n"
529 " -t assemble in SciTech TASM compatible mode\n"
530 " -g generate debug information in selected format.\n");
532 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
533 " -a don't preprocess (assemble only)\n"
534 " -M generate Makefile dependencies on stdout\n"
535 " -MG d:o, missing files assumed generated\n\n"
536 " -Z<file> redirect error messages to file\n"
537 " -s redirect error messages to stdout\n\n"
538 " -F format select a debugging format\n\n"
539 " -I<path> adds a pathname to the include file path\n");
541 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
542 " -P<file> pre-includes a file\n"
543 " -D<macro>[=<value>] pre-defines a macro\n"
544 " -U<macro> undefines a macro\n"
545 " -X<format> specifies error reporting format (gnu or vc)\n"
546 " -w+foo enables warning foo (equiv. -Wfoo)\n"
547 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
549 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
550 printf(" %-23s %s (default %s)\n",
551 suppressed_names
[i
], suppressed_what
[i
],
552 suppressed
[i
] ? "off" : "on");
554 ("\nresponse files should contain command line parameters"
555 ", one per line.\n");
557 printf("\nvalid output formats for -f are"
558 " (`*' denotes default):\n");
559 ofmt_list(ofmt
, stdout
);
561 printf("\nFor a list of valid output formats, use -hf.\n");
562 printf("For a list of debug formats, use -f <form> -y.\n");
564 exit(0); /* never need usage message here */
568 printf("\nvalid debug formats for '%s' output format are"
569 " ('*' denotes default):\n", ofmt
->shortname
);
570 dfmt_list(ofmt
, stdout
);
575 tasm_compatible_mode
= true;
580 const char *nasm_version_string
=
581 "NASM version " NASM_VER
" compiled on " __DATE__
586 puts(nasm_version_string
);
587 exit(0); /* never need usage message here */
591 case 'e': /* preprocess only */
593 operating_mode
= op_preprocess
;
596 case 'a': /* assemble only - don't preprocess */
601 if (param
[0] == 'n' && param
[1] == 'o' && param
[2] == '-') {
610 if (param
[0] != '+' && param
[0] != '-') {
611 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
612 "invalid option to `-w'");
615 suppress
= (param
[0] == '-');
619 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
620 if (!nasm_stricmp(param
, suppressed_names
[i
]))
622 if (i
<= ERR_WARN_MAX
)
623 suppressed
[i
] = suppress
;
624 else if (!nasm_stricmp(param
, "all"))
625 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
626 suppressed
[i
] = suppress
;
627 else if (!nasm_stricmp(param
, "none"))
628 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
629 suppressed
[i
] = !suppress
;
631 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
632 "invalid warning `%s'", param
);
636 operating_mode
= p
[2] == 'G' ? op_depend_missing_ok
: op_depend
;
643 if (p
[2] == 0) { /* -- => stop processing options */
647 for (s
= 0; textopts
[s
].label
; s
++) {
648 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
659 report_error(ERR_NONFATAL
| ERR_NOFILE
|
661 "option `--%s' requires an argument",
665 advance
= 1, param
= q
;
668 if (s
== OPT_PREFIX
) {
669 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
670 lprefix
[PREFIX_MAX
- 1] = 0;
673 if (s
== OPT_POSTFIX
) {
674 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
675 lpostfix
[POSTFIX_MAX
- 1] = 0;
682 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
683 "unrecognised option `--%s'", p
+ 2);
691 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
692 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
693 "unrecognised option `-%c'", p
[1]);
698 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
699 "more than one input file specified");
701 copy_filename(inname
, p
);
708 #define ARG_BUF_DELTA 128
710 static void process_respfile(FILE * rfile
)
712 char *buffer
, *p
, *q
, *prevarg
;
713 int bufsize
, prevargsize
;
715 bufsize
= prevargsize
= ARG_BUF_DELTA
;
716 buffer
= nasm_malloc(ARG_BUF_DELTA
);
717 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
720 while (1) { /* Loop to handle all lines in file */
722 while (1) { /* Loop to handle long lines */
723 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
727 if (p
> buffer
&& p
[-1] == '\n')
729 if (p
- buffer
> bufsize
- 10) {
732 bufsize
+= ARG_BUF_DELTA
;
733 buffer
= nasm_realloc(buffer
, bufsize
);
738 if (!q
&& p
== buffer
) {
740 process_arg(prevarg
, NULL
);
747 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
748 * them are present at the end of the line.
750 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
752 while (p
> buffer
&& isspace(p
[-1]))
759 if (process_arg(prevarg
, p
))
762 if ((int) strlen(p
) > prevargsize
- 10) {
763 prevargsize
+= ARG_BUF_DELTA
;
764 prevarg
= nasm_realloc(prevarg
, prevargsize
);
766 strncpy(prevarg
, p
, prevargsize
);
770 /* Function to process args from a string of args, rather than the
771 * argv array. Used by the environment variable and response file
774 static void process_args(char *args
)
776 char *p
, *q
, *arg
, *prevarg
;
777 char separator
= ' ';
785 while (*p
&& *p
!= separator
)
787 while (*p
== separator
)
791 if (process_arg(prevarg
, arg
))
795 process_arg(arg
, NULL
);
798 static void parse_cmdline(int argc
, char **argv
)
801 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
803 *inname
= *outname
= *listname
= *errname
= '\0';
806 * First, process the NASMENV environment variable.
808 envreal
= getenv("NASMENV");
811 envcopy
= nasm_strdup(envreal
);
812 process_args(envcopy
);
817 * Now process the actual command line.
822 if (argv
[0][0] == '@') {
823 /* We have a response file, so process this as a set of
824 * arguments like the environment variable. This allows us
825 * to have multiple arguments on a single line, which is
826 * different to the -@resp file processing below for regular
829 char *str
= malloc(2048);
830 FILE *f
= fopen(&argv
[0][1], "r");
832 printf("out of memory");
836 while (fgets(str
, 2048, f
)) {
845 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
846 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &advance
);
848 rfile
= fopen(p
, "r");
850 process_respfile(rfile
);
853 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
854 "unable to open response file `%s'", p
);
857 advance
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
858 argv
+= advance
, argc
-= advance
;
861 /* Look for basic command line typos. This definitely doesn't
862 catch all errors, but it might help cases of fumbled fingers. */
864 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
865 "no input file specified");
866 else if (!strcmp(inname
, errname
) || !strcmp(inname
, outname
) ||
867 !strcmp(inname
, listname
))
868 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
869 "file `%s' is both input and output file",
873 error_file
= fopen(errname
, "w");
875 error_file
= stderr
; /* Revert to default! */
876 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
877 "cannot open file `%s' for error messages",
883 /* List of directives */
885 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
886 D_EXTERN
, D_FLOAT
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
888 static const char *directives
[] = {
889 "", "absolute", "bits", "common", "cpu", "debug", "default",
890 "extern", "float", "global", "list", "section", "segment", "warning"
892 static enum directives
getkw(char **directive
, char **value
);
894 static void assemble_file(char *fname
)
896 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
902 struct tokenval tokval
;
905 int pass_cnt
= 0; /* count actual passes */
907 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
908 report_error(ERR_FATAL
, "command line: "
909 "32-bit segment size requires a higher cpu");
911 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
912 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
913 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
917 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
918 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
919 /* pass0 seq is 0, 0, 0,..., 1, 2 */
921 def_label
= pass
> 1 ? redefine_label
: define_label
;
923 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
927 nasmlist
.init(listname
, report_error
);
930 global_offset_changed
= false; /* set by redefine_label */
931 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
934 saa_rewind(forwrefs
);
935 forwref
= saa_rstruct(forwrefs
);
937 offsets
= raa_init();
939 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
942 location
.known
= true;
943 location
.offset
= offs
= GET_CURR_OFFS
;
945 while ((line
= preproc
->getline())) {
949 /* here we parse our directives; this is not handled by the 'real'
952 d
= getkw(&directive
, &value
);
957 case D_SEGMENT
: /* [SEGMENT n] */
959 seg
= ofmt
->section(value
, pass2
, &sb
);
961 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
962 "segment name `%s' not recognized",
966 location
.segment
= seg
;
969 case D_EXTERN
: /* [EXTERN label:special] */
971 value
++; /* skip initial $ if present */
974 while (*q
&& *q
!= ':')
978 ofmt
->symdef(value
, 0L, 0L, 3, q
);
980 } else if (pass
== 1) { /* pass == 1 */
985 while (*q
&& *q
!= ':') {
991 report_error(ERR_NONFATAL
,
992 "identifier expected after EXTERN");
1000 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
1002 pass0
= 1; /* fake pass 1 in labels.c */
1003 declare_as_global(value
, special
,
1005 define_label(value
, seg_alloc(), 0L, NULL
,
1006 false, true, ofmt
, report_error
);
1009 } /* else pass0 == 1 */
1011 case D_BITS
: /* [BITS bits] */
1012 globalbits
= sb
= get_bits(value
);
1014 case D_GLOBAL
: /* [GLOBAL symbol:special] */
1016 value
++; /* skip initial $ if present */
1017 if (pass0
== 2) { /* pass 2 */
1019 while (*q
&& *q
!= ':')
1023 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1025 } else if (pass2
== 1) { /* pass == 1 */
1030 while (*q
&& *q
!= ':') {
1036 report_error(ERR_NONFATAL
,
1037 "identifier expected after GLOBAL");
1045 declare_as_global(value
, special
, report_error
);
1048 case D_COMMON
: /* [COMMON symbol size:special] */
1050 value
++; /* skip initial $ if present */
1056 while (*p
&& !isspace(*p
)) {
1062 report_error(ERR_NONFATAL
,
1063 "identifier expected after COMMON");
1069 while (*p
&& isspace(*p
))
1072 while (*q
&& *q
!= ':')
1079 size
= readnum(p
, &rn_error
);
1081 report_error(ERR_NONFATAL
,
1082 "invalid size specified"
1083 " in COMMON declaration");
1085 define_common(value
, seg_alloc(), size
,
1086 special
, ofmt
, report_error
);
1088 report_error(ERR_NONFATAL
,
1089 "no size specified in"
1090 " COMMON declaration");
1091 } else if (pass0
== 2) { /* pass == 2 */
1093 while (*q
&& *q
!= ':') {
1100 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1104 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1106 stdscan_bufptr
= value
;
1107 tokval
.t_type
= TOKEN_INVALID
;
1108 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1109 report_error
, NULL
);
1112 report_error(pass0
==
1113 1 ? ERR_NONFATAL
: ERR_PANIC
,
1114 "cannot use non-relocatable expression as "
1115 "ABSOLUTE address");
1117 abs_seg
= reloc_seg(e
);
1118 abs_offset
= reloc_value(e
);
1120 } else if (pass
== 1)
1121 abs_offset
= 0x100; /* don't go near zero in case of / */
1123 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1126 location
.segment
= NO_SEG
;
1128 case D_DEBUG
: /* [DEBUG] */
1134 while (*p
&& !isspace(*p
)) {
1141 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1142 "identifier expected after DEBUG");
1145 while (*p
&& isspace(*p
))
1147 if (pass
== pass_max
)
1148 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1150 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1152 while (*value
&& isspace(*value
))
1155 if (*value
== '+' || *value
== '-') {
1156 validid
= (*value
== '-') ? true : false;
1161 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1162 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1164 if (i
<= ERR_WARN_MAX
)
1165 suppressed
[i
] = validid
;
1167 report_error(ERR_NONFATAL
,
1168 "invalid warning id in WARNING directive");
1171 case D_CPU
: /* [CPU] */
1172 cpu
= get_cpu(value
);
1174 case D_LIST
: /* [LIST {+|-}] */
1175 while (*value
&& isspace(*value
))
1178 if (*value
== '+') {
1181 if (*value
== '-') {
1188 case D_DEFAULT
: /* [DEFAULT] */
1190 stdscan_bufptr
= value
;
1191 tokval
.t_type
= TOKEN_INVALID
;
1192 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1193 switch ((int)tokval
.t_integer
) {
1209 if (float_option(value
)) {
1210 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1211 "unknown 'float' directive: %s",
1216 if (!ofmt
->directive(directive
, value
, pass2
))
1217 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1218 "unrecognised directive [%s]",
1222 report_error(ERR_NONFATAL
,
1223 "invalid parameter to [%s] directive",
1226 } else { /* it isn't a directive */
1228 parse_line(pass1
, line
, &output_ins
,
1229 report_error
, evaluate
, def_label
);
1231 if (!(optimizing
> 0) && pass
== 2) {
1232 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1233 output_ins
.forw_ref
= true;
1235 output_ins
.oprs
[forwref
->operand
].opflags
|=
1237 forwref
= saa_rstruct(forwrefs
);
1238 } while (forwref
!= NULL
1239 && forwref
->lineno
== globallineno
);
1241 output_ins
.forw_ref
= false;
1244 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1246 for (i
= 0; i
< output_ins
.operands
; i
++) {
1247 if (output_ins
.oprs
[i
].
1248 opflags
& OPFLAG_FORWARD
) {
1249 struct forwrefinfo
*fwinf
=
1250 (struct forwrefinfo
*)
1251 saa_wstruct(forwrefs
);
1252 fwinf
->lineno
= globallineno
;
1256 } else { /* pass == 2 */
1258 * Hack to prevent phase error in the code
1262 * If the second operand is a forward reference,
1263 * the UNITY property of the number 1 in that
1264 * operand is cancelled. Otherwise the above
1265 * sequence will cause a phase error.
1267 * This hack means that the above code will
1268 * generate 286+ code.
1270 * The forward reference will mean that the
1271 * operand will not have the UNITY property on
1272 * the first pass, so the pass behaviours will
1276 if (output_ins
.operands
>= 2 &&
1277 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1278 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1280 /* Remove special properties bits */
1281 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1289 if (output_ins
.opcode
== I_EQU
) {
1292 * Special `..' EQUs get processed in pass two,
1293 * except `..@' macro-processor EQUs which are done
1294 * in the normal place.
1296 if (!output_ins
.label
)
1297 report_error(ERR_NONFATAL
,
1298 "EQU not preceded by label");
1300 else 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 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1308 opflags
& OPFLAG_EXTERN
;
1309 def_label(output_ins
.label
,
1310 output_ins
.oprs
[0].segment
,
1311 output_ins
.oprs
[0].offset
, NULL
,
1314 } else if (output_ins
.operands
== 2
1315 && (output_ins
.oprs
[0].
1317 && (output_ins
.oprs
[0].type
& COLON
)
1318 && output_ins
.oprs
[0].segment
==
1320 && output_ins
.oprs
[0].wrt
== NO_SEG
1321 && (output_ins
.oprs
[1].
1323 && output_ins
.oprs
[1].segment
==
1325 && output_ins
.oprs
[1].wrt
==
1327 def_label(output_ins
.label
,
1330 output_ins
.oprs
[1].offset
, NULL
,
1334 report_error(ERR_NONFATAL
,
1335 "bad syntax for EQU");
1337 } else { /* pass == 2 */
1339 * Special `..' EQUs get processed here, except
1340 * `..@' macro processor EQUs which are done above.
1342 if (output_ins
.label
[0] == '.' &&
1343 output_ins
.label
[1] == '.' &&
1344 output_ins
.label
[2] != '@') {
1345 if (output_ins
.operands
== 1 &&
1346 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1347 define_label(output_ins
.label
,
1348 output_ins
.oprs
[0].segment
,
1349 output_ins
.oprs
[0].offset
,
1350 NULL
, false, false, ofmt
,
1352 } else if (output_ins
.operands
== 2
1353 && (output_ins
.oprs
[0].
1355 && (output_ins
.oprs
[0].type
& COLON
)
1356 && output_ins
.oprs
[0].segment
==
1358 && (output_ins
.oprs
[1].
1360 && output_ins
.oprs
[1].segment
==
1362 define_label(output_ins
.label
,
1365 output_ins
.oprs
[1].offset
,
1366 NULL
, false, false, ofmt
,
1369 report_error(ERR_NONFATAL
,
1370 "bad syntax for EQU");
1373 } else { /* instruction isn't an EQU */
1377 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1378 &output_ins
, report_error
);
1380 /* if (using_debug_info) && output_ins.opcode != -1) */
1381 if (using_debug_info
)
1382 { /* fbk 03/25/01 */
1383 /* this is done here so we can do debug type info */
1385 TYS_ELEMENTS(output_ins
.operands
);
1386 switch (output_ins
.opcode
) {
1389 TYS_ELEMENTS(output_ins
.oprs
[0].
1394 TYS_ELEMENTS(output_ins
.oprs
[0].
1399 TYS_ELEMENTS(output_ins
.oprs
[0].
1404 TYS_ELEMENTS(output_ins
.oprs
[0].
1409 TYS_ELEMENTS(output_ins
.oprs
[0].
1413 typeinfo
|= TY_BYTE
;
1416 typeinfo
|= TY_WORD
;
1419 if (output_ins
.eops_float
)
1420 typeinfo
|= TY_FLOAT
;
1422 typeinfo
|= TY_DWORD
;
1425 typeinfo
|= TY_QWORD
;
1428 typeinfo
|= TY_TBYTE
;
1431 typeinfo
|= TY_OWORD
;
1434 typeinfo
= TY_LABEL
;
1438 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1443 SET_CURR_OFFS(offs
);
1446 * else l == -1 => invalid instruction, which will be
1447 * flagged as an error on pass 2
1450 } else { /* pass == 2 */
1451 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1452 &output_ins
, ofmt
, report_error
,
1454 SET_CURR_OFFS(offs
);
1458 cleanup_insn(&output_ins
);
1461 location
.offset
= offs
= GET_CURR_OFFS
;
1462 } /* end while (line = preproc->getline... */
1464 if (pass1
== 2 && global_offset_changed
)
1465 report_error(ERR_NONFATAL
,
1466 "phase error detected at end of assembly.");
1469 preproc
->cleanup(1);
1471 if (pass1
== 1 && terminate_after_phase
) {
1479 if (pass
> 1 && !global_offset_changed
) {
1482 pass
= pass_max
- 1;
1483 } else if (!(optimizing
> 0))
1486 } /* for (pass=1; pass<=2; pass++) */
1488 preproc
->cleanup(0);
1491 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1493 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1495 } /* exit from assemble_file (...) */
1497 static enum directives
getkw(char **directive
, char **value
)
1503 /* allow leading spaces or tabs */
1504 while (*buf
== ' ' || *buf
== '\t')
1512 while (*p
&& *p
!= ']')
1520 while (*p
&& *p
!= ';') {
1527 *directive
= p
= buf
+ 1;
1528 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1535 while (isspace(*buf
))
1536 buf
++; /* beppu - skip leading whitespace */
1543 return bsii(*directive
, directives
, elements(directives
));
1547 * gnu style error reporting
1548 * This function prints an error message to error_file in the
1549 * style used by GNU. An example would be:
1550 * file.asm:50: error: blah blah blah
1551 * where file.asm is the name of the file, 50 is the line number on
1552 * which the error occurs (or is detected) and "error:" is one of
1553 * the possible optional diagnostics -- it can be "error" or "warning"
1554 * or something else. Finally the line terminates with the actual
1557 * @param severity the severity of the warning or error
1558 * @param fmt the printf style format string
1560 static void report_error_gnu(int severity
, const char *fmt
, ...)
1564 if (is_suppressed_warning(severity
))
1567 if (severity
& ERR_NOFILE
)
1568 fputs("nasm: ", error_file
);
1570 char *currentfile
= NULL
;
1572 src_get(&lineno
, ¤tfile
);
1573 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1574 nasm_free(currentfile
);
1577 report_error_common(severity
, fmt
, ap
);
1582 * MS style error reporting
1583 * This function prints an error message to error_file in the
1584 * style used by Visual C and some other Microsoft tools. An example
1586 * file.asm(50) : error: blah blah blah
1587 * where file.asm is the name of the file, 50 is the line number on
1588 * which the error occurs (or is detected) and "error:" is one of
1589 * the possible optional diagnostics -- it can be "error" or "warning"
1590 * or something else. Finally the line terminates with the actual
1593 * @param severity the severity of the warning or error
1594 * @param fmt the printf style format string
1596 static void report_error_vc(int severity
, const char *fmt
, ...)
1600 if (is_suppressed_warning(severity
))
1603 if (severity
& ERR_NOFILE
)
1604 fputs("nasm: ", error_file
);
1606 char *currentfile
= NULL
;
1608 src_get(&lineno
, ¤tfile
);
1609 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1610 nasm_free(currentfile
);
1613 report_error_common(severity
, fmt
, ap
);
1618 * check for supressed warning
1619 * checks for suppressed warning or pass one only warning and we're
1622 * @param severity the severity of the warning or error
1623 * @return true if we should abort error/warning printing
1625 static int is_suppressed_warning(int severity
)
1628 * See if it's a suppressed warning.
1630 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1631 (severity
& ERR_WARN_MASK
) != 0 &&
1632 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1634 * See if it's a pass-one only warning and we're not in pass one.
1636 ((severity
& ERR_PASS1
) && pass0
== 2);
1640 * common error reporting
1641 * This is the common back end of the error reporting schemes currently
1642 * implemented. It prints the nature of the warning and then the
1643 * specific error message to error_file and may or may not return. It
1644 * doesn't return if the error severity is a "panic" or "debug" type.
1646 * @param severity the severity of the warning or error
1647 * @param fmt the printf style format string
1649 static void report_error_common(int severity
, const char *fmt
,
1652 switch (severity
& ERR_MASK
) {
1654 fputs("warning: ", error_file
);
1657 fputs("error: ", error_file
);
1660 fputs("fatal: ", error_file
);
1663 fputs("panic: ", error_file
);
1666 fputs("debug: ", error_file
);
1670 vfprintf(error_file
, fmt
, args
);
1671 putc('\n', error_file
);
1673 if (severity
& ERR_USAGE
)
1676 switch (severity
& ERR_MASK
) {
1678 /* no further action, by definition */
1681 if (!suppressed
[0]) /* Treat warnings as errors */
1682 terminate_after_phase
= true;
1685 terminate_after_phase
= true;
1694 exit(1); /* instantly die */
1695 break; /* placate silly compilers */
1698 /* abort(); *//* halt, catch fire, and dump core */
1704 static void usage(void)
1706 fputs("type `nasm -h' for help\n", error_file
);
1709 static void register_output_formats(void)
1711 ofmt
= ofmt_register(report_error
);
1714 #define BUF_DELTA 512
1716 static FILE *no_pp_fp
;
1717 static efunc no_pp_err
;
1718 static ListGen
*no_pp_list
;
1719 static int32_t no_pp_lineinc
;
1721 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1724 src_set_fname(nasm_strdup(file
));
1728 no_pp_fp
= fopen(file
, "r");
1730 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1731 "unable to open input file `%s'", file
);
1732 no_pp_list
= listgen
;
1733 (void)pass
; /* placate compilers */
1734 (void)eval
; /* placate compilers */
1737 static char *no_pp_getline(void)
1739 char *buffer
, *p
, *q
;
1742 bufsize
= BUF_DELTA
;
1743 buffer
= nasm_malloc(BUF_DELTA
);
1744 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1746 while (1) { /* Loop to handle %line */
1749 while (1) { /* Loop to handle long lines */
1750 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1754 if (p
> buffer
&& p
[-1] == '\n')
1756 if (p
- buffer
> bufsize
- 10) {
1758 offset
= p
- buffer
;
1759 bufsize
+= BUF_DELTA
;
1760 buffer
= nasm_realloc(buffer
, bufsize
);
1761 p
= buffer
+ offset
;
1765 if (!q
&& p
== buffer
) {
1771 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1772 * them are present at the end of the line.
1774 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1776 if (!nasm_strnicmp(buffer
, "%line", 5)) {
1779 char *nm
= nasm_malloc(strlen(buffer
));
1780 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1781 nasm_free(src_set_fname(nm
));
1791 no_pp_list
->line(LIST_READ
, buffer
);
1796 static void no_pp_cleanup(int pass
)
1798 (void)pass
; /* placate GCC */
1802 static uint32_t get_cpu(char *value
)
1804 if (!strcmp(value
, "8086"))
1806 if (!strcmp(value
, "186"))
1808 if (!strcmp(value
, "286"))
1810 if (!strcmp(value
, "386"))
1812 if (!strcmp(value
, "486"))
1814 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1816 if (!strcmp(value
, "686") ||
1817 !nasm_stricmp(value
, "ppro") ||
1818 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1820 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1822 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1823 !nasm_stricmp(value
, "willamette"))
1824 return IF_WILLAMETTE
;
1825 if (!nasm_stricmp(value
, "prescott"))
1827 if (!nasm_stricmp(value
, "x64") ||
1828 !nasm_stricmp(value
, "x86-64"))
1830 if (!nasm_stricmp(value
, "ia64") ||
1831 !nasm_stricmp(value
, "ia-64") ||
1832 !nasm_stricmp(value
, "itanium") ||
1833 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1836 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1837 "unknown 'cpu' type");
1839 return IF_PLEVEL
; /* the maximum level */
1842 static int get_bits(char *value
)
1846 if ((i
= atoi(value
)) == 16)
1847 return i
; /* set for a 16-bit segment */
1850 report_error(ERR_NONFATAL
,
1851 "cannot specify 32-bit segment on processor below a 386");
1854 } else if (i
== 64) {
1855 if (cpu
< IF_X86_64
) {
1856 report_error(ERR_NONFATAL
,
1857 "cannot specify 64-bit segment on processor below an x86-64");
1861 report_error(ERR_NONFATAL
,
1862 "%s output format does not support 64-bit code",
1867 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1868 "`%s' is not a valid segment size; must be 16, 32 or 64",