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.
25 struct forwrefinfo
{ /* info held on forward refs. */
30 static void report_error (int, char *, ...);
31 static void parse_cmdline (int, char **);
32 static void assemble_file (char *);
33 static int getkw (char *buf
, char **value
);
34 static void register_output_formats(void);
35 static void usage(void);
37 static int using_debug_info
;
39 static char inname
[FILENAME_MAX
];
40 static char outname
[FILENAME_MAX
];
41 static char listname
[FILENAME_MAX
];
42 static int globallineno
; /* for forward-reference tracking */
44 static struct ofmt
*ofmt
= NULL
;
46 static FILE *ofile
= NULL
;
47 static int sb
= 16; /* by default */
49 static loc_t location
;
50 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
53 static struct RAA
*offsets
;
54 static long abs_offset
;
56 static struct SAA
*forwrefs
; /* keep track of forward references */
57 static struct forwrefinfo
*forwref
;
59 static Preproc
*preproc
;
60 static int preprocess_only
;
62 /* used by error function to report location */
65 * Which of the suppressible warnings are suppressed. Entry zero
66 * doesn't do anything. Initial defaults are given here.
68 static char suppressed
[1+ERR_WARN_MAX
] = {
73 * The option names for the suppressible warnings. As before, entry
76 static char *suppressed_names
[1+ERR_WARN_MAX
] = {
77 NULL
, "macro-params", "orphan-labels", "number-overflow"
81 * The explanations for the suppressible warnings. As before, entry
84 static char *suppressed_what
[1+ERR_WARN_MAX
] = {
85 NULL
, "macro calls with wrong no. of params",
86 "labels alone on lines without trailing `:'",
87 "numeric constants greater than 0xFFFFFFFF"
91 * This is a null preprocessor which just copies lines from input
92 * to output. It's used when someone explicitly requests that NASM
93 * not preprocess their source file.
96 static void no_pp_reset (char *, int, efunc
, evalfunc
, ListGen
*);
97 static char *no_pp_getline (void);
98 static void no_pp_cleanup (void);
99 static Preproc no_pp
= {
106 * get/set current offset...
108 #define get_curr_ofs (in_abs_seg?abs_offset:\
109 raa_read(offsets,location.segment))
110 #define set_curr_ofs(x) (in_abs_seg?(void)(abs_offset=(x)):\
111 (void)(offsets=raa_write(offsets,location.segment,(x))))
113 static int want_usage
;
114 static int terminate_after_phase
;
116 static void nasm_fputs(char *line
, FILE *ofile
)
125 int main(int argc
, char **argv
)
127 want_usage
= terminate_after_phase
= FALSE
;
129 nasm_set_malloc_error (report_error
);
130 offsets
= raa_init();
131 forwrefs
= saa_init ((long)sizeof(struct forwrefinfo
));
134 preprocess_only
= FALSE
;
138 register_output_formats();
140 parse_cmdline(argc
, argv
);
142 if (terminate_after_phase
)
150 pp_extra_stdmac (ofmt
->stdmac
);
151 parser_global_info (ofmt
, &location
);
152 eval_global_info (ofmt
, lookup_label
, &location
);
157 char *file_name
= NULL
;
162 ofile
= fopen(outname
, "w");
164 report_error (ERR_FATAL
| ERR_NOFILE
,
165 "unable to open output file `%s'", outname
);
169 location
.known
= FALSE
;
171 preproc
->reset (inname
, 2, report_error
, evaluate
, &nasmlist
);
172 while ( (line
= preproc
->getline()) ) {
174 * We generate %line directives if needed for later programs
176 long linnum
= prior_linnum
+= lineinc
;
177 int altline
= src_get(&linnum
, &file_name
);
179 if (altline
==1 && lineinc
==1)
180 nasm_fputs("", ofile
);
182 lineinc
= (altline
!= -1 || lineinc
!=1);
183 fprintf(ofile
? ofile
: stdout
, "%%line %ld+%d %s\n",
184 linnum
, lineinc
, file_name
);
186 prior_linnum
= linnum
;
188 nasm_fputs(line
, ofile
);
191 nasm_free(file_name
);
195 if (ofile
&& terminate_after_phase
)
198 else /* NOT preprocess only */
201 * We must call ofmt->filename _anyway_, even if the user
202 * has specified their own output file, because some
203 * formats (eg OBJ and COFF) use ofmt->filename to find out
204 * the name of the input file and then put that inside the
207 ofmt
->filename (inname
, outname
, report_error
);
209 ofile
= fopen(outname
, "wb");
211 report_error (ERR_FATAL
| ERR_NOFILE
,
212 "unable to open output file `%s'", outname
);
216 * We must call init_labels() before ofmt->init() since
217 * some object formats will want to define labels in their
218 * init routines. (eg OS/2 defines the FLAT group)
222 ofmt
->init (ofile
, report_error
, define_label
, evaluate
);
224 assemble_file (inname
);
226 if (!terminate_after_phase
) {
227 ofmt
->cleanup (using_debug_info
);
233 * We had an fclose on the output file here, but we
234 * actually do that in all the object file drivers as well,
235 * so we're leaving out the one here.
253 if (terminate_after_phase
)
261 * Get a parameter for a command line option.
262 * First arg must be in the form of e.g. -f...
264 static char *get_param (char *p
, char *q
, int *advance
)
267 if (p
[2]) /* the parameter's in the option */
279 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
280 "option `-%c' requires an argument",
286 static int process_arg (char *p
, char *q
)
294 if (p
[0]=='-' && ! stopoptions
)
297 case '-': /* -- => stop processing options */
300 case 's': /* silently ignored for compatibility */
302 case 'o': /* these parameters take values */
309 if ( !(param
= get_param (p
, q
, &advance
)) )
311 if (p
[1]=='o') { /* output file */
312 strcpy (outname
, param
);
313 } else if (p
[1]=='f') { /* output format */
314 ofmt
= ofmt_find(param
);
316 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
317 "unrecognised output format `%s' - "
318 "use -hf for a list",
322 ofmt
->current_dfmt
= ofmt
->debug_formats
[0];
323 } else if (p
[1]=='p') { /* pre-include */
324 pp_pre_include (param
);
325 } else if (p
[1]=='d') { /* pre-define */
326 pp_pre_define (param
);
327 } else if (p
[1]=='i') { /* include search path */
328 pp_include_path (param
);
329 } else if (p
[1]=='l') { /* listing file */
330 strcpy (listname
, param
);
331 } else if (p
[1] == 'F') { /* specify debug format */
332 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
333 if (!ofmt
->current_dfmt
) {
334 report_error (ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
335 "unrecognized debug format `%s' for"
336 " output format `%s'",
337 param
, ofmt
->shortname
);
342 using_debug_info
= TRUE
;
345 printf("usage: nasm [-@ response file] [-o outfile] [-f format] "
347 " [options...] [--] filename\n");
348 printf(" or nasm -r for version info\n\n");
349 printf(" -e preprocess only (writes output to "
350 "stdout by default)\n"
351 " -a don't preprocess\n\n");
352 printf(" -g enable debug info\n"
353 " -F format select a debugging format\n\n");
354 printf(" -i<path> adds a pathname to the include file path\n"
355 " -p<file> pre-includes a file\n"
356 " -d<macro>[=<value>] pre-defines a macro\n");
357 printf(" -w+foo enables warnings about foo; "
358 "-w-foo disables them\n where foo can be:\n");
359 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
360 printf(" %-16s%s (default %s)\n",
361 suppressed_names
[i
], suppressed_what
[i
],
362 suppressed
[i
] ? "off" : "on");
363 printf ("\nresponse files should contain command line parameters"
364 ", one per line.\n");
366 printf("\nvalid output formats for -f are"
367 " (`*' denotes default):\n");
368 ofmt_list(ofmt
, stdout
);
371 printf ("\nFor a list of valid output formats, use -hf.\n");
372 printf ("For a list of debug formats, use -f <form> -y.\n");
374 exit (0); /* never need usage message here */
377 printf("\nvalid debug formats for '%s' output format are"
378 " ('*' denotes default):\n",
380 dfmt_list(ofmt
, stdout
);
384 printf("NASM version %s\n", NASM_VER
);
386 printf("Compiled with -DDEBUG on " __DATE__
"\n");
388 exit (0); /* never need usage message here */
390 case 'e': /* preprocess only */
391 preprocess_only
= TRUE
;
393 case 'a': /* assemble only - don't preprocess */
397 if (p
[2] != '+' && p
[2] != '-') {
398 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
399 "invalid option to `-w'");
401 for (i
=1; i
<=ERR_WARN_MAX
; i
++)
402 if (!nasm_stricmp(p
+3, suppressed_names
[i
]))
404 if (i
<= ERR_WARN_MAX
)
405 suppressed
[i
] = (p
[2] == '-');
407 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
408 "invalid option to `-w'");
412 if (!ofmt
->setinfo(GI_SWITCH
,&p
))
413 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
414 "unrecognised option `-%c'",
422 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
423 "more than one input file specified");
431 #define ARG_BUF_DELTA 128
433 static void process_respfile (FILE *rfile
)
435 char *buffer
, *p
, *q
, *prevarg
;
436 int bufsize
, prevargsize
;
438 bufsize
= prevargsize
= ARG_BUF_DELTA
;
439 buffer
= nasm_malloc(ARG_BUF_DELTA
);
440 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
443 while (1) { /* Loop to handle all lines in file */
446 while (1) { /* Loop to handle long lines */
447 q
= fgets(p
, bufsize
-(p
-buffer
), rfile
);
451 if (p
> buffer
&& p
[-1] == '\n')
453 if (p
-buffer
> bufsize
-10) {
456 bufsize
+= ARG_BUF_DELTA
;
457 buffer
= nasm_realloc(buffer
, bufsize
);
462 if (!q
&& p
== buffer
) {
464 process_arg (prevarg
, NULL
);
471 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
472 * them are present at the end of the line.
474 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
476 while (p
> buffer
&& isspace(p
[-1]))
483 if (process_arg (prevarg
, p
))
486 if (strlen(p
) > prevargsize
-10) {
487 prevargsize
+= ARG_BUF_DELTA
;
488 prevarg
= nasm_realloc(prevarg
, prevargsize
);
494 static void parse_cmdline(int argc
, char **argv
)
497 char *envreal
, *envcopy
=NULL
, *p
, *q
, *arg
, *prevarg
;
498 char separator
= ' ';
500 *inname
= *outname
= *listname
= '\0';
503 * First, process the NASM environment variable.
505 envreal
= getenv("NASM");
508 envcopy
= nasm_strdup(envreal
);
514 while (*p
&& *p
!= separator
) p
++;
515 while (*p
== separator
) *p
++ = '\0';
518 if (process_arg (prevarg
, arg
))
522 process_arg (arg
, NULL
);
527 * Now process the actual command line.
533 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
534 if ((p
= get_param (argv
[0], argc
> 1 ? argv
[1] : NULL
, &i
)))
535 if ((rfile
= fopen(p
, "r"))) {
536 process_respfile (rfile
);
539 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
540 "unable to open response file `%s'", p
);
542 i
= process_arg (argv
[0], argc
> 1 ? argv
[1] : NULL
);
543 argv
+= i
, argc
-= i
;
547 report_error (ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
548 "no input file specified");
551 static void assemble_file (char *fname
)
553 char * value
, * p
, * q
, * special
, * line
, debugid
[80];
555 int i
, rn_error
, validid
;
557 struct tokenval tokval
;
565 location
.segment
= ofmt
->section(NULL
, pass
, &sb
);
566 preproc
->reset(fname
, 1, report_error
, evaluate
, &nasmlist
);
568 location
.known
= TRUE
;
569 location
.offset
= offs
= get_curr_ofs
;
571 while ( (line
= preproc
->getline()) )
575 /* here we parse our directives; this is not handled by the 'real'
577 if ( (i
= getkw (line
, &value
)) )
580 case 1: /* [SEGMENT n] */
581 seg
= ofmt
->section (value
, pass
, &sb
);
583 report_error (ERR_NONFATAL
,
584 "segment name `%s' not recognised",
588 location
.segment
= seg
;
591 case 2: /* [EXTERN label:special] */
593 value
++; /* skip initial $ if present */
598 while (*q
&& *q
!= ':') {
604 report_error (ERR_NONFATAL
,
605 "identifier expected after EXTERN");
613 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
614 declare_as_global (value
, special
, report_error
);
615 define_label (value
, seg_alloc(), 0L, NULL
, FALSE
, TRUE
,
619 case 3: /* [BITS bits] */
620 switch (atoi(value
)) {
626 report_error(ERR_NONFATAL
,
627 "`%s' is not a valid argument to [BITS]",
632 case 4: /* [GLOBAL symbol:special] */
634 value
++; /* skip initial $ if present */
639 while (*q
&& *q
!= ':') {
645 report_error (ERR_NONFATAL
,
646 "identifier expected after GLOBAL");
654 declare_as_global (value
, special
, report_error
);
656 case 5: /* [COMMON symbol size:special] */
661 while (*p
&& !isspace(*p
)) {
667 report_error (ERR_NONFATAL
,
668 "identifier expected after COMMON");
674 while (*p
&& isspace(*p
))
677 while (*q
&& *q
!= ':')
684 size
= readnum (p
, &rn_error
);
686 report_error (ERR_NONFATAL
, "invalid size specified"
687 " in COMMON declaration");
689 define_common (value
, seg_alloc(), size
,
690 special
, ofmt
, report_error
);
692 report_error (ERR_NONFATAL
, "no size specified in"
693 " COMMON declaration");
695 case 6: /* [ABSOLUTE address] */
697 stdscan_bufptr
= value
;
698 tokval
.t_type
= TOKEN_INVALID
;
699 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 1, report_error
,
703 report_error (ERR_NONFATAL
, "cannot use non-"
704 "relocatable expression as ABSOLUTE"
707 abs_seg
= reloc_seg(e
);
708 abs_offset
= reloc_value(e
);
711 abs_offset
= 0x100;/* don't go near zero in case of / */
713 location
.segment
= abs_seg
;
720 while (*p
&& !isspace(*p
)) {
726 report_error (ERR_NONFATAL
,
727 "identifier expected after DEBUG");
730 while (*p
&& isspace(*p
)) p
++;
733 if (!ofmt
->directive (line
+1, value
, 1))
734 report_error (ERR_NONFATAL
, "unrecognised directive [%s]",
739 else /* it isn't a directive */
741 parse_line (1, line
, &output_ins
,
742 report_error
, evaluate
, define_label
);
744 if (output_ins
.forw_ref
)
746 for(i
= 0; i
< output_ins
.operands
; i
++)
748 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
)
750 struct forwrefinfo
*fwinf
=
751 (struct forwrefinfo
*)saa_wstruct(forwrefs
);
752 fwinf
->lineno
= globallineno
;
758 if (output_ins
.opcode
== I_EQU
)
761 * Special `..' EQUs get processed in pass two,
762 * except `..@' macro-processor EQUs which are done
763 * in the normal place.
765 if (!output_ins
.label
)
766 report_error (ERR_NONFATAL
,
767 "EQU not preceded by label");
770 * EQU cannot be used to declare a label relative to
771 * an external symbol.
773 else if ((output_ins
.oprs
[0].opflags
& OPFLAG_EXTERN
)
774 || (output_ins
.operands
> 1
775 && (output_ins
.oprs
[1].opflags
& OPFLAG_EXTERN
)))
777 report_error (ERR_NONFATAL
,
778 "EQU used relative to external symbol");
781 else if (output_ins
.label
[0] != '.' ||
782 output_ins
.label
[1] != '.' ||
783 output_ins
.label
[2] == '@')
785 if (output_ins
.operands
== 1 &&
786 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
787 output_ins
.oprs
[0].wrt
== NO_SEG
)
789 define_label (output_ins
.label
,
790 output_ins
.oprs
[0].segment
,
791 output_ins
.oprs
[0].offset
,
792 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
794 else if (output_ins
.operands
== 2 &&
795 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
796 (output_ins
.oprs
[0].type
& COLON
) &&
797 output_ins
.oprs
[0].segment
== NO_SEG
&&
798 output_ins
.oprs
[0].wrt
== NO_SEG
&&
799 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
800 output_ins
.oprs
[1].segment
== NO_SEG
&&
801 output_ins
.oprs
[1].wrt
== NO_SEG
)
803 define_label (output_ins
.label
,
804 output_ins
.oprs
[0].offset
| SEG_ABS
,
805 output_ins
.oprs
[1].offset
,
806 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
809 report_error(ERR_NONFATAL
, "bad syntax for EQU");
812 else /* instruction isn't an EQU */
814 long l
= insn_size (location
.segment
, offs
, sb
,
815 &output_ins
, report_error
);
816 if (using_debug_info
&& output_ins
.opcode
!= -1) {
817 /* this is done here so we can do debug type info */
818 long typeinfo
= TYS_ELEMENTS(output_ins
.operands
);
819 switch (output_ins
.opcode
) {
821 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
824 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
827 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
830 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
833 typeinfo
= TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
842 if (output_ins
.eops_float
)
843 typeinfo
|= TY_FLOAT
;
845 typeinfo
|= TY_DWORD
;
848 typeinfo
|= TY_QWORD
;
851 typeinfo
|= TY_TBYTE
;
856 ofmt
->current_dfmt
->debug_typevalue(typeinfo
);
863 * else l == -1 => invalid instruction, which will be
864 * flagged as an error on pass 2
867 cleanup_insn (&output_ins
);
870 location
.offset
= offs
= get_curr_ofs
;
875 if (terminate_after_phase
) {
888 saa_rewind (forwrefs
);
890 nasmlist
.init(listname
, report_error
);
891 forwref
= saa_rstruct (forwrefs
);
893 location
.segment
= ofmt
->section(NULL
, pass
, &sb
);
895 offsets
= raa_init();
896 preproc
->reset(fname
, 2, report_error
, evaluate
, &nasmlist
);
898 location
.offset
= offs
= get_curr_ofs
;
900 while ( (line
= preproc
->getline()) )
904 /* here we parse our directives; this is not handled by
905 * the 'real' parser. */
906 if ( (i
= getkw (line
, &value
)) ) {
908 case 1: /* [SEGMENT n] */
909 seg
= ofmt
->section (value
, pass
, &sb
);
911 report_error (ERR_PANIC
,
912 "invalid segment name on pass two");
915 location
.segment
= seg
;
917 case 2: /* [EXTERN label] */
919 while (*q
&& *q
!= ':')
923 ofmt
->symdef(value
, 0L, 0L, 3, q
);
926 case 3: /* [BITS bits] */
927 switch (atoi(value
)) {
933 report_error(ERR_PANIC
,
934 "invalid [BITS] value on pass two",
939 case 4: /* [GLOBAL symbol] */
941 while (*q
&& *q
!= ':')
945 ofmt
->symdef(value
, 0L, 0L, 3, q
);
948 case 5: /* [COMMON symbol size] */
950 while (*q
&& *q
!= ':') {
957 ofmt
->symdef(value
, 0L, 0L, 3, q
);
960 case 6: /* [ABSOLUTE addr] */
962 stdscan_bufptr
= value
;
963 tokval
.t_type
= TOKEN_INVALID
;
964 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, 2, report_error
,
968 report_error (ERR_PANIC
, "non-reloc ABSOLUTE address"
971 abs_seg
= reloc_seg(e
);
972 abs_offset
= reloc_value(e
);
975 report_error (ERR_PANIC
, "invalid ABSOLUTE address "
978 location
.segment
= abs_seg
;
986 while (*p
&& !isspace(*p
)) {
993 report_error (ERR_PANIC
,
994 "identifier expected after DEBUG in pass 2");
997 while (*p
&& isspace(*p
))
999 ofmt
->current_dfmt
->debug_directive (debugid
, p
);
1002 if (!ofmt
->directive (line
+1, value
, 2))
1003 report_error (ERR_PANIC
, "invalid directive on pass two");
1007 else /* not a directive */
1009 parse_line (2, line
, &output_ins
,
1010 report_error
, evaluate
, redefine_label
);
1011 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1012 output_ins
.forw_ref
= TRUE
;
1014 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1015 forwref
= saa_rstruct (forwrefs
);
1016 } while (forwref
!= NULL
&& forwref
->lineno
== globallineno
);
1018 output_ins
.forw_ref
= FALSE
;
1021 * Hack to prevent phase error in the code
1025 * If the second operand is a forward reference,
1026 * the UNITY property of the number 1 in that
1027 * operand is cancelled. Otherwise the above
1028 * sequence will cause a phase error.
1030 * This hack means that the above code will
1031 * generate 286+ code.
1033 * The forward reference will mean that the
1034 * operand will not have the UNITY property on
1035 * the first pass, so the pass behaviours will
1039 if (output_ins
.forw_ref
&&
1040 output_ins
.operands
>= 2 &&
1041 (output_ins
.oprs
[1].opflags
& OPFLAG_FORWARD
))
1043 output_ins
.oprs
[1].type
&= ~ONENESS
;
1046 if (output_ins
.opcode
== I_EQU
)
1049 * Special `..' EQUs get processed here, except
1050 * `..@' macro processor EQUs which are done above.
1052 if (output_ins
.label
[0] == '.' &&
1053 output_ins
.label
[1] == '.' &&
1054 output_ins
.label
[2] != '@')
1056 if (output_ins
.operands
== 1 &&
1057 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1058 define_label (output_ins
.label
,
1059 output_ins
.oprs
[0].segment
,
1060 output_ins
.oprs
[0].offset
,
1061 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1063 else if (output_ins
.operands
== 2 &&
1064 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1065 (output_ins
.oprs
[0].type
& COLON
) &&
1066 output_ins
.oprs
[0].segment
== NO_SEG
&&
1067 (output_ins
.oprs
[1].type
& IMMEDIATE
) &&
1068 output_ins
.oprs
[1].segment
== NO_SEG
)
1070 define_label (output_ins
.label
,
1071 output_ins
.oprs
[0].offset
| SEG_ABS
,
1072 output_ins
.oprs
[1].offset
,
1073 NULL
, FALSE
, FALSE
, ofmt
, report_error
);
1076 report_error(ERR_NONFATAL
, "bad syntax for EQU");
1079 offs
+= assemble (location
.segment
, offs
, sb
,
1080 &output_ins
, ofmt
, report_error
, &nasmlist
);
1081 cleanup_insn (&output_ins
);
1082 set_curr_ofs (offs
);
1087 location
.offset
= offs
= get_curr_ofs
;
1094 static int getkw (char *buf
, char **value
)
1103 while (*p
&& *p
!= ']') p
++;
1110 while (*p
&& *p
!= ';') {
1118 while (*buf
&& *buf
!=' ' && *buf
!=']' && *buf
!='\t')
1125 while (isspace(*buf
)) buf
++; /* beppu - skip leading whitespace */
1127 while (*buf
!=']') buf
++;
1132 if (!strcmp(p
, "segment") || !strcmp(p
, "section"))
1134 if (!strcmp(p
, "extern"))
1136 if (!strcmp(p
, "bits"))
1138 if (!strcmp(p
, "global"))
1140 if (!strcmp(p
, "common"))
1142 if (!strcmp(p
, "absolute"))
1144 if (!strcmp(p
, "debug"))
1149 static void report_error (int severity
, char *fmt
, ...)
1154 * See if it's a suppressed warning.
1156 if ((severity
& ERR_MASK
) == ERR_WARNING
&&
1157 (severity
& ERR_WARN_MASK
) != 0 &&
1158 suppressed
[ (severity
& ERR_WARN_MASK
) >> ERR_WARN_SHR
])
1159 return; /* and bail out if so */
1162 * See if it's a pass-one only warning and we're not in pass one.
1164 if ((severity
& ERR_PASS1
) && pass
!= 1)
1167 if (severity
& ERR_NOFILE
)
1168 fputs ("nasm: ", stdout
);
1170 char * currentfile
= NULL
;
1172 src_get (&lineno
, ¤tfile
);
1173 fprintf (stdout
, "%s:%ld: ", currentfile
, lineno
);
1174 nasm_free (currentfile
);
1177 if ( (severity
& ERR_MASK
) == ERR_WARNING
)
1178 fputs ("warning: ", stdout
);
1179 else if ( (severity
& ERR_MASK
) == ERR_PANIC
)
1180 fputs ("panic: ", stdout
);
1183 vfprintf (stdout
, fmt
, ap
);
1184 fputc ('\n', stdout
);
1186 if (severity
& ERR_USAGE
)
1189 switch (severity
& ERR_MASK
) {
1191 /* no further action, by definition */
1194 terminate_after_phase
= TRUE
;
1203 exit(1); /* instantly die */
1204 break; /* placate silly compilers */
1206 abort(); /* halt, catch fire, and dump core */
1211 static void usage(void)
1213 fputs("type `nasm -h' for help\n", stdout
);
1216 static void register_output_formats(void)
1218 ofmt
= ofmt_register (report_error
);
1221 #define BUF_DELTA 512
1223 static FILE *no_pp_fp
;
1224 static efunc no_pp_err
;
1225 static ListGen
*no_pp_list
;
1226 static long no_pp_lineinc
;
1228 static void no_pp_reset (char *file
, int pass
, efunc error
, evalfunc eval
,
1231 src_set_fname(nasm_strdup(file
));
1235 no_pp_fp
= fopen(file
, "r");
1237 no_pp_err (ERR_FATAL
| ERR_NOFILE
,
1238 "unable to open input file `%s'", file
);
1239 no_pp_list
= listgen
;
1240 (void) pass
; /* placate compilers */
1241 (void) eval
; /* placate compilers */
1244 static char *no_pp_getline (void)
1246 char *buffer
, *p
, *q
;
1249 bufsize
= BUF_DELTA
;
1250 buffer
= nasm_malloc(BUF_DELTA
);
1251 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
1253 while (1) { /* Loop to handle %line */
1256 while (1) { /* Loop to handle long lines */
1257 q
= fgets(p
, bufsize
-(p
-buffer
), no_pp_fp
);
1261 if (p
> buffer
&& p
[-1] == '\n')
1263 if (p
-buffer
> bufsize
-10) {
1265 offset
= p
- buffer
;
1266 bufsize
+= BUF_DELTA
;
1267 buffer
= nasm_realloc(buffer
, bufsize
);
1268 p
= buffer
+ offset
;
1272 if (!q
&& p
== buffer
) {
1278 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1279 * them are present at the end of the line.
1281 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
1283 if (!strncmp(buffer
, "%line", 5)) {
1286 char *nm
= nasm_malloc(strlen(buffer
));
1287 if (sscanf(buffer
+5, "%ld+%d %s", &ln
, &li
, nm
) == 3) {
1288 nasm_free( src_set_fname(nm
) );
1298 no_pp_list
->line (LIST_READ
, buffer
);
1303 static void no_pp_cleanup (void)