1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2012 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * The Netwide Assembler main program module
61 #include "output/outform.h"
65 * This is the maximum number of optimization passes to do. If we ever
66 * find a case where the optimizer doesn't naturally converge, we might
67 * have to drop this value so the assembler doesn't appear to just hang.
69 #define MAX_OPTIMIZE (INT_MAX >> 1)
71 struct forwrefinfo
{ /* info held on forward refs. */
76 static int get_bits(char *value
);
77 static uint32_t get_cpu(char *cpu_str
);
78 static void parse_cmdline(int, char **);
79 static void assemble_file(char *, StrList
**);
80 static void nasm_verror_gnu(int severity
, const char *fmt
, va_list args
);
81 static void nasm_verror_vc(int severity
, const char *fmt
, va_list args
);
82 static void nasm_verror_common(int severity
, const char *fmt
, va_list args
);
83 static bool is_suppressed_warning(int severity
);
84 static void usage(void);
86 static int using_debug_info
, opt_verbose_info
;
87 bool tasm_compatible_mode
= false;
92 static time_t official_compile_time
;
94 static char inname
[FILENAME_MAX
];
95 static char outname
[FILENAME_MAX
];
96 static char listname
[FILENAME_MAX
];
97 static char errname
[FILENAME_MAX
];
98 static int globallineno
; /* for forward-reference tracking */
99 /* static int pass = 0; */
100 struct ofmt
*ofmt
= &OF_DEFAULT
;
101 struct ofmt_alias
*ofmt_alias
= NULL
;
102 const struct dfmt
*dfmt
;
104 static FILE *error_file
; /* Where to write error messages */
107 int optimizing
= MAX_OPTIMIZE
; /* number of optimization passes to take */
108 static int sb
, cmd_sb
= 16; /* by default */
109 static uint32_t cmd_cpu
= IF_PLEVEL
; /* highest level by default */
110 static uint32_t cpu
= IF_PLEVEL
; /* passed to insn_size & assemble.c */
111 int64_t global_offset_changed
; /* referenced in labels.c */
112 int64_t prev_offset_changed
;
115 static struct location location
;
116 int in_abs_seg
; /* Flag we are in ABSOLUTE seg */
117 int32_t abs_seg
; /* ABSOLUTE segment basis */
118 int32_t abs_offset
; /* ABSOLUTE offset */
120 static struct RAA
*offsets
;
122 static struct SAA
*forwrefs
; /* keep track of forward references */
123 static const struct forwrefinfo
*forwref
;
125 static struct preproc_ops
*preproc
;
128 op_normal
, /* Preprocess and assemble */
129 op_preprocess
, /* Preprocess only */
130 op_depend
, /* Generate dependencies */
132 static enum op_type operating_mode
;
133 /* Dependency flags */
134 static bool depend_emit_phony
= false;
135 static bool depend_missing_ok
= false;
136 static const char *depend_target
= NULL
;
137 static const char *depend_file
= NULL
;
140 * Which of the suppressible warnings are suppressed. Entry zero
141 * isn't an actual warning, but it used for -w+error/-Werror.
144 static bool warning_on
[ERR_WARN_MAX
+1]; /* Current state */
145 static bool warning_on_global
[ERR_WARN_MAX
+1]; /* Command-line state */
147 static const struct warning
{
151 } warnings
[ERR_WARN_MAX
+1] = {
152 {"error", "treat warnings as errors", false},
153 {"macro-params", "macro calls with wrong parameter count", true},
154 {"macro-selfref", "cyclic macro references", false},
155 {"macro-defaults", "macros with more default than optional parameters", true},
156 {"orphan-labels", "labels alone on lines without trailing `:'", true},
157 {"number-overflow", "numeric constant does not fit", true},
158 {"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
159 {"float-overflow", "floating point overflow", true},
160 {"float-denorm", "floating point denormal", false},
161 {"float-underflow", "floating point underflow", false},
162 {"float-toolong", "too many digits in floating-point number", true},
163 {"user", "%warning directives", true},
164 {"lock", "lock prefix on unlockable instructions", true},
165 {"hle", "invalid hle prefixes", true},
169 * This is a null preprocessor which just copies lines from input
170 * to output. It's used when someone explicitly requests that NASM
171 * not preprocess their source file.
174 static void no_pp_reset(char *file
, int pass
, ListGen
*listgen
, StrList
**deplist
);
175 static char *no_pp_getline(void);
176 static void no_pp_cleanup(int pass
);
177 static void no_pp_extra_stdmac(macros_t
*macros
);
178 static void no_pp_pre_define(char *definition
);
179 static void no_pp_pre_undefine(char *definition
);
180 static void no_pp_pre_include(char *fname
);
181 static void no_pp_include_path(char *path
);
183 static struct preproc_ops no_pp
= {
195 * get/set current offset...
197 #define GET_CURR_OFFS (in_abs_seg?abs_offset:\
198 raa_read(offsets,location.segment))
199 #define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
200 (void)(offsets=raa_write(offsets,location.segment,(x))))
202 static bool want_usage
;
203 static bool terminate_after_phase
;
204 int user_nolist
= 0; /* fbk 9/2/00 */
206 static void nasm_fputs(const char *line
, FILE * outfile
)
209 fputs(line
, outfile
);
215 /* Convert a struct tm to a POSIX-style time constant */
216 static int64_t posix_mktime(struct tm
*tm
)
219 int64_t y
= tm
->tm_year
;
221 /* See IEEE 1003.1:2004, section 4.14 */
223 t
= (y
-70)*365 + (y
-69)/4 - (y
-1)/100 + (y
+299)/400;
235 static void define_macros_early(void)
238 struct tm lt
, *lt_p
, gm
, *gm_p
;
241 lt_p
= localtime(&official_compile_time
);
245 strftime(temp
, sizeof temp
, "__DATE__=\"%Y-%m-%d\"", <
);
246 preproc
->pre_define(temp
);
247 strftime(temp
, sizeof temp
, "__DATE_NUM__=%Y%m%d", <
);
248 preproc
->pre_define(temp
);
249 strftime(temp
, sizeof temp
, "__TIME__=\"%H:%M:%S\"", <
);
250 preproc
->pre_define(temp
);
251 strftime(temp
, sizeof temp
, "__TIME_NUM__=%H%M%S", <
);
252 preproc
->pre_define(temp
);
255 gm_p
= gmtime(&official_compile_time
);
259 strftime(temp
, sizeof temp
, "__UTC_DATE__=\"%Y-%m-%d\"", &gm
);
260 preproc
->pre_define(temp
);
261 strftime(temp
, sizeof temp
, "__UTC_DATE_NUM__=%Y%m%d", &gm
);
262 preproc
->pre_define(temp
);
263 strftime(temp
, sizeof temp
, "__UTC_TIME__=\"%H:%M:%S\"", &gm
);
264 preproc
->pre_define(temp
);
265 strftime(temp
, sizeof temp
, "__UTC_TIME_NUM__=%H%M%S", &gm
);
266 preproc
->pre_define(temp
);
270 posix_time
= posix_mktime(&gm
);
272 posix_time
= posix_mktime(<
);
277 snprintf(temp
, sizeof temp
, "__POSIX_TIME__=%"PRId64
, posix_time
);
278 preproc
->pre_define(temp
);
282 static void define_macros_late(void)
287 * In case if output format is defined by alias
288 * we have to put shortname of the alias itself here
289 * otherwise ABI backward compatibility gets broken.
291 snprintf(temp
, sizeof(temp
), "__OUTPUT_FORMAT__=%s",
292 ofmt_alias
? ofmt_alias
->shortname
: ofmt
->shortname
);
293 preproc
->pre_define(temp
);
296 static void emit_dependencies(StrList
*list
)
302 if (depend_file
&& strcmp(depend_file
, "-")) {
303 deps
= fopen(depend_file
, "w");
305 nasm_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
306 "unable to write dependency file `%s'", depend_file
);
313 linepos
= fprintf(deps
, "%s:", depend_target
);
314 list_for_each(l
, list
) {
315 len
= strlen(l
->str
);
316 if (linepos
+ len
> 62) {
317 fprintf(deps
, " \\\n ");
320 fprintf(deps
, " %s", l
->str
);
323 fprintf(deps
, "\n\n");
325 list_for_each_safe(l
, nl
, list
) {
326 if (depend_emit_phony
)
327 fprintf(deps
, "%s:\n\n", l
->str
);
335 int main(int argc
, char **argv
)
337 StrList
*depend_list
= NULL
, **depend_ptr
;
339 time(&official_compile_time
);
342 want_usage
= terminate_after_phase
= false;
343 nasm_set_verror(nasm_verror_gnu
);
349 nasm_init_malloc_error();
350 offsets
= raa_init();
351 forwrefs
= saa_init((int32_t)sizeof(struct forwrefinfo
));
354 operating_mode
= op_normal
;
358 /* Define some macros dependent on the runtime, but not
359 on the command line. */
360 define_macros_early();
362 parse_cmdline(argc
, argv
);
364 if (terminate_after_phase
) {
370 /* If debugging info is disabled, suppress any debug calls */
371 if (!using_debug_info
)
372 ofmt
->current_dfmt
= &null_debug_form
;
375 preproc
->extra_stdmac(ofmt
->stdmac
);
376 parser_global_info(&location
);
377 eval_global_info(ofmt
, lookup_label
, &location
);
379 /* define some macros dependent of command-line */
380 define_macros_late();
382 depend_ptr
= (depend_file
|| (operating_mode
== op_depend
))
383 ? &depend_list
: NULL
;
385 depend_target
= outname
;
387 switch (operating_mode
) {
392 if (depend_missing_ok
)
393 preproc
->include_path(NULL
); /* "assume generated" */
395 preproc
->reset(inname
, 0, &nasmlist
, depend_ptr
);
396 if (outname
[0] == '\0')
397 ofmt
->filename(inname
, outname
);
399 while ((line
= preproc
->getline()))
408 char *file_name
= NULL
;
409 int32_t prior_linnum
= 0;
413 ofile
= fopen(outname
, "w");
415 nasm_error(ERR_FATAL
| ERR_NOFILE
,
416 "unable to open output file `%s'",
421 location
.known
= false;
424 preproc
->reset(inname
, 3, &nasmlist
, depend_ptr
);
425 memcpy(warning_on
, warning_on_global
, (ERR_WARN_MAX
+1) * sizeof(bool));
427 while ((line
= preproc
->getline())) {
429 * We generate %line directives if needed for later programs
431 int32_t linnum
= prior_linnum
+= lineinc
;
432 int altline
= src_get(&linnum
, &file_name
);
434 if (altline
== 1 && lineinc
== 1)
435 nasm_fputs("", ofile
);
437 lineinc
= (altline
!= -1 || lineinc
!= 1);
438 fprintf(ofile
? ofile
: stdout
,
439 "%%line %"PRId32
"+%d %s\n", linnum
, lineinc
,
442 prior_linnum
= linnum
;
444 nasm_fputs(line
, ofile
);
447 nasm_free(file_name
);
451 if (ofile
&& terminate_after_phase
)
460 * We must call ofmt->filename _anyway_, even if the user
461 * has specified their own output file, because some
462 * formats (eg OBJ and COFF) use ofmt->filename to find out
463 * the name of the input file and then put that inside the
466 ofmt
->filename(inname
, outname
);
468 ofile
= fopen(outname
, (ofmt
->flags
& OFMT_TEXT
) ? "w" : "wb");
470 nasm_error(ERR_FATAL
| ERR_NOFILE
,
471 "unable to open output file `%s'", outname
);
475 * We must call init_labels() before ofmt->init() since
476 * some object formats will want to define labels in their
477 * init routines. (eg OS/2 defines the FLAT group)
482 dfmt
= ofmt
->current_dfmt
;
485 assemble_file(inname
, depend_ptr
);
487 if (!terminate_after_phase
) {
488 ofmt
->cleanup(using_debug_info
);
492 nasm_error(ERR_NONFATAL
|ERR_NOFILE
,
493 "write error on output file `%s'", outname
);
499 if (terminate_after_phase
)
507 if (depend_list
&& !terminate_after_phase
)
508 emit_dependencies(depend_list
);
518 return terminate_after_phase
;
522 * Get a parameter for a command line option.
523 * First arg must be in the form of e.g. -f...
525 static char *get_param(char *p
, char *q
, bool *advance
)
528 if (p
[2]) /* the parameter's in the option */
529 return nasm_skip_spaces(p
+ 2);
534 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
535 "option `-%c' requires an argument", p
[1]);
542 static void copy_filename(char *dst
, const char *src
)
544 size_t len
= strlen(src
);
546 if (len
>= (size_t)FILENAME_MAX
) {
547 nasm_error(ERR_FATAL
| ERR_NOFILE
, "file name too long");
550 strncpy(dst
, src
, FILENAME_MAX
);
554 * Convert a string to Make-safe form
556 static char *quote_for_make(const char *str
)
561 size_t n
= 1; /* Terminating zero */
567 for (p
= str
; *p
; p
++) {
571 /* Convert N backslashes + ws -> 2N+1 backslashes + ws */
591 /* Convert N backslashes at the end of filename to 2N backslashes */
595 os
= q
= nasm_malloc(n
);
598 for (p
= str
; *p
; p
++) {
641 #define OPT_POSTFIX 1
642 struct textargs textopts
[] = {
643 {"prefix", OPT_PREFIX
},
644 {"postfix", OPT_POSTFIX
},
648 static bool stopoptions
= false;
649 static bool process_arg(char *p
, char *q
)
653 bool advance
= false;
659 if (p
[0] == '-' && !stopoptions
) {
660 if (strchr("oOfpPdDiIlFXuUZwW", p
[1])) {
661 /* These parameters take values */
662 if (!(param
= get_param(p
, q
, &advance
)))
671 case 'o': /* output file */
672 copy_filename(outname
, param
);
675 case 'f': /* output format */
676 ofmt
= ofmt_find(param
, &ofmt_alias
);
678 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
679 "unrecognised output format `%s' - "
680 "use -hf for a list", param
);
684 case 'O': /* Optimization level */
689 /* Naked -O == -Ox */
690 optimizing
= MAX_OPTIMIZE
;
694 case '0': case '1': case '2': case '3': case '4':
695 case '5': case '6': case '7': case '8': case '9':
696 opt
= strtoul(param
, ¶m
, 10);
698 /* -O0 -> optimizing == -1, 0.98 behaviour */
699 /* -O1 -> optimizing == 0, 0.98.09 behaviour */
701 optimizing
= opt
- 1;
709 opt_verbose_info
= true;
714 optimizing
= MAX_OPTIMIZE
;
718 nasm_error(ERR_FATAL
,
719 "unknown optimization option -O%c\n",
724 if (optimizing
> MAX_OPTIMIZE
)
725 optimizing
= MAX_OPTIMIZE
;
730 case 'p': /* pre-include */
732 preproc
->pre_include(param
);
735 case 'd': /* pre-define */
737 preproc
->pre_define(param
);
740 case 'u': /* un-define */
742 preproc
->pre_undefine(param
);
745 case 'i': /* include search path */
747 preproc
->include_path(param
);
750 case 'l': /* listing file */
751 copy_filename(listname
, param
);
754 case 'Z': /* error messages file */
755 copy_filename(errname
, param
);
758 case 'F': /* specify debug format */
759 ofmt
->current_dfmt
= dfmt_find(ofmt
, param
);
760 if (!ofmt
->current_dfmt
) {
761 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
762 "unrecognized debug format `%s' for"
763 " output format `%s'",
764 param
, ofmt
->shortname
);
766 using_debug_info
= true;
769 case 'X': /* specify error reporting format */
770 if (nasm_stricmp("vc", param
) == 0)
771 nasm_set_verror(nasm_verror_vc
);
772 else if (nasm_stricmp("gnu", param
) == 0)
773 nasm_set_verror(nasm_verror_gnu
);
775 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
776 "unrecognized error reporting format `%s'",
781 using_debug_info
= true;
786 ("usage: nasm [-@ response file] [-o outfile] [-f format] "
788 " [options...] [--] filename\n"
789 " or nasm -v for version info\n\n"
790 " -t assemble in SciTech TASM compatible mode\n"
791 " -g generate debug information in selected format\n");
793 (" -E (or -e) preprocess only (writes output to stdout by default)\n"
794 " -a don't preprocess (assemble only)\n"
795 " -M generate Makefile dependencies on stdout\n"
796 " -MG d:o, missing files assumed generated\n"
797 " -MF <file> set Makefile dependency file\n"
798 " -MD <file> assemble and generate dependencies\n"
799 " -MT <file> dependency target name\n"
800 " -MQ <file> dependency target name (quoted)\n"
801 " -MP emit phony target\n\n"
802 " -Z<file> redirect error messages to file\n"
803 " -s redirect error messages to stdout\n\n"
804 " -F format select a debugging format\n\n"
805 " -I<path> adds a pathname to the include file path\n");
807 (" -O<digit> optimize branch offsets\n"
808 " -O0: No optimization\n"
809 " -O1: Minimal optimization\n"
810 " -Ox: Multipass optimization (default)\n\n"
811 " -P<file> pre-includes a file\n"
812 " -D<macro>[=<value>] pre-defines a macro\n"
813 " -U<macro> undefines a macro\n"
814 " -X<format> specifies error reporting format (gnu or vc)\n"
815 " -w+foo enables warning foo (equiv. -Wfoo)\n"
816 " -w-foo disable warning foo (equiv. -Wno-foo)\n\n"
817 "--prefix,--postfix\n"
818 " this options prepend or append the given argument to all\n"
819 " extern and global variables\n\n"
821 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
822 printf(" %-23s %s (default %s)\n",
823 warnings
[i
].name
, warnings
[i
].help
,
824 warnings
[i
].enabled
? "on" : "off");
826 ("\nresponse files should contain command line parameters"
827 ", one per line.\n");
829 printf("\nvalid output formats for -f are"
830 " (`*' denotes default):\n");
831 ofmt_list(ofmt
, stdout
);
833 printf("\nFor a list of valid output formats, use -hf.\n");
834 printf("For a list of debug formats, use -f <form> -y.\n");
836 exit(0); /* never need usage message here */
840 printf("\nvalid debug formats for '%s' output format are"
841 " ('*' denotes default):\n", ofmt
->shortname
);
842 dfmt_list(ofmt
, stdout
);
847 tasm_compatible_mode
= true;
851 printf("NASM version %s compiled on %s%s\n",
852 nasm_version
, nasm_date
, nasm_compile_options
);
853 exit(0); /* never need usage message here */
856 case 'e': /* preprocess only */
858 operating_mode
= op_preprocess
;
861 case 'a': /* assemble only - don't preprocess */
866 if (param
[0] == 'n' && param
[1] == 'o' && param
[2] == '-') {
875 if (param
[0] != '+' && param
[0] != '-') {
876 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
877 "invalid option to `-w'");
880 do_warn
= (param
[0] == '+');
884 for (i
= 0; i
<= ERR_WARN_MAX
; i
++) {
885 if (!nasm_stricmp(param
, warnings
[i
].name
))
888 if (i
<= ERR_WARN_MAX
) {
889 warning_on_global
[i
] = do_warn
;
890 } else if (!nasm_stricmp(param
, "all")) {
891 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
892 warning_on_global
[i
] = do_warn
;
893 } else if (!nasm_stricmp(param
, "none")) {
894 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
895 warning_on_global
[i
] = !do_warn
;
897 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
898 "invalid warning `%s'", param
);
905 operating_mode
= op_depend
;
908 operating_mode
= op_depend
;
909 depend_missing_ok
= true;
912 depend_emit_phony
= true;
923 depend_target
= quote_for_make(q
);
927 nasm_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
928 "unknown dependency option `-M%c'", p
[2]);
931 if (advance
&& (!q
|| !q
[0])) {
932 nasm_error(ERR_NONFATAL
|ERR_NOFILE
|ERR_USAGE
,
933 "option `-M%c' requires a parameter", p
[2]);
942 if (p
[2] == 0) { /* -- => stop processing options */
946 for (s
= 0; textopts
[s
].label
; s
++) {
947 if (!nasm_stricmp(p
+ 2, textopts
[s
].label
)) {
958 nasm_error(ERR_NONFATAL
| ERR_NOFILE
|
960 "option `--%s' requires an argument",
964 advance
= 1, param
= q
;
967 if (s
== OPT_PREFIX
) {
968 strncpy(lprefix
, param
, PREFIX_MAX
- 1);
969 lprefix
[PREFIX_MAX
- 1] = 0;
972 if (s
== OPT_POSTFIX
) {
973 strncpy(lpostfix
, param
, POSTFIX_MAX
- 1);
974 lpostfix
[POSTFIX_MAX
- 1] = 0;
981 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
982 "unrecognised option `--%s'", p
+ 2);
990 if (!ofmt
->setinfo(GI_SWITCH
, &p
))
991 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
992 "unrecognised option `-%c'", p
[1]);
997 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
998 "more than one input file specified");
1000 copy_filename(inname
, p
);
1007 #define ARG_BUF_DELTA 128
1009 static void process_respfile(FILE * rfile
)
1011 char *buffer
, *p
, *q
, *prevarg
;
1012 int bufsize
, prevargsize
;
1014 bufsize
= prevargsize
= ARG_BUF_DELTA
;
1015 buffer
= nasm_malloc(ARG_BUF_DELTA
);
1016 prevarg
= nasm_malloc(ARG_BUF_DELTA
);
1019 while (1) { /* Loop to handle all lines in file */
1021 while (1) { /* Loop to handle long lines */
1022 q
= fgets(p
, bufsize
- (p
- buffer
), rfile
);
1026 if (p
> buffer
&& p
[-1] == '\n')
1028 if (p
- buffer
> bufsize
- 10) {
1030 offset
= p
- buffer
;
1031 bufsize
+= ARG_BUF_DELTA
;
1032 buffer
= nasm_realloc(buffer
, bufsize
);
1033 p
= buffer
+ offset
;
1037 if (!q
&& p
== buffer
) {
1039 process_arg(prevarg
, NULL
);
1046 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
1047 * them are present at the end of the line.
1049 *(p
= &buffer
[strcspn(buffer
, "\r\n\032")]) = '\0';
1051 while (p
> buffer
&& nasm_isspace(p
[-1]))
1054 p
= nasm_skip_spaces(buffer
);
1056 if (process_arg(prevarg
, p
))
1059 if ((int) strlen(p
) > prevargsize
- 10) {
1060 prevargsize
+= ARG_BUF_DELTA
;
1061 prevarg
= nasm_realloc(prevarg
, prevargsize
);
1063 strncpy(prevarg
, p
, prevargsize
);
1067 /* Function to process args from a string of args, rather than the
1068 * argv array. Used by the environment variable and response file
1071 static void process_args(char *args
)
1073 char *p
, *q
, *arg
, *prevarg
;
1074 char separator
= ' ';
1077 if (*p
&& *p
!= '-')
1082 while (*p
&& *p
!= separator
)
1084 while (*p
== separator
)
1088 if (process_arg(prevarg
, arg
))
1092 process_arg(arg
, NULL
);
1095 static void process_response_file(const char *file
)
1098 FILE *f
= fopen(file
, "r");
1103 while (fgets(str
, sizeof str
, f
)) {
1109 static void parse_cmdline(int argc
, char **argv
)
1112 char *envreal
, *envcopy
= NULL
, *p
;
1115 *inname
= *outname
= *listname
= *errname
= '\0';
1117 for (i
= 0; i
<= ERR_WARN_MAX
; i
++)
1118 warning_on_global
[i
] = warnings
[i
].enabled
;
1121 * First, process the NASMENV environment variable.
1123 envreal
= getenv("NASMENV");
1125 envcopy
= nasm_strdup(envreal
);
1126 process_args(envcopy
);
1131 * Now process the actual command line.
1136 if (argv
[0][0] == '@') {
1138 * We have a response file, so process this as a set of
1139 * arguments like the environment variable. This allows us
1140 * to have multiple arguments on a single line, which is
1141 * different to the -@resp file processing below for regular
1144 process_response_file(argv
[0]+1);
1148 if (!stopoptions
&& argv
[0][0] == '-' && argv
[0][1] == '@') {
1149 p
= get_param(argv
[0], argc
> 1 ? argv
[1] : NULL
, &advance
);
1151 rfile
= fopen(p
, "r");
1153 process_respfile(rfile
);
1156 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
1157 "unable to open response file `%s'", p
);
1160 advance
= process_arg(argv
[0], argc
> 1 ? argv
[1] : NULL
);
1161 argv
+= advance
, argc
-= advance
;
1165 * Look for basic command line typos. This definitely doesn't
1166 * catch all errors, but it might help cases of fumbled fingers.
1169 nasm_error(ERR_NONFATAL
| ERR_NOFILE
| ERR_USAGE
,
1170 "no input file specified");
1171 else if (!strcmp(inname
, errname
) ||
1172 !strcmp(inname
, outname
) ||
1173 !strcmp(inname
, listname
) ||
1174 (depend_file
&& !strcmp(inname
, depend_file
)))
1175 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
1176 "file `%s' is both input and output file",
1180 error_file
= fopen(errname
, "w");
1182 error_file
= stderr
; /* Revert to default! */
1183 nasm_error(ERR_FATAL
| ERR_NOFILE
| ERR_USAGE
,
1184 "cannot open file `%s' for error messages",
1190 static enum directives
getkw(char **directive
, char **value
);
1192 static void assemble_file(char *fname
, StrList
**depend_ptr
)
1194 char *directive
, *value
, *p
, *q
, *special
, *line
;
1200 struct tokenval tokval
;
1204 if (cmd_sb
== 32 && cmd_cpu
< IF_386
)
1205 nasm_error(ERR_FATAL
, "command line: "
1206 "32-bit segment size requires a higher cpu");
1208 pass_max
= prev_offset_changed
= (INT_MAX
>> 1) + 2; /* Almost unlimited */
1209 for (passn
= 1; pass0
<= 2; passn
++) {
1213 pass1
= pass0
== 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
1214 pass2
= passn
> 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
1215 /* pass0 0, 0, 0, ..., 1, 2 */
1217 def_label
= passn
> 1 ? redefine_label
: define_label
;
1219 globalbits
= sb
= cmd_sb
; /* set 'bits' to command line default */
1223 nasmlist
.init(listname
, nasm_error
);
1226 global_offset_changed
= 0; /* set by redefine_label */
1227 location
.segment
= ofmt
->section(NULL
, pass2
, &sb
);
1230 saa_rewind(forwrefs
);
1231 forwref
= saa_rstruct(forwrefs
);
1233 offsets
= raa_init();
1235 preproc
->reset(fname
, pass1
, &nasmlist
,
1236 pass1
== 2 ? depend_ptr
: NULL
);
1237 memcpy(warning_on
, warning_on_global
, (ERR_WARN_MAX
+1) * sizeof(bool));
1241 location
.known
= true;
1242 location
.offset
= offs
= GET_CURR_OFFS
;
1244 while ((line
= preproc
->getline())) {
1249 * Here we parse our directives; this is not handled by the
1250 * 'real' parser. This really should be a separate function.
1253 d
= getkw(&directive
, &value
);
1258 case D_SEGMENT
: /* [SEGMENT n] */
1260 seg
= ofmt
->section(value
, pass2
, &sb
);
1261 if (seg
== NO_SEG
) {
1262 nasm_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1263 "segment name `%s' not recognized",
1267 location
.segment
= seg
;
1270 case D_SECTALIGN
: /* [SECTALIGN n] */
1274 tokval
.t_type
= TOKEN_INVALID
;
1275 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
, nasm_error
, NULL
);
1277 unsigned int align
= (unsigned int)e
->value
;
1278 if ((uint64_t)e
->value
> 0x7fffffff) {
1280 * FIXME: Please make some sane message here
1281 * ofmt should have some 'check' method which
1282 * would report segment alignment bounds.
1284 nasm_error(ERR_FATAL
,
1285 "incorrect segment alignment `%s'", value
);
1286 } else if (!is_power2(align
)) {
1287 nasm_error(ERR_NONFATAL
,
1288 "segment alignment `%s' is not power of two",
1291 /* callee should be able to handle all details */
1292 ofmt
->sectalign(location
.segment
, align
);
1296 case D_EXTERN
: /* [EXTERN label:special] */
1298 value
++; /* skip initial $ if present */
1301 while (*q
&& *q
!= ':')
1305 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1307 } else if (passn
== 1) {
1312 while (*q
&& *q
!= ':') {
1318 nasm_error(ERR_NONFATAL
,
1319 "identifier expected after EXTERN");
1327 if (!is_extern(value
)) { /* allow re-EXTERN to be ignored */
1329 pass0
= 1; /* fake pass 1 in labels.c */
1330 declare_as_global(value
, special
);
1331 define_label(value
, seg_alloc(), 0L, NULL
,
1335 } /* else pass0 == 1 */
1337 case D_BITS
: /* [BITS bits] */
1338 globalbits
= sb
= get_bits(value
);
1340 case D_GLOBAL
: /* [GLOBAL symbol:special] */
1342 value
++; /* skip initial $ if present */
1343 if (pass0
== 2) { /* pass 2 */
1345 while (*q
&& *q
!= ':')
1349 ofmt
->symdef(value
, 0L, 0L, 3, q
);
1351 } else if (pass2
== 1) { /* pass == 1 */
1356 while (*q
&& *q
!= ':') {
1362 nasm_error(ERR_NONFATAL
,
1363 "identifier expected after GLOBAL");
1371 declare_as_global(value
, special
);
1374 case D_COMMON
: /* [COMMON symbol size:special] */
1379 value
++; /* skip initial $ if present */
1384 while (*p
&& !nasm_isspace(*p
)) {
1390 nasm_error(ERR_NONFATAL
,
1391 "identifier expected after COMMON");
1395 p
= nasm_zap_spaces_fwd(p
);
1397 while (*q
&& *q
!= ':')
1405 size
= readnum(p
, &rn_error
);
1407 nasm_error(ERR_NONFATAL
,
1408 "invalid size specified"
1409 " in COMMON declaration");
1413 nasm_error(ERR_NONFATAL
,
1414 "no size specified in"
1415 " COMMON declaration");
1420 define_common(value
, seg_alloc(), size
, special
);
1421 } else if (pass0
== 2) {
1423 ofmt
->symdef(value
, 0L, 0L, 3, special
);
1427 case D_ABSOLUTE
: /* [ABSOLUTE address] */
1430 tokval
.t_type
= TOKEN_INVALID
;
1431 e
= evaluate(stdscan
, NULL
, &tokval
, NULL
, pass2
,
1436 1 ? ERR_NONFATAL
: ERR_PANIC
,
1437 "cannot use non-relocatable expression as "
1438 "ABSOLUTE address");
1440 abs_seg
= reloc_seg(e
);
1441 abs_offset
= reloc_value(e
);
1443 } else if (passn
== 1)
1444 abs_offset
= 0x100; /* don't go near zero in case of / */
1446 nasm_error(ERR_PANIC
, "invalid ABSOLUTE address "
1449 location
.segment
= NO_SEG
;
1451 case D_DEBUG
: /* [DEBUG] */
1454 bool badid
, overlong
;
1458 badid
= overlong
= false;
1459 if (!isidstart(*p
)) {
1462 while (*p
&& !nasm_isspace(*p
)) {
1463 if (q
>= debugid
+ sizeof debugid
- 1) {
1474 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1475 "identifier expected after DEBUG");
1479 nasm_error(passn
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1480 "DEBUG identifier too long");
1483 p
= nasm_skip_spaces(p
);
1485 dfmt
->debug_directive(debugid
, p
);
1488 case D_WARNING
: /* [WARNING {+|-|*}warn-name] */
1489 value
= nasm_skip_spaces(value
);
1491 case '-': validid
= 0; value
++; break;
1492 case '+': validid
= 1; value
++; break;
1493 case '*': validid
= 2; value
++; break;
1494 default: validid
= 1; break;
1497 for (i
= 1; i
<= ERR_WARN_MAX
; i
++)
1498 if (!nasm_stricmp(value
, warnings
[i
].name
))
1500 if (i
<= ERR_WARN_MAX
) {
1503 warning_on
[i
] = false;
1506 warning_on
[i
] = true;
1509 warning_on
[i
] = warning_on_global
[i
];
1514 nasm_error(ERR_NONFATAL
,
1515 "invalid warning id in WARNING directive");
1517 case D_CPU
: /* [CPU] */
1518 cpu
= get_cpu(value
);
1520 case D_LIST
: /* [LIST {+|-}] */
1521 value
= nasm_skip_spaces(value
);
1522 if (*value
== '+') {
1525 if (*value
== '-') {
1532 case D_DEFAULT
: /* [DEFAULT] */
1535 tokval
.t_type
= TOKEN_INVALID
;
1536 if (stdscan(NULL
, &tokval
) == TOKEN_SPECIAL
) {
1537 switch ((int)tokval
.t_integer
) {
1553 if (float_option(value
)) {
1554 nasm_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1555 "unknown 'float' directive: %s",
1560 if (ofmt
->directive(d
, value
, pass2
))
1562 /* else fall through */
1564 nasm_error(pass1
== 1 ? ERR_NONFATAL
: ERR_PANIC
,
1565 "unrecognised directive [%s]",
1570 nasm_error(ERR_NONFATAL
,
1571 "invalid parameter to [%s] directive",
1574 } else { /* it isn't a directive */
1575 parse_line(pass1
, line
, &output_ins
, def_label
);
1577 if (optimizing
> 0) {
1578 if (forwref
!= NULL
&& globallineno
== forwref
->lineno
) {
1579 output_ins
.forw_ref
= true;
1581 output_ins
.oprs
[forwref
->operand
].opflags
|= OPFLAG_FORWARD
;
1582 forwref
= saa_rstruct(forwrefs
);
1583 } while (forwref
!= NULL
1584 && forwref
->lineno
== globallineno
);
1586 output_ins
.forw_ref
= false;
1588 if (output_ins
.forw_ref
) {
1590 for (i
= 0; i
< output_ins
.operands
; i
++) {
1591 if (output_ins
.oprs
[i
].opflags
& OPFLAG_FORWARD
) {
1592 struct forwrefinfo
*fwinf
=
1593 (struct forwrefinfo
*)
1594 saa_wstruct(forwrefs
);
1595 fwinf
->lineno
= globallineno
;
1604 if (output_ins
.opcode
== I_EQU
) {
1607 * Special `..' EQUs get processed in pass two,
1608 * except `..@' macro-processor EQUs which are done
1609 * in the normal place.
1611 if (!output_ins
.label
)
1612 nasm_error(ERR_NONFATAL
,
1613 "EQU not preceded by label");
1615 else if (output_ins
.label
[0] != '.' ||
1616 output_ins
.label
[1] != '.' ||
1617 output_ins
.label
[2] == '@') {
1618 if (output_ins
.operands
== 1 &&
1619 (output_ins
.oprs
[0].type
& IMMEDIATE
) &&
1620 output_ins
.oprs
[0].wrt
== NO_SEG
) {
1621 bool isext
= !!(output_ins
.oprs
[0].opflags
1623 def_label(output_ins
.label
,
1624 output_ins
.oprs
[0].segment
,
1625 output_ins
.oprs
[0].offset
, NULL
,
1627 } else if (output_ins
.operands
== 2
1628 && (output_ins
.oprs
[0].type
& IMMEDIATE
)
1629 && (output_ins
.oprs
[0].type
& COLON
)
1630 && output_ins
.oprs
[0].segment
== NO_SEG
1631 && output_ins
.oprs
[0].wrt
== NO_SEG
1632 && (output_ins
.oprs
[1].type
& IMMEDIATE
)
1633 && output_ins
.oprs
[1].segment
== NO_SEG
1634 && output_ins
.oprs
[1].wrt
== NO_SEG
) {
1635 def_label(output_ins
.label
,
1636 output_ins
.oprs
[0].offset
| SEG_ABS
,
1637 output_ins
.oprs
[1].offset
,
1638 NULL
, false, false);
1640 nasm_error(ERR_NONFATAL
,
1641 "bad syntax for EQU");
1645 * Special `..' EQUs get processed here, except
1646 * `..@' macro processor EQUs which are done above.
1648 if (output_ins
.label
[0] == '.' &&
1649 output_ins
.label
[1] == '.' &&
1650 output_ins
.label
[2] != '@') {
1651 if (output_ins
.operands
== 1 &&
1652 (output_ins
.oprs
[0].type
& IMMEDIATE
)) {
1653 define_label(output_ins
.label
,
1654 output_ins
.oprs
[0].segment
,
1655 output_ins
.oprs
[0].offset
,
1656 NULL
, false, false);
1657 } else if (output_ins
.operands
== 2
1658 && (output_ins
.oprs
[0].type
& IMMEDIATE
)
1659 && (output_ins
.oprs
[0].type
& COLON
)
1660 && output_ins
.oprs
[0].segment
== NO_SEG
1661 && (output_ins
.oprs
[1].type
& IMMEDIATE
)
1662 && output_ins
.oprs
[1].segment
== NO_SEG
) {
1663 define_label(output_ins
.label
,
1664 output_ins
.oprs
[0].offset
| SEG_ABS
,
1665 output_ins
.oprs
[1].offset
,
1666 NULL
, false, false);
1668 nasm_error(ERR_NONFATAL
,
1669 "bad syntax for EQU");
1672 } else { /* instruction isn't an EQU */
1676 int64_t l
= insn_size(location
.segment
, offs
, sb
, cpu
,
1677 &output_ins
, nasm_error
);
1679 /* if (using_debug_info) && output_ins.opcode != -1) */
1680 if (using_debug_info
)
1681 { /* fbk 03/25/01 */
1682 /* this is done here so we can do debug type info */
1684 TYS_ELEMENTS(output_ins
.operands
);
1685 switch (output_ins
.opcode
) {
1688 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_BYTE
;
1692 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_WORD
;
1696 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_DWORD
;
1700 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_QWORD
;
1704 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_TBYTE
;
1708 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_OWORD
;
1712 TYS_ELEMENTS(output_ins
.oprs
[0].offset
) | TY_YWORD
;
1715 typeinfo
|= TY_BYTE
;
1718 typeinfo
|= TY_WORD
;
1721 if (output_ins
.eops_float
)
1722 typeinfo
|= TY_FLOAT
;
1724 typeinfo
|= TY_DWORD
;
1727 typeinfo
|= TY_QWORD
;
1730 typeinfo
|= TY_TBYTE
;
1733 typeinfo
|= TY_OWORD
;
1736 typeinfo
|= TY_YWORD
;
1739 typeinfo
= TY_LABEL
;
1743 dfmt
->debug_typevalue(typeinfo
);
1747 SET_CURR_OFFS(offs
);
1750 * else l == -1 => invalid instruction, which will be
1751 * flagged as an error on pass 2
1755 offs
+= assemble(location
.segment
, offs
, sb
, cpu
,
1756 &output_ins
, ofmt
, nasm_error
,
1758 SET_CURR_OFFS(offs
);
1762 cleanup_insn(&output_ins
);
1765 location
.offset
= offs
= GET_CURR_OFFS
;
1766 } /* end while (line = preproc->getline... */
1768 if (pass0
== 2 && global_offset_changed
&& !terminate_after_phase
)
1769 nasm_error(ERR_NONFATAL
,
1770 "phase error detected at end of assembly.");
1773 preproc
->cleanup(1);
1775 if ((passn
> 1 && !global_offset_changed
) || pass0
== 2) {
1777 } else if (global_offset_changed
&&
1778 global_offset_changed
< prev_offset_changed
) {
1779 prev_offset_changed
= global_offset_changed
;
1785 if (terminate_after_phase
)
1788 if ((stall_count
> 997) || (passn
>= pass_max
)) {
1789 /* We get here if the labels don't converge
1790 * Example: FOO equ FOO + 1
1792 nasm_error(ERR_NONFATAL
,
1793 "Can't find valid values for all labels "
1794 "after %d passes, giving up.", passn
);
1795 nasm_error(ERR_NONFATAL
,
1796 "Possible causes: recursive EQUs, macro abuse.");
1801 preproc
->cleanup(0);
1803 if (!terminate_after_phase
&& opt_verbose_info
) {
1804 /* -On and -Ov switches */
1805 fprintf(stdout
, "info: assembly required 1+%d+1 passes\n", passn
-3);
1809 static enum directives
getkw(char **directive
, char **value
)
1813 buf
= nasm_skip_spaces(*directive
);
1815 /* it should be enclosed in [ ] */
1818 q
= strchr(buf
, ']');
1822 /* stip off the comments */
1823 p
= strchr(buf
, ';');
1825 if (p
< q
) /* ouch! somwhere inside */
1830 /* no brace, no trailing spaces */
1832 nasm_zap_spaces_rev(--q
);
1835 p
= nasm_skip_spaces(++buf
);
1836 q
= nasm_skip_word(p
);
1838 return D_none
; /* sigh... no value there */
1842 /* and value finally */
1843 p
= nasm_skip_spaces(++q
);
1846 return find_directive(*directive
);
1850 * gnu style error reporting
1851 * This function prints an error message to error_file in the
1852 * style used by GNU. An example would be:
1853 * file.asm:50: error: blah blah blah
1854 * where file.asm is the name of the file, 50 is the line number on
1855 * which the error occurs (or is detected) and "error:" is one of
1856 * the possible optional diagnostics -- it can be "error" or "warning"
1857 * or something else. Finally the line terminates with the actual
1860 * @param severity the severity of the warning or error
1861 * @param fmt the printf style format string
1863 static void nasm_verror_gnu(int severity
, const char *fmt
, va_list ap
)
1865 char *currentfile
= NULL
;
1868 if (is_suppressed_warning(severity
))
1871 if (!(severity
& ERR_NOFILE
))
1872 src_get(&lineno
, ¤tfile
);
1875 fprintf(error_file
, "%s:%"PRId32
": ", currentfile
, lineno
);
1876 nasm_free(currentfile
);
1878 fputs("nasm: ", error_file
);
1881 nasm_verror_common(severity
, fmt
, ap
);
1885 * MS style error reporting
1886 * This function prints an error message to error_file in the
1887 * style used by Visual C and some other Microsoft tools. An example
1889 * file.asm(50) : error: blah blah blah
1890 * where file.asm is the name of the file, 50 is the line number on
1891 * which the error occurs (or is detected) and "error:" is one of
1892 * the possible optional diagnostics -- it can be "error" or "warning"
1893 * or something else. Finally the line terminates with the actual
1896 * @param severity the severity of the warning or error
1897 * @param fmt the printf style format string
1899 static void nasm_verror_vc(int severity
, const char *fmt
, va_list ap
)
1901 char *currentfile
= NULL
;
1904 if (is_suppressed_warning(severity
))
1907 if (!(severity
& ERR_NOFILE
))
1908 src_get(&lineno
, ¤tfile
);
1911 fprintf(error_file
, "%s(%"PRId32
") : ", currentfile
, lineno
);
1912 nasm_free(currentfile
);
1914 fputs("nasm: ", error_file
);
1917 nasm_verror_common(severity
, fmt
, ap
);
1921 * check for supressed warning
1922 * checks for suppressed warning or pass one only warning and we're
1925 * @param severity the severity of the warning or error
1926 * @return true if we should abort error/warning printing
1928 static bool is_suppressed_warning(int severity
)
1930 /* Not a warning at all */
1931 if ((severity
& ERR_MASK
) != ERR_WARNING
)
1934 /* See if it's a pass-one only warning and we're not in pass one. */
1935 if (((severity
& ERR_PASS1
) && pass0
!= 1) ||
1936 ((severity
& ERR_PASS2
) && pass0
!= 2))
1939 /* Might be a warning but suppresed explicitly */
1940 if (severity
& ERR_WARN_MASK
)
1941 return !warning_on
[WARN_IDX(severity
)];
1947 * common error reporting
1948 * This is the common back end of the error reporting schemes currently
1949 * implemented. It prints the nature of the warning and then the
1950 * specific error message to error_file and may or may not return. It
1951 * doesn't return if the error severity is a "panic" or "debug" type.
1953 * @param severity the severity of the warning or error
1954 * @param fmt the printf style format string
1956 static void nasm_verror_common(int severity
, const char *fmt
, va_list args
)
1961 switch (severity
& (ERR_MASK
|ERR_NO_SEVERITY
)) {
1982 vsnprintf(msg
, sizeof msg
, fmt
, args
);
1984 fprintf(error_file
, "%s%s\n", pfx
, msg
);
1987 nasmlist
.error(severity
, pfx
, msg
);
1989 if (severity
& ERR_USAGE
)
1992 switch (severity
& ERR_MASK
) {
1994 /* no further action, by definition */
1997 /* Treat warnings as errors */
1998 if (warning_on
[WARN_IDX(ERR_WARN_TERM
)])
1999 terminate_after_phase
= true;
2002 terminate_after_phase
= true;
2012 exit(1); /* instantly die */
2013 break; /* placate silly compilers */
2016 /* abort(); *//* halt, catch fire, and dump core */
2022 static void usage(void)
2024 fputs("type `nasm -h' for help\n", error_file
);
2027 #define BUF_DELTA 512
2029 static FILE *no_pp_fp
;
2030 static ListGen
*no_pp_list
;
2031 static int32_t no_pp_lineinc
;
2033 static void no_pp_reset(char *file
, int pass
, ListGen
* listgen
,
2036 src_set_fname(nasm_strdup(file
));
2039 no_pp_fp
= fopen(file
, "r");
2041 nasm_error(ERR_FATAL
| ERR_NOFILE
,
2042 "unable to open input file `%s'", file
);
2043 no_pp_list
= listgen
;
2044 (void)pass
; /* placate compilers */
2047 StrList
*sl
= nasm_malloc(strlen(file
)+1+sizeof sl
->next
);
2049 strcpy(sl
->str
, file
);
2054 static char *no_pp_getline(void)
2056 char *buffer
, *p
, *q
;
2059 bufsize
= BUF_DELTA
;
2060 buffer
= nasm_malloc(BUF_DELTA
);
2061 src_set_linnum(src_get_linnum() + no_pp_lineinc
);
2063 while (1) { /* Loop to handle %line */
2066 while (1) { /* Loop to handle long lines */
2067 q
= fgets(p
, bufsize
- (p
- buffer
), no_pp_fp
);
2071 if (p
> buffer
&& p
[-1] == '\n')
2073 if (p
- buffer
> bufsize
- 10) {
2075 offset
= p
- buffer
;
2076 bufsize
+= BUF_DELTA
;
2077 buffer
= nasm_realloc(buffer
, bufsize
);
2078 p
= buffer
+ offset
;
2082 if (!q
&& p
== buffer
) {
2088 * Play safe: remove CRs, LFs and any spurious ^Zs, if any of
2089 * them are present at the end of the line.
2091 buffer
[strcspn(buffer
, "\r\n\032")] = '\0';
2093 if (!nasm_strnicmp(buffer
, "%line", 5)) {
2096 char *nm
= nasm_malloc(strlen(buffer
));
2097 if (sscanf(buffer
+ 5, "%"PRId32
"+%d %s", &ln
, &li
, nm
) == 3) {
2098 nasm_free(src_set_fname(nm
));
2108 no_pp_list
->line(LIST_READ
, buffer
);
2113 static void no_pp_cleanup(int pass
)
2115 (void)pass
; /* placate GCC */
2122 static void no_pp_extra_stdmac(macros_t
*macros
)
2127 static void no_pp_pre_define(char *definition
)
2132 static void no_pp_pre_undefine(char *definition
)
2137 static void no_pp_pre_include(char *fname
)
2142 static void no_pp_include_path(char *path
)
2147 static uint32_t get_cpu(char *value
)
2149 if (!strcmp(value
, "8086"))
2151 if (!strcmp(value
, "186"))
2153 if (!strcmp(value
, "286"))
2155 if (!strcmp(value
, "386"))
2157 if (!strcmp(value
, "486"))
2159 if (!strcmp(value
, "586") || !nasm_stricmp(value
, "pentium"))
2161 if (!strcmp(value
, "686") ||
2162 !nasm_stricmp(value
, "ppro") ||
2163 !nasm_stricmp(value
, "pentiumpro") || !nasm_stricmp(value
, "p2"))
2165 if (!nasm_stricmp(value
, "p3") || !nasm_stricmp(value
, "katmai"))
2167 if (!nasm_stricmp(value
, "p4") || /* is this right? -- jrc */
2168 !nasm_stricmp(value
, "willamette"))
2169 return IF_WILLAMETTE
;
2170 if (!nasm_stricmp(value
, "prescott"))
2172 if (!nasm_stricmp(value
, "x64") ||
2173 !nasm_stricmp(value
, "x86-64"))
2175 if (!nasm_stricmp(value
, "ia64") ||
2176 !nasm_stricmp(value
, "ia-64") ||
2177 !nasm_stricmp(value
, "itanium") ||
2178 !nasm_stricmp(value
, "itanic") || !nasm_stricmp(value
, "merced"))
2181 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
2182 "unknown 'cpu' type");
2184 return IF_PLEVEL
; /* the maximum level */
2187 static int get_bits(char *value
)
2191 if ((i
= atoi(value
)) == 16)
2192 return i
; /* set for a 16-bit segment */
2195 nasm_error(ERR_NONFATAL
,
2196 "cannot specify 32-bit segment on processor below a 386");
2199 } else if (i
== 64) {
2200 if (cpu
< IF_X86_64
) {
2201 nasm_error(ERR_NONFATAL
,
2202 "cannot specify 64-bit segment on processor below an x86-64");
2206 nasm_error(ERR_NONFATAL
,
2207 "%s output format does not support 64-bit code",
2212 nasm_error(pass0
< 2 ? ERR_NONFATAL
: ERR_FATAL
,
2213 "`%s' is not a valid segment size; must be 16, 32 or 64",