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 license given in the file "LICENSE"
6 * distributed in the NASM archive.
35 struct forwrefinfo
{ /* info held on forward refs. */
40 static int get_bits(char *value
);
41 static uint32_t get_cpu(char *cpu_str
);
42 static void parse_cmdline(int, char **);
43 static void assemble_file(char *, StrList
**);
44 static void register_output_formats(void);
45 static void report_error_gnu(int severity
, const char *fmt
, ...);
46 static void report_error_vc(int severity
, const char *fmt
, ...);
47 static void report_error_common(int severity
, const char *fmt
,
49 static bool is_suppressed_warning(int severity
);
50 static void usage(void);
51 static efunc report_error
;
53 static int using_debug_info
, opt_verbose_info
;
54 bool tasm_compatible_mode
= false;
59 time_t official_compile_time
;
61 static char inname
[FILENAME_MAX
];
62 static char outname
[FILENAME_MAX
];
63 static char listname
[FILENAME_MAX
];
64 static char errname
[FILENAME_MAX
];
65 static int globallineno
; /* for forward-reference tracking */
66 /* static int pass = 0; */
67 static struct ofmt
*ofmt
= NULL
;
69 static FILE *error_file
; /* Where to write error messages */
71 static FILE *ofile
= NULL
;
72 int optimizing
= -1; /* number of optimization passes to take */
73 static int sb
, cmd_sb
= 16; /* by default */
74 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
75 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
76 bool global_offset_changed
; /* referenced in labels.c */
78 static struct location location
;
79 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
80 int32_t abs_seg
; /* ABSOLUTE segment basis */
81 int32_t abs_offset
; /* ABSOLUTE offset */
83 static struct RAA
*offsets
;
85 static struct SAA
*forwrefs
; /* keep track of forward references */
86 static const struct forwrefinfo
*forwref
;
88 static Preproc
*preproc
;
90 op_normal
, /* Preprocess and assemble */
91 op_preprocess
, /* Preprocess only */
92 op_depend
, /* Generate dependencies */
94 static enum op_type operating_mode
;
95 /* Dependency flags */
96 static bool depend_emit_phony
= false;
97 static bool depend_missing_ok
= false;
98 static const char *depend_target
= NULL
;
99 static const char *depend_file
= NULL
;
102 * Which of the suppressible warnings are suppressed. Entry zero
103 * isn't an actual warning, but it used for -w+error/-Werror.
105 static bool suppressed
[ERR_WARN_MAX
+1];
107 static bool suppressed_global
[ERR_WARN_MAX
+1] = {
108 true, false, true, false, false, false, true, false, true, true, false
111 * The option names for the suppressible warnings. As before, entry
114 static const char *suppressed_names
[ERR_WARN_MAX
+1] = {
115 "error", "macro-params", "macro-selfref", "macro-defaults",
116 "orphan-labels", "number-overflow", "gnu-elf-extensions",
117 "float-overflow", "float-denorm", "float-underflow", "float-toolong"
121 * The explanations for the suppressible warnings. As before, entry
124 static const char *suppressed_what
[ERR_WARN_MAX
+1] = {
125 "treat warnings as errors",
126 "macro calls with wrong parameter count",
127 "cyclic macro references",
128 "macros with more default than optional parameters",
129 "labels alone on lines without trailing `:'",
130 "numeric constants does not fit in 64 bits",
131 "using 8- or 16-bit relocation in ELF32, a GNU extension",
132 "floating point overflow",
133 "floating point denormal",
134 "floating point underflow",
135 "too many digits in floating-point number"
139 * This is a null preprocessor which just copies lines from input
140 * to output. It's used when someone explicitly requests that NASM
141 * not preprocess their source file.
144 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*, StrList
**);
145 static char *no_pp_getline(void);
146 static void no_pp_cleanup(int);
147 static Preproc no_pp
= {
154 * get/set current offset...
156 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
157 raa_read(offsets,location.segment))
158 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
159 (void)(offsets=raa_write(offsets,location.segment,(x))))
161 static int want_usage
;
162 static int terminate_after_phase
;
163 int user_nolist
= 0; /* fbk 9/2/00 */
165 static void nasm_fputs(const char *line
, FILE * outfile
)
168 fputs(line
, outfile
);
174 /* Convert a struct tm to a POSIX-style time constant */
175 static int64_t posix_mktime(struct tm
*tm
)
178 int64_t y
= tm
->tm_year
;
180 /* See IEEE 1003.1:2004, section 4.14 */
182 t
= (y
-70)*365 + (y
-69)/4 - (y
-1)/100 + (y
+299)/400;
194 static void define_macros_early(void)
197 struct tm lt
, *lt_p
, gm
, *gm_p
;
200 lt_p
= localtime(&official_compile_time
);
204 strftime(temp
, sizeof temp
, "__DATE__=\"%Y-%m-%d\"", <
);
206 strftime(temp
, sizeof temp
, "__DATE_NUM__=%Y%m%d", <
);
208 strftime(temp
, sizeof temp
, "__TIME__=\"%H:%M:%S\"", <
);
210 strftime(temp
, sizeof temp
, "__TIME_NUM__=%H%M%S", <
);
214 gm_p
= gmtime(&official_compile_time
);
218 strftime(temp
, sizeof temp
, "__UTC_DATE__=\"%Y-%m-%d\"", &gm
);
220 strftime(temp
, sizeof temp
, "__UTC_DATE_NUM__=%Y%m%d", &gm
);
222 strftime(temp
, sizeof temp
, "__UTC_TIME__=\"%H:%M:%S\"", &gm
);
224 strftime(temp
, sizeof temp
, "__UTC_TIME_NUM__=%H%M%S", &gm
);
229 posix_time
= posix_mktime(&gm
);
231 posix_time
= posix_mktime(<
);
236 snprintf(temp
, sizeof temp
, "__POSIX_TIME__=%"PRId64
, posix_time
);
241 static void define_macros_late(void)
245 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
250 static void emit_dependencies(StrList
*list
)
256 if (depend_file
&& strcmp(depend_file
, "-")) {
257 deps
= fopen(depend_file
, "w");
259 report_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
260 "unable to write dependency file `%s'", depend_file
);
267 linepos
= fprintf(deps
, "%s:", depend_target
);
268 for (l
= list
; l
; l
= l
->next
) {
269 len
= strlen(l
->str
);
270 if (linepos
+ len
> 62) {
271 fprintf(deps
, " \\\n ");
274 fprintf(deps
, " %s", l
->str
);
277 fprintf(deps
, "\n\n");
279 for (l
= list
; l
; l
= nl
) {
280 if (depend_emit_phony
)
281 fprintf(deps
, "%s:\n\n", l
->str
);
291 int main(int argc
, char **argv
)
293 StrList
*depend_list
= NULL
, **depend_ptr
;
295 time(&official_compile_time
);
298 want_usage
= terminate_after_phase
= false;
299 report_error
= report_error_gnu
;
305 nasm_set_malloc_error(report_error
);
306 offsets
= raa_init();
307 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
310 operating_mode
= op_normal
;
314 register_output_formats();
316 /* Define some macros dependent on the runtime, but not
317 on the command line. */
318 define_macros_early();
320 parse_cmdline(argc
, argv
);
322 if (terminate_after_phase
) {
328 /* If debugging info is disabled, suppress any debug calls */
329 if (!using_debug_info
)
330 ofmt
->current_dfmt
= &null_debug_form
;
333 pp_extra_stdmac(ofmt
->stdmac
);
334 parser_global_info(ofmt
, &location
);
335 eval_global_info(ofmt
, lookup_label
, &location
);
337 /* define some macros dependent of command-line */
338 define_macros_late();
340 depend_ptr
= (depend_file
|| (operating_mode
== op_depend
))
341 ? &depend_list
: NULL
;
343 depend_target
= outname
;
345 switch (operating_mode
) {
350 if (depend_missing_ok
)
351 pp_include_path(NULL
); /* "assume generated" */
353 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
,
355 if (outname
[0] == '\0')
356 ofmt
->filename(inname
, outname
, report_error
);
358 while ((line
= preproc
->getline()))
367 char *file_name
= NULL
;
368 int32_t prior_linnum
= 0;
372 ofile
= fopen(outname
, "w");
374 report_error(ERR_FATAL
| ERR_NOFILE
,
375 "unable to open output file `%s'",
380 location
.known
= false;
383 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
,
386 while ((line
= preproc
->getline())) {
388 * We generate %line directives if needed for later programs
390 int32_t linnum
= prior_linnum
+= lineinc
;
391 int altline
= src_get(&linnum
, &file_name
);
393 if (altline
== 1 && lineinc
== 1)
394 nasm_fputs("", ofile
);
396 lineinc
= (altline
!= -1 || lineinc
!= 1);
397 fprintf(ofile
? ofile
: stdout
,
398 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
401 prior_linnum
= linnum
;
403 nasm_fputs(line
, ofile
);
406 nasm_free(file_name
);
410 if (ofile
&& terminate_after_phase
)
418 * We must call ofmt->filename _anyway_, even if the user
419 * has specified their own output file, because some
420 * formats (eg OBJ and COFF) use ofmt->filename to find out
421 * the name of the input file and then put that inside the
424 ofmt
->filename(inname
, outname
, report_error
);
426 ofile
= fopen(outname
, "wb");
428 report_error(ERR_FATAL
| ERR_NOFILE
,
429 "unable to open output file `%s'", outname
);
433 * We must call init_labels() before ofmt->init() since
434 * some object formats will want to define labels in their
435 * init routines. (eg OS/2 defines the FLAT group)
439 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
441 assemble_file(inname
, depend_ptr
);
443 if (!terminate_after_phase
) {
444 ofmt
->cleanup(using_debug_info
);
448 * Despite earlier comments, we need this fclose.
449 * The object output drivers only fclose on cleanup,
450 * and we just skipped that.
463 emit_dependencies(depend_list
);
473 if (terminate_after_phase
)
480 * Get a parameter for a command line option.
481 * First arg must be in the form of e.g. -f...
483 static char *get_param(char *p
, char *q
, bool *advance
)
486 if (p
[2]) { /* the parameter's in the option */
488 while (nasm_isspace(*p
))
496 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
497 "option `-%c' requires an argument", p
[1]);
504 static void copy_filename(char *dst
, const char *src
)
506 size_t len
= strlen(src
);
508 if (len
>= (size_t)FILENAME_MAX
) {
509 report_error(ERR_FATAL
| ERR_NOFILE
, "file name too long");
512 strncpy(dst
, src
, FILENAME_MAX
);
516 * Convert a string to Make-safe form
518 static char *quote_for_make(const char *str
)
523 size_t n
= 1; /* Terminating zero */
529 for (p
= str
; *p
; p
++) {
533 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
553 /* Convert N backslashes at the end of filename to 2N backslashes */
557 os
= q
= nasm_malloc(n
);
560 for (p
= str
; *p
; p
++) {
603 #define OPT_POSTFIX 1
604 struct textargs textopts
[] = {
605 {"prefix", OPT_PREFIX
},
606 {"postfix", OPT_POSTFIX
},
610 static bool stopoptions
= false;
611 static bool process_arg(char *p
, char *q
)
615 bool advance
= false;
621 if (p
[0] == '-' && !stopoptions
) {
622 if (strchr("oOfpPdDiIlFXuUZwW", p
[1])) {
623 /* These parameters take values */
624 if (!(param
= get_param(p
, q
, &advance
)))
633 case 'o': /* output file */
634 copy_filename(outname
, param
);
637 case 'f': /* output format */
638 ofmt
= ofmt_find(param
);
640 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
641 "unrecognised output format `%s' - "
642 "use -hf for a list", param
);
644 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
648 case 'O': /* Optimization level */
653 /* Naked -O == -Ox */
654 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
658 case '0': case '1': case '2': case '3': case '4':
659 case '5': case '6': case '7': case '8': case '9':
660 opt
= strtoul(param
, ¶m
, 10);
662 /* -O0 -> optimizing == -1, 0.98 behaviour */
663 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
665 optimizing
= opt
- 1;
673 opt_verbose_info
= true;
678 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
682 report_error(ERR_FATAL
,
683 "unknown optimization option -O%c\n",
692 case 'p': /* pre-include */
694 pp_pre_include(param
);
697 case 'd': /* pre-define */
699 pp_pre_define(param
);
702 case 'u': /* un-define */
704 pp_pre_undefine(param
);
707 case 'i': /* include search path */
709 pp_include_path(param
);
712 case 'l': /* listing file */
713 copy_filename(listname
, param
);
716 case 'Z': /* error messages file */
717 strcpy(errname
, param
);
720 case 'F': /* specify debug format */
721 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
722 if (!ofmt
->current_dfmt
) {
723 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
724 "unrecognized debug format `%s' for"
725 " output format `%s'",
726 param
, ofmt
->shortname
);
728 using_debug_info
= true;
731 case 'X': /* specify error reporting format */
732 if (nasm_stricmp("vc", param
) == 0)
733 report_error
= report_error_vc
;
734 else if (nasm_stricmp("gnu", param
) == 0)
735 report_error
= report_error_gnu
;
737 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
738 "unrecognized error reporting format `%s'",
743 using_debug_info
= true;
748 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
750 " [options...] [--] filename\n"
751 " or nasm -v for version info\n\n"
752 " -t assemble in SciTech TASM compatible mode\n"
753 " -g generate debug information in selected format.\n");
755 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
756 " -a don't preprocess (assemble only)\n"
757 " -M generate Makefile dependencies on stdout\n"
758 " -MG d:o, missing files assumed generated\n\n"
759 " -Z<file> redirect error messages to file\n"
760 " -s redirect error messages to stdout\n\n"
761 " -F format select a debugging format\n\n"
762 " -I<path> adds a pathname to the include file path\n");
764 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
765 " -P<file> pre-includes a file\n"
766 " -D<macro>[=<value>] pre-defines a macro\n"
767 " -U<macro> undefines a macro\n"
768 " -X<format> specifies error reporting format (gnu or vc)\n"
769 " -w+foo enables warning foo (equiv. -Wfoo)\n"
770 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
772 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
773 printf(" %-23s %s (default %s)\n",
774 suppressed_names
[i
], suppressed_what
[i
],
775 suppressed_global
[i
] ? "off" : "on");
777 ("\nresponse files should contain command line parameters"
778 ", one per line.\n");
780 printf("\nvalid output formats for -f are"
781 " (`*' denotes default):\n");
782 ofmt_list(ofmt
, stdout
);
784 printf("\nFor a list of valid output formats, use -hf.\n");
785 printf("For a list of debug formats, use -f <form> -y.\n");
787 exit(0); /* never need usage message here */
791 printf("\nvalid debug formats for '%s' output format are"
792 " ('*' denotes default):\n", ofmt
->shortname
);
793 dfmt_list(ofmt
, stdout
);
798 tasm_compatible_mode
= true;
803 const char *nasm_version_string
=
804 "NASM version " NASM_VER
" compiled on " __DATE__
809 puts(nasm_version_string
);
810 exit(0); /* never need usage message here */
814 case 'e': /* preprocess only */
816 operating_mode
= op_preprocess
;
819 case 'a': /* assemble only - don't preprocess */
824 if (param
[0] == 'n' && param
[1] == 'o' && param
[2] == '-') {
833 if (param
[0] != '+' && param
[0] != '-') {
834 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
835 "invalid option to `-w'");
838 suppress
= (param
[0] == '-');
842 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
843 if (!nasm_stricmp(param
, suppressed_names
[i
]))
845 if (i
<= ERR_WARN_MAX
)
846 suppressed_global
[i
] = suppress
;
847 else if (!nasm_stricmp(param
, "all"))
848 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
849 suppressed_global
[i
] = suppress
;
850 else if (!nasm_stricmp(param
, "none"))
851 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
852 suppressed_global
[i
] = !suppress
;
854 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
855 "invalid warning `%s'", param
);
861 operating_mode
= op_depend
;
864 operating_mode
= op_depend
;
865 depend_missing_ok
= true;
868 depend_emit_phony
= true;
879 depend_target
= quote_for_make(q
);
883 report_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
884 "unknown dependency option `-M%c'", p
[2]);
887 if (advance
&& (!q
|| !q
[0])) {
888 report_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
889 "option `-M%c' requires a parameter", p
[2]);
898 if (p
[2] == 0) { /* -- => stop processing options */
902 for (s
= 0; textopts
[s
].label
; s
++) {
903 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
914 report_error(ERR_NONFATAL
| ERR_NOFILE
|
916 "option `--%s' requires an argument",
920 advance
= 1, param
= q
;
923 if (s
== OPT_PREFIX
) {
924 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
925 lprefix
[PREFIX_MAX
- 1] = 0;
928 if (s
== OPT_POSTFIX
) {
929 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
930 lpostfix
[POSTFIX_MAX
- 1] = 0;
937 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
938 "unrecognised option `--%s'", p
+ 2);
946 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
947 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
948 "unrecognised option `-%c'", p
[1]);
953 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
954 "more than one input file specified");
956 copy_filename(inname
, p
);
963 #define ARG_BUF_DELTA 128
965 static void process_respfile(FILE * rfile
)
967 char *buffer
, *p
, *q
, *prevarg
;
968 int bufsize
, prevargsize
;
970 bufsize
= prevargsize
= ARG_BUF_DELTA
;
971 buffer
= nasm_malloc(ARG_BUF_DELTA
);
972 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
975 while (1) { /* Loop to handle all lines in file */
977 while (1) { /* Loop to handle long lines */
978 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
982 if (p
> buffer
&& p
[-1] == '\n')
984 if (p
- buffer
> bufsize
- 10) {
987 bufsize
+= ARG_BUF_DELTA
;
988 buffer
= nasm_realloc(buffer
, bufsize
);
993 if (!q
&& p
== buffer
) {
995 process_arg(prevarg
, NULL
);
1002 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1003 * them are present at the end of the line.
1005 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
1007 while (p
> buffer
&& nasm_isspace(p
[-1]))
1011 while (nasm_isspace(*p
))
1014 if (process_arg(prevarg
, p
))
1017 if ((int) strlen(p
) > prevargsize
- 10) {
1018 prevargsize
+= ARG_BUF_DELTA
;
1019 prevarg
= nasm_realloc(prevarg
, prevargsize
);
1021 strncpy(prevarg
, p
, prevargsize
);
1025 /* Function to process args from a string of args, rather than the
1026 * argv array. Used by the environment variable and response file
1029 static void process_args(char *args
)
1031 char *p
, *q
, *arg
, *prevarg
;
1032 char separator
= ' ';
1035 if (*p
&& *p
!= '-')
1040 while (*p
&& *p
!= separator
)
1042 while (*p
== separator
)
1046 if (process_arg(prevarg
, arg
))
1050 process_arg(arg
, NULL
);
1053 static void process_response_file(const char *file
)
1056 FILE *f
= fopen(file
, "r");
1061 while (fgets(str
, sizeof str
, f
)) {
1067 static void parse_cmdline(int argc
, char **argv
)
1070 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
1072 *inname
= *outname
= *listname
= *errname
= '\0';
1075 * First, process the NASMENV environment variable.
1077 envreal
= getenv("NASMENV");
1080 envcopy
= nasm_strdup(envreal
);
1081 process_args(envcopy
);
1086 * Now process the actual command line.
1091 if (argv
[0][0] == '@') {
1092 /* We have a response file, so process this as a set of
1093 * arguments like the environment variable. This allows us
1094 * to have multiple arguments on a single line, which is
1095 * different to the -@resp file processing below for regular
1098 process_response_file(argv
[0]+1);
1102 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
1103 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &advance
);
1105 rfile
= fopen(p
, "r");
1107 process_respfile(rfile
);
1110 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
1111 "unable to open response file `%s'", p
);
1114 advance
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
1115 argv
+= advance
, argc
-= advance
;
1118 /* Look for basic command line typos. This definitely doesn't
1119 catch all errors, but it might help cases of fumbled fingers. */
1121 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
1122 "no input file specified");
1123 else if (!strcmp(inname
, errname
) ||
1124 !strcmp(inname
, outname
) ||
1125 !strcmp(inname
, listname
) ||
1126 (depend_file
&& !strcmp(inname
, depend_file
)))
1127 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
1128 "file `%s' is both input and output file",
1132 error_file
= fopen(errname
, "w");
1134 error_file
= stderr
; /* Revert to default! */
1135 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
1136 "cannot open file `%s' for error messages",
1142 /* List of directives */
1144 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
1145 D_EXTERN
, D_FLOAT
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
1147 static const char *directives
[] = {
1148 "", "absolute", "bits", "common", "cpu", "debug", "default",
1149 "extern", "float", "global", "list", "section", "segment", "warning"
1151 static enum directives
getkw(char **directive
, char **value
);
1153 static void assemble_file(char *fname
, StrList
**depend_ptr
)
1155 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
1161 struct tokenval tokval
;
1165 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
1166 report_error(ERR_FATAL
, "command line: "
1167 "32-bit segment size requires a higher cpu");
1169 pass_max
= 1000; /* Always terminate in a reasonable time */
1170 /* No real program should need this many passes */
1172 for (passn
= 1; pass0
<= 2; passn
++) {
1176 pass1
= pass0
== 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
1177 pass2
= passn
> 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1178 /* pass0 0, 0, 0, ..., 1, 2 */
1180 def_label
= passn
> 1 ? redefine_label
: define_label
;
1182 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
1186 nasmlist
.init(listname
, report_error
);
1189 global_offset_changed
= false; /* set by redefine_label */
1190 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
1193 saa_rewind(forwrefs
);
1194 forwref
= saa_rstruct(forwrefs
);
1196 offsets
= raa_init();
1198 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
,
1199 pass1
== 2 ? depend_ptr
: NULL
);
1200 memcpy(suppressed
, suppressed_global
, (ERR_WARN_MAX
+1) * sizeof(bool));
1204 location
.known
= true;
1205 location
.offset
= offs
= GET_CURR_OFFS
;
1207 while ((line
= preproc
->getline())) {
1211 /* here we parse our directives; this is not handled by the 'real'
1214 d
= getkw(&directive
, &value
);
1219 case D_SEGMENT
: /* [SEGMENT n] */
1221 seg
= ofmt
->section(value
, pass2
, &sb
);
1222 if (seg
== NO_SEG
) {
1223 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1224 "segment name `%s' not recognized",
1228 location
.segment
= seg
;
1231 case D_EXTERN
: /* [EXTERN label:special] */
1233 value
++; /* skip initial $ if present */
1236 while (*q
&& *q
!= ':')
1240 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1242 } else if (passn
== 1) {
1247 while (*q
&& *q
!= ':') {
1253 report_error(ERR_NONFATAL
,
1254 "identifier expected after EXTERN");
1262 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
1264 pass0
= 1; /* fake pass 1 in labels.c */
1265 declare_as_global(value
, special
,
1267 define_label(value
, seg_alloc(), 0L, NULL
,
1268 false, true, ofmt
, report_error
);
1271 } /* else pass0 == 1 */
1273 case D_BITS
: /* [BITS bits] */
1274 globalbits
= sb
= get_bits(value
);
1276 case D_GLOBAL
: /* [GLOBAL symbol:special] */
1278 value
++; /* skip initial $ if present */
1279 if (pass0
== 2) { /* pass 2 */
1281 while (*q
&& *q
!= ':')
1285 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1287 } else if (pass2
== 1) { /* pass == 1 */
1292 while (*q
&& *q
!= ':') {
1298 report_error(ERR_NONFATAL
,
1299 "identifier expected after GLOBAL");
1307 declare_as_global(value
, special
, report_error
);
1310 case D_COMMON
: /* [COMMON symbol size:special] */
1312 value
++; /* skip initial $ if present */
1318 while (*p
&& !nasm_isspace(*p
)) {
1324 report_error(ERR_NONFATAL
,
1325 "identifier expected after COMMON");
1331 while (*p
&& nasm_isspace(*p
))
1334 while (*q
&& *q
!= ':')
1341 size
= readnum(p
, &rn_error
);
1343 report_error(ERR_NONFATAL
,
1344 "invalid size specified"
1345 " in COMMON declaration");
1347 define_common(value
, seg_alloc(), size
,
1348 special
, ofmt
, report_error
);
1350 report_error(ERR_NONFATAL
,
1351 "no size specified in"
1352 " COMMON declaration");
1353 } else if (pass0
== 2) { /* pass == 2 */
1355 while (*q
&& *q
!= ':') {
1356 if (nasm_isspace(*q
))
1362 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1366 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1368 stdscan_bufptr
= value
;
1369 tokval
.t_type
= TOKEN_INVALID
;
1370 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1371 report_error
, NULL
);
1374 report_error(pass0
==
1375 1 ? ERR_NONFATAL
: ERR_PANIC
,
1376 "cannot use non-relocatable expression as "
1377 "ABSOLUTE address");
1379 abs_seg
= reloc_seg(e
);
1380 abs_offset
= reloc_value(e
);
1382 } else if (passn
== 1)
1383 abs_offset
= 0x100; /* don't go near zero in case of / */
1385 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1388 location
.segment
= NO_SEG
;
1390 case D_DEBUG
: /* [DEBUG] */
1396 while (*p
&& !nasm_isspace(*p
)) {
1403 report_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1404 "identifier expected after DEBUG");
1407 while (*p
&& nasm_isspace(*p
))
1410 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1412 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
1414 while (*value
&& nasm_isspace(*value
))
1418 case '-': validid
= 0; value
++; break;
1419 case '+': validid
= 1; value
++; break;
1420 case '*': validid
= 2; value
++; break;
1422 * Should this error out?
1423 * I'll keep it so nothing breaks.
1428 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1429 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1431 if (i
<= ERR_WARN_MAX
) {
1433 case 0: suppressed
[i
] = true; break;
1434 case 1: suppressed
[i
] = false; break;
1435 case 2: suppressed
[i
] = suppressed_global
[i
];
1440 report_error(ERR_NONFATAL
,
1441 "invalid warning id in WARNING directive");
1444 case D_CPU
: /* [CPU] */
1445 cpu
= get_cpu(value
);
1447 case D_LIST
: /* [LIST {+|-}] */
1448 while (*value
&& nasm_isspace(*value
))
1451 if (*value
== '+') {
1454 if (*value
== '-') {
1461 case D_DEFAULT
: /* [DEFAULT] */
1463 stdscan_bufptr
= value
;
1464 tokval
.t_type
= TOKEN_INVALID
;
1465 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1466 switch ((int)tokval
.t_integer
) {
1482 if (float_option(value
)) {
1483 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1484 "unknown 'float' directive: %s",
1489 if (!ofmt
->directive(directive
, value
, pass2
))
1490 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1491 "unrecognised directive [%s]",
1495 report_error(ERR_NONFATAL
,
1496 "invalid parameter to [%s] directive",
1499 } else { /* it isn't a directive */
1501 parse_line(pass1
, line
, &output_ins
,
1502 report_error
, evaluate
, def_label
);
1504 if (optimizing
> 0) {
1505 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1506 output_ins
.forw_ref
= true;
1508 output_ins
.oprs
[forwref
->operand
].opflags
|=
1510 forwref
= saa_rstruct(forwrefs
);
1511 } while (forwref
!= NULL
1512 && forwref
->lineno
== globallineno
);
1514 output_ins
.forw_ref
= false;
1517 if (optimizing
> 0) {
1519 for (i
= 0; i
< output_ins
.operands
; i
++) {
1520 if (output_ins
.oprs
[i
].
1521 opflags
& OPFLAG_FORWARD
) {
1522 struct forwrefinfo
*fwinf
=
1523 (struct forwrefinfo
*)
1524 saa_wstruct(forwrefs
);
1525 fwinf
->lineno
= globallineno
;
1533 if (output_ins
.opcode
== I_EQU
) {
1536 * Special `..' EQUs get processed in pass two,
1537 * except `..@' macro-processor EQUs which are done
1538 * in the normal place.
1540 if (!output_ins
.label
)
1541 report_error(ERR_NONFATAL
,
1542 "EQU not preceded by label");
1544 else if (output_ins
.label
[0] != '.' ||
1545 output_ins
.label
[1] != '.' ||
1546 output_ins
.label
[2] == '@') {
1547 if (output_ins
.operands
== 1 &&
1548 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1549 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1552 opflags
& OPFLAG_EXTERN
;
1553 def_label(output_ins
.label
,
1554 output_ins
.oprs
[0].segment
,
1555 output_ins
.oprs
[0].offset
, NULL
,
1558 } else if (output_ins
.operands
== 2
1559 && (output_ins
.oprs
[0].
1561 && (output_ins
.oprs
[0].type
& COLON
)
1562 && output_ins
.oprs
[0].segment
==
1564 && output_ins
.oprs
[0].wrt
== NO_SEG
1565 && (output_ins
.oprs
[1].
1567 && output_ins
.oprs
[1].segment
==
1569 && output_ins
.oprs
[1].wrt
==
1571 def_label(output_ins
.label
,
1574 output_ins
.oprs
[1].offset
, NULL
,
1578 report_error(ERR_NONFATAL
,
1579 "bad syntax for EQU");
1583 * Special `..' EQUs get processed here, except
1584 * `..@' macro processor EQUs which are done above.
1586 if (output_ins
.label
[0] == '.' &&
1587 output_ins
.label
[1] == '.' &&
1588 output_ins
.label
[2] != '@') {
1589 if (output_ins
.operands
== 1 &&
1590 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1591 define_label(output_ins
.label
,
1592 output_ins
.oprs
[0].segment
,
1593 output_ins
.oprs
[0].offset
,
1594 NULL
, false, false, ofmt
,
1596 } else if (output_ins
.operands
== 2
1597 && (output_ins
.oprs
[0].
1599 && (output_ins
.oprs
[0].type
& COLON
)
1600 && output_ins
.oprs
[0].segment
==
1602 && (output_ins
.oprs
[1].
1604 && output_ins
.oprs
[1].segment
==
1606 define_label(output_ins
.label
,
1609 output_ins
.oprs
[1].offset
,
1610 NULL
, false, false, ofmt
,
1613 report_error(ERR_NONFATAL
,
1614 "bad syntax for EQU");
1617 } else { /* instruction isn't an EQU */
1621 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1622 &output_ins
, report_error
);
1624 /* if (using_debug_info) && output_ins.opcode != -1) */
1625 if (using_debug_info
)
1626 { /* fbk 03/25/01 */
1627 /* this is done here so we can do debug type info */
1629 TYS_ELEMENTS(output_ins
.operands
);
1630 switch (output_ins
.opcode
) {
1633 TYS_ELEMENTS(output_ins
.oprs
[0].
1638 TYS_ELEMENTS(output_ins
.oprs
[0].
1643 TYS_ELEMENTS(output_ins
.oprs
[0].
1648 TYS_ELEMENTS(output_ins
.oprs
[0].
1653 TYS_ELEMENTS(output_ins
.oprs
[0].
1658 TYS_ELEMENTS(output_ins
.oprs
[0].
1663 TYS_ELEMENTS(output_ins
.oprs
[0].
1667 typeinfo
|= TY_BYTE
;
1670 typeinfo
|= TY_WORD
;
1673 if (output_ins
.eops_float
)
1674 typeinfo
|= TY_FLOAT
;
1676 typeinfo
|= TY_DWORD
;
1679 typeinfo
|= TY_QWORD
;
1682 typeinfo
|= TY_TBYTE
;
1685 typeinfo
|= TY_OWORD
;
1688 typeinfo
|= TY_YWORD
;
1691 typeinfo
= TY_LABEL
;
1695 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1700 SET_CURR_OFFS(offs
);
1703 * else l == -1 => invalid instruction, which will be
1704 * flagged as an error on pass 2
1708 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1709 &output_ins
, ofmt
, report_error
,
1711 SET_CURR_OFFS(offs
);
1715 cleanup_insn(&output_ins
);
1718 location
.offset
= offs
= GET_CURR_OFFS
;
1719 } /* end while (line = preproc->getline... */
1721 if (pass1
== 2 && global_offset_changed
)
1722 report_error(ERR_NONFATAL
,
1723 "phase error detected at end of assembly.");
1726 preproc
->cleanup(1);
1728 if (pass1
== 1 && terminate_after_phase
) {
1735 if (passn
> 1 && !global_offset_changed
)
1738 if(passn
>= pass_max
)
1739 /* We get here if the labels don't converge
1740 * Example: FOO equ FOO + 1
1742 report_error(ERR_NONFATAL
,
1743 "Can't find valid values for all labels "
1744 "after %d passes, giving up. "
1745 "Possible cause: recursive equ's.", passn
);
1748 preproc
->cleanup(0);
1751 if (opt_verbose_info
) /* -On and -Ov switches */
1753 "info:: assembly required 1+%d+1 passes\n", passn
-3);
1755 } /* exit from assemble_file (...) */
1757 static enum directives
getkw(char **directive
, char **value
)
1763 /* allow leading spaces or tabs */
1764 while (*buf
== ' ' || *buf
== '\t')
1772 while (*p
&& *p
!= ']')
1780 while (*p
&& *p
!= ';') {
1781 if (!nasm_isspace(*p
))
1787 *directive
= p
= buf
+ 1;
1788 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1795 while (nasm_isspace(*buf
))
1796 buf
++; /* beppu - skip leading whitespace */
1803 return bsii(*directive
, directives
, elements(directives
));
1807 * gnu style error reporting
1808 * This function prints an error message to error_file in the
1809 * style used by GNU. An example would be:
1810 * file.asm:50: error: blah blah blah
1811 * where file.asm is the name of the file, 50 is the line number on
1812 * which the error occurs (or is detected) and "error:" is one of
1813 * the possible optional diagnostics -- it can be "error" or "warning"
1814 * or something else. Finally the line terminates with the actual
1817 * @param severity the severity of the warning or error
1818 * @param fmt the printf style format string
1820 static void report_error_gnu(int severity
, const char *fmt
, ...)
1824 if (is_suppressed_warning(severity
))
1827 if (severity
& ERR_NOFILE
)
1828 fputs("nasm: ", error_file
);
1830 char *currentfile
= NULL
;
1832 src_get(&lineno
, ¤tfile
);
1833 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1834 nasm_free(currentfile
);
1837 report_error_common(severity
, fmt
, ap
);
1842 * MS style error reporting
1843 * This function prints an error message to error_file in the
1844 * style used by Visual C and some other Microsoft tools. An example
1846 * file.asm(50) : error: blah blah blah
1847 * where file.asm is the name of the file, 50 is the line number on
1848 * which the error occurs (or is detected) and "error:" is one of
1849 * the possible optional diagnostics -- it can be "error" or "warning"
1850 * or something else. Finally the line terminates with the actual
1853 * @param severity the severity of the warning or error
1854 * @param fmt the printf style format string
1856 static void report_error_vc(int severity
, const char *fmt
, ...)
1860 if (is_suppressed_warning(severity
))
1863 if (severity
& ERR_NOFILE
)
1864 fputs("nasm: ", error_file
);
1866 char *currentfile
= NULL
;
1868 src_get(&lineno
, ¤tfile
);
1869 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1870 nasm_free(currentfile
);
1873 report_error_common(severity
, fmt
, ap
);
1878 * check for supressed warning
1879 * checks for suppressed warning or pass one only warning and we're
1882 * @param severity the severity of the warning or error
1883 * @return true if we should abort error/warning printing
1885 static bool is_suppressed_warning(int severity
)
1888 * See if it's a suppressed warning.
1890 return (severity
& ERR_MASK
) == ERR_WARNING
&&
1891 (((severity
& ERR_WARN_MASK
) != 0 &&
1892 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1893 /* See if it's a pass-one only warning and we're not in pass one. */
1894 ((severity
& ERR_PASS1
) && pass0
!= 1));
1898 * common error reporting
1899 * This is the common back end of the error reporting schemes currently
1900 * implemented. It prints the nature of the warning and then the
1901 * specific error message to error_file and may or may not return. It
1902 * doesn't return if the error severity is a "panic" or "debug" type.
1904 * @param severity the severity of the warning or error
1905 * @param fmt the printf style format string
1907 static void report_error_common(int severity
, const char *fmt
,
1910 switch (severity
& (ERR_MASK
|ERR_NO_SEVERITY
)) {
1912 fputs("warning: ", error_file
);
1915 fputs("error: ", error_file
);
1918 fputs("fatal: ", error_file
);
1921 fputs("panic: ", error_file
);
1924 fputs("debug: ", error_file
);
1930 vfprintf(error_file
, fmt
, args
);
1931 putc('\n', error_file
);
1933 if (severity
& ERR_USAGE
)
1936 switch (severity
& ERR_MASK
) {
1938 /* no further action, by definition */
1941 if (!suppressed
[0]) /* Treat warnings as errors */
1942 terminate_after_phase
= true;
1945 terminate_after_phase
= true;
1954 exit(1); /* instantly die */
1955 break; /* placate silly compilers */
1958 /* abort(); *//* halt, catch fire, and dump core */
1964 static void usage(void)
1966 fputs("type `nasm -h' for help\n", error_file
);
1969 static void register_output_formats(void)
1971 ofmt
= ofmt_register(report_error
);
1974 #define BUF_DELTA 512
1976 static FILE *no_pp_fp
;
1977 static efunc no_pp_err
;
1978 static ListGen
*no_pp_list
;
1979 static int32_t no_pp_lineinc
;
1981 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1982 ListGen
* listgen
, StrList
**deplist
)
1984 src_set_fname(nasm_strdup(file
));
1988 no_pp_fp
= fopen(file
, "r");
1990 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1991 "unable to open input file `%s'", file
);
1992 no_pp_list
= listgen
;
1993 (void)pass
; /* placate compilers */
1994 (void)eval
; /* placate compilers */
1997 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
1999 strcpy(sl
->str
, file
);
2004 static char *no_pp_getline(void)
2006 char *buffer
, *p
, *q
;
2009 bufsize
= BUF_DELTA
;
2010 buffer
= nasm_malloc(BUF_DELTA
);
2011 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
2013 while (1) { /* Loop to handle %line */
2016 while (1) { /* Loop to handle long lines */
2017 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
2021 if (p
> buffer
&& p
[-1] == '\n')
2023 if (p
- buffer
> bufsize
- 10) {
2025 offset
= p
- buffer
;
2026 bufsize
+= BUF_DELTA
;
2027 buffer
= nasm_realloc(buffer
, bufsize
);
2028 p
= buffer
+ offset
;
2032 if (!q
&& p
== buffer
) {
2038 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
2039 * them are present at the end of the line.
2041 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
2043 if (!nasm_strnicmp(buffer
, "%line", 5)) {
2046 char *nm
= nasm_malloc(strlen(buffer
));
2047 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
2048 nasm_free(src_set_fname(nm
));
2058 no_pp_list
->line(LIST_READ
, buffer
);
2063 static void no_pp_cleanup(int pass
)
2065 (void)pass
; /* placate GCC */
2069 static uint32_t get_cpu(char *value
)
2071 if (!strcmp(value
, "8086"))
2073 if (!strcmp(value
, "186"))
2075 if (!strcmp(value
, "286"))
2077 if (!strcmp(value
, "386"))
2079 if (!strcmp(value
, "486"))
2081 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
2083 if (!strcmp(value
, "686") ||
2084 !nasm_stricmp(value
, "ppro") ||
2085 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
2087 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
2089 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
2090 !nasm_stricmp(value
, "willamette"))
2091 return IF_WILLAMETTE
;
2092 if (!nasm_stricmp(value
, "prescott"))
2094 if (!nasm_stricmp(value
, "x64") ||
2095 !nasm_stricmp(value
, "x86-64"))
2097 if (!nasm_stricmp(value
, "ia64") ||
2098 !nasm_stricmp(value
, "ia-64") ||
2099 !nasm_stricmp(value
, "itanium") ||
2100 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
2103 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
2104 "unknown 'cpu' type");
2106 return IF_PLEVEL
; /* the maximum level */
2109 static int get_bits(char *value
)
2113 if ((i
= atoi(value
)) == 16)
2114 return i
; /* set for a 16-bit segment */
2117 report_error(ERR_NONFATAL
,
2118 "cannot specify 32-bit segment on processor below a 386");
2121 } else if (i
== 64) {
2122 if (cpu
< IF_X86_64
) {
2123 report_error(ERR_NONFATAL
,
2124 "cannot specify 64-bit segment on processor below an x86-64");
2128 report_error(ERR_NONFATAL
,
2129 "%s output format does not support 64-bit code",
2134 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
2135 "`%s' is not a valid segment size; must be 16, 32 or 64",