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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
|
/* ----------------------------------------------------------------------- *
*
* Copyright 1996-2009 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 <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include <limits.h>
#include <time.h>
#include "nasm.h"
#include "nasmlib.h"
#include "saa.h"
#include "raa.h"
#include "float.h"
#include "stdscan.h"
#include "insns.h"
#include "preproc.h"
#include "parser.h"
#include "eval.h"
#include "assemble.h"
#include "labels.h"
#include "output/outform.h"
#include "listing.h"
struct forwrefinfo { /* info held on forward refs. */
int lineno;
int operand;
};
static int get_bits(char *value);
static uint32_t get_cpu(char *cpu_str);
static void parse_cmdline(int, char **);
static void assemble_file(char *, StrList **);
static void nasm_verror_gnu(int severity, const char *fmt, va_list args);
static void nasm_verror_vc(int severity, const char *fmt, va_list args);
static void nasm_verror_common(int severity, const char *fmt, va_list args);
static bool is_suppressed_warning(int severity);
static void usage(void);
static int using_debug_info, opt_verbose_info;
bool tasm_compatible_mode = false;
int pass0, passn;
int maxbits = 0;
int globalrel = 0;
static time_t official_compile_time;
static char inname[FILENAME_MAX];
static char outname[FILENAME_MAX];
static char listname[FILENAME_MAX];
static char errname[FILENAME_MAX];
static int globallineno; /* for forward-reference tracking */
/* static int pass = 0; */
struct ofmt *ofmt = &OF_DEFAULT;
const struct dfmt *dfmt;
static FILE *error_file; /* Where to write error messages */
FILE *ofile = NULL;
int optimizing = -1; /* number of optimization passes to take */
static int sb, cmd_sb = 16; /* by default */
static uint32_t cmd_cpu = IF_PLEVEL; /* highest level by default */
static uint32_t cpu = IF_PLEVEL; /* passed to insn_size & assemble.c */
int64_t global_offset_changed; /* referenced in labels.c */
int64_t prev_offset_changed;
int32_t stall_count;
static struct location location;
int in_abs_seg; /* Flag we are in ABSOLUTE seg */
int32_t abs_seg; /* ABSOLUTE segment basis */
int32_t abs_offset; /* ABSOLUTE offset */
static struct RAA *offsets;
static struct SAA *forwrefs; /* keep track of forward references */
static const struct forwrefinfo *forwref;
static Preproc *preproc;
enum op_type {
op_normal, /* Preprocess and assemble */
op_preprocess, /* Preprocess only */
op_depend, /* Generate dependencies */
};
static enum op_type 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;
/*
* Which of the suppressible warnings are suppressed. Entry zero
* isn't an actual warning, but it used for -w+error/-Werror.
*/
static bool warning_on[ERR_WARN_MAX+1]; /* Current state */
static bool warning_on_global[ERR_WARN_MAX+1]; /* Command-line state */
static const struct warning {
const char *name;
const char *help;
bool enabled;
} warnings[ERR_WARN_MAX+1] = {
{"error", "treat warnings as errors", false},
{"macro-params", "macro calls with wrong parameter count", true},
{"macro-selfref", "cyclic macro references", false},
{"macro-defaults", "macros with more default than optional parameters", true},
{"orphan-labels", "labels alone on lines without trailing `:'", true},
{"number-overflow", "numeric constant does not fit", true},
{"gnu-elf-extensions", "using 8- or 16-bit relocation in ELF32, a GNU extension", false},
{"float-overflow", "floating point overflow", true},
{"float-denorm", "floating point denormal", false},
{"float-underflow", "floating point underflow", false},
{"float-toolong", "too many digits in floating-point number", true},
{"user", "%warning directives", true},
};
/*
* This is a null preprocessor which just copies lines from input
* to output. It's used when someone explicitly requests that NASM
* not preprocess their source file.
*/
static void no_pp_reset(char *, int, efunc, evalfunc, ListGen *, StrList **);
static char *no_pp_getline(void);
static void no_pp_cleanup(int);
static Preproc no_pp = {
no_pp_reset,
no_pp_getline,
no_pp_cleanup
};
/*
* get/set current offset...
*/
#define GET_CURR_OFFS (in_abs_seg?abs_offset:\
raa_read(offsets,location.segment))
#define SET_CURR_OFFS(x) (in_abs_seg?(void)(abs_offset=(x)):\
(void)(offsets=raa_write(offsets,location.segment,(x))))
static bool want_usage;
static bool terminate_after_phase;
int user_nolist = 0; /* fbk 9/2/00 */
static void nasm_fputs(const char *line, FILE * outfile)
{
if (outfile) {
fputs(line, outfile);
putc('\n', outfile);
} else
puts(line);
}
/* Convert a struct tm to a POSIX-style time constant */
static int64_t posix_mktime(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;
}
static void define_macros_early(void)
{
char temp[128];
struct tm lt, *lt_p, gm, *gm_p;
int64_t posix_time;
lt_p = localtime(&official_compile_time);
if (lt_p) {
lt = *lt_p;
strftime(temp, sizeof temp, "__DATE__=\"%Y-%m-%d\"", <);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__DATE_NUM__=%Y%m%d", <);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__TIME__=\"%H:%M:%S\"", <);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__TIME_NUM__=%H%M%S", <);
pp_pre_define(temp);
}
gm_p = gmtime(&official_compile_time);
if (gm_p) {
gm = *gm_p;
strftime(temp, sizeof temp, "__UTC_DATE__=\"%Y-%m-%d\"", &gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__UTC_DATE_NUM__=%Y%m%d", &gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__UTC_TIME__=\"%H:%M:%S\"", &gm);
pp_pre_define(temp);
strftime(temp, sizeof temp, "__UTC_TIME_NUM__=%H%M%S", &gm);
pp_pre_define(temp);
}
if (gm_p)
posix_time = posix_mktime(&gm);
else if (lt_p)
posix_time = posix_mktime(<);
else
posix_time = 0;
if (posix_time) {
snprintf(temp, sizeof temp, "__POSIX_TIME__=%"PRId64, posix_time);
pp_pre_define(temp);
}
}
static void define_macros_late(void)
{
char temp[128];
snprintf(temp, sizeof(temp), "__OUTPUT_FORMAT__=%s\n",
ofmt->shortname);
pp_pre_define(temp);
}
static void emit_dependencies(StrList *list)
{
FILE *deps;
int linepos, len;
StrList *l, *nl;
if (depend_file && strcmp(depend_file, "-")) {
deps = fopen(depend_file, "w");
if (!deps) {
nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
"unable to write dependency file `%s'", depend_file);
return;
}
} else {
deps = stdout;
}
linepos = fprintf(deps, "%s:", depend_target);
for (l = list; l; l = l->next) {
len = strlen(l->str);
if (linepos + len > 62) {
fprintf(deps, " \\\n ");
linepos = 1;
}
fprintf(deps, " %s", l->str);
linepos += len+1;
}
fprintf(deps, "\n\n");
for (l = list; l; l = nl) {
if (depend_emit_phony)
fprintf(deps, "%s:\n\n", l->str);
nl = l->next;
nasm_free(l);
}
if (deps != stdout)
fclose(deps);
}
int main(int argc, char **argv)
{
StrList *depend_list = NULL, **depend_ptr;
time(&official_compile_time);
pass0 = 0;
want_usage = terminate_after_phase = false;
nasm_set_verror(nasm_verror_gnu);
error_file = stderr;
tolower_init();
nasm_init_malloc_error();
offsets = raa_init();
forwrefs = saa_init((int32_t)sizeof(struct forwrefinfo));
preproc = &nasmpp;
operating_mode = op_normal;
seg_init();
/* Define some macros dependent on the runtime, but not
on the command line. */
define_macros_early();
parse_cmdline(argc, argv);
if (terminate_after_phase) {
if (want_usage)
usage();
return 1;
}
/* If debugging info is disabled, suppress any debug calls */
if (!using_debug_info)
ofmt->current_dfmt = &null_debug_form;
if (ofmt->stdmac)
pp_extra_stdmac(ofmt->stdmac);
parser_global_info(&location);
eval_global_info(ofmt, lookup_label, &location);
/* define some macros dependent of command-line */
define_macros_late();
depend_ptr = (depend_file || (operating_mode == op_depend))
? &depend_list : NULL;
if (!depend_target)
depend_target = outname;
switch (operating_mode) {
case op_depend:
{
char *line;
if (depend_missing_ok)
pp_include_path(NULL); /* "assume generated" */
preproc->reset(inname, 0, nasm_error, evaluate, &nasmlist,
depend_ptr);
if (outname[0] == '\0')
ofmt->filename(inname, outname);
ofile = NULL;
while ((line = preproc->getline()))
nasm_free(line);
preproc->cleanup(0);
}
break;
case op_preprocess:
{
char *line;
char *file_name = NULL;
int32_t prior_linnum = 0;
int lineinc = 0;
if (*outname) {
ofile = fopen(outname, "w");
if (!ofile)
nasm_error(ERR_FATAL | ERR_NOFILE,
"unable to open output file `%s'",
outname);
} else
ofile = NULL;
location.known = false;
/* pass = 1; */
preproc->reset(inname, 3, nasm_error, evaluate, &nasmlist,
depend_ptr);
while ((line = preproc->getline())) {
/*
* We generate %line directives if needed for later programs
*/
int32_t linnum = prior_linnum += lineinc;
int altline = src_get(&linnum, &file_name);
if (altline) {
if (altline == 1 && lineinc == 1)
nasm_fputs("", ofile);
else {
lineinc = (altline != -1 || lineinc != 1);
fprintf(ofile ? ofile : stdout,
"%%line %"PRId32"+%d %s\n", linnum, lineinc,
file_name);
}
prior_linnum = linnum;
}
nasm_fputs(line, ofile);
nasm_free(line);
}
nasm_free(file_name);
preproc->cleanup(0);
if (ofile)
fclose(ofile);
if (ofile && terminate_after_phase)
remove(outname);
ofile = NULL;
}
break;
case op_normal:
{
/*
* We must call ofmt->filename _anyway_, even if the user
* has specified their own output file, because some
* formats (eg OBJ and COFF) use ofmt->filename to find out
* the name of the input file and then put that inside the
* file.
*/
ofmt->filename(inname, outname);
ofile = fopen(outname, (ofmt->flags & OFMT_TEXT) ? "w" : "wb");
if (!ofile) {
nasm_error(ERR_FATAL | ERR_NOFILE,
"unable to open output file `%s'", outname);
}
/*
* We must call init_labels() before ofmt->init() since
* some object formats will want to define labels in their
* init routines. (eg OS/2 defines the FLAT group)
*/
init_labels();
ofmt->init();
dfmt = ofmt->current_dfmt;
dfmt->init();
assemble_file(inname, depend_ptr);
if (!terminate_after_phase) {
ofmt->cleanup(using_debug_info);
cleanup_labels();
fflush(ofile);
if (ferror(ofile)) {
nasm_error(ERR_NONFATAL|ERR_NOFILE,
"write error on output file `%s'", outname);
}
}
if (ofile) {
fclose(ofile);
if (terminate_after_phase)
remove(outname);
ofile = NULL;
}
}
break;
}
if (depend_list && !terminate_after_phase)
emit_dependencies(depend_list);
if (want_usage)
usage();
raa_free(offsets);
saa_free(forwrefs);
eval_cleanup();
stdscan_cleanup();
return terminate_after_phase;
}
/*
* Get a parameter for a command line option.
* First arg must be in the form of e.g. -f...
*/
static char *get_param(char *p, char *q, bool *advance)
{
*advance = false;
if (p[2]) { /* the parameter's in the option */
p += 2;
while (nasm_isspace(*p))
p++;
return p;
}
if (q && q[0]) {
*advance = true;
return q;
}
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"option `-%c' requires an argument", p[1]);
return NULL;
}
/*
* Copy a filename
*/
static void copy_filename(char *dst, const char *src)
{
size_t len = strlen(src);
if (len >= (size_t)FILENAME_MAX) {
nasm_error(ERR_FATAL | ERR_NOFILE, "file name too long");
return;
}
strncpy(dst, src, FILENAME_MAX);
}
/*
* Convert a string to Make-safe form
*/
static char *quote_for_make(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 */
if (nbs)
n += nbs;
os = q = nasm_malloc(n);
nbs = 0;
for (p = str; *p; p++) {
switch (*p) {
case ' ':
case '\t':
while (nbs--)
*q++ = '\\';
*q++ = '\\';
*q++ = *p;
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;
}
}
while (nbs--)
*q++ = '\\';
*q = '\0';
return os;
}
struct textargs {
const char *label;
int value;
};
#define OPT_PREFIX 0
#define OPT_POSTFIX 1
struct textargs textopts[] = {
{"prefix", OPT_PREFIX},
{"postfix", OPT_POSTFIX},
{NULL, 0}
};
static bool stopoptions = false;
static bool process_arg(char *p, char *q)
{
char *param;
int i;
bool advance = false;
bool do_warn;
if (!p || !p[0])
return false;
if (p[0] == '-' && !stopoptions) {
if (strchr("oOfpPdDiIlFXuUZwW", p[1])) {
/* These parameters take values */
if (!(param = get_param(p, q, &advance)))
return advance;
}
switch (p[1]) {
case 's':
error_file = stdout;
break;
case 'o': /* output file */
copy_filename(outname, param);
break;
case 'f': /* output format */
ofmt = ofmt_find(param);
if (!ofmt) {
nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"unrecognised output format `%s' - "
"use -hf for a list", param);
}
break;
case 'O': /* Optimization level */
{
int opt;
if (!*param) {
/* Naked -O == -Ox */
optimizing = INT_MAX >> 1; /* Almost unlimited */
} else {
while (*param) {
switch (*param) {
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
opt = strtoul(param, ¶m, 10);
/* -O0 -> optimizing == -1, 0.98 behaviour */
/* -O1 -> optimizing == 0, 0.98.09 behaviour */
if (opt < 2)
optimizing = opt - 1;
else
optimizing = opt;
break;
case 'v':
case '+':
param++;
opt_verbose_info = true;
break;
case 'x':
param++;
optimizing = INT_MAX >> 1; /* Almost unlimited */
break;
default:
nasm_error(ERR_FATAL,
"unknown optimization option -O%c\n",
*param);
break;
}
}
}
break;
}
case 'p': /* pre-include */
case 'P':
pp_pre_include(param);
break;
case 'd': /* pre-define */
case 'D':
pp_pre_define(param);
break;
case 'u': /* un-define */
case 'U':
pp_pre_undefine(param);
break;
case 'i': /* include search path */
case 'I':
pp_include_path(param);
break;
case 'l': /* listing file */
copy_filename(listname, param);
break;
case 'Z': /* error messages file */
strcpy(errname, param);
break;
case 'F': /* specify debug format */
ofmt->current_dfmt = dfmt_find(ofmt, param);
if (!ofmt->current_dfmt) {
nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"unrecognized debug format `%s' for"
" output format `%s'",
param, ofmt->shortname);
}
using_debug_info = true;
break;
case 'X': /* specify error reporting format */
if (nasm_stricmp("vc", param) == 0)
nasm_set_verror(nasm_verror_vc);
else if (nasm_stricmp("gnu", param) == 0)
nasm_set_verror(nasm_verror_gnu);
else
nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"unrecognized error reporting format `%s'",
param);
break;
case 'g':
using_debug_info = true;
break;
case 'h':
printf
("usage: nasm [-@ response file] [-o outfile] [-f format] "
"[-l listfile]\n"
" [options...] [--] filename\n"
" or nasm -v for version info\n\n"
" -t assemble in SciTech TASM compatible mode\n"
" -g generate debug information in selected format.\n");
printf
(" -E (or -e) preprocess only (writes output to stdout by default)\n"
" -a don't preprocess (assemble only)\n"
" -M generate Makefile dependencies on stdout\n"
" -MG d:o, missing files assumed generated\n\n"
" -Z<file> redirect error messages to file\n"
" -s redirect error messages to stdout\n\n"
" -F format select a debugging format\n\n"
" -I<path> adds a pathname to the include file path\n");
printf
(" -O<digit> optimize branch offsets (-O0 disables, default)\n"
" -P<file> pre-includes a file\n"
" -D<macro>[=<value>] pre-defines a macro\n"
" -U<macro> undefines a macro\n"
" -X<format> specifies error reporting format (gnu or vc)\n"
" -w+foo enables warning foo (equiv. -Wfoo)\n"
" -w-foo disable warning foo (equiv. -Wno-foo)\n"
"Warnings:\n");
for (i = 0; i <= ERR_WARN_MAX; i++)
printf(" %-23s %s (default %s)\n",
warnings[i].name, warnings[i].help,
warnings[i].enabled ? "on" : "off");
printf
("\nresponse files should contain command line parameters"
", one per line.\n");
if (p[2] == 'f') {
printf("\nvalid output formats for -f are"
" (`*' denotes default):\n");
ofmt_list(ofmt, stdout);
} else {
printf("\nFor a list of valid output formats, use -hf.\n");
printf("For a list of debug formats, use -f <form> -y.\n");
}
exit(0); /* never need usage message here */
break;
case 'y':
printf("\nvalid debug formats for '%s' output format are"
" ('*' denotes default):\n", ofmt->shortname);
dfmt_list(ofmt, stdout);
exit(0);
break;
case 't':
tasm_compatible_mode = true;
break;
case 'v':
printf("NASM version %s compiled on %s%s\n",
nasm_version, nasm_date, nasm_compile_options);
exit(0); /* never need usage message here */
break;
case 'e': /* preprocess only */
case 'E':
operating_mode = op_preprocess;
break;
case 'a': /* assemble only - don't preprocess */
preproc = &no_pp;
break;
case 'W':
if (param[0] == 'n' && param[1] == 'o' && param[2] == '-') {
do_warn = false;
param += 3;
} else {
do_warn = true;
}
goto set_warning;
case 'w':
if (param[0] != '+' && param[0] != '-') {
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"invalid option to `-w'");
break;
}
do_warn = (param[0] == '+');
param++;
goto set_warning;
set_warning:
for (i = 0; i <= ERR_WARN_MAX; i++)
if (!nasm_stricmp(param, warnings[i].name))
break;
if (i <= ERR_WARN_MAX)
warning_on_global[i] = do_warn;
else if (!nasm_stricmp(param, "all"))
for (i = 1; i <= ERR_WARN_MAX; i++)
warning_on_global[i] = do_warn;
else if (!nasm_stricmp(param, "none"))
for (i = 1; i <= ERR_WARN_MAX; i++)
warning_on_global[i] = !do_warn;
else
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"invalid warning `%s'", param);
break;
case 'M':
switch (p[2]) {
case 0:
operating_mode = op_depend;
break;
case 'G':
operating_mode = op_depend;
depend_missing_ok = true;
break;
case 'P':
depend_emit_phony = true;
break;
case 'D':
depend_file = q;
advance = true;
break;
case 'T':
depend_target = q;
advance = true;
break;
case 'Q':
depend_target = quote_for_make(q);
advance = true;
break;
default:
nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
"unknown dependency option `-M%c'", p[2]);
break;
}
if (advance && (!q || !q[0])) {
nasm_error(ERR_NONFATAL|ERR_NOFILE|ERR_USAGE,
"option `-M%c' requires a parameter", p[2]);
break;
}
break;
case '-':
{
int s;
if (p[2] == 0) { /* -- => stop processing options */
stopoptions = 1;
break;
}
for (s = 0; textopts[s].label; s++) {
if (!nasm_stricmp(p + 2, textopts[s].label)) {
break;
}
}
switch (s) {
case OPT_PREFIX:
case OPT_POSTFIX:
{
if (!q) {
nasm_error(ERR_NONFATAL | ERR_NOFILE |
ERR_USAGE,
"option `--%s' requires an argument",
p + 2);
break;
} else {
advance = 1, param = q;
}
if (s == OPT_PREFIX) {
strncpy(lprefix, param, PREFIX_MAX - 1);
lprefix[PREFIX_MAX - 1] = 0;
break;
}
if (s == OPT_POSTFIX) {
strncpy(lpostfix, param, POSTFIX_MAX - 1);
lpostfix[POSTFIX_MAX - 1] = 0;
break;
}
break;
}
default:
{
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"unrecognised option `--%s'", p + 2);
break;
}
}
break;
}
default:
if (!ofmt->setinfo(GI_SWITCH, &p))
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"unrecognised option `-%c'", p[1]);
break;
}
} else {
if (*inname) {
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"more than one input file specified");
} else {
copy_filename(inname, p);
}
}
return advance;
}
#define ARG_BUF_DELTA 128
static void process_respfile(FILE * rfile)
{
char *buffer, *p, *q, *prevarg;
int bufsize, prevargsize;
bufsize = prevargsize = ARG_BUF_DELTA;
buffer = nasm_malloc(ARG_BUF_DELTA);
prevarg = nasm_malloc(ARG_BUF_DELTA);
prevarg[0] = '\0';
while (1) { /* Loop to handle all lines in file */
p = buffer;
while (1) { /* Loop to handle long lines */
q = fgets(p, bufsize - (p - buffer), rfile);
if (!q)
break;
p += strlen(p);
if (p > buffer && p[-1] == '\n')
break;
if (p - buffer > bufsize - 10) {
int offset;
offset = p - buffer;
bufsize += ARG_BUF_DELTA;
buffer = nasm_realloc(buffer, bufsize);
p = buffer + offset;
}
}
if (!q && p == buffer) {
if (prevarg[0])
process_arg(prevarg, NULL);
nasm_free(buffer);
nasm_free(prevarg);
return;
}
/*
* Play safe: remove CRs, LFs and any spurious ^Zs, if any of
* them are present at the end of the line.
*/
*(p = &buffer[strcspn(buffer, "\r\n\032")]) = '\0';
while (p > buffer && nasm_isspace(p[-1]))
*--p = '\0';
p = buffer;
while (nasm_isspace(*p))
p++;
if (process_arg(prevarg, p))
*p = '\0';
if ((int) strlen(p) > prevargsize - 10) {
prevargsize += ARG_BUF_DELTA;
prevarg = nasm_realloc(prevarg, prevargsize);
}
strncpy(prevarg, p, prevargsize);
}
}
/* Function to process args from a string of args, rather than the
* argv array. Used by the environment variable and response file
* processing.
*/
static void process_args(char *args)
{
char *p, *q, *arg, *prevarg;
char separator = ' ';
p = args;
if (*p && *p != '-')
separator = *p++;
arg = NULL;
while (*p) {
q = p;
while (*p && *p != separator)
p++;
while (*p == separator)
*p++ = '\0';
prevarg = arg;
arg = q;
if (process_arg(prevarg, arg))
arg = NULL;
}
if (arg)
process_arg(arg, NULL);
}
static void process_response_file(const char *file)
{
char str[2048];
FILE *f = fopen(file, "r");
if (!f) {
perror(file);
exit(-1);
}
while (fgets(str, sizeof str, f)) {
process_args(str);
}
fclose(f);
}
static void parse_cmdline(int argc, char **argv)
{
FILE *rfile;
char *envreal, *envcopy = NULL, *p, *arg;
int i;
*inname = *outname = *listname = *errname = '\0';
for (i = 0; i <= ERR_WARN_MAX; i++)
warning_on_global[i] = warnings[i].enabled;
/*
* First, process the NASMENV environment variable.
*/
envreal = getenv("NASMENV");
arg = NULL;
if (envreal) {
envcopy = nasm_strdup(envreal);
process_args(envcopy);
nasm_free(envcopy);
}
/*
* Now process the actual command line.
*/
while (--argc) {
bool advance;
argv++;
if (argv[0][0] == '@') {
/* We have a response file, so process this as a set of
* arguments like the environment variable. This allows us
* to have multiple arguments on a single line, which is
* different to the -@resp file processing below for regular
* NASM.
*/
process_response_file(argv[0]+1);
argc--;
argv++;
}
if (!stopoptions && argv[0][0] == '-' && argv[0][1] == '@') {
p = get_param(argv[0], argc > 1 ? argv[1] : NULL, &advance);
if (p) {
rfile = fopen(p, "r");
if (rfile) {
process_respfile(rfile);
fclose(rfile);
} else
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"unable to open response file `%s'", p);
}
} else
advance = process_arg(argv[0], argc > 1 ? argv[1] : NULL);
argv += advance, argc -= advance;
}
/* Look for basic command line typos. This definitely doesn't
catch all errors, but it might help cases of fumbled fingers. */
if (!*inname)
nasm_error(ERR_NONFATAL | ERR_NOFILE | ERR_USAGE,
"no input file specified");
else if (!strcmp(inname, errname) ||
!strcmp(inname, outname) ||
!strcmp(inname, listname) ||
(depend_file && !strcmp(inname, depend_file)))
nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"file `%s' is both input and output file",
inname);
if (*errname) {
error_file = fopen(errname, "w");
if (!error_file) {
error_file = stderr; /* Revert to default! */
nasm_error(ERR_FATAL | ERR_NOFILE | ERR_USAGE,
"cannot open file `%s' for error messages",
errname);
}
}
}
static enum directives getkw(char **directive, char **value);
static void assemble_file(char *fname, StrList **depend_ptr)
{
char *directive, *value, *p, *q, *special, *line, debugid[80];
insn output_ins;
int i, validid;
bool rn_error;
int32_t seg;
int64_t offs;
struct tokenval tokval;
expr *e;
int pass_max;
if (cmd_sb == 32 && cmd_cpu < IF_386)
nasm_error(ERR_FATAL, "command line: "
"32-bit segment size requires a higher cpu");
pass_max = prev_offset_changed = (INT_MAX >> 1) + 2; /* Almost unlimited */
for (passn = 1; pass0 <= 2; passn++) {
int pass1, pass2;
ldfunc def_label;
pass1 = pass0 == 2 ? 2 : 1; /* 1, 1, 1, ..., 1, 2 */
pass2 = passn > 1 ? 2 : 1; /* 1, 2, 2, ..., 2, 2 */
/* pass0 0, 0, 0, ..., 1, 2 */
def_label = passn > 1 ? redefine_label : define_label;
globalbits = sb = cmd_sb; /* set 'bits' to command line default */
cpu = cmd_cpu;
if (pass0 == 2) {
if (*listname)
nasmlist.init(listname, nasm_error);
}
in_abs_seg = false;
global_offset_changed = 0; /* set by redefine_label */
location.segment = ofmt->section(NULL, pass2, &sb);
globalbits = sb;
if (passn > 1) {
saa_rewind(forwrefs);
forwref = saa_rstruct(forwrefs);
raa_free(offsets);
offsets = raa_init();
}
preproc->reset(fname, pass1, nasm_error, evaluate, &nasmlist,
pass1 == 2 ? depend_ptr : NULL);
memcpy(warning_on, warning_on_global, (ERR_WARN_MAX+1) * sizeof(bool));
globallineno = 0;
if (passn == 1)
location.known = true;
location.offset = offs = GET_CURR_OFFS;
while ((line = preproc->getline())) {
enum directives d;
globallineno++;
/*
* Here we parse our directives; this is not handled by the
* 'real' parser. This really should be a separate function.
*/
directive = line;
d = getkw(&directive, &value);
if (d) {
int err = 0;
switch (d) {
case D_SEGMENT: /* [SEGMENT n] */
case D_SECTION:
seg = ofmt->section(value, pass2, &sb);
if (seg == NO_SEG) {
nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
"segment name `%s' not recognized",
value);
} else {
in_abs_seg = false;
location.segment = seg;
}
break;
case D_EXTERN: /* [EXTERN label:special] */
if (*value == '$')
value++; /* skip initial $ if present */
if (pass0 == 2) {
q = value;
while (*q && *q != ':')
q++;
if (*q == ':') {
*q++ = '\0';
ofmt->symdef(value, 0L, 0L, 3, q);
}
} else if (passn == 1) {
q = value;
validid = true;
if (!isidstart(*q))
validid = false;
while (*q && *q != ':') {
if (!isidchar(*q))
validid = false;
q++;
}
if (!validid) {
nasm_error(ERR_NONFATAL,
"identifier expected after EXTERN");
break;
}
if (*q == ':') {
*q++ = '\0';
special = q;
} else
special = NULL;
if (!is_extern(value)) { /* allow re-EXTERN to be ignored */
int temp = pass0;
pass0 = 1; /* fake pass 1 in labels.c */
declare_as_global(value, special);
define_label(value, seg_alloc(), 0L, NULL,
false, true);
pass0 = temp;
}
} /* else pass0 == 1 */
break;
case D_BITS: /* [BITS bits] */
globalbits = sb = get_bits(value);
break;
case D_GLOBAL: /* [GLOBAL symbol:special] */
if (*value == '$')
value++; /* skip initial $ if present */
if (pass0 == 2) { /* pass 2 */
q = value;
while (*q && *q != ':')
q++;
if (*q == ':') {
*q++ = '\0';
ofmt->symdef(value, 0L, 0L, 3, q);
}
} else if (pass2 == 1) { /* pass == 1 */
q = value;
validid = true;
if (!isidstart(*q))
validid = false;
while (*q && *q != ':') {
if (!isidchar(*q))
validid = false;
q++;
}
if (!validid) {
nasm_error(ERR_NONFATAL,
"identifier expected after GLOBAL");
break;
}
if (*q == ':') {
*q++ = '\0';
special = q;
} else
special = NULL;
declare_as_global(value, special);
} /* pass == 1 */
break;
case D_COMMON: /* [COMMON symbol size:special] */
{
int64_t size;
if (*value == '$')
value++; /* skip initial $ if present */
p = value;
validid = true;
if (!isidstart(*p))
validid = false;
while (*p && !nasm_isspace(*p)) {
if (!isidchar(*p))
validid = false;
p++;
}
if (!validid) {
nasm_error(ERR_NONFATAL,
"identifier expected after COMMON");
break;
}
if (*p) {
while (*p && nasm_isspace(*p))
*p++ = '\0';
q = p;
while (*q && *q != ':')
q++;
if (*q == ':') {
*q++ = '\0';
special = q;
} else {
special = NULL;
}
size = readnum(p, &rn_error);
if (rn_error) {
nasm_error(ERR_NONFATAL,
"invalid size specified"
" in COMMON declaration");
break;
}
} else {
nasm_error(ERR_NONFATAL,
"no size specified in"
" COMMON declaration");
break;
}
if (pass0 < 2) {
define_common(value, seg_alloc(), size, special);
} else if (pass0 == 2) {
if (special)
ofmt->symdef(value, 0L, 0L, 3, special);
}
break;
}
case D_ABSOLUTE: /* [ABSOLUTE address] */
stdscan_reset();
stdscan_bufptr = value;
tokval.t_type = TOKEN_INVALID;
e = evaluate(stdscan, NULL, &tokval, NULL, pass2,
nasm_error, NULL);
if (e) {
if (!is_reloc(e))
nasm_error(pass0 ==
1 ? ERR_NONFATAL : ERR_PANIC,
"cannot use non-relocatable expression as "
"ABSOLUTE address");
else {
abs_seg = reloc_seg(e);
abs_offset = reloc_value(e);
}
} else if (passn == 1)
abs_offset = 0x100; /* don't go near zero in case of / */
else
nasm_error(ERR_PANIC, "invalid ABSOLUTE address "
"in pass two");
in_abs_seg = true;
location.segment = NO_SEG;
break;
case D_DEBUG: /* [DEBUG] */
p = value;
q = debugid;
validid = true;
if (!isidstart(*p))
validid = false;
while (*p && !nasm_isspace(*p)) {
if (!isidchar(*p))
validid = false;
*q++ = *p++;
}
*q++ = 0;
if (!validid) {
nasm_error(passn == 1 ? ERR_NONFATAL : ERR_PANIC,
"identifier expected after DEBUG");
break;
}
while (*p && nasm_isspace(*p))
p++;
if (pass0 == 2)
dfmt->debug_directive(debugid, p);
break;
case D_WARNING: /* [WARNING {+|-|*}warn-name] */
while (*value && nasm_isspace(*value))
value++;
switch(*value) {
case '-': validid = 0; value++; break;
case '+': validid = 1; value++; break;
case '*': validid = 2; value++; break;
default: validid = 1; break;
}
for (i = 1; i <= ERR_WARN_MAX; i++)
if (!nasm_stricmp(value, warnings[i].name))
break;
if (i <= ERR_WARN_MAX) {
switch(validid) {
case 0:
warning_on[i] = false;
break;
case 1:
warning_on[i] = true;
break;
case 2:
warning_on[i] = warning_on_global[i];
break;
}
}
else
nasm_error(ERR_NONFATAL,
"invalid warning id in WARNING directive");
break;
case D_CPU: /* [CPU] */
cpu = get_cpu(value);
break;
case D_LIST: /* [LIST {+|-}] */
while (*value && nasm_isspace(*value))
value++;
if (*value == '+') {
user_nolist = 0;
} else {
if (*value == '-') {
user_nolist = 1;
} else {
err = 1;
}
}
break;
case D_DEFAULT: /* [DEFAULT] */
stdscan_reset();
stdscan_bufptr = value;
tokval.t_type = TOKEN_INVALID;
if (stdscan(NULL, &tokval) == TOKEN_SPECIAL) {
switch ((int)tokval.t_integer) {
case S_REL:
globalrel = 1;
break;
case S_ABS:
globalrel = 0;
break;
default:
err = 1;
break;
}
} else {
err = 1;
}
break;
case D_FLOAT:
if (float_option(value)) {
nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
"unknown 'float' directive: %s",
value);
}
break;
default:
if (!d || !ofmt->directive(d, value, pass2))
nasm_error(pass1 == 1 ? ERR_NONFATAL : ERR_PANIC,
"unrecognised directive [%s]",
directive);
break;
}
if (err) {
nasm_error(ERR_NONFATAL,
"invalid parameter to [%s] directive",
directive);
}
} else { /* it isn't a directive */
parse_line(pass1, line, &output_ins,
nasm_error, evaluate, def_label);
if (optimizing > 0) {
if (forwref != NULL && globallineno == forwref->lineno) {
output_ins.forw_ref = true;
do {
output_ins.oprs[forwref->operand].opflags |=
OPFLAG_FORWARD;
forwref = saa_rstruct(forwrefs);
} while (forwref != NULL
&& forwref->lineno == globallineno);
} else
output_ins.forw_ref = false;
if (output_ins.forw_ref) {
if (passn == 1) {
for (i = 0; i < output_ins.operands; i++) {
if (output_ins.oprs[i].
opflags & OPFLAG_FORWARD) {
struct forwrefinfo *fwinf =
(struct forwrefinfo *)
saa_wstruct(forwrefs);
fwinf->lineno = globallineno;
fwinf->operand = i;
}
}
}
}
}
/* forw_ref */
if (output_ins.opcode == I_EQU) {
if (pass1 == 1) {
/*
* Special `..' EQUs get processed in pass two,
* except `..@' macro-processor EQUs which are done
* in the normal place.
*/
if (!output_ins.label)
nasm_error(ERR_NONFATAL,
"EQU not preceded by label");
else if (output_ins.label[0] != '.' ||
output_ins.label[1] != '.' ||
output_ins.label[2] == '@') {
if (output_ins.operands == 1 &&
(output_ins.oprs[0].type & IMMEDIATE) &&
output_ins.oprs[0].wrt == NO_SEG) {
bool isext = !!(output_ins.oprs[0].opflags
& OPFLAG_EXTERN);
def_label(output_ins.label,
output_ins.oprs[0].segment,
output_ins.oprs[0].offset, NULL,
false, isext);
} else if (output_ins.operands == 2
&& (output_ins.oprs[0].type & IMMEDIATE)
&& (output_ins.oprs[0].type & COLON)
&& output_ins.oprs[0].segment == NO_SEG
&& output_ins.oprs[0].wrt == NO_SEG
&& (output_ins.oprs[1].type & IMMEDIATE)
&& output_ins.oprs[1].segment == NO_SEG
&& output_ins.oprs[1].wrt == NO_SEG) {
def_label(output_ins.label,
output_ins.oprs[0].offset | SEG_ABS,
output_ins.oprs[1].offset,
NULL, false, false);
} else
nasm_error(ERR_NONFATAL,
"bad syntax for EQU");
}
} else {
/*
* Special `..' EQUs get processed here, except
* `..@' macro processor EQUs which are done above.
*/
if (output_ins.label[0] == '.' &&
output_ins.label[1] == '.' &&
output_ins.label[2] != '@') {
if (output_ins.operands == 1 &&
(output_ins.oprs[0].type & IMMEDIATE)) {
define_label(output_ins.label,
output_ins.oprs[0].segment,
output_ins.oprs[0].offset,
NULL, false, false);
} else if (output_ins.operands == 2
&& (output_ins.oprs[0].
type & IMMEDIATE)
&& (output_ins.oprs[0].type & COLON)
&& output_ins.oprs[0].segment ==
NO_SEG
&& (output_ins.oprs[1].
type & IMMEDIATE)
&& output_ins.oprs[1].segment ==
NO_SEG) {
define_label(output_ins.label,
output_ins.oprs[0].
offset | SEG_ABS,
output_ins.oprs[1].offset,
NULL, false, false);
} else
nasm_error(ERR_NONFATAL,
"bad syntax for EQU");
}
}
} else { /* instruction isn't an EQU */
if (pass1 == 1) {
int64_t l = insn_size(location.segment, offs, sb, cpu,
&output_ins, nasm_error);
/* if (using_debug_info) && output_ins.opcode != -1) */
if (using_debug_info)
{ /* fbk 03/25/01 */
/* this is done here so we can do debug type info */
int32_t typeinfo =
TYS_ELEMENTS(output_ins.operands);
switch (output_ins.opcode) {
case I_RESB:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_BYTE;
break;
case I_RESW:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_WORD;
break;
case I_RESD:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_DWORD;
break;
case I_RESQ:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_QWORD;
break;
case I_REST:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_TBYTE;
break;
case I_RESO:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_OWORD;
break;
case I_RESY:
typeinfo =
TYS_ELEMENTS(output_ins.oprs[0].
offset) | TY_YWORD;
break;
case I_DB:
typeinfo |= TY_BYTE;
break;
case I_DW:
typeinfo |= TY_WORD;
break;
case I_DD:
if (output_ins.eops_float)
typeinfo |= TY_FLOAT;
else
typeinfo |= TY_DWORD;
break;
case I_DQ:
typeinfo |= TY_QWORD;
break;
case I_DT:
typeinfo |= TY_TBYTE;
break;
case I_DO:
typeinfo |= TY_OWORD;
break;
case I_DY:
typeinfo |= TY_YWORD;
break;
default:
typeinfo = TY_LABEL;
}
dfmt->debug_typevalue(typeinfo);
}
if (l != -1) {
offs += l;
SET_CURR_OFFS(offs);
}
/*
* else l == -1 => invalid instruction, which will be
* flagged as an error on pass 2
*/
} else {
offs += assemble(location.segment, offs, sb, cpu,
&output_ins, ofmt, nasm_error,
&nasmlist);
SET_CURR_OFFS(offs);
}
} /* not an EQU */
cleanup_insn(&output_ins);
}
nasm_free(line);
location.offset = offs = GET_CURR_OFFS;
} /* end while (line = preproc->getline... */
if (pass0 == 2 && global_offset_changed && !terminate_after_phase)
nasm_error(ERR_NONFATAL,
"phase error detected at end of assembly.");
if (pass1 == 1)
preproc->cleanup(1);
if ((passn > 1 && !global_offset_changed) || pass0 == 2) {
pass0++;
} else if (global_offset_changed &&
global_offset_changed < prev_offset_changed) {
prev_offset_changed = global_offset_changed;
stall_count = 0;
} else {
stall_count++;
}
if (terminate_after_phase)
break;
if ((stall_count > 997) || (passn >= pass_max)) {
/* We get here if the labels don't converge
* Example: FOO equ FOO + 1
*/
nasm_error(ERR_NONFATAL,
"Can't find valid values for all labels "
"after %d passes, giving up.", passn);
nasm_error(ERR_NONFATAL,
"Possible causes: recursive EQUs, macro abuse.");
break;
}
}
preproc->cleanup(0);
nasmlist.cleanup();
if (!terminate_after_phase && opt_verbose_info) {
/* -On and -Ov switches */
fprintf(stdout, "info: assembly required 1+%d+1 passes\n", passn-3);
}
}
static enum directives getkw(char **directive, char **value)
{
char *p, *q, *buf;
buf = *directive;
/* allow leading spaces or tabs */
while (*buf == ' ' || *buf == '\t')
buf++;
if (*buf != '[')
return 0;
p = buf;
while (*p && *p != ']')
p++;
if (!*p)
return 0;
q = p++;
while (*p && *p != ';') {
if (!nasm_isspace(*p))
return 0;
p++;
}
q[1] = '\0';
*directive = p = buf + 1;
while (*buf && *buf != ' ' && *buf != ']' && *buf != '\t')
buf++;
if (*buf == ']') {
*buf = '\0';
*value = buf;
} else {
*buf++ = '\0';
while (nasm_isspace(*buf))
buf++; /* beppu - skip leading whitespace */
*value = buf;
while (*buf != ']')
buf++;
*buf++ = '\0';
}
return find_directive(*directive);
}
/**
* gnu style error reporting
* This function prints an error message to error_file in the
* style used by GNU. An example would be:
* file.asm:50: error: blah blah blah
* where file.asm is the name of the file, 50 is the line number on
* which the error occurs (or is detected) and "error:" is one of
* the possible optional diagnostics -- it can be "error" or "warning"
* or something else. Finally the line terminates with the actual
* error message.
*
* @param severity the severity of the warning or error
* @param fmt the printf style format string
*/
static void nasm_verror_gnu(int severity, const char *fmt, va_list ap)
{
char *currentfile = NULL;
int32_t lineno = 0;
if (is_suppressed_warning(severity))
return;
if (!(severity & ERR_NOFILE))
src_get(&lineno, ¤tfile);
if (currentfile) {
fprintf(error_file, "%s:%"PRId32": ", currentfile, lineno);
nasm_free(currentfile);
} else {
fputs("nasm: ", error_file);
}
nasm_verror_common(severity, fmt, ap);
}
/**
* MS style error reporting
* This function prints an error message to error_file in the
* style used by Visual C and some other Microsoft tools. An example
* would be:
* file.asm(50) : error: blah blah blah
* where file.asm is the name of the file, 50 is the line number on
* which the error occurs (or is detected) and "error:" is one of
* the possible optional diagnostics -- it can be "error" or "warning"
* or something else. Finally the line terminates with the actual
* error message.
*
* @param severity the severity of the warning or error
* @param fmt the printf style format string
*/
static void nasm_verror_vc(int severity, const char *fmt, va_list ap)
{
char *currentfile = NULL;
int32_t lineno = 0;
if (is_suppressed_warning(severity))
return;
if (!(severity & ERR_NOFILE))
src_get(&lineno, ¤tfile);
if (currentfile) {
fprintf(error_file, "%s(%"PRId32") : ", currentfile, lineno);
nasm_free(currentfile);
} else {
fputs("nasm: ", error_file);
}
nasm_verror_common(severity, fmt, ap);
}
/**
* check for supressed warning
* checks for suppressed warning or pass one only warning and we're
* not in pass 1
*
* @param severity the severity of the warning or error
* @return true if we should abort error/warning printing
*/
static bool is_suppressed_warning(int severity)
{
/*
* See if it's a suppressed warning.
*/
return (severity & ERR_MASK) == ERR_WARNING &&
(((severity & ERR_WARN_MASK) != 0 &&
!warning_on[(severity & ERR_WARN_MASK) >> ERR_WARN_SHR]) ||
/* See if it's a pass-one only warning and we're not in pass one. */
((severity & ERR_PASS1) && pass0 != 1) ||
((severity & ERR_PASS2) && pass0 != 2));
}
/**
* common error reporting
* This is the common back end of the error reporting schemes currently
* implemented. It prints the nature of the warning and then the
* specific error message to error_file and may or may not return. It
* doesn't return if the error severity is a "panic" or "debug" type.
*
* @param severity the severity of the warning or error
* @param fmt the printf style format string
*/
static void nasm_verror_common(int severity, const char *fmt, va_list args)
{
char msg[1024];
const char *pfx;
switch (severity & (ERR_MASK|ERR_NO_SEVERITY)) {
case ERR_WARNING:
pfx = "warning: ";
break;
case ERR_NONFATAL:
pfx = "error: ";
break;
case ERR_FATAL:
pfx = "fatal: ";
break;
case ERR_PANIC:
pfx = "panic: ";
break;
case ERR_DEBUG:
pfx = "debug: ";
break;
default:
pfx = "";
break;
}
vsnprintf(msg, sizeof msg, fmt, args);
fprintf(error_file, "%s%s\n", pfx, msg);
if (*listname)
nasmlist.error(severity, pfx, msg);
if (severity & ERR_USAGE)
want_usage = true;
switch (severity & ERR_MASK) {
case ERR_DEBUG:
/* no further action, by definition */
break;
case ERR_WARNING:
if (warning_on[0]) /* Treat warnings as errors */
terminate_after_phase = true;
break;
case ERR_NONFATAL:
terminate_after_phase = true;
break;
case ERR_FATAL:
if (ofile) {
fclose(ofile);
remove(outname);
ofile = NULL;
}
if (want_usage)
usage();
exit(1); /* instantly die */
break; /* placate silly compilers */
case ERR_PANIC:
fflush(NULL);
/* abort(); *//* halt, catch fire, and dump core */
exit(3);
break;
}
}
static void usage(void)
{
fputs("type `nasm -h' for help\n", error_file);
}
#define BUF_DELTA 512
static FILE *no_pp_fp;
static efunc no_pp_err;
static ListGen *no_pp_list;
static int32_t no_pp_lineinc;
static void no_pp_reset(char *file, int pass, efunc error, evalfunc eval,
ListGen * listgen, StrList **deplist)
{
src_set_fname(nasm_strdup(file));
src_set_linnum(0);
no_pp_lineinc = 1;
no_pp_err = error;
no_pp_fp = fopen(file, "r");
if (!no_pp_fp)
no_pp_err(ERR_FATAL | ERR_NOFILE,
"unable to open input file `%s'", file);
no_pp_list = listgen;
(void)pass; /* placate compilers */
(void)eval; /* placate compilers */
if (deplist) {
StrList *sl = nasm_malloc(strlen(file)+1+sizeof sl->next);
sl->next = NULL;
strcpy(sl->str, file);
*deplist = sl;
}
}
static char *no_pp_getline(void)
{
char *buffer, *p, *q;
int bufsize;
bufsize = BUF_DELTA;
buffer = nasm_malloc(BUF_DELTA);
src_set_linnum(src_get_linnum() + no_pp_lineinc);
while (1) { /* Loop to handle %line */
p = buffer;
while (1) { /* Loop to handle long lines */
q = fgets(p, bufsize - (p - buffer), no_pp_fp);
if (!q)
break;
p += strlen(p);
if (p > buffer && p[-1] == '\n')
break;
if (p - buffer > bufsize - 10) {
int offset;
offset = p - buffer;
bufsize += BUF_DELTA;
buffer = nasm_realloc(buffer, bufsize);
p = buffer + offset;
}
}
if (!q && p == buffer) {
nasm_free(buffer);
return NULL;
}
/*
* Play safe: remove CRs, LFs and any spurious ^Zs, if any of
* them are present at the end of the line.
*/
buffer[strcspn(buffer, "\r\n\032")] = '\0';
if (!nasm_strnicmp(buffer, "%line", 5)) {
int32_t ln;
int li;
char *nm = nasm_malloc(strlen(buffer));
if (sscanf(buffer + 5, "%"PRId32"+%d %s", &ln, &li, nm) == 3) {
nasm_free(src_set_fname(nm));
src_set_linnum(ln);
no_pp_lineinc = li;
continue;
}
nasm_free(nm);
}
break;
}
no_pp_list->line(LIST_READ, buffer);
return buffer;
}
static void no_pp_cleanup(int pass)
{
(void)pass; /* placate GCC */
fclose(no_pp_fp);
}
static uint32_t get_cpu(char *value)
{
if (!strcmp(value, "8086"))
return IF_8086;
if (!strcmp(value, "186"))
return IF_186;
if (!strcmp(value, "286"))
return IF_286;
if (!strcmp(value, "386"))
return IF_386;
if (!strcmp(value, "486"))
return IF_486;
if (!strcmp(value, "586") || !nasm_stricmp(value, "pentium"))
return IF_PENT;
if (!strcmp(value, "686") ||
!nasm_stricmp(value, "ppro") ||
!nasm_stricmp(value, "pentiumpro") || !nasm_stricmp(value, "p2"))
return IF_P6;
if (!nasm_stricmp(value, "p3") || !nasm_stricmp(value, "katmai"))
return IF_KATMAI;
if (!nasm_stricmp(value, "p4") || /* is this right? -- jrc */
!nasm_stricmp(value, "willamette"))
return IF_WILLAMETTE;
if (!nasm_stricmp(value, "prescott"))
return IF_PRESCOTT;
if (!nasm_stricmp(value, "x64") ||
!nasm_stricmp(value, "x86-64"))
return IF_X86_64;
if (!nasm_stricmp(value, "ia64") ||
!nasm_stricmp(value, "ia-64") ||
!nasm_stricmp(value, "itanium") ||
!nasm_stricmp(value, "itanic") || !nasm_stricmp(value, "merced"))
return IF_IA64;
nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
"unknown 'cpu' type");
return IF_PLEVEL; /* the maximum level */
}
static int get_bits(char *value)
{
int i;
if ((i = atoi(value)) == 16)
return i; /* set for a 16-bit segment */
else if (i == 32) {
if (cpu < IF_386) {
nasm_error(ERR_NONFATAL,
"cannot specify 32-bit segment on processor below a 386");
i = 16;
}
} else if (i == 64) {
if (cpu < IF_X86_64) {
nasm_error(ERR_NONFATAL,
"cannot specify 64-bit segment on processor below an x86-64");
i = 16;
}
if (i != maxbits) {
nasm_error(ERR_NONFATAL,
"%s output format does not support 64-bit code",
ofmt->shortname);
i = 16;
}
} else {
nasm_error(pass0 < 2 ? ERR_NONFATAL : ERR_FATAL,
"`%s' is not a valid segment size; must be 16, 32 or 64",
value);
i = 16;
}
return i;
}
/* end of nasm.c */
|