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.
26 struct forwrefinfo
{ /* info held on forward refs. */
31 static int get_bits (char *value
);
32 static unsigned long get_cpu (char *cpu_str
);
33 static void parse_cmdline (int, char **);
34 static void assemble_file (char *);
35 static int getkw (char *buf
, char **value
);
36 static void register_output_formats(void);
37 static void report_error_gnu (int severity
, const char *fmt
, ...);
38 static void report_error_vc (int severity
, const char *fmt
, ...);
39 static void report_error_common (int severity
, const char *fmt
, va_list args
);
40 static int is_suppressed_warning (int severity
);
41 static void usage(void);
42 static efunc report_error
;
44 static int using_debug_info
, opt_verbose_info
;
45 int tasm_compatible_mode
= FALSE
;
48 static char inname
[FILENAME_MAX
];
49 static char outname
[FILENAME_MAX
];
50 static char listname
[FILENAME_MAX
];
51 static int globallineno
; /* for forward-reference tracking */
52 /* static int pass = 0; */
53 static struct ofmt
*ofmt
= NULL
;
55 static FILE *error_file
; /* Where to write error messages */
57 static FILE *ofile
= NULL
;
58 int optimizing
= -1; /* number of optimization passes to take */
59 static int sb
, cmd_sb
= 16; /* by default */
60 static unsigned long cmd_cpu
= IF_PLEVEL
; /* highest level by default */
61 static unsigned long cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
62 int global_offset_changed
; /* referenced in labels.c */
64 static loc_t location
;
65 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
66 long abs_seg
; /* ABSOLUTE segment basis */
67 long abs_offset
; /* ABSOLUTE offset */
69 static struct RAA
*offsets
;
71 static struct SAA
*forwrefs
; /* keep track of forward references */
72 static struct forwrefinfo
*forwref
;
74 static Preproc
*preproc
;
76 op_normal
, /* Preprocess and assemble */
77 op_preprocess
, /* Preprocess only */
78 op_depend
/* Generate dependencies */
80 static enum op_type operating_mode
;
83 * Which of the suppressible warnings are suppressed. Entry zero
84 * doesn't do anything. Initial defaults are given here.
86 static char suppressed
[1+ERR_WARN_MAX
] = {
87 0, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
91 * The option names for the suppressible warnings. As before, entry
94 static const char *suppressed_names
[1+ERR_WARN_MAX
] = {
95 NULL
, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
100 * The explanations for the suppressible warnings. As before, entry
103 static const char *suppressed_what
[1+ERR_WARN_MAX
] = {
105 "macro calls with wrong no. of params",
106 "cyclic macro self-references",
107 "labels alone on lines without trailing `:'",
108 "numeric constants greater than 0xFFFFFFFF",
109 "using 8- or 16-bit relocation in ELF, a GNU extension"
113 * This is a null preprocessor which just copies lines from input
114 * to output. It's used when someone explicitly requests that NASM
115 * not preprocess their source file.
118 static void no_pp_reset (char *, int, efunc
, evalfunc
, ListGen
*);
119 static char *no_pp_getline (void);
120 static void no_pp_cleanup (int);
121 static Preproc no_pp
= {
128 * get/set current offset...
130 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
131 raa_read(offsets,location.segment))
132 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
133 (void)(offsets=raa_write(offsets,location.segment,(x))))
135 static int want_usage
;
136 static int terminate_after_phase
;
137 int user_nolist
= 0; /* fbk 9/2/00 */
139 static void nasm_fputs(const char *line
, FILE *outfile
)
142 fputs(line
, outfile
);
143 fputc('\n', outfile
);
148 int main(int argc
, char **argv
)
151 want_usage
= terminate_after_phase
= FALSE
;
152 report_error
= report_error_gnu
;
154 nasm_set_malloc_error (report_error
);
155 offsets
= raa_init();
156 forwrefs
= saa_init ((long)sizeof(struct forwrefinfo
));
159 operating_mode
= op_normal
;
165 register_output_formats();
167 parse_cmdline(argc
, argv
);
169 if (terminate_after_phase
)
177 pp_extra_stdmac (ofmt
->stdmac
);
178 parser_global_info (ofmt
, &location
);
179 eval_global_info (ofmt
, lookup_label
, &location
);
181 /* define some macros dependent of command-line */
184 sprintf (temp
, "__OUTPUT_FORMAT__=%s\n", ofmt
->shortname
);
185 pp_pre_define (temp
);
188 switch ( operating_mode
) {
192 preproc
->reset (inname
, 0, report_error
, evaluate
, &nasmlist
);
193 if (outname
[0] == '\0')
194 ofmt
->filename (inname
, outname
, report_error
);
196 fprintf(stdout
, "%s: %s", outname
, inname
);
197 while ( (line
= preproc
->getline()) )
207 char *file_name
= NULL
;
212 ofile
= fopen(outname
, "w");
214 report_error (ERR_FATAL
| ERR_NOFILE
,
215 "unable to open output file `%s'", outname
);
219 location
.known
= FALSE
;
222 preproc
->reset (inname
, 2, report_error
, evaluate
, &nasmlist
);
223 while ( (line
= preproc
->getline()) ) {
225 * We generate %line directives if needed for later programs
227 long linnum
= prior_linnum
+= lineinc
;
228 int altline
= src_get(&linnum
, &file_name
);
230 if (altline
==1 && lineinc
==1)
231 nasm_fputs("", ofile
);
233 lineinc
= (altline
!= -1 || lineinc
!=1);
234 fprintf(ofile
? ofile
: stdout
, "%%line %ld+%d %s\n",
235 linnum
, lineinc
, file_name
);
237 prior_linnum
= linnum
;
239 nasm_fputs(line
, ofile
);
242 nasm_free(file_name
);
246 if (ofile
&& terminate_after_phase
)
254 * We must call ofmt->filename _anyway_, even if the user
255 * has specified their own output file, because some
256 * formats (eg OBJ and COFF) use ofmt->filename to find out
257 * the name of the input file and then put that inside the
260 ofmt
->filename (inname
, outname
, report_error
);
262 ofile
= fopen(outname
, "wb");
264 report_error (ERR_FATAL
| ERR_NOFILE
,
265 "unable to open output file `%s'", outname
);
269 * We must call init_labels() before ofmt->init() since
270 * some object formats will want to define labels in their
271 * init routines. (eg OS/2 defines the FLAT group)
275 ofmt
->init (ofile
, report_error
, define_label
, evaluate
);
277 assemble_file (inname
);
279 if (!terminate_after_phase
) {
280 ofmt
->cleanup (using_debug_info
);
284 * We had an fclose on the output file here, but we
285 * actually do that in all the object file drivers as well,
286 * so we're leaving out the one here.
305 if (terminate_after_phase
)
313 * Get a parameter for a command line option.
314 * First arg must be in the form of e.g. -f...
316 static char *get_param (char *p
, char *q
, int *advance
)
319 if (p
[2]) /* the parameter's in the option */
331 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
332 "option `-%c' requires an argument",
344 #define OPT_POSTFIX 1
345 struct textargs textopts
[] =
347 {"prefix",OPT_PREFIX
},
348 {"postfix",OPT_POSTFIX
},
354 static int process_arg (char *p
, char *q
)
362 if (p
[0]=='-' && ! stopoptions
)
368 case 'o': /* these parameters take values */
383 if ( !(param
= get_param (p
, q
, &advance
)) )
385 if (p
[1]=='o') { /* output file */
386 strcpy (outname
, param
);
387 } else if (p
[1]=='f') { /* output format */
388 ofmt
= ofmt_find(param
);
390 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
391 "unrecognised output format `%s' - "
392 "use -hf for a list",
396 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
397 } else if (p
[1]=='O') { /* Optimization level */
401 if (isdigit(*param
)) {
403 while(isdigit(*++param
)) ;
404 if (opt
<=0) optimizing
= -1; /* 0.98 behaviour */
405 else if (opt
==1) optimizing
= 0; /* Two passes, 0.98.09 behavior */
406 else if (opt
<=3) optimizing
= opt
*5; /* Multiple passes */
407 else optimizing
= opt
; /* Multiple passes */
409 if (*param
== 'v' || *param
== '+') {
411 opt_verbose_info
= TRUE
;
413 } else { /* garbage */
418 } /* while (*param) */
419 if (opt
== -99) report_error(ERR_FATAL
,
420 "command line optimization level must be 'v', 0..3 or <nn>");
421 } else if (p
[1]=='P' || p
[1]=='p') { /* pre-include */
422 pp_pre_include (param
);
423 } else if (p
[1]=='D' || p
[1]=='d') { /* pre-define */
424 pp_pre_define (param
);
425 } else if (p
[1]=='U' || p
[1]=='u') { /* un-define */
426 pp_pre_undefine (param
);
427 } else if (p
[1]=='I' || p
[1]=='i') { /* include search path */
428 pp_include_path (param
);
429 } else if (p
[1]=='l') { /* listing file */
430 strcpy (listname
, param
);
431 } else if (p
[1]=='E') { /* error messages file */
432 error_file
= fopen(param
, "w");
434 error_file
= stderr
; /* Revert to default! */
435 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
436 "cannot open file `%s' for error messages",
439 } else if (p
[1] == 'F') { /* specify debug format */
440 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
441 if (!ofmt
->current_dfmt
) {
442 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
443 "unrecognized debug format `%s' for"
444 " output format `%s'",
445 param
, ofmt
->shortname
);
447 } else if (p
[1] == 'X') { /* specify error reporting format */
448 if (nasm_stricmp("vc", param
) == 0)
449 report_error
= report_error_vc
;
450 else if (nasm_stricmp("gnu", param
) == 0)
451 report_error
= report_error_gnu
;
453 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
454 "unrecognized error reporting format `%s'",
459 using_debug_info
= TRUE
;
462 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
464 " [options...] [--] filename\n"
465 " or nasm -r for version info (obsolete)\n"
466 " or nasm -v for version info (preferred)\n\n"
467 " -t Assemble in SciTech TASM compatible mode\n"
468 " -g Generate debug information in selected format.\n");
469 printf(" -e preprocess only (writes output to stdout by default)\n"
470 " -a don't preprocess (assemble only)\n"
471 " -M generate Makefile dependencies on stdout\n\n"
472 " -E<file> redirect error messages to file\n"
473 " -s redirect error messages to stdout\n\n"
474 " -F format select a debugging format\n\n"
475 " -I<path> adds a pathname to the include file path\n");
476 printf(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
477 " -P<file> pre-includes a file\n"
478 " -D<macro>[=<value>] pre-defines a macro\n"
479 " -U<macro> undefines a macro\n"
480 " -X<format> specifies error reporting format (gnu or vc)\n"
481 " -w+foo enables warnings about foo; -w-foo disables them\n"
482 "where foo can be:\n");
483 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
484 printf(" %-23s %s (default %s)\n",
485 suppressed_names
[i
], suppressed_what
[i
],
486 suppressed
[i
] ? "off" : "on");
487 printf ("\nresponse files should contain command line parameters"
488 ", one per line.\n");
490 printf("\nvalid output formats for -f are"
491 " (`*' denotes default):\n");
492 ofmt_list(ofmt
, stdout
);
495 printf ("\nFor a list of valid output formats, use -hf.\n");
496 printf ("For a list of debug formats, use -f <form> -y.\n");
498 exit (0); /* never need usage message here */
501 printf("\nvalid debug formats for '%s' output format are"
502 " ('*' denotes default):\n",
504 dfmt_list(ofmt
, stdout
);
508 tasm_compatible_mode
= TRUE
;
513 const char *nasm_version_string
=
514 "NASM version " NASM_VER
" compiled on " __DATE__
519 puts(nasm_version_string
);
520 exit (0); /* never need usage message here */
523 case 'e': /* preprocess only */
524 operating_mode
= op_preprocess
;
526 case 'a': /* assemble only - don't preprocess */
530 if (p
[2] != '+' && p
[2] != '-') {
531 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
532 "invalid option to `-w'");
534 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
535 if (!nasm_stricmp(p
+3, suppressed_names
[i
]))
537 if (i
<= ERR_WARN_MAX
)
538 suppressed
[i
] = (p
[2] == '-');
540 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
541 "invalid option to `-w'");
545 operating_mode
= op_depend
;
552 if (p
[2]==0) { /* -- => stop processing options */
556 for(s
=0; textopts
[s
].label
; s
++)
558 if(!nasm_stricmp(p
+2, textopts
[s
].label
))
572 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
573 "option `--%s' requires an argument",
579 advance
= 1, param
= q
;
584 strncpy(lprefix
,param
,PREFIX_MAX
-1);
585 lprefix
[PREFIX_MAX
-1]=0;
590 strncpy(lpostfix
,param
,POSTFIX_MAX
-1);
591 lpostfix
[POSTFIX_MAX
-1]=0;
598 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
599 "unrecognised option `--%s'",
608 if (!ofmt
->setinfo(GI_SWITCH
,&p
))
609 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
610 "unrecognised option `-%c'",
618 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
619 "more than one input file specified");
627 #define ARG_BUF_DELTA 128
629 static void process_respfile (FILE *rfile
)
631 char *buffer
, *p
, *q
, *prevarg
;
632 int bufsize
, prevargsize
;
634 bufsize
= prevargsize
= ARG_BUF_DELTA
;
635 buffer
= nasm_malloc(ARG_BUF_DELTA
);
636 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
639 while (1) { /* Loop to handle all lines in file */
642 while (1) { /* Loop to handle long lines */
643 q
= fgets(p
, bufsize
-(p
-buffer
), rfile
);
647 if (p
> buffer
&& p
[-1] == '\n')
649 if (p
-buffer
> bufsize
-10) {
652 bufsize
+= ARG_BUF_DELTA
;
653 buffer
= nasm_realloc(buffer
, bufsize
);
658 if (!q
&& p
== buffer
) {
660 process_arg (prevarg
, NULL
);
667 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
668 * them are present at the end of the line.
670 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
672 while (p
> buffer
&& isspace(p
[-1]))
679 if (process_arg (prevarg
, p
))
682 if (strlen(p
) > prevargsize
-10) {
683 prevargsize
+= ARG_BUF_DELTA
;
684 prevarg
= nasm_realloc(prevarg
, prevargsize
);
690 /* Function to process args from a string of args, rather than the
691 * argv array. Used by the environment variable and response file
694 static void process_args (char *args
) {
695 char *p
, *q
, *arg
, *prevarg
;
696 char separator
= ' ';
704 while (*p
&& *p
!= separator
) p
++;
705 while (*p
== separator
) *p
++ = '\0';
708 if (process_arg (prevarg
, arg
))
712 process_arg (arg
, NULL
);
715 static void parse_cmdline(int argc
, char **argv
)
718 char *envreal
, *envcopy
=NULL
, *p
, *arg
;
720 *inname
= *outname
= *listname
= '\0';
723 * First, process the NASMENV environment variable.
725 envreal
= getenv("NASMENV");
728 envcopy
= nasm_strdup(envreal
);
729 process_args(envcopy
);
734 * Now process the actual command line.
740 if (argv
[0][0] == '@') {
741 /* We have a response file, so process this as a set of
742 * arguments like the environment variable. This allows us
743 * to have multiple arguments on a single line, which is
744 * different to the -@resp file processing below for regular
747 char *str
= malloc(2048);
748 FILE *f
= fopen(&argv
[0][1],"r");
750 printf("out of memory");
754 while (fgets(str
,2048,f
)) {
763 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
764 if ((p
= get_param (argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
))) {
765 if ((rfile
= fopen(p
, "r"))) {
766 process_respfile (rfile
);
769 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
770 "unable to open response file `%s'", p
);
773 i
= process_arg (argv
[0], argc
> 1 ? argv
[1] : NULL
);
774 argv
+= i
, argc
-= i
;
778 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
779 "no input file specified");
783 static void assemble_file (char *fname
)
785 char * value
, * p
, * q
, * special
, * line
, debugid
[80];
787 int i
, rn_error
, validid
;
789 struct tokenval tokval
;
792 int pass_cnt
= 0; /* count actual passes */
794 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
795 report_error(ERR_FATAL
, "command line: "
796 "32-bit segment size requires a higher cpu");
798 pass_max
= (optimizing
>0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
799 pass0
= !(optimizing
>0); /* start at 1 if not optimizing */
800 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
804 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
805 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
806 /* pass0 seq is 0, 0, 0,..., 1, 2 */
808 def_label
= pass
> 1 ? redefine_label
: define_label
;
811 sb
= cmd_sb
; /* set 'bits' to command line default */
815 nasmlist
.init(listname
, report_error
);
818 global_offset_changed
= FALSE
; /* set by redefine_label */
819 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
821 saa_rewind (forwrefs
);
822 forwref
= saa_rstruct (forwrefs
);
824 offsets
= raa_init();
826 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
828 if (pass
== 1) location
.known
= TRUE
;
829 location
.offset
= offs
= GET_CURR_OFFS
;
831 while ( (line
= preproc
->getline()) )
835 /* here we parse our directives; this is not handled by the 'real'
837 if ( (i
= getkw (line
, &value
)) )
840 case 1: /* [SEGMENT n] */
841 seg
= ofmt
->section (value
, pass2
, &sb
);
843 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
844 "segment name `%s' not recognised",
848 location
.segment
= seg
;
851 case 2: /* [EXTERN label:special] */
852 if (*value
== '$') value
++; /* skip initial $ if present */
855 while (*q
&& *q
!= ':')
859 ofmt
->symdef(value
, 0L, 0L, 3, q
);
861 } else if (pass
== 1) { /* pass == 1 */
866 while (*q
&& *q
!= ':') {
872 report_error (ERR_NONFATAL
,
873 "identifier expected after EXTERN");
881 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
883 pass0
= 1; /* fake pass 1 in labels.c */
884 declare_as_global (value
, special
, report_error
);
885 define_label (value
, seg_alloc(), 0L, NULL
, FALSE
, TRUE
,
889 } /* else pass0 == 1 */
891 case 3: /* [BITS bits] */
892 sb
= get_bits(value
);
894 case 4: /* [GLOBAL symbol:special] */
895 if (*value
== '$') value
++; /* skip initial $ if present */
896 if (pass0
== 2) { /* pass 2 */
898 while (*q
&& *q
!= ':')
902 ofmt
->symdef(value
, 0L, 0L, 3, q
);
904 } else if (pass2
== 1) { /* pass == 1 */
909 while (*q
&& *q
!= ':') {
915 report_error (ERR_NONFATAL
,
916 "identifier expected after GLOBAL");
924 declare_as_global (value
, special
, report_error
);
927 case 5: /* [COMMON symbol size:special] */
928 if (*value
== '$') value
++; /* skip initial $ if present */
934 while (*p
&& !isspace(*p
)) {
940 report_error (ERR_NONFATAL
,
941 "identifier expected after COMMON");
947 while (*p
&& isspace(*p
))
950 while (*q
&& *q
!= ':')
957 size
= readnum (p
, &rn_error
);
959 report_error (ERR_NONFATAL
, "invalid size specified"
960 " in COMMON declaration");
962 define_common (value
, seg_alloc(), size
,
963 special
, ofmt
, report_error
);
965 report_error (ERR_NONFATAL
, "no size specified in"
966 " COMMON declaration");
967 } else if (pass0
== 2) { /* pass == 2 */
969 while (*q
&& *q
!= ':') {
976 ofmt
->symdef(value
, 0L, 0L, 3, q
);
980 case 6: /* [ABSOLUTE address] */
982 stdscan_bufptr
= value
;
983 tokval
.t_type
= TOKEN_INVALID
;
984 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, report_error
,
988 report_error (pass0
==1 ? ERR_NONFATAL
: ERR_PANIC
,
989 "cannot use non-relocatable expression as "
992 abs_seg
= reloc_seg(e
);
993 abs_offset
= reloc_value(e
);
996 if (pass
==1) abs_offset
= 0x100;/* don't go near zero in case of / */
997 else report_error (ERR_PANIC
, "invalid ABSOLUTE address "
1000 location
.segment
= NO_SEG
;
1008 while (*p
&& !isspace(*p
)) {
1015 report_error (pass
==1 ? ERR_NONFATAL
: ERR_PANIC
,
1016 "identifier expected after DEBUG");
1019 while (*p
&& isspace(*p
)) p
++;
1020 if (pass
==pass_max
) ofmt
->current_dfmt
->debug_directive (debugid
, p
);
1022 case 8: /* [WARNING {+|-}warn-name] */
1024 while (*value
&& isspace(*value
))
1027 if (*value
== '+' || *value
== '-') {
1028 validid
= (*value
== '-') ? TRUE
: FALSE
;
1033 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
1034 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1036 if (i
<= ERR_WARN_MAX
)
1037 suppressed
[i
] = validid
;
1039 report_error (ERR_NONFATAL
, "invalid warning id in WARNING directive");
1043 cpu
= get_cpu (value
);
1045 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1046 while (*value
&& isspace(*value
))
1049 if (*value
== '+') {
1053 if (*value
== '-') {
1057 report_error (ERR_NONFATAL
, "invalid parameter to \"list\" directive");
1062 if (!ofmt
->directive (line
+1, value
, pass2
))
1063 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
1064 "unrecognised directive [%s]",
1068 else /* it isn't a directive */
1070 parse_line (pass1
, line
, &output_ins
,
1071 report_error
, evaluate
,
1074 if (!(optimizing
>0) && pass
== 2) {
1075 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1076 output_ins
.forw_ref
= TRUE
;
1078 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1079 forwref
= saa_rstruct (forwrefs
);
1080 } while (forwref
!= NULL
&& forwref
->lineno
== globallineno
);
1082 output_ins
.forw_ref
= FALSE
;
1086 if (!(optimizing
>0) && output_ins
.forw_ref
)
1089 for(i
= 0; i
< output_ins
.operands
; i
++)
1091 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
)
1093 struct forwrefinfo
*fwinf
=
1094 (struct forwrefinfo
*)saa_wstruct(forwrefs
);
1095 fwinf
->lineno
= globallineno
;
1099 } else { /* pass == 2 */
1101 * Hack to prevent phase error in the code
1105 * If the second operand is a forward reference,
1106 * the UNITY property of the number 1 in that
1107 * operand is cancelled. Otherwise the above
1108 * sequence will cause a phase error.
1110 * This hack means that the above code will
1111 * generate 286+ code.
1113 * The forward reference will mean that the
1114 * operand will not have the UNITY property on
1115 * the first pass, so the pass behaviours will
1119 if (output_ins
.operands
>= 2 &&
1120 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1122 output_ins
.oprs
[1].type
&= ~(ONENESS
|BYTENESS
);
1130 if (output_ins
.opcode
== I_EQU
) {
1134 * Special `..' EQUs get processed in pass two,
1135 * except `..@' macro-processor EQUs which are done
1136 * in the normal place.
1138 if (!output_ins
.label
)
1139 report_error (ERR_NONFATAL
,
1140 "EQU not preceded by label");
1142 else if (output_ins
.label
[0] != '.' ||
1143 output_ins
.label
[1] != '.' ||
1144 output_ins
.label
[2] == '@')
1146 if (output_ins
.operands
== 1 &&
1147 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1148 output_ins
.oprs
[0].wrt
== NO_SEG
)
1150 int isext
= output_ins
.oprs
[0].opflags
& OPFLAG_EXTERN
;
1151 def_label (output_ins
.label
,
1152 output_ins
.oprs
[0].segment
,
1153 output_ins
.oprs
[0].offset
,
1154 NULL
, FALSE
, isext
, ofmt
, report_error
);
1156 else if (output_ins
.operands
== 2 &&
1157 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1158 (output_ins
.oprs
[0].type
& COLON
) &&
1159 output_ins
.oprs
[0].segment
== NO_SEG
&&
1160 output_ins
.oprs
[0].wrt
== NO_SEG
&&
1161 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1162 output_ins
.oprs
[1].segment
== NO_SEG
&&
1163 output_ins
.oprs
[1].wrt
== NO_SEG
)
1165 def_label (output_ins
.label
,
1166 output_ins
.oprs
[0].offset
| SEG_ABS
,
1167 output_ins
.oprs
[1].offset
,
1168 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1171 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1173 } else { /* pass == 2 */
1175 * Special `..' EQUs get processed here, except
1176 * `..@' macro processor EQUs which are done above.
1178 if (output_ins
.label
[0] == '.' &&
1179 output_ins
.label
[1] == '.' &&
1180 output_ins
.label
[2] != '@')
1182 if (output_ins
.operands
== 1 &&
1183 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1184 define_label (output_ins
.label
,
1185 output_ins
.oprs
[0].segment
,
1186 output_ins
.oprs
[0].offset
,
1187 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1189 else if (output_ins
.operands
== 2 &&
1190 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1191 (output_ins
.oprs
[0].type
& COLON
) &&
1192 output_ins
.oprs
[0].segment
== NO_SEG
&&
1193 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1194 output_ins
.oprs
[1].segment
== NO_SEG
)
1196 define_label (output_ins
.label
,
1197 output_ins
.oprs
[0].offset
| SEG_ABS
,
1198 output_ins
.oprs
[1].offset
,
1199 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1202 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1205 } else { /* instruction isn't an EQU */
1209 long l
= insn_size (location
.segment
, offs
, sb
, cpu
,
1210 &output_ins
, report_error
);
1212 /* if (using_debug_info) && output_ins.opcode != -1)*/
1213 if (using_debug_info
) /* fbk 03/25/01 */
1216 /* this is done here so we can do debug type info */
1217 long typeinfo
= TYS_ELEMENTS(output_ins
.operands
);
1218 switch (output_ins
.opcode
) {
1220 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
1223 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
1226 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
1229 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
1232 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
1235 typeinfo
|= TY_BYTE
;
1238 typeinfo
|= TY_WORD
;
1241 if (output_ins
.eops_float
)
1242 typeinfo
|= TY_FLOAT
;
1244 typeinfo
|= TY_DWORD
;
1247 typeinfo
|= TY_QWORD
;
1250 typeinfo
|= TY_TBYTE
;
1253 typeinfo
= TY_LABEL
;
1257 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1262 SET_CURR_OFFS (offs
);
1265 * else l == -1 => invalid instruction, which will be
1266 * flagged as an error on pass 2
1269 } else { /* pass == 2 */
1270 offs
+= assemble (location
.segment
, offs
, sb
, cpu
,
1271 &output_ins
, ofmt
, report_error
, &nasmlist
);
1272 SET_CURR_OFFS (offs
);
1276 cleanup_insn (&output_ins
);
1279 location
.offset
= offs
= GET_CURR_OFFS
;
1280 } /* end while (line = preproc->getline... */
1282 if (pass1
==2 && global_offset_changed
)
1283 report_error(ERR_NONFATAL
, "phase error detected at end of assembly.");
1285 if (pass1
== 1) preproc
->cleanup(1);
1287 if (pass1
==1 && terminate_after_phase
) {
1295 if (pass
>1 && !global_offset_changed
) {
1297 if (pass0
==2) pass
= pass_max
- 1;
1298 } else if (!(optimizing
>0)) pass0
++;
1300 } /* for (pass=1; pass<=2; pass++) */
1302 preproc
->cleanup(0);
1305 if (optimizing
>0 && opt_verbose_info
) /* -On and -Ov switches */
1307 "info:: assembly required 1+%d+1 passes\n", pass_cnt
-2);
1309 } /* exit from assemble_file (...) */
1312 static int getkw (char *buf
, char **value
)
1316 /* allow leading spaces or tabs */
1317 while (*buf
==' ' || *buf
=='\t')
1325 while (*p
&& *p
!= ']') p
++;
1332 while (*p
&& *p
!= ';') {
1340 while (*buf
&& *buf
!=' ' && *buf
!=']' && *buf
!='\t')
1347 while (isspace(*buf
)) buf
++; /* beppu - skip leading whitespace */
1349 while (*buf
!=']') buf
++;
1356 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1358 if (!nasm_stricmp(p
, "extern"))
1360 if (!nasm_stricmp(p
, "bits"))
1362 if (!nasm_stricmp(p
, "global"))
1364 if (!nasm_stricmp(p
, "common"))
1366 if (!nasm_stricmp(p
, "absolute"))
1368 if (!nasm_stricmp(p
, "debug"))
1370 if (!nasm_stricmp(p
, "warning"))
1372 if (!nasm_stricmp(p
, "cpu"))
1374 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1380 * gnu style error reporting
1381 * This function prints an error message to error_file in the
1382 * style used by GNU. An example would be:
1383 * file.asm:50: error: blah blah blah
1384 * where file.asm is the name of the file, 50 is the line number on
1385 * which the error occurs (or is detected) and "error:" is one of
1386 * the possible optional diagnostics -- it can be "error" or "warning"
1387 * or something else. Finally the line terminates with the actual
1390 * @param severity the severity of the warning or error
1391 * @param fmt the printf style format string
1393 static void report_error_gnu (int severity
, const char *fmt
, ...)
1397 if (is_suppressed_warning(severity
))
1400 if (severity
& ERR_NOFILE
)
1401 fputs ("nasm: ", error_file
);
1403 char * currentfile
= NULL
;
1405 src_get (&lineno
, ¤tfile
);
1406 fprintf (error_file
, "%s:%ld: ", currentfile
, lineno
);
1407 nasm_free (currentfile
);
1410 report_error_common (severity
, fmt
, ap
);
1415 * MS style error reporting
1416 * This function prints an error message to error_file in the
1417 * style used by Visual C and some other Microsoft tools. An example
1419 * file.asm(50) : error: blah blah blah
1420 * where file.asm is the name of the file, 50 is the line number on
1421 * which the error occurs (or is detected) and "error:" is one of
1422 * the possible optional diagnostics -- it can be "error" or "warning"
1423 * or something else. Finally the line terminates with the actual
1426 * @param severity the severity of the warning or error
1427 * @param fmt the printf style format string
1429 static void report_error_vc (int severity
, const char *fmt
, ...)
1433 if (is_suppressed_warning (severity
))
1436 if (severity
& ERR_NOFILE
)
1437 fputs ("nasm: ", error_file
);
1439 char * currentfile
= NULL
;
1441 src_get (&lineno
, ¤tfile
);
1442 fprintf (error_file
, "%s(%ld) : ", currentfile
, lineno
);
1443 nasm_free (currentfile
);
1446 report_error_common (severity
, fmt
, ap
);
1451 * check for supressed warning
1452 * checks for suppressed warning or pass one only warning and we're
1455 * @param severity the severity of the warning or error
1456 * @return true if we should abort error/warning printing
1458 static int is_suppressed_warning (int severity
)
1461 * See if it's a suppressed warning.
1463 return ((severity
& ERR_MASK
) == ERR_WARNING
&&
1464 (severity
& ERR_WARN_MASK
) != 0 &&
1465 suppressed
[ (severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
]) ||
1467 * See if it's a pass-one only warning and we're not in pass one.
1469 ((severity
& ERR_PASS1
) && pass0
== 2);
1473 * common error reporting
1474 * This is the common back end of the error reporting schemes currently
1475 * implemented. It prints the nature of the warning and then the
1476 * specific error message to error_file and may or may not return. It
1477 * doesn't return if the error severity is a "panic" or "debug" type.
1479 * @param severity the severity of the warning or error
1480 * @param fmt the printf style format string
1482 static void report_error_common (int severity
, const char *fmt
, va_list args
)
1484 switch (severity
& ERR_MASK
) {
1486 fputs ("warning: ", error_file
); break;
1488 fputs ("error: ", error_file
); break;
1490 fputs ("fatal: ", error_file
); break;
1492 fputs ("panic: ", error_file
); break;
1494 fputs("debug: ", error_file
); break;
1497 vfprintf (error_file
, fmt
, args
);
1498 fputc ('\n', error_file
);
1500 if (severity
& ERR_USAGE
)
1503 switch (severity
& ERR_MASK
) {
1504 case ERR_WARNING
: case ERR_DEBUG
:
1505 /* no further action, by definition */
1508 /* hack enables listing(!) on errors */
1509 terminate_after_phase
= TRUE
;
1518 exit(1); /* instantly die */
1519 break; /* placate silly compilers */
1522 /* abort(); */ /* halt, catch fire, and dump core */
1528 static void usage(void)
1530 fputs("type `nasm -h' for help\n", error_file
);
1533 static void register_output_formats(void)
1535 ofmt
= ofmt_register (report_error
);
1538 #define BUF_DELTA 512
1540 static FILE *no_pp_fp
;
1541 static efunc no_pp_err
;
1542 static ListGen
*no_pp_list
;
1543 static long no_pp_lineinc
;
1545 static void no_pp_reset (char *file
, int pass
, efunc error
, evalfunc eval
,
1548 src_set_fname(nasm_strdup(file
));
1552 no_pp_fp
= fopen(file
, "r");
1554 no_pp_err (ERR_FATAL
| ERR_NOFILE
,
1555 "unable to open input file `%s'", file
);
1556 no_pp_list
= listgen
;
1557 (void) pass
; /* placate compilers */
1558 (void) eval
; /* placate compilers */
1561 static char *no_pp_getline (void)
1563 char *buffer
, *p
, *q
;
1566 bufsize
= BUF_DELTA
;
1567 buffer
= nasm_malloc(BUF_DELTA
);
1568 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1570 while (1) { /* Loop to handle %line */
1573 while (1) { /* Loop to handle long lines */
1574 q
= fgets(p
, bufsize
-(p
-buffer
), no_pp_fp
);
1578 if (p
> buffer
&& p
[-1] == '\n')
1580 if (p
-buffer
> bufsize
-10) {
1582 offset
= p
- buffer
;
1583 bufsize
+= BUF_DELTA
;
1584 buffer
= nasm_realloc(buffer
, bufsize
);
1585 p
= buffer
+ offset
;
1589 if (!q
&& p
== buffer
) {
1595 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1596 * them are present at the end of the line.
1598 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1600 if (!strncmp(buffer
, "%line", 5)) {
1603 char *nm
= nasm_malloc(strlen(buffer
));
1604 if (sscanf(buffer
+5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1605 nasm_free( src_set_fname(nm
) );
1615 no_pp_list
->line (LIST_READ
, buffer
);
1620 static void no_pp_cleanup (int pass
)
1625 static unsigned long get_cpu (char *value
)
1628 if (!strcmp(value
, "8086")) return IF_8086
;
1629 if (!strcmp(value
, "186")) return IF_186
;
1630 if (!strcmp(value
, "286")) return IF_286
;
1631 if (!strcmp(value
, "386")) return IF_386
;
1632 if (!strcmp(value
, "486")) return IF_486
;
1633 if (!strcmp(value
, "586") ||
1634 !nasm_stricmp(value
, "pentium") ) return IF_PENT
;
1635 if (!strcmp(value
, "686") ||
1636 !nasm_stricmp(value
, "ppro") ||
1637 !nasm_stricmp(value
, "pentiumpro") ||
1638 !nasm_stricmp(value
, "p2") ) return IF_P6
;
1639 if (!nasm_stricmp(value
, "p3") ||
1640 !nasm_stricmp(value
, "katmai") ) return IF_KATMAI
;
1641 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1642 !nasm_stricmp(value
, "willamette") ) return IF_WILLAMETTE
;
1643 if (!nasm_stricmp(value
, "prescott") ) return IF_PRESCOTT
;
1644 if (!nasm_stricmp(value
, "ia64") ||
1645 !nasm_stricmp(value
, "ia-64") ||
1646 !nasm_stricmp(value
, "itanium") ||
1647 !nasm_stricmp(value
, "itanic") ||
1648 !nasm_stricmp(value
, "merced") ) return IF_IA64
;
1650 report_error (pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
, "unknown 'cpu' type");
1652 return IF_PLEVEL
; /* the maximum level */
1656 static int get_bits (char *value
)
1660 if ((i
= atoi(value
)) == 16) return i
; /* set for a 16-bit segment */
1663 report_error(ERR_NONFATAL
,
1664 "cannot specify 32-bit segment on processor below a 386");
1668 report_error(pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
,
1669 "`%s' is not a valid segment size; must be 16 or 32",