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.
33 struct forwrefinfo
{ /* info held on forward refs. */
38 static int get_bits(char *value
);
39 static uint32_t get_cpu(char *cpu_str
);
40 static void parse_cmdline(int, char **);
41 static void assemble_file(char *);
42 static void register_output_formats(void);
43 static void report_error_gnu(int severity
, const char *fmt
, ...);
44 static void report_error_vc(int severity
, const char *fmt
, ...);
45 static void report_error_common(int severity
, const char *fmt
,
47 static bool is_suppressed_warning(int severity
);
48 static void usage(void);
49 static efunc report_error
;
51 static int using_debug_info
, opt_verbose_info
;
52 bool tasm_compatible_mode
= false;
57 time_t official_compile_time
;
59 static char inname
[FILENAME_MAX
];
60 static char outname
[FILENAME_MAX
];
61 static char listname
[FILENAME_MAX
];
62 static char errname
[FILENAME_MAX
];
63 static int globallineno
; /* for forward-reference tracking */
64 /* static int pass = 0; */
65 static struct ofmt
*ofmt
= NULL
;
67 static FILE *error_file
; /* Where to write error messages */
69 static FILE *ofile
= NULL
;
70 int optimizing
= -1; /* number of optimization passes to take */
71 static int sb
, cmd_sb
= 16; /* by default */
72 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
73 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
74 bool global_offset_changed
; /* referenced in labels.c */
76 static struct location location
;
77 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
78 int32_t abs_seg
; /* ABSOLUTE segment basis */
79 int32_t abs_offset
; /* ABSOLUTE offset */
81 static struct RAA
*offsets
;
83 static struct SAA
*forwrefs
; /* keep track of forward references */
84 static const struct forwrefinfo
*forwref
;
86 static Preproc
*preproc
;
88 op_normal
, /* Preprocess and assemble */
89 op_preprocess
, /* Preprocess only */
90 op_depend
, /* Generate dependencies */
91 op_depend_missing_ok
, /* Generate dependencies, missing OK */
93 static enum op_type operating_mode
;
96 * Which of the suppressible warnings are suppressed. Entry zero
97 * isn't an actual warning, but it used for -w+error/-Werror.
99 static bool suppressed
[ERR_WARN_MAX
+1] = {
100 true, false, true, false, false, true, false, true, true, false
104 * The option names for the suppressible warnings. As before, entry
107 static const char *suppressed_names
[ERR_WARN_MAX
+1] = {
108 "error", "macro-params", "macro-selfref", "orphan-labels",
109 "number-overflow", "gnu-elf-extensions", "float-overflow",
110 "float-denorm", "float-underflow", "float-toolong"
114 * The explanations for the suppressible warnings. As before, entry
117 static const char *suppressed_what
[ERR_WARN_MAX
+1] = {
118 "treat warnings as errors",
119 "macro calls with wrong parameter count",
120 "cyclic macro references",
121 "labels alone on lines without trailing `:'",
122 "numeric constants does not fit in 64 bits",
123 "using 8- or 16-bit relocation in ELF32, a GNU extension",
124 "floating point overflow",
125 "floating point denormal",
126 "floating point underflow",
127 "too many digits in floating-point number"
131 * This is a null preprocessor which just copies lines from input
132 * to output. It's used when someone explicitly requests that NASM
133 * not preprocess their source file.
136 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*);
137 static char *no_pp_getline(void);
138 static void no_pp_cleanup(int);
139 static Preproc no_pp
= {
146 * get/set current offset...
148 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
149 raa_read(offsets,location.segment))
150 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
151 (void)(offsets=raa_write(offsets,location.segment,(x))))
153 static int want_usage
;
154 static int terminate_after_phase
;
155 int user_nolist
= 0; /* fbk 9/2/00 */
157 static void nasm_fputs(const char *line
, FILE * outfile
)
160 fputs(line
, outfile
);
166 /* Convert a struct tm to a POSIX-style time constant */
167 static int64_t posix_mktime(struct tm
*tm
)
170 int64_t y
= tm
->tm_year
;
172 /* See IEEE 1003.1:2004, section 4.14 */
174 t
= (y
-70)*365 + (y
-69)/4 - (y
-1)/100 + (y
+299)/400;
186 static void define_macros_early(void)
189 struct tm lt
, *lt_p
, gm
, *gm_p
;
192 lt_p
= localtime(&official_compile_time
);
196 strftime(temp
, sizeof temp
, "__DATE__=\"%Y-%m-%d\"", <
);
198 strftime(temp
, sizeof temp
, "__DATE_NUM__=%Y%m%d", <
);
200 strftime(temp
, sizeof temp
, "__TIME__=\"%H:%M:%S\"", <
);
202 strftime(temp
, sizeof temp
, "__TIME_NUM__=%H%M%S", <
);
206 gm_p
= gmtime(&official_compile_time
);
210 strftime(temp
, sizeof temp
, "__UTC_DATE__=\"%Y-%m-%d\"", &gm
);
212 strftime(temp
, sizeof temp
, "__UTC_DATE_NUM__=%Y%m%d", &gm
);
214 strftime(temp
, sizeof temp
, "__UTC_TIME__=\"%H:%M:%S\"", &gm
);
216 strftime(temp
, sizeof temp
, "__UTC_TIME_NUM__=%H%M%S", &gm
);
221 posix_time
= posix_mktime(&gm
);
223 posix_time
= posix_mktime(<
);
228 snprintf(temp
, sizeof temp
, "__POSIX_TIME__=%"PRId64
, posix_time
);
233 static void define_macros_late(void)
237 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
242 int main(int argc
, char **argv
)
244 time(&official_compile_time
);
247 want_usage
= terminate_after_phase
= false;
248 report_error
= report_error_gnu
;
252 nasm_set_malloc_error(report_error
);
253 offsets
= raa_init();
254 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
257 operating_mode
= op_normal
;
261 register_output_formats();
263 /* Define some macros dependent on the runtime, but not
264 on the command line. */
265 define_macros_early();
267 parse_cmdline(argc
, argv
);
269 if (terminate_after_phase
) {
275 /* If debugging info is disabled, suppress any debug calls */
276 if (!using_debug_info
)
277 ofmt
->current_dfmt
= &null_debug_form
;
280 pp_extra_stdmac(ofmt
->stdmac
);
281 parser_global_info(ofmt
, &location
);
282 eval_global_info(ofmt
, lookup_label
, &location
);
284 /* define some macros dependent of command-line */
285 define_macros_late();
287 switch (operating_mode
) {
288 case op_depend_missing_ok
:
289 pp_include_path(NULL
); /* "assume generated" */
294 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
295 if (outname
[0] == '\0')
296 ofmt
->filename(inname
, outname
, report_error
);
298 fprintf(stdout
, "%s: %s", outname
, inname
);
299 while ((line
= preproc
->getline()))
309 char *file_name
= NULL
;
310 int32_t prior_linnum
= 0;
314 ofile
= fopen(outname
, "w");
316 report_error(ERR_FATAL
| ERR_NOFILE
,
317 "unable to open output file `%s'",
322 location
.known
= false;
325 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
326 while ((line
= preproc
->getline())) {
328 * We generate %line directives if needed for later programs
330 int32_t linnum
= prior_linnum
+= lineinc
;
331 int altline
= src_get(&linnum
, &file_name
);
333 if (altline
== 1 && lineinc
== 1)
334 nasm_fputs("", ofile
);
336 lineinc
= (altline
!= -1 || lineinc
!= 1);
337 fprintf(ofile
? ofile
: stdout
,
338 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
341 prior_linnum
= linnum
;
343 nasm_fputs(line
, ofile
);
346 nasm_free(file_name
);
350 if (ofile
&& terminate_after_phase
)
358 * We must call ofmt->filename _anyway_, even if the user
359 * has specified their own output file, because some
360 * formats (eg OBJ and COFF) use ofmt->filename to find out
361 * the name of the input file and then put that inside the
364 ofmt
->filename(inname
, outname
, report_error
);
366 ofile
= fopen(outname
, "wb");
368 report_error(ERR_FATAL
| ERR_NOFILE
,
369 "unable to open output file `%s'", outname
);
373 * We must call init_labels() before ofmt->init() since
374 * some object formats will want to define labels in their
375 * init routines. (eg OS/2 defines the FLAT group)
379 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
381 assemble_file(inname
);
383 if (!terminate_after_phase
) {
384 ofmt
->cleanup(using_debug_info
);
388 * We had an fclose on the output file here, but we
389 * actually do that in all the object file drivers as well,
390 * so we're leaving out the one here.
409 if (terminate_after_phase
)
416 * Get a parameter for a command line option.
417 * First arg must be in the form of e.g. -f...
419 static char *get_param(char *p
, char *q
, bool *advance
)
422 if (p
[2]) { /* the parameter's in the option */
432 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
433 "option `-%c' requires an argument", p
[1]);
440 static void copy_filename(char *dst
, const char *src
)
442 size_t len
= strlen(src
);
444 if (len
>= (size_t)FILENAME_MAX
) {
445 report_error(ERR_FATAL
| ERR_NOFILE
, "file name too long");
448 strncpy(dst
, src
, FILENAME_MAX
);
457 #define OPT_POSTFIX 1
458 struct textargs textopts
[] = {
459 {"prefix", OPT_PREFIX
},
460 {"postfix", OPT_POSTFIX
},
464 static bool stopoptions
= false;
465 static bool process_arg(char *p
, char *q
)
469 bool advance
= false;
475 if (p
[0] == '-' && !stopoptions
) {
476 if (strchr("oOfpPdDiIlFXuUZwW", p
[1])) {
477 /* These parameters take values */
478 if (!(param
= get_param(p
, q
, &advance
)))
487 case 'o': /* output file */
488 copy_filename(outname
, param
);
491 case 'f': /* output format */
492 ofmt
= ofmt_find(param
);
494 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
495 "unrecognised output format `%s' - "
496 "use -hf for a list", param
);
498 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
502 case 'O': /* Optimization level */
507 /* Naked -O == -Ox */
508 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
512 case '0': case '1': case '2': case '3': case '4':
513 case '5': case '6': case '7': case '8': case '9':
514 opt
= strtoul(param
, ¶m
, 10);
516 /* -O0 -> optimizing == -1, 0.98 behaviour */
517 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
519 optimizing
= opt
- 1;
527 opt_verbose_info
= true;
532 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
536 report_error(ERR_FATAL
,
537 "unknown optimization option -O%c\n",
546 case 'p': /* pre-include */
548 pp_pre_include(param
);
551 case 'd': /* pre-define */
553 pp_pre_define(param
);
556 case 'u': /* un-define */
558 pp_pre_undefine(param
);
561 case 'i': /* include search path */
563 pp_include_path(param
);
566 case 'l': /* listing file */
567 copy_filename(listname
, param
);
570 case 'Z': /* error messages file */
571 strcpy(errname
, param
);
574 case 'F': /* specify debug format */
575 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
576 if (!ofmt
->current_dfmt
) {
577 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
578 "unrecognized debug format `%s' for"
579 " output format `%s'",
580 param
, ofmt
->shortname
);
584 case 'X': /* specify error reporting format */
585 if (nasm_stricmp("vc", param
) == 0)
586 report_error
= report_error_vc
;
587 else if (nasm_stricmp("gnu", param
) == 0)
588 report_error
= report_error_gnu
;
590 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
591 "unrecognized error reporting format `%s'",
596 using_debug_info
= true;
601 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
603 " [options...] [--] filename\n"
604 " or nasm -v for version info\n\n"
605 " -t assemble in SciTech TASM compatible mode\n"
606 " -g generate debug information in selected format.\n");
608 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
609 " -a don't preprocess (assemble only)\n"
610 " -M generate Makefile dependencies on stdout\n"
611 " -MG d:o, missing files assumed generated\n\n"
612 " -Z<file> redirect error messages to file\n"
613 " -s redirect error messages to stdout\n\n"
614 " -F format select a debugging format\n\n"
615 " -I<path> adds a pathname to the include file path\n");
617 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
618 " -P<file> pre-includes a file\n"
619 " -D<macro>[=<value>] pre-defines a macro\n"
620 " -U<macro> undefines a macro\n"
621 " -X<format> specifies error reporting format (gnu or vc)\n"
622 " -w+foo enables warning foo (equiv. -Wfoo)\n"
623 " -w-foo disable warning foo (equiv. -Wno-foo)\n"
625 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
626 printf(" %-23s %s (default %s)\n",
627 suppressed_names
[i
], suppressed_what
[i
],
628 suppressed
[i
] ? "off" : "on");
630 ("\nresponse files should contain command line parameters"
631 ", one per line.\n");
633 printf("\nvalid output formats for -f are"
634 " (`*' denotes default):\n");
635 ofmt_list(ofmt
, stdout
);
637 printf("\nFor a list of valid output formats, use -hf.\n");
638 printf("For a list of debug formats, use -f <form> -y.\n");
640 exit(0); /* never need usage message here */
644 printf("\nvalid debug formats for '%s' output format are"
645 " ('*' denotes default):\n", ofmt
->shortname
);
646 dfmt_list(ofmt
, stdout
);
651 tasm_compatible_mode
= true;
656 const char *nasm_version_string
=
657 "NASM version " NASM_VER
" compiled on " __DATE__
662 puts(nasm_version_string
);
663 exit(0); /* never need usage message here */
667 case 'e': /* preprocess only */
669 operating_mode
= op_preprocess
;
672 case 'a': /* assemble only - don't preprocess */
677 if (param
[0] == 'n' && param
[1] == 'o' && param
[2] == '-') {
686 if (param
[0] != '+' && param
[0] != '-') {
687 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
688 "invalid option to `-w'");
691 suppress
= (param
[0] == '-');
695 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
696 if (!nasm_stricmp(param
, suppressed_names
[i
]))
698 if (i
<= ERR_WARN_MAX
)
699 suppressed
[i
] = suppress
;
700 else if (!nasm_stricmp(param
, "all"))
701 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
702 suppressed
[i
] = suppress
;
703 else if (!nasm_stricmp(param
, "none"))
704 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
705 suppressed
[i
] = !suppress
;
707 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
708 "invalid warning `%s'", param
);
712 operating_mode
= p
[2] == 'G' ? op_depend_missing_ok
: op_depend
;
719 if (p
[2] == 0) { /* -- => stop processing options */
723 for (s
= 0; textopts
[s
].label
; s
++) {
724 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
735 report_error(ERR_NONFATAL
| ERR_NOFILE
|
737 "option `--%s' requires an argument",
741 advance
= 1, param
= q
;
744 if (s
== OPT_PREFIX
) {
745 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
746 lprefix
[PREFIX_MAX
- 1] = 0;
749 if (s
== OPT_POSTFIX
) {
750 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
751 lpostfix
[POSTFIX_MAX
- 1] = 0;
758 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
759 "unrecognised option `--%s'", p
+ 2);
767 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
768 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
769 "unrecognised option `-%c'", p
[1]);
774 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
775 "more than one input file specified");
777 copy_filename(inname
, p
);
784 #define ARG_BUF_DELTA 128
786 static void process_respfile(FILE * rfile
)
788 char *buffer
, *p
, *q
, *prevarg
;
789 int bufsize
, prevargsize
;
791 bufsize
= prevargsize
= ARG_BUF_DELTA
;
792 buffer
= nasm_malloc(ARG_BUF_DELTA
);
793 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
796 while (1) { /* Loop to handle all lines in file */
798 while (1) { /* Loop to handle long lines */
799 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
803 if (p
> buffer
&& p
[-1] == '\n')
805 if (p
- buffer
> bufsize
- 10) {
808 bufsize
+= ARG_BUF_DELTA
;
809 buffer
= nasm_realloc(buffer
, bufsize
);
814 if (!q
&& p
== buffer
) {
816 process_arg(prevarg
, NULL
);
823 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
824 * them are present at the end of the line.
826 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
828 while (p
> buffer
&& isspace(p
[-1]))
835 if (process_arg(prevarg
, p
))
838 if ((int) strlen(p
) > prevargsize
- 10) {
839 prevargsize
+= ARG_BUF_DELTA
;
840 prevarg
= nasm_realloc(prevarg
, prevargsize
);
842 strncpy(prevarg
, p
, prevargsize
);
846 /* Function to process args from a string of args, rather than the
847 * argv array. Used by the environment variable and response file
850 static void process_args(char *args
)
852 char *p
, *q
, *arg
, *prevarg
;
853 char separator
= ' ';
861 while (*p
&& *p
!= separator
)
863 while (*p
== separator
)
867 if (process_arg(prevarg
, arg
))
871 process_arg(arg
, NULL
);
874 static void process_response_file(const char *file
)
877 FILE *f
= fopen(file
, "r");
882 while (fgets(str
, sizeof str
, f
)) {
888 static void parse_cmdline(int argc
, char **argv
)
891 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
893 *inname
= *outname
= *listname
= *errname
= '\0';
896 * First, process the NASMENV environment variable.
898 envreal
= getenv("NASMENV");
901 envcopy
= nasm_strdup(envreal
);
902 process_args(envcopy
);
907 * Now process the actual command line.
912 if (argv
[0][0] == '@') {
913 /* We have a response file, so process this as a set of
914 * arguments like the environment variable. This allows us
915 * to have multiple arguments on a single line, which is
916 * different to the -@resp file processing below for regular
919 process_response_file(argv
[0]+1);
923 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
924 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &advance
);
926 rfile
= fopen(p
, "r");
928 process_respfile(rfile
);
931 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
932 "unable to open response file `%s'", p
);
935 advance
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
936 argv
+= advance
, argc
-= advance
;
939 /* Look for basic command line typos. This definitely doesn't
940 catch all errors, but it might help cases of fumbled fingers. */
942 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
943 "no input file specified");
944 else if (!strcmp(inname
, errname
) || !strcmp(inname
, outname
) ||
945 !strcmp(inname
, listname
))
946 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
947 "file `%s' is both input and output file",
951 error_file
= fopen(errname
, "w");
953 error_file
= stderr
; /* Revert to default! */
954 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
955 "cannot open file `%s' for error messages",
961 /* List of directives */
963 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
964 D_EXTERN
, D_FLOAT
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
966 static const char *directives
[] = {
967 "", "absolute", "bits", "common", "cpu", "debug", "default",
968 "extern", "float", "global", "list", "section", "segment", "warning"
970 static enum directives
getkw(char **directive
, char **value
);
972 static void assemble_file(char *fname
)
974 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
980 struct tokenval tokval
;
984 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
985 report_error(ERR_FATAL
, "command line: "
986 "32-bit segment size requires a higher cpu");
988 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
989 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
990 for (passn
= 1; pass0
<= 2; passn
++) {
994 pass1
= pass0
== 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
995 pass2
= passn
> 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
996 /* pass0 0, 0, 0, ..., 1, 2 */
998 def_label
= passn
> 1 ? redefine_label
: define_label
;
1000 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
1004 nasmlist
.init(listname
, report_error
);
1007 global_offset_changed
= false; /* set by redefine_label */
1008 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
1011 saa_rewind(forwrefs
);
1012 forwref
= saa_rstruct(forwrefs
);
1014 offsets
= raa_init();
1016 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
1019 location
.known
= true;
1020 location
.offset
= offs
= GET_CURR_OFFS
;
1022 while ((line
= preproc
->getline())) {
1026 /* here we parse our directives; this is not handled by the 'real'
1029 d
= getkw(&directive
, &value
);
1034 case D_SEGMENT
: /* [SEGMENT n] */
1036 seg
= ofmt
->section(value
, pass2
, &sb
);
1037 if (seg
== NO_SEG
) {
1038 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1039 "segment name `%s' not recognized",
1043 location
.segment
= seg
;
1046 case D_EXTERN
: /* [EXTERN label:special] */
1048 value
++; /* skip initial $ if present */
1051 while (*q
&& *q
!= ':')
1055 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1057 } else if (passn
== 1) {
1062 while (*q
&& *q
!= ':') {
1068 report_error(ERR_NONFATAL
,
1069 "identifier expected after EXTERN");
1077 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
1079 pass0
= 1; /* fake pass 1 in labels.c */
1080 declare_as_global(value
, special
,
1082 define_label(value
, seg_alloc(), 0L, NULL
,
1083 false, true, ofmt
, report_error
);
1086 } /* else pass0 == 1 */
1088 case D_BITS
: /* [BITS bits] */
1089 globalbits
= sb
= get_bits(value
);
1091 case D_GLOBAL
: /* [GLOBAL symbol:special] */
1093 value
++; /* skip initial $ if present */
1094 if (pass0
== 2) { /* pass 2 */
1096 while (*q
&& *q
!= ':')
1100 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1102 } else if (pass2
== 1) { /* pass == 1 */
1107 while (*q
&& *q
!= ':') {
1113 report_error(ERR_NONFATAL
,
1114 "identifier expected after GLOBAL");
1122 declare_as_global(value
, special
, report_error
);
1125 case D_COMMON
: /* [COMMON symbol size:special] */
1127 value
++; /* skip initial $ if present */
1133 while (*p
&& !isspace(*p
)) {
1139 report_error(ERR_NONFATAL
,
1140 "identifier expected after COMMON");
1146 while (*p
&& isspace(*p
))
1149 while (*q
&& *q
!= ':')
1156 size
= readnum(p
, &rn_error
);
1158 report_error(ERR_NONFATAL
,
1159 "invalid size specified"
1160 " in COMMON declaration");
1162 define_common(value
, seg_alloc(), size
,
1163 special
, ofmt
, report_error
);
1165 report_error(ERR_NONFATAL
,
1166 "no size specified in"
1167 " COMMON declaration");
1168 } else if (pass0
== 2) { /* pass == 2 */
1170 while (*q
&& *q
!= ':') {
1177 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1181 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1183 stdscan_bufptr
= value
;
1184 tokval
.t_type
= TOKEN_INVALID
;
1185 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1186 report_error
, NULL
);
1189 report_error(pass0
==
1190 1 ? ERR_NONFATAL
: ERR_PANIC
,
1191 "cannot use non-relocatable expression as "
1192 "ABSOLUTE address");
1194 abs_seg
= reloc_seg(e
);
1195 abs_offset
= reloc_value(e
);
1197 } else if (passn
== 1)
1198 abs_offset
= 0x100; /* don't go near zero in case of / */
1200 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1203 location
.segment
= NO_SEG
;
1205 case D_DEBUG
: /* [DEBUG] */
1211 while (*p
&& !isspace(*p
)) {
1218 report_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1219 "identifier expected after DEBUG");
1222 while (*p
&& isspace(*p
))
1225 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1227 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1229 while (*value
&& isspace(*value
))
1232 if (*value
== '+' || *value
== '-') {
1233 validid
= (*value
== '-') ? true : false;
1238 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1239 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1241 if (i
<= ERR_WARN_MAX
)
1242 suppressed
[i
] = validid
;
1244 report_error(ERR_NONFATAL
,
1245 "invalid warning id in WARNING directive");
1248 case D_CPU
: /* [CPU] */
1249 cpu
= get_cpu(value
);
1251 case D_LIST
: /* [LIST {+|-}] */
1252 while (*value
&& isspace(*value
))
1255 if (*value
== '+') {
1258 if (*value
== '-') {
1265 case D_DEFAULT
: /* [DEFAULT] */
1267 stdscan_bufptr
= value
;
1268 tokval
.t_type
= TOKEN_INVALID
;
1269 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1270 switch ((int)tokval
.t_integer
) {
1286 if (float_option(value
)) {
1287 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1288 "unknown 'float' directive: %s",
1293 if (!ofmt
->directive(directive
, value
, pass2
))
1294 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1295 "unrecognised directive [%s]",
1299 report_error(ERR_NONFATAL
,
1300 "invalid parameter to [%s] directive",
1303 } else { /* it isn't a directive */
1305 parse_line(pass1
, line
, &output_ins
,
1306 report_error
, evaluate
, def_label
);
1308 if (!(optimizing
> 0) && pass0
== 2) {
1309 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1310 output_ins
.forw_ref
= true;
1312 output_ins
.oprs
[forwref
->operand
].opflags
|=
1314 forwref
= saa_rstruct(forwrefs
);
1315 } while (forwref
!= NULL
1316 && forwref
->lineno
== globallineno
);
1318 output_ins
.forw_ref
= false;
1321 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1323 for (i
= 0; i
< output_ins
.operands
; i
++) {
1324 if (output_ins
.oprs
[i
].
1325 opflags
& OPFLAG_FORWARD
) {
1326 struct forwrefinfo
*fwinf
=
1327 (struct forwrefinfo
*)
1328 saa_wstruct(forwrefs
);
1329 fwinf
->lineno
= globallineno
;
1333 } else { /* passn > 1 */
1335 * Hack to prevent phase error in the code
1339 * If the second operand is a forward reference,
1340 * the UNITY property of the number 1 in that
1341 * operand is cancelled. Otherwise the above
1342 * sequence will cause a phase error.
1344 * This hack means that the above code will
1345 * generate 286+ code.
1347 * The forward reference will mean that the
1348 * operand will not have the UNITY property on
1349 * the first pass, so the pass behaviours will
1353 if (output_ins
.operands
>= 2 &&
1354 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1355 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1357 /* Remove special properties bits */
1358 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1366 if (output_ins
.opcode
== I_EQU
) {
1369 * Special `..' EQUs get processed in pass two,
1370 * except `..@' macro-processor EQUs which are done
1371 * in the normal place.
1373 if (!output_ins
.label
)
1374 report_error(ERR_NONFATAL
,
1375 "EQU not preceded by label");
1377 else if (output_ins
.label
[0] != '.' ||
1378 output_ins
.label
[1] != '.' ||
1379 output_ins
.label
[2] == '@') {
1380 if (output_ins
.operands
== 1 &&
1381 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1382 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1385 opflags
& OPFLAG_EXTERN
;
1386 def_label(output_ins
.label
,
1387 output_ins
.oprs
[0].segment
,
1388 output_ins
.oprs
[0].offset
, NULL
,
1391 } else if (output_ins
.operands
== 2
1392 && (output_ins
.oprs
[0].
1394 && (output_ins
.oprs
[0].type
& COLON
)
1395 && output_ins
.oprs
[0].segment
==
1397 && output_ins
.oprs
[0].wrt
== NO_SEG
1398 && (output_ins
.oprs
[1].
1400 && output_ins
.oprs
[1].segment
==
1402 && output_ins
.oprs
[1].wrt
==
1404 def_label(output_ins
.label
,
1407 output_ins
.oprs
[1].offset
, NULL
,
1411 report_error(ERR_NONFATAL
,
1412 "bad syntax for EQU");
1416 * Special `..' EQUs get processed here, except
1417 * `..@' macro processor EQUs which are done above.
1419 if (output_ins
.label
[0] == '.' &&
1420 output_ins
.label
[1] == '.' &&
1421 output_ins
.label
[2] != '@') {
1422 if (output_ins
.operands
== 1 &&
1423 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1424 define_label(output_ins
.label
,
1425 output_ins
.oprs
[0].segment
,
1426 output_ins
.oprs
[0].offset
,
1427 NULL
, false, false, ofmt
,
1429 } else if (output_ins
.operands
== 2
1430 && (output_ins
.oprs
[0].
1432 && (output_ins
.oprs
[0].type
& COLON
)
1433 && output_ins
.oprs
[0].segment
==
1435 && (output_ins
.oprs
[1].
1437 && output_ins
.oprs
[1].segment
==
1439 define_label(output_ins
.label
,
1442 output_ins
.oprs
[1].offset
,
1443 NULL
, false, false, ofmt
,
1446 report_error(ERR_NONFATAL
,
1447 "bad syntax for EQU");
1450 } else { /* instruction isn't an EQU */
1454 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1455 &output_ins
, report_error
);
1457 /* if (using_debug_info) && output_ins.opcode != -1) */
1458 if (using_debug_info
)
1459 { /* fbk 03/25/01 */
1460 /* this is done here so we can do debug type info */
1462 TYS_ELEMENTS(output_ins
.operands
);
1463 switch (output_ins
.opcode
) {
1466 TYS_ELEMENTS(output_ins
.oprs
[0].
1471 TYS_ELEMENTS(output_ins
.oprs
[0].
1476 TYS_ELEMENTS(output_ins
.oprs
[0].
1481 TYS_ELEMENTS(output_ins
.oprs
[0].
1486 TYS_ELEMENTS(output_ins
.oprs
[0].
1491 TYS_ELEMENTS(output_ins
.oprs
[0].
1496 TYS_ELEMENTS(output_ins
.oprs
[0].
1500 typeinfo
|= TY_BYTE
;
1503 typeinfo
|= TY_WORD
;
1506 if (output_ins
.eops_float
)
1507 typeinfo
|= TY_FLOAT
;
1509 typeinfo
|= TY_DWORD
;
1512 typeinfo
|= TY_QWORD
;
1515 typeinfo
|= TY_TBYTE
;
1518 typeinfo
|= TY_OWORD
;
1521 typeinfo
|= TY_YWORD
;
1524 typeinfo
= TY_LABEL
;
1528 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1533 SET_CURR_OFFS(offs
);
1536 * else l == -1 => invalid instruction, which will be
1537 * flagged as an error on pass 2
1541 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1542 &output_ins
, ofmt
, report_error
,
1544 SET_CURR_OFFS(offs
);
1548 cleanup_insn(&output_ins
);
1551 location
.offset
= offs
= GET_CURR_OFFS
;
1552 } /* end while (line = preproc->getline... */
1554 if (pass1
== 2 && global_offset_changed
)
1555 report_error(ERR_NONFATAL
,
1556 "phase error detected at end of assembly.");
1559 preproc
->cleanup(1);
1561 if (pass1
== 1 && terminate_after_phase
) {
1568 if (passn
>= pass_max
- 2 ||
1569 (passn
> 1 && !global_offset_changed
))
1573 preproc
->cleanup(0);
1576 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1578 "info:: assembly required 1+%d+1 passes\n", passn
-3);
1580 } /* exit from assemble_file (...) */
1582 static enum directives
getkw(char **directive
, char **value
)
1588 /* allow leading spaces or tabs */
1589 while (*buf
== ' ' || *buf
== '\t')
1597 while (*p
&& *p
!= ']')
1605 while (*p
&& *p
!= ';') {
1612 *directive
= p
= buf
+ 1;
1613 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1620 while (isspace(*buf
))
1621 buf
++; /* beppu - skip leading whitespace */
1628 return bsii(*directive
, directives
, elements(directives
));
1632 * gnu style error reporting
1633 * This function prints an error message to error_file in the
1634 * style used by GNU. An example would be:
1635 * file.asm:50: error: blah blah blah
1636 * where file.asm is the name of the file, 50 is the line number on
1637 * which the error occurs (or is detected) and "error:" is one of
1638 * the possible optional diagnostics -- it can be "error" or "warning"
1639 * or something else. Finally the line terminates with the actual
1642 * @param severity the severity of the warning or error
1643 * @param fmt the printf style format string
1645 static void report_error_gnu(int severity
, const char *fmt
, ...)
1649 if (is_suppressed_warning(severity
))
1652 if (severity
& ERR_NOFILE
)
1653 fputs("nasm: ", error_file
);
1655 char *currentfile
= NULL
;
1657 src_get(&lineno
, ¤tfile
);
1658 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1659 nasm_free(currentfile
);
1662 report_error_common(severity
, fmt
, ap
);
1667 * MS style error reporting
1668 * This function prints an error message to error_file in the
1669 * style used by Visual C and some other Microsoft tools. An example
1671 * file.asm(50) : error: blah blah blah
1672 * where file.asm is the name of the file, 50 is the line number on
1673 * which the error occurs (or is detected) and "error:" is one of
1674 * the possible optional diagnostics -- it can be "error" or "warning"
1675 * or something else. Finally the line terminates with the actual
1678 * @param severity the severity of the warning or error
1679 * @param fmt the printf style format string
1681 static void report_error_vc(int severity
, const char *fmt
, ...)
1685 if (is_suppressed_warning(severity
))
1688 if (severity
& ERR_NOFILE
)
1689 fputs("nasm: ", error_file
);
1691 char *currentfile
= NULL
;
1693 src_get(&lineno
, ¤tfile
);
1694 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1695 nasm_free(currentfile
);
1698 report_error_common(severity
, fmt
, ap
);
1703 * check for supressed warning
1704 * checks for suppressed warning or pass one only warning and we're
1707 * @param severity the severity of the warning or error
1708 * @return true if we should abort error/warning printing
1710 static bool is_suppressed_warning(int severity
)
1713 * See if it's a suppressed warning.
1715 return (severity
& ERR_MASK
) == ERR_WARNING
&&
1716 (((severity
& ERR_WARN_MASK
) != 0 &&
1717 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1718 /* See if it's a pass-one only warning and we're not in pass one. */
1719 ((severity
& ERR_PASS1
) && pass0
!= 1));
1723 * common error reporting
1724 * This is the common back end of the error reporting schemes currently
1725 * implemented. It prints the nature of the warning and then the
1726 * specific error message to error_file and may or may not return. It
1727 * doesn't return if the error severity is a "panic" or "debug" type.
1729 * @param severity the severity of the warning or error
1730 * @param fmt the printf style format string
1732 static void report_error_common(int severity
, const char *fmt
,
1735 switch (severity
& ERR_MASK
) {
1737 fputs("warning: ", error_file
);
1740 fputs("error: ", error_file
);
1743 fputs("fatal: ", error_file
);
1746 fputs("panic: ", error_file
);
1749 fputs("debug: ", error_file
);
1753 vfprintf(error_file
, fmt
, args
);
1754 putc('\n', error_file
);
1756 if (severity
& ERR_USAGE
)
1759 switch (severity
& ERR_MASK
) {
1761 /* no further action, by definition */
1764 if (!suppressed
[0]) /* Treat warnings as errors */
1765 terminate_after_phase
= true;
1768 terminate_after_phase
= true;
1777 exit(1); /* instantly die */
1778 break; /* placate silly compilers */
1781 /* abort(); *//* halt, catch fire, and dump core */
1787 static void usage(void)
1789 fputs("type `nasm -h' for help\n", error_file
);
1792 static void register_output_formats(void)
1794 ofmt
= ofmt_register(report_error
);
1797 #define BUF_DELTA 512
1799 static FILE *no_pp_fp
;
1800 static efunc no_pp_err
;
1801 static ListGen
*no_pp_list
;
1802 static int32_t no_pp_lineinc
;
1804 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1807 src_set_fname(nasm_strdup(file
));
1811 no_pp_fp
= fopen(file
, "r");
1813 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1814 "unable to open input file `%s'", file
);
1815 no_pp_list
= listgen
;
1816 (void)pass
; /* placate compilers */
1817 (void)eval
; /* placate compilers */
1820 static char *no_pp_getline(void)
1822 char *buffer
, *p
, *q
;
1825 bufsize
= BUF_DELTA
;
1826 buffer
= nasm_malloc(BUF_DELTA
);
1827 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1829 while (1) { /* Loop to handle %line */
1832 while (1) { /* Loop to handle long lines */
1833 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1837 if (p
> buffer
&& p
[-1] == '\n')
1839 if (p
- buffer
> bufsize
- 10) {
1841 offset
= p
- buffer
;
1842 bufsize
+= BUF_DELTA
;
1843 buffer
= nasm_realloc(buffer
, bufsize
);
1844 p
= buffer
+ offset
;
1848 if (!q
&& p
== buffer
) {
1854 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1855 * them are present at the end of the line.
1857 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1859 if (!nasm_strnicmp(buffer
, "%line", 5)) {
1862 char *nm
= nasm_malloc(strlen(buffer
));
1863 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1864 nasm_free(src_set_fname(nm
));
1874 no_pp_list
->line(LIST_READ
, buffer
);
1879 static void no_pp_cleanup(int pass
)
1881 (void)pass
; /* placate GCC */
1885 static uint32_t get_cpu(char *value
)
1887 if (!strcmp(value
, "8086"))
1889 if (!strcmp(value
, "186"))
1891 if (!strcmp(value
, "286"))
1893 if (!strcmp(value
, "386"))
1895 if (!strcmp(value
, "486"))
1897 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1899 if (!strcmp(value
, "686") ||
1900 !nasm_stricmp(value
, "ppro") ||
1901 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1903 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1905 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1906 !nasm_stricmp(value
, "willamette"))
1907 return IF_WILLAMETTE
;
1908 if (!nasm_stricmp(value
, "prescott"))
1910 if (!nasm_stricmp(value
, "x64") ||
1911 !nasm_stricmp(value
, "x86-64"))
1913 if (!nasm_stricmp(value
, "ia64") ||
1914 !nasm_stricmp(value
, "ia-64") ||
1915 !nasm_stricmp(value
, "itanium") ||
1916 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1919 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1920 "unknown 'cpu' type");
1922 return IF_PLEVEL
; /* the maximum level */
1925 static int get_bits(char *value
)
1929 if ((i
= atoi(value
)) == 16)
1930 return i
; /* set for a 16-bit segment */
1933 report_error(ERR_NONFATAL
,
1934 "cannot specify 32-bit segment on processor below a 386");
1937 } else if (i
== 64) {
1938 if (cpu
< IF_X86_64
) {
1939 report_error(ERR_NONFATAL
,
1940 "cannot specify 64-bit segment on processor below an x86-64");
1944 report_error(ERR_NONFATAL
,
1945 "%s output format does not support 64-bit code",
1950 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1951 "`%s' is not a valid segment size; must be 16, 32 or 64",