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(char *value
);
33 static uint32_t get_cpu(char *cpu_str
);
34 static void parse_cmdline(int, char **);
35 static void assemble_file(char *);
36 static int getkw(char **directive
, char **value
);
37 static void register_output_formats(void);
38 static void report_error_gnu(int severity
, const char *fmt
, ...);
39 static void report_error_vc(int severity
, const char *fmt
, ...);
40 static void report_error_common(int severity
, const char *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 char inname
[FILENAME_MAX
];
52 static char outname
[FILENAME_MAX
];
53 static char 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 char 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 char *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 char *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(char *, int, efunc
, evalfunc
, ListGen
*);
123 static char *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 char *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 char *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 %"PRId32
"+%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 char *get_param(char *p
, char *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(char *p
, char *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 char *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 char *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(char *args
)
690 char *p
, *q
, *arg
, *prevarg
;
691 char 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
, char **argv
)
715 char *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 char *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 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
);
762 rfile
= fopen(p
, "r");
764 process_respfile(rfile
);
767 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
768 "unable to open response file `%s'", p
);
771 i
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
772 argv
+= i
, argc
-= i
;
776 report_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
777 "no input file specified");
780 static void assemble_file(char *fname
)
782 char *directive
, *value
, *p
, *q
, *special
, *line
, debugid
[80];
784 int i
, rn_error
, validid
;
786 struct tokenval tokval
;
789 int pass_cnt
= 0; /* count actual passes */
791 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
792 report_error(ERR_FATAL
, "command line: "
793 "32-bit segment size requires a higher cpu");
795 pass_max
= (optimizing
> 0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
796 pass0
= !(optimizing
> 0); /* start at 1 if not optimizing */
797 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
801 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
802 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
803 /* pass0 seq is 0, 0, 0,..., 1, 2 */
805 def_label
= pass
> 1 ? redefine_label
: define_label
;
807 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
811 nasmlist
.init(listname
, report_error
);
814 global_offset_changed
= FALSE
; /* set by redefine_label */
815 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
818 saa_rewind(forwrefs
);
819 forwref
= saa_rstruct(forwrefs
);
821 offsets
= raa_init();
823 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
826 location
.known
= TRUE
;
827 location
.offset
= offs
= GET_CURR_OFFS
;
829 while ((line
= preproc
->getline())) {
832 /* here we parse our directives; this is not handled by the 'real'
835 i
= getkw(&directive
, &value
);
838 case 1: /* [SEGMENT n] */
839 seg
= ofmt
->section(value
, pass2
, &sb
);
841 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
842 "segment name `%s' not recognized",
846 location
.segment
= seg
;
849 case 2: /* [EXTERN label:special] */
851 value
++; /* skip initial $ if present */
854 while (*q
&& *q
!= ':')
858 ofmt
->symdef(value
, 0L, 0L, 3, q
);
860 } else if (pass
== 1) { /* pass == 1 */
865 while (*q
&& *q
!= ':') {
871 report_error(ERR_NONFATAL
,
872 "identifier expected after EXTERN");
880 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
882 pass0
= 1; /* fake pass 1 in labels.c */
883 declare_as_global(value
, special
,
885 define_label(value
, seg_alloc(), 0L, NULL
,
886 FALSE
, TRUE
, ofmt
, report_error
);
889 } /* else pass0 == 1 */
891 case 3: /* [BITS bits] */
892 globalbits
= sb
= get_bits(value
);
894 case 4: /* [GLOBAL symbol:special] */
896 value
++; /* skip initial $ if present */
897 if (pass0
== 2) { /* pass 2 */
899 while (*q
&& *q
!= ':')
903 ofmt
->symdef(value
, 0L, 0L, 3, q
);
905 } else if (pass2
== 1) { /* pass == 1 */
910 while (*q
&& *q
!= ':') {
916 report_error(ERR_NONFATAL
,
917 "identifier expected after GLOBAL");
925 declare_as_global(value
, special
, report_error
);
928 case 5: /* [COMMON symbol size:special] */
930 value
++; /* skip initial $ if present */
936 while (*p
&& !isspace(*p
)) {
942 report_error(ERR_NONFATAL
,
943 "identifier expected after COMMON");
949 while (*p
&& isspace(*p
))
952 while (*q
&& *q
!= ':')
959 size
= readnum(p
, &rn_error
);
961 report_error(ERR_NONFATAL
,
962 "invalid size specified"
963 " in COMMON declaration");
965 define_common(value
, seg_alloc(), size
,
966 special
, ofmt
, report_error
);
968 report_error(ERR_NONFATAL
,
969 "no size specified in"
970 " COMMON declaration");
971 } else if (pass0
== 2) { /* pass == 2 */
973 while (*q
&& *q
!= ':') {
980 ofmt
->symdef(value
, 0L, 0L, 3, q
);
984 case 6: /* [ABSOLUTE address] */
986 stdscan_bufptr
= value
;
987 tokval
.t_type
= TOKEN_INVALID
;
988 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
992 report_error(pass0
==
993 1 ? ERR_NONFATAL
: ERR_PANIC
,
994 "cannot use non-relocatable expression as "
997 abs_seg
= reloc_seg(e
);
998 abs_offset
= reloc_value(e
);
1000 } else if (pass
== 1)
1001 abs_offset
= 0x100; /* don't go near zero in case of / */
1003 report_error(ERR_PANIC
, "invalid ABSOLUTE address "
1006 location
.segment
= NO_SEG
;
1014 while (*p
&& !isspace(*p
)) {
1021 report_error(pass
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1022 "identifier expected after DEBUG");
1025 while (*p
&& isspace(*p
))
1027 if (pass
== pass_max
)
1028 ofmt
->current_dfmt
->debug_directive(debugid
, p
);
1030 case 8: /* [WARNING {+|-}warn-name] */
1032 while (*value
&& isspace(*value
))
1035 if (*value
== '+' || *value
== '-') {
1036 validid
= (*value
== '-') ? TRUE
: FALSE
;
1041 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1042 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1044 if (i
<= ERR_WARN_MAX
)
1045 suppressed
[i
] = validid
;
1047 report_error(ERR_NONFATAL
,
1048 "invalid warning id in WARNING directive");
1052 cpu
= get_cpu(value
);
1054 case 10: /* fbk 9/2/00 *//* [LIST {+|-}] */
1055 while (*value
&& isspace(*value
))
1058 if (*value
== '+') {
1061 if (*value
== '-') {
1064 report_error(ERR_NONFATAL
,
1065 "invalid parameter to \"list\" directive");
1070 if (!ofmt
->directive(directive
, value
, pass2
))
1071 report_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1072 "unrecognised directive [%s]",
1075 } else { /* it isn't a directive */
1077 parse_line(pass1
, line
, &output_ins
,
1078 report_error
, evaluate
, def_label
);
1080 if (!(optimizing
> 0) && pass
== 2) {
1081 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1082 output_ins
.forw_ref
= TRUE
;
1084 output_ins
.oprs
[forwref
->operand
].opflags
|=
1086 forwref
= saa_rstruct(forwrefs
);
1087 } while (forwref
!= NULL
1088 && forwref
->lineno
== globallineno
);
1090 output_ins
.forw_ref
= FALSE
;
1093 if (!(optimizing
> 0) && output_ins
.forw_ref
) {
1095 for (i
= 0; i
< output_ins
.operands
; i
++) {
1096 if (output_ins
.oprs
[i
].
1097 opflags
& OPFLAG_FORWARD
) {
1098 struct forwrefinfo
*fwinf
=
1099 (struct forwrefinfo
*)
1100 saa_wstruct(forwrefs
);
1101 fwinf
->lineno
= globallineno
;
1105 } else { /* pass == 2 */
1107 * Hack to prevent phase error in the code
1111 * If the second operand is a forward reference,
1112 * the UNITY property of the number 1 in that
1113 * operand is cancelled. Otherwise the above
1114 * sequence will cause a phase error.
1116 * This hack means that the above code will
1117 * generate 286+ code.
1119 * The forward reference will mean that the
1120 * operand will not have the UNITY property on
1121 * the first pass, so the pass behaviours will
1125 if (output_ins
.operands
>= 2 &&
1126 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1128 output_ins
.oprs
[1].type
&=
1129 ~(ONENESS
| BYTENESS
);
1137 if (output_ins
.opcode
== I_EQU
) {
1140 * Special `..' EQUs get processed in pass two,
1141 * except `..@' macro-processor EQUs which are done
1142 * in the normal place.
1144 if (!output_ins
.label
)
1145 report_error(ERR_NONFATAL
,
1146 "EQU not preceded by label");
1148 else if (output_ins
.label
[0] != '.' ||
1149 output_ins
.label
[1] != '.' ||
1150 output_ins
.label
[2] == '@') {
1151 if (output_ins
.operands
== 1 &&
1152 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1153 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1156 opflags
& OPFLAG_EXTERN
;
1157 def_label(output_ins
.label
,
1158 output_ins
.oprs
[0].segment
,
1159 output_ins
.oprs
[0].offset
, NULL
,
1162 } else if (output_ins
.operands
== 2
1163 && (output_ins
.oprs
[0].
1165 && (output_ins
.oprs
[0].type
& COLON
)
1166 && output_ins
.oprs
[0].segment
==
1168 && output_ins
.oprs
[0].wrt
== NO_SEG
1169 && (output_ins
.oprs
[1].
1171 && output_ins
.oprs
[1].segment
==
1173 && output_ins
.oprs
[1].wrt
==
1175 def_label(output_ins
.label
,
1178 output_ins
.oprs
[1].offset
, NULL
,
1182 report_error(ERR_NONFATAL
,
1183 "bad syntax for EQU");
1185 } else { /* pass == 2 */
1187 * Special `..' EQUs get processed here, except
1188 * `..@' macro processor EQUs which are done above.
1190 if (output_ins
.label
[0] == '.' &&
1191 output_ins
.label
[1] == '.' &&
1192 output_ins
.label
[2] != '@') {
1193 if (output_ins
.operands
== 1 &&
1194 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1195 define_label(output_ins
.label
,
1196 output_ins
.oprs
[0].segment
,
1197 output_ins
.oprs
[0].offset
,
1198 NULL
, FALSE
, FALSE
, ofmt
,
1200 } else if (output_ins
.operands
== 2
1201 && (output_ins
.oprs
[0].
1203 && (output_ins
.oprs
[0].type
& COLON
)
1204 && output_ins
.oprs
[0].segment
==
1206 && (output_ins
.oprs
[1].
1208 && output_ins
.oprs
[1].segment
==
1210 define_label(output_ins
.label
,
1213 output_ins
.oprs
[1].offset
,
1214 NULL
, FALSE
, FALSE
, ofmt
,
1217 report_error(ERR_NONFATAL
,
1218 "bad syntax for EQU");
1221 } else { /* instruction isn't an EQU */
1225 int32_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1226 &output_ins
, report_error
);
1228 /* if (using_debug_info) && output_ins.opcode != -1) */
1229 if (using_debug_info
)
1230 { /* fbk 03/25/01 */
1231 /* this is done here so we can do debug type info */
1233 TYS_ELEMENTS(output_ins
.operands
);
1234 switch (output_ins
.opcode
) {
1237 TYS_ELEMENTS(output_ins
.oprs
[0].
1242 TYS_ELEMENTS(output_ins
.oprs
[0].
1247 TYS_ELEMENTS(output_ins
.oprs
[0].
1252 TYS_ELEMENTS(output_ins
.oprs
[0].
1257 TYS_ELEMENTS(output_ins
.oprs
[0].
1261 typeinfo
|= TY_BYTE
;
1264 typeinfo
|= TY_WORD
;
1267 if (output_ins
.eops_float
)
1268 typeinfo
|= TY_FLOAT
;
1270 typeinfo
|= TY_DWORD
;
1273 typeinfo
|= TY_QWORD
;
1276 typeinfo
|= TY_TBYTE
;
1279 typeinfo
= TY_LABEL
;
1283 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1288 SET_CURR_OFFS(offs
);
1291 * else l == -1 => invalid instruction, which will be
1292 * flagged as an error on pass 2
1295 } else { /* pass == 2 */
1296 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1297 &output_ins
, ofmt
, report_error
,
1299 SET_CURR_OFFS(offs
);
1303 cleanup_insn(&output_ins
);
1306 location
.offset
= offs
= GET_CURR_OFFS
;
1307 } /* end while (line = preproc->getline... */
1309 if (pass1
== 2 && global_offset_changed
)
1310 report_error(ERR_NONFATAL
,
1311 "phase error detected at end of assembly.");
1314 preproc
->cleanup(1);
1316 if (pass1
== 1 && terminate_after_phase
) {
1324 if (pass
> 1 && !global_offset_changed
) {
1327 pass
= pass_max
- 1;
1328 } else if (!(optimizing
> 0))
1331 } /* for (pass=1; pass<=2; pass++) */
1333 preproc
->cleanup(0);
1336 if (optimizing
> 0 && opt_verbose_info
) /* -On and -Ov switches */
1338 "info:: assembly required 1+%d+1 passes\n", pass_cnt
- 2);
1340 } /* exit from assemble_file (...) */
1342 static int getkw(char **directive
, char **value
)
1348 /* allow leading spaces or tabs */
1349 while (*buf
== ' ' || *buf
== '\t')
1357 while (*p
&& *p
!= ']')
1365 while (*p
&& *p
!= ';') {
1372 *directive
= p
= buf
+ 1;
1373 while (*buf
&& *buf
!= ' ' && *buf
!= ']' && *buf
!= '\t')
1380 while (isspace(*buf
))
1381 buf
++; /* beppu - skip leading whitespace */
1388 for (q
= p
; *q
; q
++)
1391 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1393 if (!nasm_stricmp(p
, "extern"))
1395 if (!nasm_stricmp(p
, "bits"))
1397 if (!nasm_stricmp(p
, "global"))
1399 if (!nasm_stricmp(p
, "common"))
1401 if (!nasm_stricmp(p
, "absolute"))
1403 if (!nasm_stricmp(p
, "debug"))
1405 if (!nasm_stricmp(p
, "warning"))
1407 if (!nasm_stricmp(p
, "cpu"))
1409 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1415 * gnu style error reporting
1416 * This function prints an error message to error_file in the
1417 * style used by GNU. An example would be:
1418 * file.asm:50: error: blah blah blah
1419 * where file.asm is the name of the file, 50 is the line number on
1420 * which the error occurs (or is detected) and "error:" is one of
1421 * the possible optional diagnostics -- it can be "error" or "warning"
1422 * or something else. Finally the line terminates with the actual
1425 * @param severity the severity of the warning or error
1426 * @param fmt the printf style format string
1428 static void report_error_gnu(int severity
, const char *fmt
, ...)
1432 if (is_suppressed_warning(severity
))
1435 if (severity
& ERR_NOFILE
)
1436 fputs("nasm: ", error_file
);
1438 char *currentfile
= NULL
;
1440 src_get(&lineno
, ¤tfile
);
1441 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1442 nasm_free(currentfile
);
1445 report_error_common(severity
, fmt
, ap
);
1450 * MS style error reporting
1451 * This function prints an error message to error_file in the
1452 * style used by Visual C and some other Microsoft tools. An example
1454 * file.asm(50) : error: blah blah blah
1455 * where file.asm is the name of the file, 50 is the line number on
1456 * which the error occurs (or is detected) and "error:" is one of
1457 * the possible optional diagnostics -- it can be "error" or "warning"
1458 * or something else. Finally the line terminates with the actual
1461 * @param severity the severity of the warning or error
1462 * @param fmt the printf style format string
1464 static void report_error_vc(int severity
, const char *fmt
, ...)
1468 if (is_suppressed_warning(severity
))
1471 if (severity
& ERR_NOFILE
)
1472 fputs("nasm: ", error_file
);
1474 char *currentfile
= NULL
;
1476 src_get(&lineno
, ¤tfile
);
1477 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1478 nasm_free(currentfile
);
1481 report_error_common(severity
, fmt
, ap
);
1486 * check for supressed warning
1487 * checks for suppressed warning or pass one only warning and we're
1490 * @param severity the severity of the warning or error
1491 * @return true if we should abort error/warning printing
1493 static int is_suppressed_warning(int severity
)
1496 * See if it's a suppressed warning.
1498 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1499 (severity
& ERR_WARN_MASK
) != 0 &&
1500 suppressed
[(severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1502 * See if it's a pass-one only warning and we're not in pass one.
1504 ((severity
& ERR_PASS1
) && pass0
== 2);
1508 * common error reporting
1509 * This is the common back end of the error reporting schemes currently
1510 * implemented. It prints the nature of the warning and then the
1511 * specific error message to error_file and may or may not return. It
1512 * doesn't return if the error severity is a "panic" or "debug" type.
1514 * @param severity the severity of the warning or error
1515 * @param fmt the printf style format string
1517 static void report_error_common(int severity
, const char *fmt
,
1520 switch (severity
& ERR_MASK
) {
1522 fputs("warning: ", error_file
);
1525 fputs("error: ", error_file
);
1528 fputs("fatal: ", error_file
);
1531 fputs("panic: ", error_file
);
1534 fputs("debug: ", error_file
);
1538 vfprintf(error_file
, fmt
, args
);
1539 fputc('\n', error_file
);
1541 if (severity
& ERR_USAGE
)
1544 switch (severity
& ERR_MASK
) {
1547 /* no further action, by definition */
1550 /* hack enables listing(!) on errors */
1551 terminate_after_phase
= TRUE
;
1560 exit(1); /* instantly die */
1561 break; /* placate silly compilers */
1564 /* abort(); *//* halt, catch fire, and dump core */
1570 static void usage(void)
1572 fputs("type `nasm -h' for help\n", error_file
);
1575 static void register_output_formats(void)
1577 ofmt
= ofmt_register(report_error
);
1580 #define BUF_DELTA 512
1582 static FILE *no_pp_fp
;
1583 static efunc no_pp_err
;
1584 static ListGen
*no_pp_list
;
1585 static int32_t no_pp_lineinc
;
1587 static void no_pp_reset(char *file
, int pass
, efunc error
, evalfunc eval
,
1590 src_set_fname(nasm_strdup(file
));
1594 no_pp_fp
= fopen(file
, "r");
1596 no_pp_err(ERR_FATAL
| ERR_NOFILE
,
1597 "unable to open input file `%s'", file
);
1598 no_pp_list
= listgen
;
1599 (void)pass
; /* placate compilers */
1600 (void)eval
; /* placate compilers */
1603 static char *no_pp_getline(void)
1605 char *buffer
, *p
, *q
;
1608 bufsize
= BUF_DELTA
;
1609 buffer
= nasm_malloc(BUF_DELTA
);
1610 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1612 while (1) { /* Loop to handle %line */
1615 while (1) { /* Loop to handle long lines */
1616 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
1620 if (p
> buffer
&& p
[-1] == '\n')
1622 if (p
- buffer
> bufsize
- 10) {
1624 offset
= p
- buffer
;
1625 bufsize
+= BUF_DELTA
;
1626 buffer
= nasm_realloc(buffer
, bufsize
);
1627 p
= buffer
+ offset
;
1631 if (!q
&& p
== buffer
) {
1637 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1638 * them are present at the end of the line.
1640 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1642 if (!strncmp(buffer
, "%line", 5)) {
1645 char *nm
= nasm_malloc(strlen(buffer
));
1646 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
1647 nasm_free(src_set_fname(nm
));
1657 no_pp_list
->line(LIST_READ
, buffer
);
1662 static void no_pp_cleanup(int pass
)
1664 (void)pass
; /* placate GCC */
1668 static uint32_t get_cpu(char *value
)
1671 if (!strcmp(value
, "8086"))
1673 if (!strcmp(value
, "186"))
1675 if (!strcmp(value
, "286"))
1677 if (!strcmp(value
, "386"))
1679 if (!strcmp(value
, "486"))
1681 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
1683 if (!strcmp(value
, "686") ||
1684 !nasm_stricmp(value
, "ppro") ||
1685 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
1687 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
1689 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1690 !nasm_stricmp(value
, "willamette"))
1691 return IF_WILLAMETTE
;
1692 if (!nasm_stricmp(value
, "prescott"))
1694 if (!nasm_stricmp(value
, "x64") ||
1695 !nasm_stricmp(value
, "x86-64"))
1697 if (!nasm_stricmp(value
, "ia64") ||
1698 !nasm_stricmp(value
, "ia-64") ||
1699 !nasm_stricmp(value
, "itanium") ||
1700 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
1703 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1704 "unknown 'cpu' type");
1706 return IF_PLEVEL
; /* the maximum level */
1709 static int get_bits(char *value
)
1713 if ((i
= atoi(value
)) == 16)
1714 return i
; /* set for a 16-bit segment */
1717 report_error(ERR_NONFATAL
,
1718 "cannot specify 32-bit segment on processor below a 386");
1721 } else if (i
== 64) {
1723 report_error(ERR_NONFATAL
,
1724 "cannot specify 64-bit segment on processor below an x86-64");
1728 report_error(ERR_NONFATAL
,
1729 "%s output format does not support 64-bit code",
1734 report_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
1735 "`%s' is not a valid segment size; must be 16, 32 or 64",