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 (int severity
, const char *fmt
, ...);
38 static void usage(void);
40 static int using_debug_info
;
41 int tasm_compatible_mode
= FALSE
;
44 static char inname
[FILENAME_MAX
];
45 static char outname
[FILENAME_MAX
];
46 static char listname
[FILENAME_MAX
];
47 static int globallineno
; /* for forward-reference tracking */
48 /* static int pass = 0; */
49 static struct ofmt
*ofmt
= NULL
;
51 static FILE *error_file
; /* Where to write error messages */
53 static FILE *ofile
= NULL
;
54 int optimizing
= -1; /* number of optimization passes to take */
55 static int sb
, cmd_sb
= 16; /* by default */
56 static unsigned long cmd_cpu
= IF_PLEVEL
; /* highest level by default */
57 static unsigned long cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
58 int global_offset_changed
; /* referenced in labels.c */
60 static loc_t location
;
61 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
63 static struct RAA
*offsets
;
64 static long abs_offset
;
66 static struct SAA
*forwrefs
; /* keep track of forward references */
67 static struct forwrefinfo
*forwref
;
69 static Preproc
*preproc
;
71 op_normal
, /* Preprocess and assemble */
72 op_preprocess
, /* Preprocess only */
73 op_depend
/* Generate dependencies */
75 static enum op_type operating_mode
;
78 * Which of the suppressible warnings are suppressed. Entry zero
79 * doesn't do anything. Initial defaults are given here.
81 static char suppressed
[1+ERR_WARN_MAX
] = {
82 0, TRUE
, TRUE
, TRUE
, FALSE
, TRUE
86 * The option names for the suppressible warnings. As before, entry
89 static const char *suppressed_names
[1+ERR_WARN_MAX
] = {
90 NULL
, "macro-params", "macro-selfref", "orphan-labels", "number-overflow",
95 * The explanations for the suppressible warnings. As before, entry
98 static const char *suppressed_what
[1+ERR_WARN_MAX
] = {
100 "macro calls with wrong no. of params",
101 "cyclic macro self-references",
102 "labels alone on lines without trailing `:'",
103 "numeric constants greater than 0xFFFFFFFF",
104 "using 8- or 16-bit relocation in ELF, a GNU extension"
108 * This is a null preprocessor which just copies lines from input
109 * to output. It's used when someone explicitly requests that NASM
110 * not preprocess their source file.
113 static void no_pp_reset (char *, int, efunc
, evalfunc
, ListGen
*);
114 static char *no_pp_getline (void);
115 static void no_pp_cleanup (int);
116 static Preproc no_pp
= {
123 * get/set current offset...
125 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
126 raa_read(offsets,location.segment))
127 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
128 (void)(offsets=raa_write(offsets,location.segment,(x))))
130 static int want_usage
;
131 static int terminate_after_phase
;
132 int user_nolist
= 0; /* fbk 9/2/00 */
134 static void nasm_fputs(const char *line
, FILE *outfile
)
137 fputs(line
, outfile
);
138 fputc('\n', outfile
);
143 int main(int argc
, char **argv
)
146 want_usage
= terminate_after_phase
= FALSE
;
148 nasm_set_malloc_error (report_error
);
149 offsets
= raa_init();
150 forwrefs
= saa_init ((long)sizeof(struct forwrefinfo
));
153 operating_mode
= op_normal
;
159 register_output_formats();
161 parse_cmdline(argc
, argv
);
163 if (terminate_after_phase
)
171 pp_extra_stdmac (ofmt
->stdmac
);
172 parser_global_info (ofmt
, &location
);
173 eval_global_info (ofmt
, lookup_label
, &location
);
175 /* define some macros dependent of command-line */
178 sprintf (temp
, "__OUTPUT_FORMAT__=%s\n", ofmt
->shortname
);
179 pp_pre_define (temp
);
182 switch ( operating_mode
) {
186 preproc
->reset (inname
, 0, report_error
, evaluate
, &nasmlist
);
187 if (outname
[0] == '\0')
188 ofmt
->filename (inname
, outname
, report_error
);
190 fprintf(stdout
, "%s: %s", outname
, inname
);
191 while ( (line
= preproc
->getline()) )
201 char *file_name
= NULL
;
206 ofile
= fopen(outname
, "w");
208 report_error (ERR_FATAL
| ERR_NOFILE
,
209 "unable to open output file `%s'", outname
);
213 location
.known
= FALSE
;
216 preproc
->reset (inname
, 2, report_error
, evaluate
, &nasmlist
);
217 while ( (line
= preproc
->getline()) ) {
219 * We generate %line directives if needed for later programs
221 long linnum
= prior_linnum
+= lineinc
;
222 int altline
= src_get(&linnum
, &file_name
);
224 if (altline
==1 && lineinc
==1)
225 nasm_fputs("", ofile
);
227 lineinc
= (altline
!= -1 || lineinc
!=1);
228 fprintf(ofile
? ofile
: stdout
, "%%line %ld+%d %s\n",
229 linnum
, lineinc
, file_name
);
231 prior_linnum
= linnum
;
233 nasm_fputs(line
, ofile
);
236 nasm_free(file_name
);
240 if (ofile
&& terminate_after_phase
)
248 * We must call ofmt->filename _anyway_, even if the user
249 * has specified their own output file, because some
250 * formats (eg OBJ and COFF) use ofmt->filename to find out
251 * the name of the input file and then put that inside the
254 ofmt
->filename (inname
, outname
, report_error
);
256 ofile
= fopen(outname
, "wb");
258 report_error (ERR_FATAL
| ERR_NOFILE
,
259 "unable to open output file `%s'", outname
);
263 * We must call init_labels() before ofmt->init() since
264 * some object formats will want to define labels in their
265 * init routines. (eg OS/2 defines the FLAT group)
269 ofmt
->init (ofile
, report_error
, define_label
, evaluate
);
271 assemble_file (inname
);
273 if (!terminate_after_phase
) {
274 ofmt
->cleanup (using_debug_info
);
278 * We had an fclose on the output file here, but we
279 * actually do that in all the object file drivers as well,
280 * so we're leaving out the one here.
299 if (terminate_after_phase
)
307 * Get a parameter for a command line option.
308 * First arg must be in the form of e.g. -f...
310 static char *get_param (char *p
, char *q
, int *advance
)
313 if (p
[2]) /* the parameter's in the option */
325 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
326 "option `-%c' requires an argument",
338 #define OPT_POSTFIX 1
339 struct textargs textopts
[] =
341 {"prefix",OPT_PREFIX
},
342 {"postfix",OPT_POSTFIX
},
348 static int process_arg (char *p
, char *q
)
356 if (p
[0]=='-' && ! stopoptions
)
362 case 'o': /* these parameters take values */
374 if ( !(param
= get_param (p
, q
, &advance
)) )
376 if (p
[1]=='o') { /* output file */
377 strcpy (outname
, param
);
378 } else if (p
[1]=='f') { /* output format */
379 ofmt
= ofmt_find(param
);
381 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
382 "unrecognised output format `%s' - "
383 "use -hf for a list",
387 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
388 } else if (p
[1]=='O') { /* Optimization level */
390 if (!isdigit(*param
)) report_error(ERR_FATAL
,
391 "command line optimization level must be 0..3 or <nn>");
393 if (opt
<=0) optimizing
= -1; /* 0.98 behaviour */
394 else if (opt
==1) optimizing
= 0; /* Two passes, 0.98.09 behavior */
395 else if (opt
<=3) optimizing
= opt
*5; /* Multiple passes */
396 else optimizing
= opt
; /* Multiple passes */
397 } else if (p
[1]=='P' || p
[1]=='p') { /* pre-include */
398 pp_pre_include (param
);
399 } else if (p
[1]=='D' || p
[1]=='d') { /* pre-define */
400 pp_pre_define (param
);
401 } else if (p
[1]=='U' || p
[1]=='u') { /* un-define */
402 pp_pre_undefine (param
);
403 } else if (p
[1]=='I' || p
[1]=='i') { /* include search path */
404 pp_include_path (param
);
405 } else if (p
[1]=='l') { /* listing file */
406 strcpy (listname
, param
);
407 } else if (p
[1]=='E') { /* error messages file */
408 error_file
= fopen(param
, "w");
410 error_file
= stderr
; /* Revert to default! */
411 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
412 "cannot open file `%s' for error messages",
415 } else if (p
[1] == 'F') { /* specify debug format */
416 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
417 if (!ofmt
->current_dfmt
) {
418 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
419 "unrecognized debug format `%s' for"
420 " output format `%s'",
421 param
, ofmt
->shortname
);
426 using_debug_info
= TRUE
;
429 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
431 " [options...] [--] filename\n"
432 " or nasm -r for version info (obsolete)\n"
433 " or nasm -v for version info (preferred)\n\n"
434 " -t Assemble in SciTech TASM compatible mode\n"
435 " -g Generate debug information in selected format.\n");
436 printf(" -e preprocess only (writes output to stdout by default)\n"
437 " -a don't preprocess (assemble only)\n"
438 " -M generate Makefile dependencies on stdout\n\n"
439 " -E<file> redirect error messages to file\n"
440 " -s redirect error messages to stdout\n\n"
441 " -F format select a debugging format\n\n"
442 " -I<path> adds a pathname to the include file path\n");
443 printf(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
444 " -P<file> pre-includes a file\n"
445 " -D<macro>[=<value>] pre-defines a macro\n"
446 " -U<macro> undefines a macro\n"
447 " -w+foo enables warnings about foo; -w-foo disables them\n"
448 "where foo can be:\n");
449 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
450 printf(" %-23s %s (default %s)\n",
451 suppressed_names
[i
], suppressed_what
[i
],
452 suppressed
[i
] ? "off" : "on");
453 printf ("\nresponse files should contain command line parameters"
454 ", one per line.\n");
456 printf("\nvalid output formats for -f are"
457 " (`*' denotes default):\n");
458 ofmt_list(ofmt
, stdout
);
461 printf ("\nFor a list of valid output formats, use -hf.\n");
462 printf ("For a list of debug formats, use -f <form> -y.\n");
464 exit (0); /* never need usage message here */
467 printf("\nvalid debug formats for '%s' output format are"
468 " ('*' denotes default):\n",
470 dfmt_list(ofmt
, stdout
);
474 tasm_compatible_mode
= TRUE
;
478 printf("NASM version %s compiled "
482 "on " __DATE__
"\n", NASM_VER
);
483 exit (0); /* never need usage message here */
485 case 'e': /* preprocess only */
486 operating_mode
= op_preprocess
;
488 case 'a': /* assemble only - don't preprocess */
492 if (p
[2] != '+' && p
[2] != '-') {
493 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
494 "invalid option to `-w'");
496 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
497 if (!nasm_stricmp(p
+3, suppressed_names
[i
]))
499 if (i
<= ERR_WARN_MAX
)
500 suppressed
[i
] = (p
[2] == '-');
502 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
503 "invalid option to `-w'");
507 operating_mode
= op_depend
;
514 if (p
[2]==0) { /* -- => stop processing options */
518 for(s
=0; textopts
[s
].label
; s
++)
520 if(!nasm_stricmp(p
+2, textopts
[s
].label
))
534 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
535 "option `--%s' requires an argument",
541 advance
= 1, param
= q
;
546 strncpy(lprefix
,param
,PREFIX_MAX
-1);
547 lprefix
[PREFIX_MAX
-1]=0;
552 strncpy(lpostfix
,param
,POSTFIX_MAX
-1);
553 lpostfix
[POSTFIX_MAX
-1]=0;
560 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
561 "unrecognised option `--%s'",
570 if (!ofmt
->setinfo(GI_SWITCH
,&p
))
571 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
572 "unrecognised option `-%c'",
580 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
581 "more than one input file specified");
589 #define ARG_BUF_DELTA 128
591 static void process_respfile (FILE *rfile
)
593 char *buffer
, *p
, *q
, *prevarg
;
594 int bufsize
, prevargsize
;
596 bufsize
= prevargsize
= ARG_BUF_DELTA
;
597 buffer
= nasm_malloc(ARG_BUF_DELTA
);
598 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
601 while (1) { /* Loop to handle all lines in file */
604 while (1) { /* Loop to handle long lines */
605 q
= fgets(p
, bufsize
-(p
-buffer
), rfile
);
609 if (p
> buffer
&& p
[-1] == '\n')
611 if (p
-buffer
> bufsize
-10) {
614 bufsize
+= ARG_BUF_DELTA
;
615 buffer
= nasm_realloc(buffer
, bufsize
);
620 if (!q
&& p
== buffer
) {
622 process_arg (prevarg
, NULL
);
629 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
630 * them are present at the end of the line.
632 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
634 while (p
> buffer
&& isspace(p
[-1]))
641 if (process_arg (prevarg
, p
))
644 if (strlen(p
) > prevargsize
-10) {
645 prevargsize
+= ARG_BUF_DELTA
;
646 prevarg
= nasm_realloc(prevarg
, prevargsize
);
652 /* Function to process args from a string of args, rather than the
653 * argv array. Used by the environment variable and response file
656 static void process_args (char *args
) {
657 char *p
, *q
, *arg
, *prevarg
;
658 char separator
= ' ';
666 while (*p
&& *p
!= separator
) p
++;
667 while (*p
== separator
) *p
++ = '\0';
670 if (process_arg (prevarg
, arg
))
674 process_arg (arg
, NULL
);
677 static void parse_cmdline(int argc
, char **argv
)
680 char *envreal
, *envcopy
=NULL
, *p
, *arg
;
682 *inname
= *outname
= *listname
= '\0';
685 * First, process the NASMENV environment variable.
687 envreal
= getenv("NASMENV");
690 envcopy
= nasm_strdup(envreal
);
691 process_args(envcopy
);
696 * Now process the actual command line.
702 if (argv
[0][0] == '@') {
703 /* We have a response file, so process this as a set of
704 * arguments like the environment variable. This allows us
705 * to have multiple arguments on a single line, which is
706 * different to the -@resp file processing below for regular
709 char *str
= malloc(2048);
710 FILE *f
= fopen(&argv
[0][1],"r");
712 printf("out of memory");
716 while (fgets(str
,2048,f
)) {
725 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
726 if ((p
= get_param (argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
))) {
727 if ((rfile
= fopen(p
, "r"))) {
728 process_respfile (rfile
);
731 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
732 "unable to open response file `%s'", p
);
735 i
= process_arg (argv
[0], argc
> 1 ? argv
[1] : NULL
);
736 argv
+= i
, argc
-= i
;
740 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
741 "no input file specified");
745 static void assemble_file (char *fname
)
747 char * value
, * p
, * q
, * special
, * line
, debugid
[80];
749 int i
, rn_error
, validid
;
751 struct tokenval tokval
;
754 int pass_cnt
= 0; /* count actual passes */
756 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
757 report_error(ERR_FATAL
, "command line: "
758 "32-bit segment size requires a higher cpu");
760 pass_max
= (optimizing
>0 ? optimizing
: 0) + 2; /* passes 1, optimizing, then 2 */
761 pass0
= !(optimizing
>0); /* start at 1 if not optimizing */
762 for (pass
= 1; pass
<= pass_max
&& pass0
<= 2; pass
++) {
766 pass1
= pass
< pass_max
? 1 : 2; /* seq is 1, 1, 1,..., 1, 2 */
767 pass2
= pass
> 1 ? 2 : 1; /* seq is 1, 2, 2,..., 2, 2 */
768 /* pass0 seq is 0, 0, 0,..., 1, 2 */
770 def_label
= pass
> 1 ? redefine_label
: define_label
;
773 sb
= cmd_sb
; /* set 'bits' to command line default */
777 nasmlist
.init(listname
, report_error
);
780 global_offset_changed
= FALSE
; /* set by redefine_label */
781 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
783 saa_rewind (forwrefs
);
784 forwref
= saa_rstruct (forwrefs
);
786 offsets
= raa_init();
788 preproc
->reset(fname
, pass1
, report_error
, evaluate
, &nasmlist
);
790 if (pass
== 1) location
.known
= TRUE
;
791 location
.offset
= offs
= GET_CURR_OFFS
;
793 while ( (line
= preproc
->getline()) )
797 /* here we parse our directives; this is not handled by the 'real'
799 if ( (i
= getkw (line
, &value
)) )
802 case 1: /* [SEGMENT n] */
803 seg
= ofmt
->section (value
, pass2
, &sb
);
805 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
806 "segment name `%s' not recognised",
810 location
.segment
= seg
;
813 case 2: /* [EXTERN label:special] */
814 if (*value
== '$') value
++; /* skip initial $ if present */
817 while (*q
&& *q
!= ':')
821 ofmt
->symdef(value
, 0L, 0L, 3, q
);
823 } else if (pass
== 1) { /* pass == 1 */
828 while (*q
&& *q
!= ':') {
834 report_error (ERR_NONFATAL
,
835 "identifier expected after EXTERN");
843 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
845 pass0
= 1; /* fake pass 1 in labels.c */
846 declare_as_global (value
, special
, report_error
);
847 define_label (value
, seg_alloc(), 0L, NULL
, FALSE
, TRUE
,
851 } /* else pass0 == 1 */
853 case 3: /* [BITS bits] */
854 sb
= get_bits(value
);
856 case 4: /* [GLOBAL symbol:special] */
857 if (*value
== '$') value
++; /* skip initial $ if present */
858 if (pass0
== 2) { /* pass 2 */
860 while (*q
&& *q
!= ':')
864 ofmt
->symdef(value
, 0L, 0L, 3, q
);
866 } else if (pass2
== 1) { /* pass == 1 */
871 while (*q
&& *q
!= ':') {
877 report_error (ERR_NONFATAL
,
878 "identifier expected after GLOBAL");
886 declare_as_global (value
, special
, report_error
);
889 case 5: /* [COMMON symbol size:special] */
890 if (*value
== '$') value
++; /* skip initial $ if present */
896 while (*p
&& !isspace(*p
)) {
902 report_error (ERR_NONFATAL
,
903 "identifier expected after COMMON");
909 while (*p
&& isspace(*p
))
912 while (*q
&& *q
!= ':')
919 size
= readnum (p
, &rn_error
);
921 report_error (ERR_NONFATAL
, "invalid size specified"
922 " in COMMON declaration");
924 define_common (value
, seg_alloc(), size
,
925 special
, ofmt
, report_error
);
927 report_error (ERR_NONFATAL
, "no size specified in"
928 " COMMON declaration");
929 } else if (pass0
== 2) { /* pass == 2 */
931 while (*q
&& *q
!= ':') {
938 ofmt
->symdef(value
, 0L, 0L, 3, q
);
942 case 6: /* [ABSOLUTE address] */
944 stdscan_bufptr
= value
;
945 tokval
.t_type
= TOKEN_INVALID
;
946 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, report_error
,
950 report_error (pass0
==1 ? ERR_NONFATAL
: ERR_PANIC
,
951 "cannot use non-relocatable expression as "
954 if ( reloc_seg(e
) != NO_SEG
) {
955 report_error(pass0
==1 ? ERR_NONFATAL
: ERR_PANIC
,
956 "non-constant expression "
957 "supplied as the address to ABSOLUTE");
959 abs_offset
= reloc_value(e
);
962 if (pass
==1) abs_offset
= 0x100;/* don't go near zero in case of / */
963 else report_error (ERR_PANIC
, "invalid ABSOLUTE address "
966 location
.segment
= NO_SEG
;
974 while (*p
&& !isspace(*p
)) {
981 report_error (pass
==1 ? ERR_NONFATAL
: ERR_PANIC
,
982 "identifier expected after DEBUG");
985 while (*p
&& isspace(*p
)) p
++;
986 if (pass
==pass_max
) ofmt
->current_dfmt
->debug_directive (debugid
, p
);
988 case 8: /* [WARNING {+|-}warn-name] */
990 while (*value
&& isspace(*value
))
993 if (*value
== '+' || *value
== '-') {
994 validid
= (*value
== '-') ? TRUE
: FALSE
;
999 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
1000 if (!nasm_stricmp(value
, suppressed_names
[i
]))
1002 if (i
<= ERR_WARN_MAX
)
1003 suppressed
[i
] = validid
;
1005 report_error (ERR_NONFATAL
, "invalid warning id in WARNING directive");
1009 cpu
= get_cpu (value
);
1011 case 10: /* fbk 9/2/00 */ /* [LIST {+|-}] */
1012 while (*value
&& isspace(*value
))
1015 if (*value
== '+') {
1019 if (*value
== '-') {
1023 report_error (ERR_NONFATAL
, "invalid parameter to \"list\" directive");
1028 if (!ofmt
->directive (line
+1, value
, pass2
))
1029 report_error (pass1
==1 ? ERR_NONFATAL
: ERR_PANIC
,
1030 "unrecognised directive [%s]",
1034 else /* it isn't a directive */
1036 parse_line (pass1
, line
, &output_ins
,
1037 report_error
, evaluate
,
1040 if (!(optimizing
>0) && pass
== 2) {
1041 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1042 output_ins
.forw_ref
= TRUE
;
1044 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1045 forwref
= saa_rstruct (forwrefs
);
1046 } while (forwref
!= NULL
&& forwref
->lineno
== globallineno
);
1048 output_ins
.forw_ref
= FALSE
;
1052 if (!(optimizing
>0) && output_ins
.forw_ref
)
1055 for(i
= 0; i
< output_ins
.operands
; i
++)
1057 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
)
1059 struct forwrefinfo
*fwinf
=
1060 (struct forwrefinfo
*)saa_wstruct(forwrefs
);
1061 fwinf
->lineno
= globallineno
;
1065 } else { /* pass == 2 */
1067 * Hack to prevent phase error in the code
1071 * If the second operand is a forward reference,
1072 * the UNITY property of the number 1 in that
1073 * operand is cancelled. Otherwise the above
1074 * sequence will cause a phase error.
1076 * This hack means that the above code will
1077 * generate 286+ code.
1079 * The forward reference will mean that the
1080 * operand will not have the UNITY property on
1081 * the first pass, so the pass behaviours will
1085 if (output_ins
.operands
>= 2 &&
1086 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1088 output_ins
.oprs
[1].type
&= ~(ONENESS
|BYTENESS
);
1096 if (output_ins
.opcode
== I_EQU
) {
1100 * Special `..' EQUs get processed in pass two,
1101 * except `..@' macro-processor EQUs which are done
1102 * in the normal place.
1104 if (!output_ins
.label
)
1105 report_error (ERR_NONFATAL
,
1106 "EQU not preceded by label");
1108 else if (output_ins
.label
[0] != '.' ||
1109 output_ins
.label
[1] != '.' ||
1110 output_ins
.label
[2] == '@')
1112 if (output_ins
.operands
== 1 &&
1113 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1114 output_ins
.oprs
[0].wrt
== NO_SEG
)
1116 int isext
= output_ins
.oprs
[0].opflags
& OPFLAG_EXTERN
;
1117 def_label (output_ins
.label
,
1118 output_ins
.oprs
[0].segment
,
1119 output_ins
.oprs
[0].offset
,
1120 NULL
, FALSE
, isext
, ofmt
, report_error
);
1122 else if (output_ins
.operands
== 2 &&
1123 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1124 (output_ins
.oprs
[0].type
& COLON
) &&
1125 output_ins
.oprs
[0].segment
== NO_SEG
&&
1126 output_ins
.oprs
[0].wrt
== NO_SEG
&&
1127 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1128 output_ins
.oprs
[1].segment
== NO_SEG
&&
1129 output_ins
.oprs
[1].wrt
== NO_SEG
)
1131 def_label (output_ins
.label
,
1132 output_ins
.oprs
[0].offset
| SEG_ABS
,
1133 output_ins
.oprs
[1].offset
,
1134 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1137 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1139 } else { /* pass == 2 */
1141 * Special `..' EQUs get processed here, except
1142 * `..@' macro processor EQUs which are done above.
1144 if (output_ins
.label
[0] == '.' &&
1145 output_ins
.label
[1] == '.' &&
1146 output_ins
.label
[2] != '@')
1148 if (output_ins
.operands
== 1 &&
1149 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1150 define_label (output_ins
.label
,
1151 output_ins
.oprs
[0].segment
,
1152 output_ins
.oprs
[0].offset
,
1153 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1155 else if (output_ins
.operands
== 2 &&
1156 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1157 (output_ins
.oprs
[0].type
& COLON
) &&
1158 output_ins
.oprs
[0].segment
== NO_SEG
&&
1159 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1160 output_ins
.oprs
[1].segment
== NO_SEG
)
1162 define_label (output_ins
.label
,
1163 output_ins
.oprs
[0].offset
| SEG_ABS
,
1164 output_ins
.oprs
[1].offset
,
1165 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1168 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1171 } else { /* instruction isn't an EQU */
1175 long l
= insn_size (location
.segment
, offs
, sb
, cpu
,
1176 &output_ins
, report_error
);
1178 /* if (using_debug_info) && output_ins.opcode != -1)*/
1179 if (using_debug_info
) /* fbk 03/25/01 */
1182 /* this is done here so we can do debug type info */
1183 long typeinfo
= TYS_ELEMENTS(output_ins
.operands
);
1184 switch (output_ins
.opcode
) {
1186 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
1189 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
1192 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
1195 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
1198 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
1201 typeinfo
|= TY_BYTE
;
1204 typeinfo
|= TY_WORD
;
1207 if (output_ins
.eops_float
)
1208 typeinfo
|= TY_FLOAT
;
1210 typeinfo
|= TY_DWORD
;
1213 typeinfo
|= TY_QWORD
;
1216 typeinfo
|= TY_TBYTE
;
1219 typeinfo
= TY_LABEL
;
1223 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
1228 SET_CURR_OFFS (offs
);
1231 * else l == -1 => invalid instruction, which will be
1232 * flagged as an error on pass 2
1235 } else { /* pass == 2 */
1236 offs
+= assemble (location
.segment
, offs
, sb
, cpu
,
1237 &output_ins
, ofmt
, report_error
, &nasmlist
);
1238 SET_CURR_OFFS (offs
);
1242 cleanup_insn (&output_ins
);
1245 location
.offset
= offs
= GET_CURR_OFFS
;
1246 } /* end while (line = preproc->getline... */
1248 if (pass1
==2 && global_offset_changed
)
1249 report_error(ERR_NONFATAL
, "phase error detected at end of assembly.");
1251 if (pass1
== 1) preproc
->cleanup(1);
1253 if (pass1
==1 && terminate_after_phase
) {
1261 if (pass
>1 && !global_offset_changed
) {
1263 if (pass0
==2) pass
= pass_max
- 1;
1264 } else if (!(optimizing
>0)) pass0
++;
1266 } /* for (pass=1; pass<=2; pass++) */
1268 preproc
->cleanup(0);
1271 if (optimizing
>0 && using_debug_info
) /* -On and -g switches */
1273 "info:: assembly required 1+%d+1 passes\n", pass_cnt
-2);
1275 } /* exit from assemble_file (...) */
1278 static int getkw (char *buf
, char **value
)
1282 /* allow leading spaces or tabs */
1283 while (*buf
==' ' || *buf
=='\t')
1291 while (*p
&& *p
!= ']') p
++;
1298 while (*p
&& *p
!= ';') {
1306 while (*buf
&& *buf
!=' ' && *buf
!=']' && *buf
!='\t')
1313 while (isspace(*buf
)) buf
++; /* beppu - skip leading whitespace */
1315 while (*buf
!=']') buf
++;
1322 if (!nasm_stricmp(p
, "segment") || !nasm_stricmp(p
, "section"))
1324 if (!nasm_stricmp(p
, "extern"))
1326 if (!nasm_stricmp(p
, "bits"))
1328 if (!nasm_stricmp(p
, "global"))
1330 if (!nasm_stricmp(p
, "common"))
1332 if (!nasm_stricmp(p
, "absolute"))
1334 if (!nasm_stricmp(p
, "debug"))
1336 if (!nasm_stricmp(p
, "warning"))
1338 if (!nasm_stricmp(p
, "cpu"))
1340 if (!nasm_stricmp(p
, "list")) /* fbk 9/2/00 */
1345 static void report_error (int severity
, const char *fmt
, ...)
1350 * See if it's a suppressed warning.
1352 if ((severity
& ERR_MASK
) == ERR_WARNING
&&
1353 (severity
& ERR_WARN_MASK
) != 0 &&
1354 suppressed
[ (severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
])
1355 return; /* and bail out if so */
1358 * See if it's a pass-one only warning and we're not in pass one.
1360 if ((severity
& ERR_PASS1
) && pass0
== 2)
1363 if (severity
& ERR_NOFILE
)
1364 fputs ("nasm: ", error_file
);
1366 char * currentfile
= NULL
;
1368 src_get (&lineno
, ¤tfile
);
1369 fprintf (error_file
, "%s:%ld: ", currentfile
, lineno
);
1370 nasm_free (currentfile
);
1373 switch (severity
& ERR_MASK
) {
1375 fputs ("warning: ", error_file
); break;
1377 fputs ("error: ", error_file
); break;
1379 fputs ("fatal: ", error_file
); break;
1381 fputs ("panic: ", error_file
); break;
1383 fputs("debug: ", error_file
); break;
1387 vfprintf (error_file
, fmt
, ap
);
1388 fputc ('\n', error_file
);
1390 if (severity
& ERR_USAGE
)
1393 switch (severity
& ERR_MASK
) {
1394 case ERR_WARNING
: case ERR_DEBUG
:
1395 /* no further action, by definition */
1398 /* terminate_after_phase = TRUE; *//**//* hack enables listing(!) on errors */
1399 terminate_after_phase
= TRUE
;
1408 exit(1); /* instantly die */
1409 break; /* placate silly compilers */
1412 /* abort(); */ /* halt, catch fire, and dump core */
1418 static void usage(void)
1420 fputs("type `nasm -h' for help\n", error_file
);
1423 static void register_output_formats(void)
1425 ofmt
= ofmt_register (report_error
);
1428 #define BUF_DELTA 512
1430 static FILE *no_pp_fp
;
1431 static efunc no_pp_err
;
1432 static ListGen
*no_pp_list
;
1433 static long no_pp_lineinc
;
1435 static void no_pp_reset (char *file
, int pass
, efunc error
, evalfunc eval
,
1438 src_set_fname(nasm_strdup(file
));
1442 no_pp_fp
= fopen(file
, "r");
1444 no_pp_err (ERR_FATAL
| ERR_NOFILE
,
1445 "unable to open input file `%s'", file
);
1446 no_pp_list
= listgen
;
1447 (void) pass
; /* placate compilers */
1448 (void) eval
; /* placate compilers */
1451 static char *no_pp_getline (void)
1453 char *buffer
, *p
, *q
;
1456 bufsize
= BUF_DELTA
;
1457 buffer
= nasm_malloc(BUF_DELTA
);
1458 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1460 while (1) { /* Loop to handle %line */
1463 while (1) { /* Loop to handle long lines */
1464 q
= fgets(p
, bufsize
-(p
-buffer
), no_pp_fp
);
1468 if (p
> buffer
&& p
[-1] == '\n')
1470 if (p
-buffer
> bufsize
-10) {
1472 offset
= p
- buffer
;
1473 bufsize
+= BUF_DELTA
;
1474 buffer
= nasm_realloc(buffer
, bufsize
);
1475 p
= buffer
+ offset
;
1479 if (!q
&& p
== buffer
) {
1485 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1486 * them are present at the end of the line.
1488 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1490 if (!strncmp(buffer
, "%line", 5)) {
1493 char *nm
= nasm_malloc(strlen(buffer
));
1494 if (sscanf(buffer
+5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1495 nasm_free( src_set_fname(nm
) );
1505 no_pp_list
->line (LIST_READ
, buffer
);
1510 static void no_pp_cleanup (int pass
)
1515 static unsigned long get_cpu (char *value
)
1518 if (!strcmp(value
, "8086")) return IF_8086
;
1519 if (!strcmp(value
, "186")) return IF_186
;
1520 if (!strcmp(value
, "286")) return IF_286
;
1521 if (!strcmp(value
, "386")) return IF_386
;
1522 if (!strcmp(value
, "486")) return IF_486
;
1523 if (!strcmp(value
, "586") ||
1524 !nasm_stricmp(value
, "pentium") ) return IF_PENT
;
1525 if (!strcmp(value
, "686") ||
1526 !nasm_stricmp(value
, "ppro") ||
1527 !nasm_stricmp(value
, "p2") ) return IF_P6
;
1528 if (!nasm_stricmp(value
, "p3") ||
1529 !nasm_stricmp(value
, "katmai") ) return IF_KATMAI
;
1530 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
1531 !nasm_stricmp(value
, "willamette") ) return IF_WILLAMETTE
;
1533 report_error (pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
, "unknown 'cpu' type");
1535 return IF_PLEVEL
; /* the maximum level */
1539 static int get_bits (char *value
)
1543 if ((i
= atoi(value
)) == 16) return i
; /* set for a 16-bit segment */
1546 report_error(ERR_NONFATAL
,
1547 "cannot specify 32-bit segment on processor below a 386");
1551 report_error(pass0
<2 ? ERR_NONFATAL
: ERR_FATAL
,
1552 "`%s' is not a valid segment size; must be 16 or 32",