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.
31 struct forwrefinfo
{ /* info held on forward refs. */
36 static int get_bits(char *value
);
37 static uint32_t get_cpu(char *cpu_str
);
38 static void parse_cmdline(int, char **);
39 static void assemble_file(char *);
40 static void register_output_formats(void);
41 static void report_error_gnu(int severity
, const char *fmt
, ...);
42 static void report_error_vc(int severity
, const char *fmt
, ...);
43 static void report_error_common(int severity
, const char *fmt
,
45 static int is_suppressed_warning(int severity
);
46 static void usage(void);
47 static efunc report_error
;
49 static int using_debug_info
, opt_verbose_info
;
50 bool tasm_compatible_mode
= false;
55 static char inname
[FILENAME_MAX
];
56 static char outname
[FILENAME_MAX
];
57 static char listname
[FILENAME_MAX
];
58 static char errname
[FILENAME_MAX
];
59 static int globallineno
; /* for forward-reference tracking */
60 /* static int pass = 0; */
61 static struct ofmt
*ofmt
= NULL
;
63 static FILE *error_file
; /* Where to write error messages */
65 static FILE *ofile
= NULL
;
66 int optimizing
= -1; /* number of optimization passes to take */
67 static int sb
, cmd_sb
= 16; /* by default */
68 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
69 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
70 bool global_offset_changed
; /* referenced in labels.c */
72 static struct location location
;
73 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
74 int32_t abs_seg
; /* ABSOLUTE segment basis */
75 int32_t abs_offset
; /* ABSOLUTE offset */
77 static struct RAA
*offsets
;
79 static struct SAA
*forwrefs
; /* keep track of forward references */
80 static const struct forwrefinfo
*forwref
;
82 static Preproc
*preproc
;
84 op_normal
, /* Preprocess and assemble */
85 op_preprocess
, /* Preprocess only */
86 op_depend
, /* Generate dependencies */
87 op_depend_missing_ok
, /* Generate dependencies, missing OK */
89 static enum op_type operating_mode
;
92 * Which of the suppressible warnings are suppressed. Entry zero
93 * doesn't do anything. Initial defaults are given here.
95 static char suppressed
[1 + ERR_WARN_MAX
] = {
96 0, true, true, true, false, true
100 * The option names for the suppressible warnings. As before, entry
103 static const char *suppressed_names
[1 + ERR_WARN_MAX
] = {
104 NULL
, "macro-params", "macro-selfref", "orphan-labels",
110 * The explanations for the suppressible warnings. As before, entry
113 static const char *suppressed_what
[1 + ERR_WARN_MAX
] = {
115 "macro calls with wrong no. of params",
116 "cyclic macro self-references",
117 "labels alone on lines without trailing `:'",
118 "numeric constants greater than 0xFFFFFFFF",
119 "using 8- or 16-bit relocation in ELF, a GNU extension"
123 * This is a null preprocessor which just copies lines from input
124 * to output. It's used when someone explicitly requests that NASM
125 * not preprocess their source file.
128 static void no_pp_reset(char *, int, efunc
, evalfunc
, ListGen
*);
129 static char *no_pp_getline(void);
130 static void no_pp_cleanup(int);
131 static Preproc no_pp
= {
138 * get/set current offset...
140 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
141 raa_read(offsets,location.segment))
142 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
143 (void)(offsets=raa_write(offsets,location.segment,(x))))
145 static int want_usage
;
146 static int terminate_after_phase
;
147 int user_nolist
= 0; /* fbk 9/2/00 */
149 static void nasm_fputs(const char *line
, FILE * outfile
)
152 fputs(line
, outfile
);
153 fputc('\n', outfile
);
158 int main(int argc
, char **argv
)
161 want_usage
= terminate_after_phase
= false;
162 report_error
= report_error_gnu
;
166 nasm_set_malloc_error(report_error
);
167 offsets
= raa_init();
168 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
171 operating_mode
= op_normal
;
175 register_output_formats();
177 parse_cmdline(argc
, argv
);
179 if (terminate_after_phase
) {
185 /* If debugging info is disabled, suppress any debug calls */
186 if (!using_debug_info
)
187 ofmt
->current_dfmt
= &null_debug_form
;
190 pp_extra_stdmac(ofmt
->stdmac
);
191 parser_global_info(ofmt
, &location
);
192 eval_global_info(ofmt
, lookup_label
, &location
);
194 /* define some macros dependent of command-line */
197 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
202 switch (operating_mode
) {
203 case op_depend_missing_ok
:
204 pp_include_path(NULL
); /* "assume generated" */
209 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
210 if (outname
[0] == '\0')
211 ofmt
->filename(inname
, outname
, report_error
);
213 fprintf(stdout
, "%s: %s", outname
, inname
);
214 while ((line
= preproc
->getline()))
224 char *file_name
= NULL
;
225 int32_t prior_linnum
= 0;
229 ofile
= fopen(outname
, "w");
231 report_error(ERR_FATAL
| ERR_NOFILE
,
232 "unable to open output file `%s'",
237 location
.known
= false;
240 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
241 while ((line
= preproc
->getline())) {
243 * We generate %line directives if needed for later programs
245 int32_t linnum
= prior_linnum
+= lineinc
;
246 int altline
= src_get(&linnum
, &file_name
);
248 if (altline
== 1 && lineinc
== 1)
249 nasm_fputs("", ofile
);
251 lineinc
= (altline
!= -1 || lineinc
!= 1);
252 fprintf(ofile
? ofile
: stdout
,
253 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
256 prior_linnum
= linnum
;
258 nasm_fputs(line
, ofile
);
261 nasm_free(file_name
);
265 if (ofile
&& terminate_after_phase
)
273 * We must call ofmt->filename _anyway_, even if the user
274 * has specified their own output file, because some
275 * formats (eg OBJ and COFF) use ofmt->filename to find out
276 * the name of the input file and then put that inside the
279 ofmt
->filename(inname
, outname
, report_error
);
281 ofile
= fopen(outname
, "wb");
283 report_error(ERR_FATAL
| ERR_NOFILE
,
284 "unable to open output file `%s'", outname
);
288 * We must call init_labels() before ofmt->init() since
289 * some object formats will want to define labels in their
290 * init routines. (eg OS/2 defines the FLAT group)
294 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
296 assemble_file(inname
);
298 if (!terminate_after_phase
) {
299 ofmt
->cleanup(using_debug_info
);
303 * We had an fclose on the output file here, but we
304 * actually do that in all the object file drivers as well,
305 * so we're leaving out the one here.
324 if (terminate_after_phase
)
331 * Get a parameter for a command line option.
332 * First arg must be in the form of e.g. -f...
334 static char *get_param(char *p
, char *q
, int *advance
)
337 if (p
[2]) { /* the parameter's in the option */
347 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
348 "option `-%c' requires an argument", p
[1]);
358 #define OPT_POSTFIX 1
359 struct textargs textopts
[] = {
360 {"prefix", OPT_PREFIX
},
361 {"postfix", OPT_POSTFIX
},
366 static int process_arg(char *p
, char *q
)
374 if (p
[0] == '-' && !stopoptions
) {
379 case 'o': /* these parameters take values */
394 if (!(param
= get_param(p
, q
, &advance
)))
396 if (p
[1] == 'o') { /* output file */
397 strcpy(outname
, param
);
398 } else if (p
[1] == 'f') { /* output format */
399 ofmt
= ofmt_find(param
);
401 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
402 "unrecognised output format `%s' - "
403 "use -hf for a list", param
);
405 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
406 } else if (p
[1] == 'O') { /* Optimization level */
410 /* Naked -O == -Ox */
411 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
415 case '0': case '1': case '2': case '3': case '4':
416 case '5': case '6': case '7': case '8': case '9':
417 opt
= strtoul(param
, ¶m
, 10);
419 /* -O0 -> optimizing == -1, 0.98 behaviour */
420 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
422 optimizing
= opt
- 1;
424 /* The optimizer seems to have problems with
425 < 5 passes? Hidden bug? */
426 optimizing
= 5; /* 5 passes */
428 optimizing
= opt
; /* More than 5 passes */
434 opt_verbose_info
= true;
439 optimizing
= INT_MAX
>> 1; /* Almost unlimited */
443 report_error(ERR_FATAL
,
444 "unknown optimization option -O%c\n",
450 } else if (p
[1] == 'P' || p
[1] == 'p') { /* pre-include */
451 pp_pre_include(param
);
452 } else if (p
[1] == 'D' || p
[1] == 'd') { /* pre-define */
453 pp_pre_define(param
);
454 } else if (p
[1] == 'U' || p
[1] == 'u') { /* un-define */
455 pp_pre_undefine(param
);
456 } else if (p
[1] == 'I' || p
[1] == 'i') { /* include search path */
457 pp_include_path(param
);
458 } else if (p
[1] == 'l') { /* listing file */
459 strcpy(listname
, param
);
460 } else if (p
[1] == 'Z') { /* error messages file */
461 strcpy(errname
, param
);
462 } else if (p
[1] == 'F') { /* specify debug format */
463 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
464 if (!ofmt
->current_dfmt
) {
465 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
466 "unrecognized debug format `%s' for"
467 " output format `%s'",
468 param
, ofmt
->shortname
);
470 } else if (p
[1] == 'X') { /* specify error reporting format */
471 if (nasm_stricmp("vc", param
) == 0)
472 report_error
= report_error_vc
;
473 else if (nasm_stricmp("gnu", param
) == 0)
474 report_error
= report_error_gnu
;
476 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
477 "unrecognized error reporting format `%s'",
482 using_debug_info
= true;
486 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
488 " [options...] [--] filename\n"
489 " or nasm -v for version info\n\n"
490 " -t assemble in SciTech TASM compatible mode\n"
491 " -g generate debug information in selected format.\n");
493 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
494 " -a don't preprocess (assemble only)\n"
495 " -M generate Makefile dependencies on stdout\n"
496 " -MG d:o, missing files assumed generated\n\n"
497 " -Z<file> redirect error messages to file\n"
498 " -s redirect error messages to stdout\n\n"
499 " -F format select a debugging format\n\n"
500 " -I<path> adds a pathname to the include file path\n");
502 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
503 " -P<file> pre-includes a file\n"
504 " -D<macro>[=<value>] pre-defines a macro\n"
505 " -U<macro> undefines a macro\n"
506 " -X<format> specifies error reporting format (gnu or vc)\n"
507 " -w+foo enables warnings about foo; -w-foo disables them\n"
508 "where foo can be:\n");
509 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
510 printf(" %-23s %s (default %s)\n",
511 suppressed_names
[i
], suppressed_what
[i
],
512 suppressed
[i
] ? "off" : "on");
514 ("\nresponse files should contain command line parameters"
515 ", one per line.\n");
517 printf("\nvalid output formats for -f are"
518 " (`*' denotes default):\n");
519 ofmt_list(ofmt
, stdout
);
521 printf("\nFor a list of valid output formats, use -hf.\n");
522 printf("For a list of debug formats, use -f <form> -y.\n");
524 exit(0); /* never need usage message here */
527 printf("\nvalid debug formats for '%s' output format are"
528 " ('*' denotes default):\n", ofmt
->shortname
);
529 dfmt_list(ofmt
, stdout
);
533 tasm_compatible_mode
= true;
537 const char *nasm_version_string
=
538 "NASM version " NASM_VER
" compiled on " __DATE__
543 puts(nasm_version_string
);
544 exit(0); /* never need usage message here */
547 case 'e': /* preprocess only */
549 operating_mode
= op_preprocess
;
551 case 'a': /* assemble only - don't preprocess */
555 if (p
[2] != '+' && p
[2] != '-') {
556 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
557 "invalid option to `-w'");
559 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
560 if (!nasm_stricmp(p
+ 3, suppressed_names
[i
]))
562 if (i
<= ERR_WARN_MAX
)
563 suppressed
[i
] = (p
[2] == '-');
565 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
566 "invalid option to `-w'");
570 operating_mode
= p
[2] == 'G' ? op_depend_missing_ok
: op_depend
;
577 if (p
[2] == 0) { /* -- => stop processing options */
581 for (s
= 0; textopts
[s
].label
; s
++) {
582 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
593 report_error(ERR_NONFATAL
| ERR_NOFILE
|
595 "option `--%s' requires an argument",
599 advance
= 1, param
= q
;
602 if (s
== OPT_PREFIX
) {
603 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
604 lprefix
[PREFIX_MAX
- 1] = 0;
607 if (s
== OPT_POSTFIX
) {
608 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
609 lpostfix
[POSTFIX_MAX
- 1] = 0;
616 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
617 "unrecognised option `--%s'", p
+ 2);
625 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
626 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
627 "unrecognised option `-%c'", p
[1]);
632 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
633 "more than one input file specified");
641 #define ARG_BUF_DELTA 128
643 static void process_respfile(FILE * rfile
)
645 char *buffer
, *p
, *q
, *prevarg
;
646 int bufsize
, prevargsize
;
648 bufsize
= prevargsize
= ARG_BUF_DELTA
;
649 buffer
= nasm_malloc(ARG_BUF_DELTA
);
650 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
653 while (1) { /* Loop to handle all lines in file */
656 while (1) { /* Loop to handle long lines */
657 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
661 if (p
> buffer
&& p
[-1] == '\n')
663 if (p
- buffer
> bufsize
- 10) {
666 bufsize
+= ARG_BUF_DELTA
;
667 buffer
= nasm_realloc(buffer
, bufsize
);
672 if (!q
&& p
== buffer
) {
674 process_arg(prevarg
, NULL
);
681 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
682 * them are present at the end of the line.
684 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
686 while (p
> buffer
&& isspace(p
[-1]))
693 if (process_arg(prevarg
, p
))
696 if (strlen(p
) > prevargsize
- 10) {
697 prevargsize
+= ARG_BUF_DELTA
;
698 prevarg
= nasm_realloc(prevarg
, prevargsize
);
704 /* Function to process args from a string of args, rather than the
705 * argv array. Used by the environment variable and response file
708 static void process_args(char *args
)
710 char *p
, *q
, *arg
, *prevarg
;
711 char separator
= ' ';
719 while (*p
&& *p
!= separator
)
721 while (*p
== separator
)
725 if (process_arg(prevarg
, arg
))
729 process_arg(arg
, NULL
);
732 static void parse_cmdline(int argc
, char **argv
)
735 char *envreal
, *envcopy
= NULL
, *p
, *arg
;
737 *inname
= *outname
= *listname
= *errname
= '\0';
740 * First, process the NASMENV environment variable.
742 envreal
= getenv("NASMENV");
745 envcopy
= nasm_strdup(envreal
);
746 process_args(envcopy
);
751 * Now process the actual command line.
756 if (argv
[0][0] == '@') {
757 /* We have a response file, so process this as a set of
758 * arguments like the environment variable. This allows us
759 * to have multiple arguments on a single line, which is
760 * different to the -@resp file processing below for regular
763 char *str
= malloc(2048);
764 FILE *f
= fopen(&argv
[0][1], "r");
766 printf("out of memory");
770 while (fgets(str
, 2048, f
)) {
779 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
780 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
);
782 rfile
= fopen(p
, "r");
784 process_respfile(rfile
);
787 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
788 "unable to open response file `%s'", p
);
791 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
792 argv
+= i
, argc
-= i
;
796 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
797 "no input file specified");
799 /* Look for basic command line typos. This definitely doesn't
800 catch all errors, but it might help cases of fumbled fingers. */
801 if (!strcmp(inname
, errname
) || !strcmp(inname
, outname
) ||
802 !strcmp(inname
, listname
))
803 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
804 "file `%s' is both input and output file",
808 error_file
= fopen(errname
, "w");
810 error_file
= stderr
; /* Revert to default! */
811 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
812 "cannot open file `%s' for error messages",
818 /* List of directives */
820 D_NONE
, D_ABSOLUTE
, D_BITS
, D_COMMON
, D_CPU
, D_DEBUG
, D_DEFAULT
,
821 D_EXTERN
, D_GLOBAL
, D_LIST
, D_SECTION
, D_SEGMENT
, D_WARNING
823 static const char *directives
[] = {
824 "", "absolute", "bits", "common", "cpu", "debug", "default",
825 "extern", "global", "list", "section", "segment", "warning"
827 static enum directives
getkw(char **directive
, char **value
);
829 static void assemble_file(char *fname
)
831 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
836 struct tokenval tokval
;
839 int pass_cnt
= 0; /* count actual passes */
841 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
842 report_error(ERR_FATAL
, "command line: "
843 "32-bit segment size requires a higher cpu");
845 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
846 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
847 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
851 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
852 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
853 /* pass0 seq is 0, 0, 0,..., 1, 2 */
855 def_label
= pass
> 1 ? redefine_label
: define_label
;
857 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
861 nasmlist
.init(listname
, report_error
);
864 global_offset_changed
= false; /* set by redefine_label */
865 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
868 saa_rewind(forwrefs
);
869 forwref
= saa_rstruct(forwrefs
);
871 offsets
= raa_init();
873 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
876 location
.known
= true;
877 location
.offset
= offs
= GET_CURR_OFFS
;
879 while ((line
= preproc
->getline())) {
883 /* here we parse our directives; this is not handled by the 'real'
886 d
= getkw(&directive
, &value
);
891 case D_SEGMENT
: /* [SEGMENT n] */
893 seg
= ofmt
->section(value
, pass2
, &sb
);
895 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
896 "segment name `%s' not recognized",
900 location
.segment
= seg
;
903 case D_EXTERN
: /* [EXTERN label:special] */
905 value
++; /* skip initial $ if present */
908 while (*q
&& *q
!= ':')
912 ofmt
->symdef(value
, 0L, 0L, 3, q
);
914 } else if (pass
== 1) { /* pass == 1 */
919 while (*q
&& *q
!= ':') {
925 report_error(ERR_NONFATAL
,
926 "identifier expected after EXTERN");
934 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
936 pass0
= 1; /* fake pass 1 in labels.c */
937 declare_as_global(value
, special
,
939 define_label(value
, seg_alloc(), 0L, NULL
,
940 false, true, ofmt
, report_error
);
943 } /* else pass0 == 1 */
945 case D_BITS
: /* [BITS bits] */
946 globalbits
= sb
= get_bits(value
);
948 case D_GLOBAL
: /* [GLOBAL symbol:special] */
950 value
++; /* skip initial $ if present */
951 if (pass0
== 2) { /* pass 2 */
953 while (*q
&& *q
!= ':')
957 ofmt
->symdef(value
, 0L, 0L, 3, q
);
959 } else if (pass2
== 1) { /* pass == 1 */
964 while (*q
&& *q
!= ':') {
970 report_error(ERR_NONFATAL
,
971 "identifier expected after GLOBAL");
979 declare_as_global(value
, special
, report_error
);
982 case D_COMMON
: /* [COMMON symbol size:special] */
984 value
++; /* skip initial $ if present */
990 while (*p
&& !isspace(*p
)) {
996 report_error(ERR_NONFATAL
,
997 "identifier expected after COMMON");
1003 while (*p
&& isspace(*p
))
1006 while (*q
&& *q
!= ':')
1013 size
= readnum(p
, &rn_error
);
1015 report_error(ERR_NONFATAL
,
1016 "invalid size specified"
1017 " in COMMON declaration");
1019 define_common(value
, seg_alloc(), size
,
1020 special
, ofmt
, report_error
);
1022 report_error(ERR_NONFATAL
,
1023 "no size specified in"
1024 " COMMON declaration");
1025 } else if (pass0
== 2) { /* pass == 2 */
1027 while (*q
&& *q
!= ':') {
1034 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1038 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1040 stdscan_bufptr
= value
;
1041 tokval
.t_type
= TOKEN_INVALID
;
1042 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1043 report_error
, NULL
);
1046 report_error(pass0
==
1047 1 ? ERR_NONFATAL
: ERR_PANIC
,
1048 "cannot use non-relocatable expression as "
1049 "ABSOLUTE address");
1051 abs_seg
= reloc_seg(e
);
1052 abs_offset
= reloc_value(e
);
1054 } else if (pass
== 1)
1055 abs_offset
= 0x100; /* don't go near zero in case of / */
1057 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1060 location
.segment
= NO_SEG
;
1062 case D_DEBUG
: /* [DEBUG] */
1068 while (*p
&& !isspace(*p
)) {
1075 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1076 "identifier expected after DEBUG");
1079 while (*p
&& isspace(*p
))
1081 if (pass
== pass_max
)
1082 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1084 case D_WARNING
: /* [WARNING {+|-}warn-name] */
1086 while (*value
&& isspace(*value
))
1089 if (*value
== '+' || *value
== '-') {
1090 validid
= (*value
== '-') ? true : false;
1095 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1096 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1098 if (i
<= ERR_WARN_MAX
)
1099 suppressed
[i
] = validid
;
1101 report_error(ERR_NONFATAL
,
1102 "invalid warning id in WARNING directive");
1105 case D_CPU
: /* [CPU] */
1106 cpu
= get_cpu(value
);
1108 case D_LIST
: /* [LIST {+|-}] */
1109 while (*value
&& isspace(*value
))
1112 if (*value
== '+') {
1115 if (*value
== '-') {
1122 case D_DEFAULT
: /* [DEFAULT] */
1124 stdscan_bufptr
= value
;
1125 tokval
.t_type
= TOKEN_INVALID
;
1126 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1127 switch ((int)tokval
.t_integer
) {
1143 if (!ofmt
->directive(directive
, value
, pass2
))
1144 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1145 "unrecognised directive [%s]",
1149 report_error(ERR_NONFATAL
,
1150 "invalid parameter to [%s] directive",
1153 } else { /* it isn't a directive */
1155 parse_line(pass1
, line
, &output_ins
,
1156 report_error
, evaluate
, def_label
);
1158 if (!(optimizing
> 0) && pass
== 2) {
1159 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1160 output_ins
.forw_ref
= true;
1162 output_ins
.oprs
[forwref
->operand
].opflags
|=
1164 forwref
= saa_rstruct(forwrefs
);
1165 } while (forwref
!= NULL
1166 && forwref
->lineno
== globallineno
);
1168 output_ins
.forw_ref
= false;
1171 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1173 for (i
= 0; i
< output_ins
.operands
; i
++) {
1174 if (output_ins
.oprs
[i
].
1175 opflags
& OPFLAG_FORWARD
) {
1176 struct forwrefinfo
*fwinf
=
1177 (struct forwrefinfo
*)
1178 saa_wstruct(forwrefs
);
1179 fwinf
->lineno
= globallineno
;
1183 } else { /* pass == 2 */
1185 * Hack to prevent phase error in the code
1189 * If the second operand is a forward reference,
1190 * the UNITY property of the number 1 in that
1191 * operand is cancelled. Otherwise the above
1192 * sequence will cause a phase error.
1194 * This hack means that the above code will
1195 * generate 286+ code.
1197 * The forward reference will mean that the
1198 * operand will not have the UNITY property on
1199 * the first pass, so the pass behaviours will
1203 if (output_ins
.operands
>= 2 &&
1204 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
) &&
1205 !(IMMEDIATE
& ~output_ins
.oprs
[1].type
))
1207 /* Remove special properties bits */
1208 output_ins
.oprs
[1].type
&= ~REG_SMASK
;
1216 if (output_ins
.opcode
== I_EQU
) {
1219 * Special `..' EQUs get processed in pass two,
1220 * except `..@' macro-processor EQUs which are done
1221 * in the normal place.
1223 if (!output_ins
.label
)
1224 report_error(ERR_NONFATAL
,
1225 "EQU not preceded by label");
1227 else if (output_ins
.label
[0] != '.' ||
1228 output_ins
.label
[1] != '.' ||
1229 output_ins
.label
[2] == '@') {
1230 if (output_ins
.operands
== 1 &&
1231 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1232 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1235 opflags
& OPFLAG_EXTERN
;
1236 def_label(output_ins
.label
,
1237 output_ins
.oprs
[0].segment
,
1238 output_ins
.oprs
[0].offset
, NULL
,
1241 } else if (output_ins
.operands
== 2
1242 && (output_ins
.oprs
[0].
1244 && (output_ins
.oprs
[0].type
& COLON
)
1245 && output_ins
.oprs
[0].segment
==
1247 && output_ins
.oprs
[0].wrt
== NO_SEG
1248 && (output_ins
.oprs
[1].
1250 && output_ins
.oprs
[1].segment
==
1252 && output_ins
.oprs
[1].wrt
==
1254 def_label(output_ins
.label
,
1257 output_ins
.oprs
[1].offset
, NULL
,
1261 report_error(ERR_NONFATAL
,
1262 "bad syntax for EQU");
1264 } else { /* pass == 2 */
1266 * Special `..' EQUs get processed here, except
1267 * `..@' macro processor EQUs which are done above.
1269 if (output_ins
.label
[0] == '.' &&
1270 output_ins
.label
[1] == '.' &&
1271 output_ins
.label
[2] != '@') {
1272 if (output_ins
.operands
== 1 &&
1273 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1274 define_label(output_ins
.label
,
1275 output_ins
.oprs
[0].segment
,
1276 output_ins
.oprs
[0].offset
,
1277 NULL
, false, false, ofmt
,
1279 } else if (output_ins
.operands
== 2
1280 && (output_ins
.oprs
[0].
1282 && (output_ins
.oprs
[0].type
& COLON
)
1283 && output_ins
.oprs
[0].segment
==
1285 && (output_ins
.oprs
[1].
1287 && output_ins
.oprs
[1].segment
==
1289 define_label(output_ins
.label
,
1292 output_ins
.oprs
[1].offset
,
1293 NULL
, false, false, ofmt
,
1296 report_error(ERR_NONFATAL
,
1297 "bad syntax for EQU");
1300 } else { /* instruction isn't an EQU */
1304 int32_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1305 &output_ins
, report_error
);
1307 /* if (using_debug_info) && output_ins.opcode != -1) */
1308 if (using_debug_info
)
1309 { /* fbk 03/25/01 */
1310 /* this is done here so we can do debug type info */
1312 TYS_ELEMENTS(output_ins
.operands
);
1313 switch (output_ins
.opcode
) {
1316 TYS_ELEMENTS(output_ins
.oprs
[0].
1321 TYS_ELEMENTS(output_ins
.oprs
[0].
1326 TYS_ELEMENTS(output_ins
.oprs
[0].
1331 TYS_ELEMENTS(output_ins
.oprs
[0].
1336 TYS_ELEMENTS(output_ins
.oprs
[0].
1340 typeinfo
|= TY_BYTE
;
1343 typeinfo
|= TY_WORD
;
1346 if (output_ins
.eops_float
)
1347 typeinfo
|= TY_FLOAT
;
1349 typeinfo
|= TY_DWORD
;
1352 typeinfo
|= TY_QWORD
;
1355 typeinfo
|= TY_TBYTE
;
1358 typeinfo
|= TY_OWORD
;
1361 typeinfo
= TY_LABEL
;
1365 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1370 SET_CURR_OFFS(offs
);
1373 * else l == -1 => invalid instruction, which will be
1374 * flagged as an error on pass 2
1377 } else { /* pass == 2 */
1378 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1379 &output_ins
, ofmt
, report_error
,
1381 SET_CURR_OFFS(offs
);
1385 cleanup_insn(&output_ins
);
1388 location
.offset
= offs
= GET_CURR_OFFS
;
1389 } /* end while (line = preproc->getline... */
1391 if (pass1
== 2 && global_offset_changed
)
1392 report_error(ERR_NONFATAL
,
1393 "phase error detected at end of assembly.");
1396 preproc
->cleanup(1);
1398 if (pass1
== 1 && terminate_after_phase
) {
1406 if (pass
> 1 && !global_offset_changed
) {
1409 pass
= pass_max
- 1;
1410 } else if (!(optimizing
> 0))
1413 } /* for (pass=1; pass<=2; pass++) */
1415 preproc
->cleanup(0);
1418 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1420 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1422 } /* exit from assemble_file (...) */
1424 static enum directives
getkw(char **directive
, char **value
)
1430 /* allow leading spaces or tabs */
1431 while (*buf
== ' ' || *buf
== '\t')
1439 while (*p
&& *p
!= ']')
1447 while (*p
&& *p
!= ';') {
1454 *directive
= p
= buf
+ 1;
1455 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1462 while (isspace(*buf
))
1463 buf
++; /* beppu - skip leading whitespace */
1470 return bsii(*directive
, directives
, elements(directives
));
1474 * gnu style error reporting
1475 * This function prints an error message to error_file in the
1476 * style used by GNU. An example would be:
1477 * file.asm:50: error: blah blah blah
1478 * where file.asm is the name of the file, 50 is the line number on
1479 * which the error occurs (or is detected) and "error:" is one of
1480 * the possible optional diagnostics -- it can be "error" or "warning"
1481 * or something else. Finally the line terminates with the actual
1484 * @param severity the severity of the warning or error
1485 * @param fmt the printf style format string
1487 static void report_error_gnu(int severity
, const char *fmt
, ...)
1491 if (is_suppressed_warning(severity
))
1494 if (severity
& ERR_NOFILE
)
1495 fputs("nasm: ", error_file
);
1497 char *currentfile
= NULL
;
1499 src_get(&lineno
, ¤tfile
);
1500 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1501 nasm_free(currentfile
);
1504 report_error_common(severity
, fmt
, ap
);
1509 * MS style error reporting
1510 * This function prints an error message to error_file in the
1511 * style used by Visual C and some other Microsoft tools. An example
1513 * file.asm(50) : error: blah blah blah
1514 * where file.asm is the name of the file, 50 is the line number on
1515 * which the error occurs (or is detected) and "error:" is one of
1516 * the possible optional diagnostics -- it can be "error" or "warning"
1517 * or something else. Finally the line terminates with the actual
1520 * @param severity the severity of the warning or error
1521 * @param fmt the printf style format string
1523 static void report_error_vc(int severity
, const char *fmt
, ...)
1527 if (is_suppressed_warning(severity
))
1530 if (severity
& ERR_NOFILE
)
1531 fputs("nasm: ", error_file
);
1533 char *currentfile
= NULL
;
1535 src_get(&lineno
, ¤tfile
);
1536 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1537 nasm_free(currentfile
);
1540 report_error_common(severity
, fmt
, ap
);
1545 * check for supressed warning
1546 * checks for suppressed warning or pass one only warning and we're
1549 * @param severity the severity of the warning or error
1550 * @return true if we should abort error/warning printing
1552 static int is_suppressed_warning(int severity
)
1555 * See if it's a suppressed warning.
1557 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1558 (severity
& ERR_WARN_MASK
) != 0 &&
1559 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1561 * See if it's a pass-one only warning and we're not in pass one.
1563 ((severity
& ERR_PASS1
) && pass0
== 2);
1567 * common error reporting
1568 * This is the common back end of the error reporting schemes currently
1569 * implemented. It prints the nature of the warning and then the
1570 * specific error message to error_file and may or may not return. It
1571 * doesn't return if the error severity is a "panic" or "debug" type.
1573 * @param severity the severity of the warning or error
1574 * @param fmt the printf style format string
1576 static void report_error_common(int severity
, const char *fmt
,
1579 switch (severity
& ERR_MASK
) {
1581 fputs("warning: ", error_file
);
1584 fputs("error: ", error_file
);
1587 fputs("fatal: ", error_file
);
1590 fputs("panic: ", error_file
);
1593 fputs("debug: ", error_file
);
1597 vfprintf(error_file
, fmt
, args
);
1598 fputc('\n', error_file
);
1600 if (severity
& ERR_USAGE
)
1603 switch (severity
& ERR_MASK
) {
1606 /* no further action, by definition */
1609 /* hack enables listing(!) on errors */
1610 terminate_after_phase
= true;
1619 exit(1); /* instantly die */
1620 break; /* placate silly compilers */
1623 /* abort(); *//* halt, catch fire, and dump core */
1629 static void usage(void)
1631 fputs("type `nasm -h' for help\n", error_file
);
1634 static void register_output_formats(void)
1636 ofmt
= ofmt_register(report_error
);
1639 #define BUF_DELTA 512
1641 static FILE *no_pp_fp
;
1642 static efunc no_pp_err
;
1643 static ListGen
*no_pp_list
;
1644 static int32_t no_pp_lineinc
;
1646 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1649 src_set_fname(nasm_strdup(file
));
1653 no_pp_fp
= fopen(file
, "r");
1655 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1656 "unable to open input file `%s'", file
);
1657 no_pp_list
= listgen
;
1658 (void)pass
; /* placate compilers */
1659 (void)eval
; /* placate compilers */
1662 static char *no_pp_getline(void)
1664 char *buffer
, *p
, *q
;
1667 bufsize
= BUF_DELTA
;
1668 buffer
= nasm_malloc(BUF_DELTA
);
1669 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1671 while (1) { /* Loop to handle %line */
1674 while (1) { /* Loop to handle long lines */
1675 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1679 if (p
> buffer
&& p
[-1] == '\n')
1681 if (p
- buffer
> bufsize
- 10) {
1683 offset
= p
- buffer
;
1684 bufsize
+= BUF_DELTA
;
1685 buffer
= nasm_realloc(buffer
, bufsize
);
1686 p
= buffer
+ offset
;
1690 if (!q
&& p
== buffer
) {
1696 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1697 * them are present at the end of the line.
1699 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1701 if (!nasm_strnicmp(buffer
, "%line", 5)) {
1704 char *nm
= nasm_malloc(strlen(buffer
));
1705 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1706 nasm_free(src_set_fname(nm
));
1716 no_pp_list
->line(LIST_READ
, buffer
);
1721 static void no_pp_cleanup(int pass
)
1723 (void)pass
; /* placate GCC */
1727 static uint32_t get_cpu(char *value
)
1730 if (!strcmp(value
, "8086"))
1732 if (!strcmp(value
, "186"))
1734 if (!strcmp(value
, "286"))
1736 if (!strcmp(value
, "386"))
1738 if (!strcmp(value
, "486"))
1740 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1742 if (!strcmp(value
, "686") ||
1743 !nasm_stricmp(value
, "ppro") ||
1744 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1746 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1748 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1749 !nasm_stricmp(value
, "willamette"))
1750 return IF_WILLAMETTE
;
1751 if (!nasm_stricmp(value
, "prescott"))
1753 if (!nasm_stricmp(value
, "x64") ||
1754 !nasm_stricmp(value
, "x86-64"))
1756 if (!nasm_stricmp(value
, "ia64") ||
1757 !nasm_stricmp(value
, "ia-64") ||
1758 !nasm_stricmp(value
, "itanium") ||
1759 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1762 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1763 "unknown 'cpu' type");
1765 return IF_PLEVEL
; /* the maximum level */
1768 static int get_bits(char *value
)
1772 if ((i
= atoi(value
)) == 16)
1773 return i
; /* set for a 16-bit segment */
1776 report_error(ERR_NONFATAL
,
1777 "cannot specify 32-bit segment on processor below a 386");
1780 } else if (i
== 64) {
1781 if (cpu
< IF_X86_64
) {
1782 report_error(ERR_NONFATAL
,
1783 "cannot specify 64-bit segment on processor below an x86-64");
1787 report_error(ERR_NONFATAL
,
1788 "%s output format does not support 64-bit code",
1793 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1794 "`%s' is not a valid segment size; must be 16, 32 or 64",