-
Notifications
You must be signed in to change notification settings - Fork 345
/
Copy pathnasm.c
2560 lines (2227 loc) · 73.9 KB
/
nasm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2024 The NASM Authors - All Rights Reserved
* See the file AUTHORS included with the NASM distribution for
* the specific copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ----------------------------------------------------------------------- */
/*
* The Netwide Assembler main program module
*/
#include "compiler.h"
#include "nasm.h"
#include "nasmlib.h"
#include "nctype.h"
#include "error.h"
#include "saa.h"
#include "raa.h"
#include "floats.h"
#include "stdscan.h"
#include "insns.h"
#include "preproc.h"
#include "parser.h"
#include "eval.h"
#include "assemble.h"
#include "labels.h"
#include "outform.h"
#include "listing.h"
#include "iflag.h"
#include "quote.h"
#include "ver.h"
/*
* This is the maximum number of optimization passes to do. If we ever
* find a case where the optimizer doesn't naturally converge, we might
* have to drop this value so the assembler doesn't appear to just hang.
*/
#define MAX_OPTIMIZE (INT_MAX >> 1)
struct forwrefinfo { /* info held on forward refs. */
int lineno;
int operand;
};
const char *_progname;
static void parse_cmdline(int, char **, int);
static void assemble_file(const char *, struct strlist *);
static bool skip_this_pass(errflags severity);
static void usage(void);
static void help(FILE *out, const char *what);
struct error_format {
const char *beforeline; /* Before line number, if present */
const char *afterline; /* After line number, if present */
const char *beforemsg; /* Before actual message */
};
static const struct error_format errfmt_gnu = { ":", "", ": " };
static const struct error_format errfmt_msvc = { "(", ")", " : " };
static const struct error_format *errfmt = &errfmt_gnu;
static struct strlist *warn_list;
static struct nasm_errhold *errhold_stack;
unsigned int debug_nasm; /* Debugging messages? */
static bool using_debug_info, opt_verbose_info;
static const char *debug_format;
#ifndef ABORT_ON_PANIC
# define ABORT_ON_PANIC 0
#endif
static bool abort_on_panic = ABORT_ON_PANIC;
static bool keep_all;
bool tasm_compatible_mode = false;
enum pass_type _pass_type;
const char * const _pass_types[] =
{
"init", "preproc-only", "first", "optimize", "stabilize", "final"
};
int64_t _passn;
int globalrel = 0;
int globalbnd = 0;
struct compile_time official_compile_time;
const char *inname;
const char *outname;
static const char *listname;
static const char *errname;
static int64_t globallineno; /* for forward-reference tracking */
const struct ofmt *ofmt = &OF_DEFAULT;
const struct ofmt_alias *ofmt_alias = NULL;
const struct dfmt *dfmt;
FILE *error_file; /* Where to write error messages */
FILE *ofile = NULL;
struct optimization optimizing =
{ MAX_OPTIMIZE, OPTIM_ALL_ENABLED }; /* number of optimization passes to take */
static int cmd_sb = 16; /* by default */
iflag_t cpu, cmd_cpu;
struct location location;
bool in_absolute; /* Flag we are in ABSOLUTE seg */
struct location absolute; /* Segment/offset inside ABSOLUTE */
static struct RAA *offsets;
static struct SAA *forwrefs; /* keep track of forward references */
static const struct forwrefinfo *forwref;
static struct strlist *include_path;
static enum preproc_opt ppopt;
#define OP_NORMAL (1U << 0)
#define OP_PREPROCESS (1U << 1)
#define OP_DEPEND (1U << 2)
static unsigned int operating_mode;
/* Dependency flags */
static bool depend_emit_phony = false;
static bool depend_missing_ok = false;
static const char *depend_target = NULL;
static const char *depend_file = NULL;
struct strlist *depend_list;
static bool want_usage;
static bool terminate_after_phase;
bool user_nolist = false;
static char *quote_for_pmake(const char *str);
static char *quote_for_wmake(const char *str);
static char *(*quote_for_make)(const char *) = quote_for_pmake;
/*
* Execution limits that can be set via a command-line option or %pragma
*/
/*
* This is really unlimited; it would take far longer than the
* current age of the universe for this limit to be reached even on
* much faster CPUs than currently exist.
*/
#define LIMIT_MAX_VAL (INT64_MAX >> 1)
int64_t nasm_limit[LIMIT_MAX+1];
struct limit_info {
const char *name;
const char *help;
int64_t default_val;
};
/* The order here must match enum nasm_limit in nasm.h */
static const struct limit_info limit_info[LIMIT_MAX+1] = {
{ "passes", "total number of passes", LIMIT_MAX_VAL },
{ "stalled-passes", "number of passes without forward progress", 1000 },
{ "macro-levels", "levels of macro expansion", 10000 },
{ "macro-tokens", "tokens processed during single-lime macro expansion", 10000000 },
{ "mmacros", "multi-line macros before final return", 100000 },
{ "rep", "%rep count", 1000000 },
{ "eval", "expression evaluation descent", 8192 },
{ "lines", "total source lines processed", 2000000000 }
};
static void set_default_limits(void)
{
int i;
size_t rl;
int64_t new_limit;
for (i = 0; i <= LIMIT_MAX; i++)
nasm_limit[i] = limit_info[i].default_val;
/*
* Try to set a sensible default value for the eval depth based
* on the limit of the stack size, if knowable...
*/
rl = nasm_get_stack_size_limit();
new_limit = rl / (128 * sizeof(void *)); /* Sensible heuristic */
if (new_limit < nasm_limit[LIMIT_EVAL])
nasm_limit[LIMIT_EVAL] = new_limit;
}
enum directive_result
nasm_set_limit(const char *limit, const char *valstr)
{
int i;
int64_t val;
bool rn_error;
int errlevel;
if (!limit)
limit = "";
if (!valstr)
valstr = "";
for (i = 0; i <= LIMIT_MAX; i++) {
if (!nasm_stricmp(limit, limit_info[i].name))
break;
}
if (i > LIMIT_MAX) {
if (not_started())
errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
else
errlevel = ERR_WARNING|WARN_PRAGMA_UNKNOWN;
nasm_error(errlevel, "unknown limit: `%s'", limit);
return DIRR_ERROR;
}
if (!nasm_stricmp(valstr, "unlimited")) {
val = LIMIT_MAX_VAL;
} else {
val = readnum(valstr, &rn_error);
if (rn_error || val < 0) {
if (not_started())
errlevel = ERR_WARNING|WARN_OTHER|ERR_USAGE;
else
errlevel = ERR_WARNING|WARN_PRAGMA_BAD;
nasm_error(errlevel, "invalid limit value: `%s'", valstr);
return DIRR_ERROR;
}
if (val > LIMIT_MAX_VAL)
val = LIMIT_MAX_VAL;
}
nasm_limit[i] = val;
return DIRR_OK;
}
int64_t switch_segment(int32_t segment)
{
location.segment = segment;
if (segment == NO_SEG) {
location.offset = absolute.offset;
in_absolute = true;
} else {
location.offset = raa_read(offsets, segment);
in_absolute = false;
}
return location.offset;
}
static void set_curr_offs(int64_t l_off)
{
if (in_absolute)
absolute.offset = l_off;
else
offsets = raa_write(offsets, location.segment, l_off);
}
static void increment_offset(int64_t delta)
{
if (unlikely(delta == 0))
return;
location.offset += delta;
set_curr_offs(location.offset);
}
/*
* Define system-defined macros that are not part of
* macros/standard.mac.
*/
static void define_macros(void)
{
const struct compile_time * const oct = &official_compile_time;
char temp[128];
if (oct->have_local) {
strftime(temp, sizeof temp, "__?DATE?__=\"%Y-%m-%d\"", &oct->local);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__?DATE_NUM?__=%Y%m%d", &oct->local);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__?TIME?__=\"%H:%M:%S\"", &oct->local);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__?TIME_NUM?__=%H%M%S", &oct->local);
pp_pre_define(temp);
}
if (oct->have_gm) {
strftime(temp, sizeof temp, "__?UTC_DATE?__=\"%Y-%m-%d\"", &oct->gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__?UTC_DATE_NUM?__=%Y%m%d", &oct->gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__?UTC_TIME?__=\"%H:%M:%S\"", &oct->gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__?UTC_TIME_NUM?__=%H%M%S", &oct->gm);
pp_pre_define(temp);
}
if (oct->have_posix) {
snprintf(temp, sizeof temp, "__?POSIX_TIME?__=%"PRId64, oct->posix);
pp_pre_define(temp);
}
/*
* In case if output format is defined by alias
* we have to put shortname of the alias itself here
* otherwise ABI backward compatibility gets broken.
*/
snprintf(temp, sizeof(temp), "__?OUTPUT_FORMAT?__=%s",
ofmt_alias ? ofmt_alias->shortname : ofmt->shortname);
pp_pre_define(temp);
/*
* Output-format specific macros.
*/
if (ofmt->stdmac)
pp_extra_stdmac(ofmt->stdmac);
/*
* Debug format, if any
*/
if (dfmt != &null_debug_form) {
snprintf(temp, sizeof(temp), "__?DEBUG_FORMAT?__=%s", dfmt->shortname);
pp_pre_define(temp);
}
}
/*
* Initialize the preprocessor, set up the include path, and define
* the system-included macros. This is called between passes 1 and 2
* of parsing the command options; ofmt and dfmt are defined at this
* point.
*
* Command-line specified preprocessor directives (-p, -d, -u,
* --pragma, --before) are processed after this function.
*/
static void preproc_init(struct strlist *ipath)
{
pp_init(ppopt);
define_macros();
pp_include_path(ipath);
}
static void emit_dependencies(struct strlist *list)
{
FILE *deps;
int linepos, len;
bool wmake = (quote_for_make == quote_for_wmake);
const char *wrapstr, *nulltarget;
const struct strlist_entry *l;
if (!list)
return;
wrapstr = wmake ? " &\n " : " \\\n ";
nulltarget = wmake ? "\t%null\n" : "";
if (depend_file && strcmp(depend_file, "-")) {
deps = nasm_open_write(depend_file, NF_TEXT);
if (!deps) {
nasm_nonfatal("unable to write dependency file `%s'", depend_file);
return;
}
} else {
deps = stdout;
}
linepos = fprintf(deps, "%s :", depend_target);
strlist_for_each(l, list) {
char *file = quote_for_make(l->str);
len = strlen(file);
if (linepos + len > 62 && linepos > 1) {
fputs(wrapstr, deps);
linepos = 1;
}
fprintf(deps, " %s", file);
linepos += len+1;
nasm_free(file);
}
fputs("\n\n", deps);
strlist_for_each(l, list) {
if (depend_emit_phony) {
char *file = quote_for_make(l->str);
fprintf(deps, "%s :\n%s\n", file, nulltarget);
nasm_free(file);
}
}
strlist_free(&list);
if (deps != stdout)
fclose(deps);
}
/* Convert a struct tm to a POSIX-style time constant */
static int64_t make_posix_time(const struct tm *tm)
{
int64_t t;
int64_t y = tm->tm_year;
/* See IEEE 1003.1:2004, section 4.14 */
t = (y-70)*365 + (y-69)/4 - (y-1)/100 + (y+299)/400;
t += tm->tm_yday;
t *= 24;
t += tm->tm_hour;
t *= 60;
t += tm->tm_min;
t *= 60;
t += tm->tm_sec;
return t;
}
/*
* Quote a filename string if and only if it is necessary.
* It is considered necessary if any one of these is true:
* 1. The filename contains control characters;
* 2. The filename starts or ends with a space or quote mark;
* 3. The filename contains more than one space in a row;
* 4. The filename is empty.
*
* The filename is returned in a newly allocated buffer.
*/
static char *nasm_quote_filename(const char *fn)
{
const unsigned char *p =
(const unsigned char *)fn;
size_t len;
if (!p || !*p)
return nasm_strdup("\"\"");
if (*p <= ' ' || nasm_isquote(*p)) {
goto quote;
} else {
unsigned char cutoff = ' ';
while (*p) {
if (*p < cutoff)
goto quote;
cutoff = ' ' + (*p == ' ');
p++;
}
if (p[-1] <= ' ' || nasm_isquote(p[-1]))
goto quote;
}
/* Quoting not necessary */
return nasm_strdup(fn);
quote:
len = strlen(fn);
return nasm_quote(fn, &len);
}
static void timestamp(void)
{
struct compile_time * const oct = &official_compile_time;
const struct tm *tp, *best_gm;
time(&oct->t);
best_gm = NULL;
tp = localtime(&oct->t);
if (tp) {
oct->local = *tp;
best_gm = &oct->local;
oct->have_local = true;
}
tp = gmtime(&oct->t);
if (tp) {
oct->gm = *tp;
best_gm = &oct->gm;
oct->have_gm = true;
if (!oct->have_local)
oct->local = oct->gm;
} else {
oct->gm = oct->local;
}
if (best_gm) {
oct->posix = make_posix_time(best_gm);
oct->have_posix = true;
}
}
int main(int argc, char **argv)
{
/* Do these as early as possible */
error_file = stderr;
_progname = argv[0];
if (!_progname || !_progname[0])
_progname = "nasm";
timestamp();
set_cpu(NULL);
cmd_cpu = cpu;
set_default_limits();
include_path = strlist_alloc(true);
_pass_type = PASS_INIT;
_passn = 0;
want_usage = terminate_after_phase = false;
nasm_ctype_init();
src_init();
/*
* We must call init_labels() before the command line parsing,
* because we may be setting prefixes/suffixes from the command
* line.
*/
init_labels();
offsets = raa_init();
forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
operating_mode = OP_NORMAL;
parse_cmdline(argc, argv, 1);
if (terminate_after_phase) {
if (want_usage)
usage();
return 1;
}
/* At this point we have ofmt and the name of the desired debug format */
if (!using_debug_info) {
/* No debug info, redirect to the null backend (empty stubs) */
dfmt = &null_debug_form;
} else if (!debug_format) {
/* Default debug format for this backend */
dfmt = ofmt->default_dfmt;
} else {
dfmt = dfmt_find(ofmt, debug_format);
if (!dfmt) {
nasm_fatalf(ERR_USAGE, "unrecognized debug format `%s' for output format `%s'",
debug_format, ofmt->shortname);
}
}
/* Have we enabled TASM mode? */
if (tasm_compatible_mode) {
ppopt |= PP_TASM;
nasm_ctype_tasm_mode();
}
preproc_init(include_path);
parse_cmdline(argc, argv, 2);
if (terminate_after_phase) {
if (want_usage)
usage();
return 1;
}
/* Save away the default state of warnings */
init_warnings();
/* Dependency filename if we are also doing other things */
if (!depend_file && (operating_mode & ~OP_DEPEND)) {
if (outname)
depend_file = nasm_strcat(outname, ".d");
else
depend_file = filename_set_extension(inname, ".d");
}
/*
* If no output file name provided and this
* is preprocess mode, we're perfectly
* fine to output into stdout.
*/
if (!outname && !(operating_mode & OP_PREPROCESS)) {
outname = filename_set_extension(inname, ofmt->extension);
if (!strcmp(outname, inname)) {
outname = "nasm.out";
nasm_warn(WARN_OTHER, "default output file same as input, using `%s' for output\n", outname);
}
}
depend_list = (operating_mode & OP_DEPEND) ? strlist_alloc(true) : NULL;
if (!depend_target)
depend_target = quote_for_make(outname);
if (!(operating_mode & (OP_PREPROCESS|OP_NORMAL))) {
char *line;
if (depend_missing_ok)
pp_include_path(NULL); /* "assume generated" */
pp_reset(inname, PP_DEPS, depend_list);
ofile = NULL;
while ((line = pp_getline()))
nasm_free(line);
pp_cleanup_pass();
reset_warnings();
} else if (operating_mode & OP_PREPROCESS) {
char *line;
const char *file_name = NULL;
char *quoted_file_name = nasm_quote_filename(file_name);
int32_t linnum = 0;
int32_t lineinc = 0;
FILE *out;
if (outname) {
ofile = nasm_open_write(outname, NF_TEXT);
if (!ofile)
nasm_fatal("unable to open output file `%s'", outname);
out = ofile;
} else {
ofile = NULL;
out = stdout;
}
location.known = false;
_pass_type = PASS_PREPROC;
pp_reset(inname, PP_PREPROC, depend_list);
while ((line = pp_getline())) {
/*
* We generate %line directives if needed for later programs
*/
struct src_location where = src_where();
if (file_name != where.filename) {
file_name = where.filename;
linnum = -1; /* Force a new %line statement */
lineinc = file_name ? 1 : 0;
nasm_free(quoted_file_name);
quoted_file_name = nasm_quote_filename(file_name);
} else if (lineinc) {
if (linnum + lineinc == where.lineno) {
/* Add one blank line to account for increment */
fputc('\n', out);
linnum += lineinc;
} else if (linnum - lineinc == where.lineno) {
/*
* Standing still, probably a macro. Set increment
* to zero.
*/
lineinc = 0;
}
} else {
/* lineinc == 0 */
if (linnum + 1 == where.lineno)
lineinc = 1;
}
/* Skip blank lines if we will need a %line anyway */
if (linnum == -1 && !line[0])
continue;
if (linnum != where.lineno) {
fprintf(out, "%%line %"PRId32"%+"PRId32" %s\n",
where.lineno, lineinc, quoted_file_name);
}
linnum = where.lineno + lineinc;
fputs(line, out);
fputc('\n', out);
}
nasm_free(quoted_file_name);
pp_cleanup_pass();
reset_warnings();
if (ofile)
fclose(ofile);
if (ofile && terminate_after_phase && !keep_all)
remove(outname);
ofile = NULL;
}
if (operating_mode & OP_NORMAL) {
ofile = nasm_open_write(outname, (ofmt->flags & OFMT_TEXT) ? NF_TEXT : NF_BINARY);
if (!ofile)
nasm_fatal("unable to open output file `%s'", outname);
ofmt->init();
dfmt->init();
assemble_file(inname, depend_list);
if (!terminate_after_phase) {
ofmt->cleanup();
cleanup_labels();
fflush(ofile);
if (ferror(ofile))
nasm_nonfatal("write error on output file `%s'", outname);
}
if (ofile) {
fclose(ofile);
if (terminate_after_phase && !keep_all)
remove(outname);
ofile = NULL;
}
}
pp_cleanup_session();
if (depend_list && !terminate_after_phase)
emit_dependencies(depend_list);
if (want_usage)
usage();
raa_free(offsets);
saa_free(forwrefs);
eval_cleanup();
stdscan_cleanup();
src_free();
strlist_free(&include_path);
return terminate_after_phase;
}
/*
* Get a parameter for a command line option.
* First arg must be in the form of e.g. -f...
*
* get_param() errors on a missing argument; get_opt_param() does not.
*/
static char *get_opt_param(char *p, char *q, bool *advance)
{
*advance = false;
if (p[2]) /* the parameter's in the option */
return nasm_skip_spaces(p + 2);
if (q && q[0]) {
*advance = true;
return q;
}
return NULL;
}
static char *get_param(char *p, char *q, bool *advance)
{
char *r = get_opt_param(p, q, advance);
if (!r)
nasm_nonfatalf(ERR_USAGE, "option `-%c' requires an argument", p[1]);
return r;
}
/*
* Copy a filename
*/
static void copy_filename(const char **dst, const char *src, const char *what)
{
if (*dst)
nasm_fatal("more than one %s file specified: %s\n", what, src);
*dst = nasm_strdup(src);
}
/*
* Convert a string to a POSIX make-safe form
*/
static char *quote_for_pmake(const char *str)
{
const char *p;
char *os, *q;
size_t n = 1; /* Terminating zero */
size_t nbs = 0;
if (!str)
return NULL;
for (p = str; *p; p++) {
switch (*p) {
case ' ':
case '\t':
/* Convert N backslashes + ws -> 2N+1 backslashes + ws */
n += nbs + 2;
nbs = 0;
break;
case '$':
case '#':
nbs = 0;
n += 2;
break;
case '\\':
nbs++;
n++;
break;
default:
nbs = 0;
n++;
break;
}
}
/* Convert N backslashes at the end of filename to 2N backslashes */
n += nbs;
os = q = nasm_malloc(n);
nbs = 0;
for (p = str; *p; p++) {
switch (*p) {
case ' ':
case '\t':
q = mempset(q, '\\', nbs);
*q++ = '\\';
*q++ = *p;
nbs = 0;
break;
case '$':
*q++ = *p;
*q++ = *p;
nbs = 0;
break;
case '#':
*q++ = '\\';
*q++ = *p;
nbs = 0;
break;
case '\\':
*q++ = *p;
nbs++;
break;
default:
*q++ = *p;
nbs = 0;
break;
}
}
q = mempset(q, '\\', nbs);
*q = '\0';
return os;
}
/*
* Convert a string to a Watcom make-safe form
*/
static char *quote_for_wmake(const char *str)
{
const char *p;
char *os, *q;
bool quote = false;
size_t n = 1; /* Terminating zero */
if (!str)
return NULL;
for (p = str; *p; p++) {
switch (*p) {
case ' ':
case '\t':
case '&':
quote = true;
n++;
break;
case '\"':
quote = true;
n += 2;
break;
case '$':
case '#':
n += 2;
break;
default:
n++;
break;
}
}
if (quote)
n += 2;
os = q = nasm_malloc(n);
if (quote)
*q++ = '\"';
for (p = str; *p; p++) {
switch (*p) {
case '$':
case '#':
*q++ = '$';
*q++ = *p;
break;
case '\"':
*q++ = *p;
*q++ = *p;
break;
default:
*q++ = *p;
break;
}
}
if (quote)
*q++ = '\"';
*q = '\0';
return os;
}
enum text_options {
OPT_BOGUS,
OPT_VERSION,
OPT_HELP,
OPT_ABORT_ON_PANIC,
OPT_MANGLE,
OPT_INCLUDE,
OPT_PRAGMA,
OPT_BEFORE,
OPT_LIMIT,
OPT_KEEP_ALL,
OPT_NO_LINE,
OPT_DEBUG,
OPT_REPRODUCIBLE
};
enum need_arg {
ARG_NO,
ARG_YES,
ARG_MAYBE
};
struct textargs {
const char *label;
enum text_options opt;
enum need_arg need_arg;
int pvt;
};
static const struct textargs textopts[] = {
{"v", OPT_VERSION, ARG_NO, 0},
{"version", OPT_VERSION, ARG_NO, 0},
{"help", OPT_HELP, ARG_MAYBE, 0},
{"abort-on-panic", OPT_ABORT_ON_PANIC, ARG_NO, 0},
{"prefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
{"postfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
{"gprefix", OPT_MANGLE, ARG_YES, LM_GPREFIX},
{"gpostfix", OPT_MANGLE, ARG_YES, LM_GSUFFIX},
{"lprefix", OPT_MANGLE, ARG_YES, LM_LPREFIX},
{"lpostfix", OPT_MANGLE, ARG_YES, LM_LSUFFIX},
{"include", OPT_INCLUDE, ARG_YES, 0},
{"pragma", OPT_PRAGMA, ARG_YES, 0},
{"before", OPT_BEFORE, ARG_YES, 0},
{"limit-", OPT_LIMIT, ARG_YES, 0},
{"keep-all", OPT_KEEP_ALL, ARG_NO, 0},
{"no-line", OPT_NO_LINE, ARG_NO, 0},
{"debug", OPT_DEBUG, ARG_MAYBE, 0},
{"reproducible", OPT_REPRODUCIBLE, ARG_NO, 0},
{NULL, OPT_BOGUS, ARG_NO, 0}
};
static void show_version(void)
{
printf("NASM version %s compiled on %s%s\n",
nasm_version, nasm_date, nasm_compile_options);
exit(0);
}
static bool stopoptions = false;
static bool process_arg(char *p, char *q, int pass)
{
char *param;
bool advance = false;
if (!p || !p[0])
return false;