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.
27 struct forwrefinfo
{ /* info held on forward refs. */
32 static int get_bits(int8_t *value
);
33 static uint32_t get_cpu(int8_t *cpu_str
);
34 static void parse_cmdline(int, int8_t **);
35 static void assemble_file(int8_t *);
36 static int getkw(int8_t **directive
, int8_t **value
);
37 static void register_output_formats(void);
38 static void report_error_gnu(int severity
, const int8_t *fmt
, ...);
39 static void report_error_vc(int severity
, const int8_t *fmt
, ...);
40 static void report_error_common(int severity
, const int8_t *fmt
,
42 static int is_suppressed_warning(int severity
);
43 static void usage(void);
44 static efunc report_error
;
46 static int using_debug_info
, opt_verbose_info
;
47 int tasm_compatible_mode
= FALSE
;
51 static int8_t inname
[FILENAME_MAX
];
52 static int8_t outname
[FILENAME_MAX
];
53 static int8_t listname
[FILENAME_MAX
];
54 static int globallineno
; /* for forward-reference tracking */
55 /* static int pass = 0; */
56 static struct ofmt
*ofmt
= NULL
;
58 static FILE *error_file
; /* Where to write error messages */
60 static FILE *ofile
= NULL
;
61 int optimizing
= -1; /* number of optimization passes to take */
62 static int sb
, cmd_sb
= 16; /* by default */
63 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
64 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
65 int global_offset_changed
; /* referenced in labels.c */
67 static loc_t location
;
68 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
69 int32_t abs_seg
; /* ABSOLUTE segment basis */
70 int32_t abs_offset
; /* ABSOLUTE offset */
72 static struct RAA
*offsets
;
74 static struct SAA
*forwrefs
; /* keep track of forward references */
75 static struct forwrefinfo
*forwref
;
77 static Preproc
*preproc
;
79 op_normal
, /* Preprocess and assemble */
80 op_preprocess
, /* Preprocess only */
81 op_depend
/* Generate dependencies */
83 static enum op_type operating_mode
;
86 * Which of the suppressible warnings are suppressed. Entry zero
87 * doesn't do anything. Initial defaults are given here.
89 static int8_t suppressed
[1 + ERR_WARN_MAX
] = {
90 0, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
94 * The option names for the suppressible warnings. As before, entry
97 static const int8_t *suppressed_names
[1 + ERR_WARN_MAX
] = {
98 NULL
, "macro-params", "macro-selfref", "orphan-labels",
104 * The explanations for the suppressible warnings. As before, entry
107 static const int8_t *suppressed_what
[1 + ERR_WARN_MAX
] = {
109 "macro calls with wrong no. of params",
110 "cyclic macro self-references",
111 "labels alone on lines without trailing `:'",
112 "numeric constants greater than 0xFFFFFFFF",
113 "using 8- or 16-bit relocation in ELF, a GNU extension"
117 * This is a null preprocessor which just copies lines from input
118 * to output. It's used when someone explicitly requests that NASM
119 * not preprocess their source file.
122 static void no_pp_reset(int8_t *, int, efunc
, evalfunc
, ListGen
*);
123 static int8_t *no_pp_getline(void);
124 static void no_pp_cleanup(int);
125 static Preproc no_pp
= {
132 * get/set current offset...
134 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
135 raa_read(offsets,location.segment))
136 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
137 (void)(offsets=raa_write(offsets,location.segment,(x))))
139 static int want_usage
;
140 static int terminate_after_phase
;
141 int user_nolist
= 0; /* fbk 9/2/00 */
143 static void nasm_fputs(const int8_t *line
, FILE * outfile
)
146 fputs(line
, outfile
);
147 fputc('\n', outfile
);
152 int main(int argc
, char **argv
)
155 want_usage
= terminate_after_phase
= FALSE
;
156 report_error
= report_error_gnu
;
158 nasm_set_malloc_error(report_error
);
159 offsets
= raa_init();
160 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
163 operating_mode
= op_normal
;
169 register_output_formats();
171 parse_cmdline(argc
, argv
);
173 if (terminate_after_phase
) {
179 /* If debugging info is disabled, suppress any debug calls */
180 if (!using_debug_info
)
181 ofmt
->current_dfmt
= &null_debug_form
;
184 pp_extra_stdmac(ofmt
->stdmac
);
185 parser_global_info(ofmt
, &location
);
186 eval_global_info(ofmt
, lookup_label
, &location
);
188 /* define some macros dependent of command-line */
191 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s\n",
196 switch (operating_mode
) {
200 preproc
->reset(inname
, 0, report_error
, evaluate
, &nasmlist
);
201 if (outname
[0] == '\0')
202 ofmt
->filename(inname
, outname
, report_error
);
204 fprintf(stdout
, "%s: %s", outname
, inname
);
205 while ((line
= preproc
->getline()))
215 int8_t *file_name
= NULL
;
216 int32_t prior_linnum
= 0;
220 ofile
= fopen(outname
, "w");
222 report_error(ERR_FATAL
| ERR_NOFILE
,
223 "unable to open output file `%s'",
228 location
.known
= FALSE
;
231 preproc
->reset(inname
, 2, report_error
, evaluate
, &nasmlist
);
232 while ((line
= preproc
->getline())) {
234 * We generate %line directives if needed for later programs
236 int32_t linnum
= prior_linnum
+= lineinc
;
237 int altline
= src_get(&linnum
, &file_name
);
239 if (altline
== 1 && lineinc
== 1)
240 nasm_fputs("", ofile
);
242 lineinc
= (altline
!= -1 || lineinc
!= 1);
243 fprintf(ofile
? ofile
: stdout
,
244 "%%line %ld+%d %s\n", linnum
, lineinc
,
247 prior_linnum
= linnum
;
249 nasm_fputs(line
, ofile
);
252 nasm_free(file_name
);
256 if (ofile
&& terminate_after_phase
)
264 * We must call ofmt->filename _anyway_, even if the user
265 * has specified their own output file, because some
266 * formats (eg OBJ and COFF) use ofmt->filename to find out
267 * the name of the input file and then put that inside the
270 ofmt
->filename(inname
, outname
, report_error
);
272 ofile
= fopen(outname
, "wb");
274 report_error(ERR_FATAL
| ERR_NOFILE
,
275 "unable to open output file `%s'", outname
);
279 * We must call init_labels() before ofmt->init() since
280 * some object formats will want to define labels in their
281 * init routines. (eg OS/2 defines the FLAT group)
285 ofmt
->init(ofile
, report_error
, define_label
, evaluate
);
287 assemble_file(inname
);
289 if (!terminate_after_phase
) {
290 ofmt
->cleanup(using_debug_info
);
294 * We had an fclose on the output file here, but we
295 * actually do that in all the object file drivers as well,
296 * so we're leaving out the one here.
315 if (terminate_after_phase
)
322 * Get a parameter for a command line option.
323 * First arg must be in the form of e.g. -f...
325 static int8_t *get_param(int8_t *p
, int8_t *q
, int *advance
)
328 if (p
[2]) { /* the parameter's in the option */
338 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
339 "option `-%c' requires an argument", p
[1]);
349 #define OPT_POSTFIX 1
350 struct textargs textopts
[] = {
351 {"prefix", OPT_PREFIX
},
352 {"postfix", OPT_POSTFIX
},
357 static int process_arg(int8_t *p
, int8_t *q
)
365 if (p
[0] == '-' && !stopoptions
) {
370 case 'o': /* these parameters take values */
385 if (!(param
= get_param(p
, q
, &advance
)))
387 if (p
[1] == 'o') { /* output file */
388 strcpy(outname
, param
);
389 } else if (p
[1] == 'f') { /* output format */
390 ofmt
= ofmt_find(param
);
392 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
393 "unrecognised output format `%s' - "
394 "use -hf for a list", param
);
396 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
397 } else if (p
[1] == 'O') { /* Optimization level */
401 if (isdigit(*param
)) {
403 while (isdigit(*++param
)) ;
405 optimizing
= -1; /* 0.98 behaviour */
407 optimizing
= 0; /* Two passes, 0.98.09 behavior */
409 optimizing
= opt
; /* Multiple passes */
411 if (*param
== 'v' || *param
== '+') {
413 opt_verbose_info
= TRUE
;
415 } else { /* garbage */
420 } /* while (*param) */
422 report_error(ERR_FATAL
,
423 "command line optimization level must be 'v', 0..3 or <nn>");
424 } else if (p
[1] == 'P' || p
[1] == 'p') { /* pre-include */
425 pp_pre_include(param
);
426 } else if (p
[1] == 'D' || p
[1] == 'd') { /* pre-define */
427 pp_pre_define(param
);
428 } else if (p
[1] == 'U' || p
[1] == 'u') { /* un-define */
429 pp_pre_undefine(param
);
430 } else if (p
[1] == 'I' || p
[1] == 'i') { /* include search path */
431 pp_include_path(param
);
432 } else if (p
[1] == 'l') { /* listing file */
433 strcpy(listname
, param
);
434 } else if (p
[1] == 'E') { /* error messages file */
435 error_file
= fopen(param
, "w");
437 error_file
= stderr
; /* Revert to default! */
438 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
439 "cannot open file `%s' for error messages",
442 } else if (p
[1] == 'F') { /* specify debug format */
443 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
444 if (!ofmt
->current_dfmt
) {
445 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
446 "unrecognized debug format `%s' for"
447 " output format `%s'",
448 param
, ofmt
->shortname
);
450 } else if (p
[1] == 'X') { /* specify error reporting format */
451 if (nasm_stricmp("vc", param
) == 0)
452 report_error
= report_error_vc
;
453 else if (nasm_stricmp("gnu", param
) == 0)
454 report_error
= report_error_gnu
;
456 report_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
457 "unrecognized error reporting format `%s'",
462 using_debug_info
= TRUE
;
466 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
468 " [options...] [--] filename\n"
469 " or nasm -r for version info (obsolete)\n"
470 " or nasm -v for version info (preferred)\n\n"
471 " -t assemble in SciTech TASM compatible mode\n"
472 " -g generate debug information in selected format.\n");
474 (" -e preprocess only (writes output to stdout by default)\n"
475 " -a don't preprocess (assemble only)\n"
476 " -M generate Makefile dependencies on stdout\n\n"
477 " -E<file> redirect error messages to file\n"
478 " -s redirect error messages to stdout\n\n"
479 " -F format select a debugging format\n\n"
480 " -I<path> adds a pathname to the include file path\n");
482 (" -O<digit> optimize branch offsets (-O0 disables, default)\n"
483 " -P<file> pre-includes a file\n"
484 " -D<macro>[=<value>] pre-defines a macro\n"
485 " -U<macro> undefines a macro\n"
486 " -X<format> specifies error reporting format (gnu or vc)\n"
487 " -w+foo enables warnings about foo; -w-foo disables them\n"
488 "where foo can be:\n");
489 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
490 printf(" %-23s %s (default %s)\n",
491 suppressed_names
[i
], suppressed_what
[i
],
492 suppressed
[i
] ? "off" : "on");
494 ("\nresponse files should contain command line parameters"
495 ", one per line.\n");
497 printf("\nvalid output formats for -f are"
498 " (`*' denotes default):\n");
499 ofmt_list(ofmt
, stdout
);
501 printf("\nFor a list of valid output formats, use -hf.\n");
502 printf("For a list of debug formats, use -f <form> -y.\n");
504 exit(0); /* never need usage message here */
507 printf("\nvalid debug formats for '%s' output format are"
508 " ('*' denotes default):\n", ofmt
->shortname
);
509 dfmt_list(ofmt
, stdout
);
513 tasm_compatible_mode
= TRUE
;
518 const int8_t *nasm_version_string
=
519 "NASM version " NASM_VER
" compiled on " __DATE__
524 puts(nasm_version_string
);
525 exit(0); /* never need usage message here */
528 case 'e': /* preprocess only */
529 operating_mode
= op_preprocess
;
531 case 'a': /* assemble only - don't preprocess */
535 if (p
[2] != '+' && p
[2] != '-') {
536 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
537 "invalid option to `-w'");
539 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
540 if (!nasm_stricmp(p
+ 3, suppressed_names
[i
]))
542 if (i
<= ERR_WARN_MAX
)
543 suppressed
[i
] = (p
[2] == '-');
545 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
546 "invalid option to `-w'");
550 operating_mode
= op_depend
;
557 if (p
[2] == 0) { /* -- => stop processing options */
561 for (s
= 0; textopts
[s
].label
; s
++) {
562 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
573 report_error(ERR_NONFATAL
| ERR_NOFILE
|
575 "option `--%s' requires an argument",
579 advance
= 1, param
= q
;
582 if (s
== OPT_PREFIX
) {
583 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
584 lprefix
[PREFIX_MAX
- 1] = 0;
587 if (s
== OPT_POSTFIX
) {
588 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
589 lpostfix
[POSTFIX_MAX
- 1] = 0;
596 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
597 "unrecognised option `--%s'", p
+ 2);
605 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
606 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
607 "unrecognised option `-%c'", p
[1]);
612 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
613 "more than one input file specified");
621 #define ARG_BUF_DELTA 128
623 static void process_respfile(FILE * rfile
)
625 int8_t *buffer
, *p
, *q
, *prevarg
;
626 int bufsize
, prevargsize
;
628 bufsize
= prevargsize
= ARG_BUF_DELTA
;
629 buffer
= nasm_malloc(ARG_BUF_DELTA
);
630 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
633 while (1) { /* Loop to handle all lines in file */
636 while (1) { /* Loop to handle long lines */
637 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
641 if (p
> buffer
&& p
[-1] == '\n')
643 if (p
- buffer
> bufsize
- 10) {
646 bufsize
+= ARG_BUF_DELTA
;
647 buffer
= nasm_realloc(buffer
, bufsize
);
652 if (!q
&& p
== buffer
) {
654 process_arg(prevarg
, NULL
);
661 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
662 * them are present at the end of the line.
664 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
666 while (p
> buffer
&& isspace(p
[-1]))
673 if (process_arg(prevarg
, p
))
676 if (strlen(p
) > prevargsize
- 10) {
677 prevargsize
+= ARG_BUF_DELTA
;
678 prevarg
= nasm_realloc(prevarg
, prevargsize
);
684 /* Function to process args from a string of args, rather than the
685 * argv array. Used by the environment variable and response file
688 static void process_args(int8_t *args
)
690 int8_t *p
, *q
, *arg
, *prevarg
;
691 int8_t separator
= ' ';
699 while (*p
&& *p
!= separator
)
701 while (*p
== separator
)
705 if (process_arg(prevarg
, arg
))
709 process_arg(arg
, NULL
);
712 static void parse_cmdline(int argc
, int8_t **argv
)
715 int8_t *envreal
, *envcopy
= NULL
, *p
, *arg
;
717 *inname
= *outname
= *listname
= '\0';
720 * First, process the NASMENV environment variable.
722 envreal
= getenv("NASMENV");
725 envcopy
= nasm_strdup(envreal
);
726 process_args(envcopy
);
731 * Now process the actual command line.
736 if (argv
[0][0] == '@') {
737 /* We have a response file, so process this as a set of
738 * arguments like the environment variable. This allows us
739 * to have multiple arguments on a single line, which is
740 * different to the -@resp file processing below for regular
743 int8_t *str
= malloc(2048);
744 FILE *f
= fopen(&argv
[0][1], "r");
746 printf("out of memory");
750 while (fgets(str
, 2048, f
)) {
759 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
760 if ((p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
))) {
761 if ((rfile
= fopen(p
, "r"))) {
762 process_respfile(rfile
);
765 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
766 "unable to open response file `%s'", p
);
769 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
770 argv
+= i
, argc
-= i
;
774 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
775 "no input file specified");
778 static void assemble_file(int8_t *fname
)
780 int8_t *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
782 int i
, rn_error
, validid
;
784 struct tokenval tokval
;
787 int pass_cnt
= 0; /* count actual passes */
789 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
790 report_error(ERR_FATAL
, "command line: "
791 "32-bit segment size requires a higher cpu");
793 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
794 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
795 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
799 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
800 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
801 /* pass0 seq is 0, 0, 0,..., 1, 2 */
803 def_label
= pass
> 1 ? redefine_label
: define_label
;
805 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
809 nasmlist
.init(listname
, report_error
);
812 global_offset_changed
= FALSE
; /* set by redefine_label */
813 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
816 saa_rewind(forwrefs
);
817 forwref
= saa_rstruct(forwrefs
);
819 offsets
= raa_init();
821 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
824 location
.known
= TRUE
;
825 location
.offset
= offs
= GET_CURR_OFFS
;
827 while ((line
= preproc
->getline())) {
830 /* here we parse our directives; this is not handled by the 'real'
833 if ((i
= getkw(&directive
, &value
))) {
835 case 1: /* [SEGMENT n] */
836 seg
= ofmt
->section(value
, pass2
, &sb
);
838 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
839 "segment name `%s' not recognized",
843 location
.segment
= seg
;
846 case 2: /* [EXTERN label:special] */
848 value
++; /* skip initial $ if present */
851 while (*q
&& *q
!= ':')
855 ofmt
->symdef(value
, 0L, 0L, 3, q
);
857 } else if (pass
== 1) { /* pass == 1 */
862 while (*q
&& *q
!= ':') {
868 report_error(ERR_NONFATAL
,
869 "identifier expected after EXTERN");
877 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
879 pass0
= 1; /* fake pass 1 in labels.c */
880 declare_as_global(value
, special
,
882 define_label(value
, seg_alloc(), 0L, NULL
,
883 FALSE
, TRUE
, ofmt
, report_error
);
886 } /* else pass0 == 1 */
888 case 3: /* [BITS bits] */
889 globalbits
= sb
= get_bits(value
);
891 case 4: /* [GLOBAL symbol:special] */
893 value
++; /* skip initial $ if present */
894 if (pass0
== 2) { /* pass 2 */
896 while (*q
&& *q
!= ':')
900 ofmt
->symdef(value
, 0L, 0L, 3, q
);
902 } else if (pass2
== 1) { /* pass == 1 */
907 while (*q
&& *q
!= ':') {
913 report_error(ERR_NONFATAL
,
914 "identifier expected after GLOBAL");
922 declare_as_global(value
, special
, report_error
);
925 case 5: /* [COMMON symbol size:special] */
927 value
++; /* skip initial $ if present */
933 while (*p
&& !isspace(*p
)) {
939 report_error(ERR_NONFATAL
,
940 "identifier expected after COMMON");
946 while (*p
&& isspace(*p
))
949 while (*q
&& *q
!= ':')
956 size
= readnum(p
, &rn_error
);
958 report_error(ERR_NONFATAL
,
959 "invalid size specified"
960 " in COMMON declaration");
962 define_common(value
, seg_alloc(), size
,
963 special
, ofmt
, report_error
);
965 report_error(ERR_NONFATAL
,
966 "no size specified in"
967 " COMMON declaration");
968 } else if (pass0
== 2) { /* pass == 2 */
970 while (*q
&& *q
!= ':') {
977 ofmt
->symdef(value
, 0L, 0L, 3, q
);
981 case 6: /* [ABSOLUTE address] */
983 stdscan_bufptr
= value
;
984 tokval
.t_type
= TOKEN_INVALID
;
985 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
989 report_error(pass0
==
990 1 ? ERR_NONFATAL
: ERR_PANIC
,
991 "cannot use non-relocatable expression as "
994 abs_seg
= reloc_seg(e
);
995 abs_offset
= reloc_value(e
);
997 } else if (pass
== 1)
998 abs_offset
= 0x100; /* don't go near zero in case of / */
1000 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1003 location
.segment
= NO_SEG
;
1011 while (*p
&& !isspace(*p
)) {
1018 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1019 "identifier expected after DEBUG");
1022 while (*p
&& isspace(*p
))
1024 if (pass
== pass_max
)
1025 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1027 case 8: /* [WARNING {+|-}warn-name] */
1029 while (*value
&& isspace(*value
))
1032 if (*value
== '+' || *value
== '-') {
1033 validid
= (*value
== '-') ? TRUE
: FALSE
;
1038 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1039 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1041 if (i
<= ERR_WARN_MAX
)
1042 suppressed
[i
] = validid
;
1044 report_error(ERR_NONFATAL
,
1045 "invalid warning id in WARNING directive");
1049 cpu
= get_cpu(value
);
1051 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1052 while (*value
&& isspace(*value
))
1055 if (*value
== '+') {
1058 if (*value
== '-') {
1061 report_error(ERR_NONFATAL
,
1062 "invalid parameter to \"list\" directive");
1067 if (!ofmt
->directive(directive
, value
, pass2
))
1068 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1069 "unrecognised directive [%s]",
1072 } else { /* it isn't a directive */
1074 parse_line(pass1
, line
, &output_ins
,
1075 report_error
, evaluate
, def_label
);
1077 if (!(optimizing
> 0) && pass
== 2) {
1078 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1079 output_ins
.forw_ref
= TRUE
;
1081 output_ins
.oprs
[forwref
->operand
].opflags
|=
1083 forwref
= saa_rstruct(forwrefs
);
1084 } while (forwref
!= NULL
1085 && forwref
->lineno
== globallineno
);
1087 output_ins
.forw_ref
= FALSE
;
1090 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1092 for (i
= 0; i
< output_ins
.operands
; i
++) {
1093 if (output_ins
.oprs
[i
].
1094 opflags
& OPFLAG_FORWARD
) {
1095 struct forwrefinfo
*fwinf
=
1096 (struct forwrefinfo
*)
1097 saa_wstruct(forwrefs
);
1098 fwinf
->lineno
= globallineno
;
1102 } else { /* pass == 2 */
1104 * Hack to prevent phase error in the code
1108 * If the second operand is a forward reference,
1109 * the UNITY property of the number 1 in that
1110 * operand is cancelled. Otherwise the above
1111 * sequence will cause a phase error.
1113 * This hack means that the above code will
1114 * generate 286+ code.
1116 * The forward reference will mean that the
1117 * operand will not have the UNITY property on
1118 * the first pass, so the pass behaviours will
1122 if (output_ins
.operands
>= 2 &&
1123 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1125 output_ins
.oprs
[1].type
&=
1126 ~(ONENESS
| BYTENESS
);
1134 if (output_ins
.opcode
== I_EQU
) {
1137 * Special `..' EQUs get processed in pass two,
1138 * except `..@' macro-processor EQUs which are done
1139 * in the normal place.
1141 if (!output_ins
.label
)
1142 report_error(ERR_NONFATAL
,
1143 "EQU not preceded by label");
1145 else if (output_ins
.label
[0] != '.' ||
1146 output_ins
.label
[1] != '.' ||
1147 output_ins
.label
[2] == '@') {
1148 if (output_ins
.operands
== 1 &&
1149 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1150 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1153 opflags
& OPFLAG_EXTERN
;
1154 def_label(output_ins
.label
,
1155 output_ins
.oprs
[0].segment
,
1156 output_ins
.oprs
[0].offset
, NULL
,
1159 } else if (output_ins
.operands
== 2
1160 && (output_ins
.oprs
[0].
1162 && (output_ins
.oprs
[0].type
& COLON
)
1163 && output_ins
.oprs
[0].segment
==
1165 && output_ins
.oprs
[0].wrt
== NO_SEG
1166 && (output_ins
.oprs
[1].
1168 && output_ins
.oprs
[1].segment
==
1170 && output_ins
.oprs
[1].wrt
==
1172 def_label(output_ins
.label
,
1175 output_ins
.oprs
[1].offset
, NULL
,
1179 report_error(ERR_NONFATAL
,
1180 "bad syntax for EQU");
1182 } else { /* pass == 2 */
1184 * Special `..' EQUs get processed here, except
1185 * `..@' macro processor EQUs which are done above.
1187 if (output_ins
.label
[0] == '.' &&
1188 output_ins
.label
[1] == '.' &&
1189 output_ins
.label
[2] != '@') {
1190 if (output_ins
.operands
== 1 &&
1191 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1192 define_label(output_ins
.label
,
1193 output_ins
.oprs
[0].segment
,
1194 output_ins
.oprs
[0].offset
,
1195 NULL
, FALSE
, FALSE
, ofmt
,
1197 } else if (output_ins
.operands
== 2
1198 && (output_ins
.oprs
[0].
1200 && (output_ins
.oprs
[0].type
& COLON
)
1201 && output_ins
.oprs
[0].segment
==
1203 && (output_ins
.oprs
[1].
1205 && output_ins
.oprs
[1].segment
==
1207 define_label(output_ins
.label
,
1210 output_ins
.oprs
[1].offset
,
1211 NULL
, FALSE
, FALSE
, ofmt
,
1214 report_error(ERR_NONFATAL
,
1215 "bad syntax for EQU");
1218 } else { /* instruction isn't an EQU */
1222 int32_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1223 &output_ins
, report_error
);
1225 /* if (using_debug_info) && output_ins.opcode != -1) */
1226 if (using_debug_info
)
1227 { /* fbk 03/25/01 */
1228 /* this is done here so we can do debug type info */
1230 TYS_ELEMENTS(output_ins
.operands
);
1231 switch (output_ins
.opcode
) {
1234 TYS_ELEMENTS(output_ins
.oprs
[0].
1239 TYS_ELEMENTS(output_ins
.oprs
[0].
1244 TYS_ELEMENTS(output_ins
.oprs
[0].
1249 TYS_ELEMENTS(output_ins
.oprs
[0].
1254 TYS_ELEMENTS(output_ins
.oprs
[0].
1258 typeinfo
|= TY_BYTE
;
1261 typeinfo
|= TY_WORD
;
1264 if (output_ins
.eops_float
)
1265 typeinfo
|= TY_FLOAT
;
1267 typeinfo
|= TY_DWORD
;
1270 typeinfo
|= TY_QWORD
;
1273 typeinfo
|= TY_TBYTE
;
1276 typeinfo
= TY_LABEL
;
1280 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1285 SET_CURR_OFFS(offs
);
1288 * else l == -1 => invalid instruction, which will be
1289 * flagged as an error on pass 2
1292 } else { /* pass == 2 */
1293 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1294 &output_ins
, ofmt
, report_error
,
1296 SET_CURR_OFFS(offs
);
1300 cleanup_insn(&output_ins
);
1303 location
.offset
= offs
= GET_CURR_OFFS
;
1304 } /* end while (line = preproc->getline... */
1306 if (pass1
== 2 && global_offset_changed
)
1307 report_error(ERR_NONFATAL
,
1308 "phase error detected at end of assembly.");
1311 preproc
->cleanup(1);
1313 if (pass1
== 1 && terminate_after_phase
) {
1321 if (pass
> 1 && !global_offset_changed
) {
1324 pass
= pass_max
- 1;
1325 } else if (!(optimizing
> 0))
1328 } /* for (pass=1; pass<=2; pass++) */
1330 preproc
->cleanup(0);
1333 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1335 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1337 } /* exit from assemble_file (...) */
1339 static int getkw(int8_t **directive
, int8_t **value
)
1341 int8_t *p
, *q
, *buf
;
1345 /* allow leading spaces or tabs */
1346 while (*buf
== ' ' || *buf
== '\t')
1354 while (*p
&& *p
!= ']')
1362 while (*p
&& *p
!= ';') {
1369 *directive
= p
= buf
+ 1;
1370 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1377 while (isspace(*buf
))
1378 buf
++; /* beppu - skip leading whitespace */
1385 for (q
= p
; *q
; q
++)
1388 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1390 if (!nasm_stricmp(p
, "extern"))
1392 if (!nasm_stricmp(p
, "bits"))
1394 if (!nasm_stricmp(p
, "global"))
1396 if (!nasm_stricmp(p
, "common"))
1398 if (!nasm_stricmp(p
, "absolute"))
1400 if (!nasm_stricmp(p
, "debug"))
1402 if (!nasm_stricmp(p
, "warning"))
1404 if (!nasm_stricmp(p
, "cpu"))
1406 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1412 * gnu style error reporting
1413 * This function prints an error message to error_file in the
1414 * style used by GNU. An example would be:
1415 * file.asm:50: error: blah blah blah
1416 * where file.asm is the name of the file, 50 is the line number on
1417 * which the error occurs (or is detected) and "error:" is one of
1418 * the possible optional diagnostics -- it can be "error" or "warning"
1419 * or something else. Finally the line terminates with the actual
1422 * @param severity the severity of the warning or error
1423 * @param fmt the printf style format string
1425 static void report_error_gnu(int severity
, const int8_t *fmt
, ...)
1429 if (is_suppressed_warning(severity
))
1432 if (severity
& ERR_NOFILE
)
1433 fputs("nasm: ", error_file
);
1435 int8_t *currentfile
= NULL
;
1437 src_get(&lineno
, ¤tfile
);
1438 fprintf(error_file
, "%s:%ld: ", currentfile
, lineno
);
1439 nasm_free(currentfile
);
1442 report_error_common(severity
, fmt
, ap
);
1447 * MS style error reporting
1448 * This function prints an error message to error_file in the
1449 * style used by Visual C and some other Microsoft tools. An example
1451 * file.asm(50) : error: blah blah blah
1452 * where file.asm is the name of the file, 50 is the line number on
1453 * which the error occurs (or is detected) and "error:" is one of
1454 * the possible optional diagnostics -- it can be "error" or "warning"
1455 * or something else. Finally the line terminates with the actual
1458 * @param severity the severity of the warning or error
1459 * @param fmt the printf style format string
1461 static void report_error_vc(int severity
, const int8_t *fmt
, ...)
1465 if (is_suppressed_warning(severity
))
1468 if (severity
& ERR_NOFILE
)
1469 fputs("nasm: ", error_file
);
1471 int8_t *currentfile
= NULL
;
1473 src_get(&lineno
, ¤tfile
);
1474 fprintf(error_file
, "%s(%ld) : ", currentfile
, lineno
);
1475 nasm_free(currentfile
);
1478 report_error_common(severity
, fmt
, ap
);
1483 * check for supressed warning
1484 * checks for suppressed warning or pass one only warning and we're
1487 * @param severity the severity of the warning or error
1488 * @return true if we should abort error/warning printing
1490 static int is_suppressed_warning(int severity
)
1493 * See if it's a suppressed warning.
1495 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1496 (severity
& ERR_WARN_MASK
) != 0 &&
1497 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1499 * See if it's a pass-one only warning and we're not in pass one.
1501 ((severity
& ERR_PASS1
) && pass0
== 2);
1505 * common error reporting
1506 * This is the common back end of the error reporting schemes currently
1507 * implemented. It prints the nature of the warning and then the
1508 * specific error message to error_file and may or may not return. It
1509 * doesn't return if the error severity is a "panic" or "debug" type.
1511 * @param severity the severity of the warning or error
1512 * @param fmt the printf style format string
1514 static void report_error_common(int severity
, const int8_t *fmt
,
1517 switch (severity
& ERR_MASK
) {
1519 fputs("warning: ", error_file
);
1522 fputs("error: ", error_file
);
1525 fputs("fatal: ", error_file
);
1528 fputs("panic: ", error_file
);
1531 fputs("debug: ", error_file
);
1535 vfprintf(error_file
, fmt
, args
);
1536 fputc('\n', error_file
);
1538 if (severity
& ERR_USAGE
)
1541 switch (severity
& ERR_MASK
) {
1544 /* no further action, by definition */
1547 /* hack enables listing(!) on errors */
1548 terminate_after_phase
= TRUE
;
1557 exit(1); /* instantly die */
1558 break; /* placate silly compilers */
1561 /* abort(); *//* halt, catch fire, and dump core */
1567 static void usage(void)
1569 fputs("type `nasm -h' for help\n", error_file
);
1572 static void register_output_formats(void)
1574 ofmt
= ofmt_register(report_error
);
1577 #define BUF_DELTA 512
1579 static FILE *no_pp_fp
;
1580 static efunc no_pp_err
;
1581 static ListGen
*no_pp_list
;
1582 static int32_t no_pp_lineinc
;
1584 static void no_pp_reset(int8_t *file
, int pass
, efunc error
, evalfunc eval
,
1587 src_set_fname(nasm_strdup(file
));
1591 no_pp_fp
= fopen(file
, "r");
1593 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1594 "unable to open input file `%s'", file
);
1595 no_pp_list
= listgen
;
1596 (void)pass
; /* placate compilers */
1597 (void)eval
; /* placate compilers */
1600 static int8_t *no_pp_getline(void)
1602 int8_t *buffer
, *p
, *q
;
1605 bufsize
= BUF_DELTA
;
1606 buffer
= nasm_malloc(BUF_DELTA
);
1607 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1609 while (1) { /* Loop to handle %line */
1612 while (1) { /* Loop to handle long lines */
1613 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1617 if (p
> buffer
&& p
[-1] == '\n')
1619 if (p
- buffer
> bufsize
- 10) {
1621 offset
= p
- buffer
;
1622 bufsize
+= BUF_DELTA
;
1623 buffer
= nasm_realloc(buffer
, bufsize
);
1624 p
= buffer
+ offset
;
1628 if (!q
&& p
== buffer
) {
1634 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1635 * them are present at the end of the line.
1637 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1639 if (!strncmp(buffer
, "%line", 5)) {
1642 int8_t *nm
= nasm_malloc(strlen(buffer
));
1643 if (sscanf(buffer
+ 5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1644 nasm_free(src_set_fname(nm
));
1654 no_pp_list
->line(LIST_READ
, buffer
);
1659 static void no_pp_cleanup(int pass
)
1664 static uint32_t get_cpu(int8_t *value
)
1667 if (!strcmp(value
, "8086"))
1669 if (!strcmp(value
, "186"))
1671 if (!strcmp(value
, "286"))
1673 if (!strcmp(value
, "386"))
1675 if (!strcmp(value
, "486"))
1677 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1679 if (!strcmp(value
, "686") ||
1680 !nasm_stricmp(value
, "ppro") ||
1681 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1683 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1685 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1686 !nasm_stricmp(value
, "willamette"))
1687 return IF_WILLAMETTE
;
1688 if (!nasm_stricmp(value
, "prescott"))
1690 if (!nasm_stricmp(value
, "x64") ||
1691 !nasm_stricmp(value
, "x86-64"))
1693 if (!nasm_stricmp(value
, "ia64") ||
1694 !nasm_stricmp(value
, "ia-64") ||
1695 !nasm_stricmp(value
, "itanium") ||
1696 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1699 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1700 "unknown 'cpu' type");
1702 return IF_PLEVEL
; /* the maximum level */
1705 static int get_bits(int8_t *value
)
1709 if ((i
= atoi(value
)) == 16)
1710 return i
; /* set for a 16-bit segment */
1713 report_error(ERR_NONFATAL
,
1714 "cannot specify 32-bit segment on processor below a 386");
1717 } else if (i
== 64) {
1719 report_error(ERR_NONFATAL
,
1720 "cannot specify 64-bit segment on processor below an x86-64");
1724 report_error(ERR_NONFATAL
,
1725 "%s output format does not support 64-bit code",
1730 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1731 "`%s' is not a valid segment size; must be 16, 32 or 64",