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
/****************************************************************************
*
* Open Watcom Project
*
* Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
*
* ========================================================================
*
* This file contains Original Code and/or Modifications of Original
* Code as defined in and that are subject to the Sybase Open Watcom
* Public License version 1.0 (the 'License'). You may not use this file
* except in compliance with the License. BY USING THIS FILE YOU AGREE TO
* ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
* provided with the Original Code and Modifications, and is also
* available at www.sybase.com/developer/opensource.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
* ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
* NON-INFRINGEMENT. Please see the License for the specific language
* governing rights and limitations under the License.
*
* ========================================================================
*
* Description: data definition. handles
* - directives DB,DW,DD,...
* - predefined types (BYTE, WORD, DWORD, ...)
* - arbitrary types
*
****************************************************************************/
#include <ctype.h>
#include "globals.h"
#include "memalloc.h"
#include "parser.h"
#include "expreval.h"
#include "input.h"
#include "fixup.h"
#include "listing.h"
#include "lqueue.h"
#include "proc.h"
#include "segment.h"
#include "types.h"
#include "fastpass.h"
#include "tokenize.h"
#include "macro.h"
#include "omf.h"
#include "atofloat.h"
#include "myassert.h"
#if defined(WINDOWSDDK)
#if defined(_WIN32)
typedef _W64 int INT_PTR, *PINT_PTR;
typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
typedef _W64 long LONG_PTR, *PLONG_PTR;
typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;
#define __int3264 __int32
#else
typedef __int64 INT_PTR, *PINT_PTR;
typedef unsigned __int64 UINT_PTR, *PUINT_PTR;
typedef __int64 LONG_PTR, *PLONG_PTR;
typedef unsigned __int64 ULONG_PTR, *PULONG_PTR;
#define __int3264 __int64
#endif
#else
#include <inttypes.h>
#if defined(_WIN32)
typedef _W64 int INT_PTR, *PINT_PTR;
typedef _W64 unsigned int UINT_PTR, *PUINT_PTR;
typedef _W64 long LONG_PTR, *PLONG_PTR;
typedef _W64 unsigned long ULONG_PTR, *PULONG_PTR;
#define __int3264 __int32
#else
typedef int64_t INT_PTR, *PINT_PTR;
typedef uint64_t UINT_PTR, *PUINT_PTR;
typedef int64_t LONG_PTR, *PLONG_PTR;
typedef uint64_t ULONG_PTR, *PULONG_PTR;
#define __int3264 int64_t
typedef int64_t __int64;
#endif
#endif
extern ret_code segm_override( const struct expr *, struct code_info * );
extern struct asym *SegOverride;
extern const char szNull[];
extern UINT_PTR UTF8toWideChar(const unsigned char *pSource, UINT_PTR nSourceLen, UINT_PTR *nSourceDone, unsigned short *szTarget, UINT_PTR nTargetMax);
static ret_code data_item( int *, struct asm_tok[], struct asym *, uint_32, const struct asym *, uint_32, bool inside_struct, bool, bool, int );
#define OutputDataBytes( x, y ) OutputBytes( x, y, NULL )
#define OutputInterleavedDataBytes( x, y ) OutputInterleavedBytes( x, y, NULL );
/* This function shifts left 128 for the RECORD */
void ShiftLeft (uint_64 *dstHi, uint_64 *dstLo,uint_64 num, int pos)
{
uint_64 orHi = 0;
uint_64 orLo = num;
if (pos >= 0x40){
orHi = orLo;
orLo = 0;
pos = pos - 0x40;
orHi = orHi << pos;
}
else {
orLo = orLo << pos;
num = num >> ~pos;
orHi |= num;
}
*dstHi |= orHi;
*dstLo |= orLo;
}
/* initialize an array inside a structure
* if there are no brackets, the next comma, '>' or '}' will terminate
*
* valid initialization are:
* - an expression, might contain DUP or a string enclosed in quotes.
* - a literal enclosed in <> or {} which then is supposed to contain
* single items.
*/
static ret_code InitializeArray( const struct sfield *f, int *pi, struct asm_tok tokenarray[] )
/*********************************************************************************************/
{
uint_32 oldofs;
uint_32 no_of_bytes;
int i = *pi;
int j;
int lvl;
int old_tokencount;
char bArray;
ret_code rc;
oldofs = GetCurrOffset();
no_of_bytes = SizeFromMemtype( f->sym.mem_type, USE_EMPTY, f->sym.type );
DebugMsg1(("InitializeArray(%s) enter, items=%" I32_SPEC "u size=%" I32_SPEC "u mem_type=%xh type=%s currofs=%" I32_SPEC "X [%s]\n",
f->sym.name, f->sym.total_length, no_of_bytes, f->sym.mem_type,
f->sym.type ? f->sym.type->name : "NULL", oldofs, tokenarray[i].tokpos ));
/* If current item is a literal enclosed in <> or {}, just use this
* item. Else, use all items until a comma or EOL is found.
*/
if ( tokenarray[i].token != T_STRING ||
( tokenarray[i].string_delim != '<' &&
tokenarray[i].string_delim != '{' )) {
/* scan for comma or final. Ignore commas inside DUP argument */
for( j = i, lvl = 0, bArray = FALSE; tokenarray[j].token != T_FINAL; j++ ) {
if ( tokenarray[j].token == T_OP_BRACKET )
lvl++;
else if ( tokenarray[j].token == T_CL_BRACKET )
lvl--;
else if ( lvl == 0 && tokenarray[j].token == T_COMMA )
break;
else if ( tokenarray[j].token == T_RES_ID && tokenarray[j].tokval == T_DUP )
bArray = TRUE;
else if ( no_of_bytes == 1 && tokenarray[j].token == T_STRING &&
( tokenarray[j].string_delim == '"' || tokenarray[j].string_delim == '\'' ))
bArray = TRUE;
}
*pi = j;
if ( bArray == FALSE ) {
DebugMsg1(("InitializeArray(%s): error, array init requires DUP, string or literal\n", f->sym.name ));
return( EmitErr( INITIALIZER_MUST_BE_A_STRING_OR_SINGLE_ITEM, tokenarray[i].tokpos ) );
}
lvl = tokenarray[j].tokpos - tokenarray[i].tokpos;
/* v2.07: accept an "empty" quoted string as array initializer for byte arrays */
if ( lvl == 2 &&
f->sym.total_size == f->sym.total_length &&
( tokenarray[i].string_delim == '"' || tokenarray[i].string_delim == '\'' ) )
rc = NOT_ERROR;
else {
lvl = i; /* i must remain the start index */
rc = data_item( &lvl, tokenarray, NULL, no_of_bytes, f->sym.type, 1, FALSE, f->sym.mem_type & MT_FLOAT, FALSE, j );
}
} else {
/* initializer is a literal */
(*pi)++;
old_tokencount = Token_Count;
j = Token_Count + 1;
/* if the string is empty, use the default initializer */
if ( tokenarray[i].stringlen == 0 ) {
Token_Count = Tokenize( (char *)f->ivalue, j, tokenarray, TOK_RESCAN );
} else {
Token_Count = Tokenize( tokenarray[i].string_ptr, j, tokenarray, TOK_RESCAN );
}
rc = data_item( &j, tokenarray, NULL, no_of_bytes, f->sym.type, 1, FALSE, f->sym.mem_type & MT_FLOAT, FALSE, Token_Count );
Token_Count = old_tokencount;
}
/* get size of array items */
no_of_bytes = GetCurrOffset() - oldofs ;
DebugMsg1(("InitializeArray(%s): new offset=%X\n", f->sym.name, no_of_bytes + oldofs ));
if ( no_of_bytes > f->sym.total_size ) {
DebugMsg1(("InitializeArray(%s): error, cursize=%u total_size=%u\n", f->sym.name, no_of_bytes, f->sym.total_size ));
EmitErr( TOO_MANY_INITIAL_VALUES_FOR_ARRAY, tokenarray[i].tokpos );
rc = ERROR;
} else if ( no_of_bytes < f->sym.total_size ) {
char filler = NULLC;
DebugMsg1(("InitializeArray: remaining bytes=%" I32_SPEC "u\n", f->sym.total_size - no_of_bytes ));
if ( CurrSeg && CurrSeg->e.seginfo->segtype == SEGTYPE_BSS )
SetCurrOffset( CurrSeg, f->sym.total_size - no_of_bytes, TRUE, TRUE );
else {
/* v2.07: if element size is 1 and a string is used as initial value,
* pad array with spaces!
*/
if ( f->sym.total_size == f->sym.total_length &&
( f->ivalue[0] == '"' || f->ivalue[0] == '\'' ) )
filler = ' ';
FillDataBytes( filler, f->sym.total_size - no_of_bytes );
}
}
DebugMsg1(("InitializeArray(%s) exit, curr ofs=%X\n", f->sym.name, GetCurrOffset() ));
return( rc );
}
/* initialize a STRUCT/UNION/RECORD data item
* index: index of initializer literal
* symtype: type of data item
* embedded: is != NULL if proc is called recursively
* up to v2.08, this proc did emit ASM lines with simple types
* to actually "fill" the structure.
* since v2.09, it calls data_item() directly.
* Since this function may be reentered, it's necessary to save/restore
* global variable Token_Count.
*/
static ret_code InitStructuredVar( int index, struct asm_tok tokenarray[], const struct dsym *symtype, struct asym *embedded )
/****************************************************************************************************************************/
{
//char *ptr;
struct sfield *f;
int_32 nextofs;
int i;
int old_tokencount = Token_Count;
char *old_stringbufferend = StringBufferEnd;
int lvl;
uint_64 dst128Hi;
uint_64 dst128Lo;
uint_64 dwRecIHi;
uint_64 dwRecILo;
uint_64 dwRecInit;
//#else
// uint_32 dwRecInit;
//#endif
bool is_record_set;
struct expr opndx;
//char line[MAX_LINE_LEN];
DebugMsg1(("InitStructuredVar(%s) enter, total_size=%" I32_SPEC "u, init=>%s<, embedded=%s, alignm=%u\n",
symtype->sym.name, symtype->sym.total_size, tokenarray[index].string_ptr, embedded ? embedded->name : "NULL", symtype->e.structinfo->alignment ));
/**/myassert( symtype->sym.state == SYM_TYPE && symtype->sym.typekind != TYPE_TYPEDEF );
if ( tokenarray[index].token == T_STRING ) {
/* v2.08: no special handling of {}-literals anymore */
if ( tokenarray[index].string_delim != '<' &&
tokenarray[index].string_delim != '{' ) {
return( EmitError( MISSING_ANGLE_BRACKET_OR_BRACE_IN_LITERAL ) );
}
i = Token_Count + 1;
//strcpy( line, tokenarray[index].string_ptr );
Token_Count = Tokenize( tokenarray[index].string_ptr, i, tokenarray, TOK_RESCAN );
/* once Token_Count has been modified, don't exit without
* restoring this value!
*/
index++;
} else if ( embedded &&
( tokenarray[index].token == T_COMMA ||
tokenarray[index].token == T_FINAL)) {
i = Token_Count;
} else {
return( EmitErr( INITIALIZER_MUST_BE_A_STRING_OR_SINGLE_ITEM, embedded ? embedded->name : "" ) );
}
if ( symtype->sym.typekind == TYPE_RECORD ) {
dwRecInit = 0;
dst128Hi = 0; /* clear Hi 64 bit for the 128 bit RECORD */
dst128Lo = 0; /* clear Lo 64 bit for the 128 bit RECORD */
is_record_set = FALSE;
}
//__debugbreak();
/* scan the STRUCT/UNION/RECORD's members */
for( f = symtype->e.structinfo->head; f != NULL; f = f->next ) {
DebugMsg1(("InitStructuredVar(%s) field=%s ofs=%" I32_SPEC "u total_size=%" I32_SPEC "u total_len=%" I32_SPEC "u value=>%s< >%s<\n",
symtype->sym.name,
f->sym.name,
f->sym.offset,
f->sym.total_size,
f->sym.total_length,
//f->initializer ? f->initializer : "NULL",
f->ivalue, tokenarray[i].tokpos ));
/* is it a RECORD field? */
if ( f->sym.mem_type == MT_BITS ) {
if ( tokenarray[i].token == T_COMMA || tokenarray[i].token == T_FINAL ) {
if ( f->ivalue[0] ) {
int j = Token_Count + 1;
int max_item = Tokenize( f->ivalue, j, tokenarray, TOK_RESCAN );
EvalOperand( &j, tokenarray, max_item, &opndx, 0 );
is_record_set = TRUE;
} else {
opndx.value = 0;
opndx.kind = EXPR_CONST;
opndx.quoted_string = NULL;
}
} else {
EvalOperand( &i, tokenarray, Token_Count, &opndx, 0 );
is_record_set = TRUE;
}
if ( opndx.kind != EXPR_CONST || opndx.quoted_string != NULL )
EmitError( CONSTANT_EXPECTED );
/* fixme: max bits in 64-bit is 64 - see MAXRECBITS! */
if ( f->sym.total_size < 32 ) {
uint_32 dwMax = (1 << f->sym.total_size);
if ( opndx.value >= dwMax )
EmitErr( INITIALIZER_MAGNITUDE_TOO_LARGE, f->sym.name );
}
if (symtype->sym.mem_type == MT_OWORD){
dwRecIHi = 0; /* clear Hi 64 bit for the 128 bit RECORD */
dwRecILo = 0; /* clear Lo 64 bit for the 128 bit RECORD */
ShiftLeft(&dwRecIHi,&dwRecILo,opndx.llvalue,f->sym.offset);
dst128Hi |= dwRecIHi; /* OR Hi 64 bit for the 128 bit RECORD */
dst128Lo |= dwRecILo; /* clear Lo 64 bit for the 128 bit RECORD */
}
else
dwRecInit |= opndx.llvalue << f->sym.offset;
//} else if ( f->init_dir == NULL ) { /* embedded struct? */
} else if ( f->ivalue[0] == NULLC ) { /* embedded struct? */
InitStructuredVar( i, tokenarray, (struct dsym *)f->sym.type, &f->sym );
if ( tokenarray[i].token == T_STRING )
i++;
} else if ( f->sym.isarray &&
tokenarray[i].token != T_FINAL &&
tokenarray[i].token != T_COMMA ) {
if ( ERROR == InitializeArray( f, &i, tokenarray ) )
break;
} else if ( f->sym.total_size == f->sym.total_length &&
tokenarray[i].token == T_STRING &&
tokenarray[i].stringlen > 1 &&
( tokenarray[i].string_delim == '"' ||
tokenarray[i].string_delim == '\'' ) ) {
/* v2.07: it's a byte type, but no array, string initializer must have true length 1 */
EmitError( STRING_OR_TEXT_LITERAL_TOO_LONG );
i++;
} else {
//struct asym *sym;
uint_32 no_of_bytes = SizeFromMemtype( f->sym.mem_type, USE_EMPTY, f->sym.type );
/* v2.09: check for empty <> or {} string if a struct/union is embedded */
//for ( sym = f->sym->type; sym && sym->type; sym = sym->type );
if ( tokenarray[i].token == T_FINAL || tokenarray[i].token == T_COMMA ) {
int tc = Token_Count;
int j = Token_Count+1;
Token_Count = Tokenize( f->ivalue, j, tokenarray, TOK_RESCAN );
data_item( &j, tokenarray, NULL, no_of_bytes, f->sym.type, 1, FALSE, f->sym.mem_type & MT_FLOAT, FALSE, Token_Count );
Token_Count = tc;
} else {
char c;
int j = i;
/* ignore commas enclosed in () ( might occur inside DUP argument! ).
*/
for ( lvl = 0, c = 0; tokenarray[i].token != T_FINAL; i++ ) {
if ( tokenarray[i].token == T_OP_BRACKET )
lvl++;
else if ( tokenarray[i].token == T_CL_BRACKET )
lvl--;
else if ( lvl == 0 && tokenarray[i].token == T_COMMA )
break;
else if ( tokenarray[i].token == T_RES_ID && tokenarray[i].tokval == T_DUP )
c++; /* v2.08: check added */
}
if ( c ) {
EmitErr( INITIALIZER_MUST_BE_A_STRING_OR_SINGLE_ITEM, tokenarray[j].tokpos );
} else
if ( ERROR == data_item( &j, tokenarray, NULL, no_of_bytes, f->sym.type, 1, FALSE, f->sym.mem_type & MT_FLOAT, FALSE, i ) ) {
EmitErr( INVALID_DATA_INITIALIZER, f->sym.name );
}
}
}
/* Add padding bytes if necessary (never inside RECORDS!).
* f->next == NULL : it's the last field of the struct/union/record
*/
if ( symtype->sym.typekind != TYPE_RECORD ) {
if ( f->next == NULL || symtype->sym.typekind == TYPE_UNION )
nextofs = symtype->sym.total_size;
else
nextofs = f->next->sym.offset;
if ( f->sym.offset + f->sym.total_size < nextofs ) {
DebugMsg1(("InitStructuredVar: padding, field=%s ofs=%" I32_SPEC "X total=%" I32_SPEC "X nextofs=%" I32_SPEC "X\n",
f->sym.name, f->sym.offset, f->sym.total_size, nextofs ));
SetCurrOffset( CurrSeg, nextofs - (f->sym.offset + f->sym.total_size), TRUE, TRUE );
}
}
/* for a union, just the first field is initialized */
if ( symtype->sym.typekind == TYPE_UNION )
break;
if ( f->next != NULL ) {
if ( tokenarray[i].token != T_FINAL )
if ( tokenarray[i].token == T_COMMA )
i++;
else {
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos );
while ( tokenarray[i].token != T_FINAL && tokenarray[i].token != T_COMMA )
i++;
}
}
} /* end for */
if ( symtype->sym.typekind == TYPE_RECORD ) {
int no_of_bytes;
switch ( symtype->sym.mem_type ) {
case MT_BYTE: no_of_bytes = 1; break;
case MT_WORD: no_of_bytes = 2; break;
#if AMD64_SUPPORT
case MT_QWORD: no_of_bytes = 8; break;
case MT_OWORD: no_of_bytes = 16; break;
#endif
default: no_of_bytes = 4;
}
if (is_record_set){
if (symtype->sym.mem_type == MT_OWORD){
/* output 128 bit data for RECORD */
OutputDataBytes((uint_8 *)&dst128Lo, no_of_bytes / 2);
OutputDataBytes((uint_8 *)&dst128Hi, no_of_bytes / 2);
}
else
OutputDataBytes((uint_8 *)&dwRecInit, no_of_bytes);
}
else
SetCurrOffset( CurrSeg, no_of_bytes, TRUE, TRUE );
}
if ( tokenarray[i].token != T_FINAL ) {
DebugMsg1(("InitStructuredVar(%s): error, i=%u token=%s\n", symtype->sym.name, i, tokenarray[i].string_ptr ));
EmitErr( TOO_MANY_INITIAL_VALUES_FOR_STRUCTURE, tokenarray[i].tokpos );
}
/* restore token status */
Token_Count = old_tokencount;
StringBufferEnd = old_stringbufferend;
DebugMsg1(("InitStructuredVar(%s) exit, current ofs=%" I32_SPEC "X\n", symtype->sym.name, GetCurrOffset() ));
return( NOT_ERROR );
}
/*
* convert a string into little endian format - ( LSB 1st, LSW 1st ... etc ).
* <len> is the TYPE, may be 2,4,6,8,10?,16
*/
static char *little_endian( const char *src, unsigned len )
/*********************************************************/
{
/* v2.06: input and output buffer must be different! */
char *dst = StringBufferEnd;
for( ; len > 1; dst++, src++, len-- ) {
len--;
*dst = *(src + len);
*(dst + len) = *src;
}
if ( len )
*dst = *src;
return( StringBufferEnd );
}
static void output_float( const struct expr *opnd, unsigned size )
/****************************************************************/
{
/* v2.07: buffer extended to max size of a data item (=32).
* test case: XMMWORD REAL10 ptr 1.0
*/
char buffer[32];
char tstr[32];
if ( opnd->mem_type != MT_EMPTY ) {
int i;
memset( buffer, 0, sizeof( buffer ) );
i = SizeFromMemtype( opnd->mem_type, USE_EMPTY, NULL );
if ( i > size )
EmitErr( INITIALIZER_OUT_OF_RANGE );
else {
atofloat( buffer, opnd->float_tok->string_ptr, i , opnd->negative, opnd->float_tok->floattype );
}
}
else if (opnd->kind == EXPR_FLOATI)
{
snprintf(tstr, sizeof(tstr), "%f", opnd->fvalue);
atofloat( buffer, tstr, size, opnd->negative, 0 );
}
else {
atofloat( buffer, opnd->float_tok->string_ptr, size, opnd->negative, opnd->float_tok->floattype );
}
OutputDataBytes( buffer, size );
return;
}
/*
* initialize a data item or struct member;
* - start_pos: contains tokenarray[] index [in/out]
* - tokenarray[]: token array
*
* - sym:
* for data item: label (may be NULL)
* for struct member: field/member (is never NULL)
*
* - no_of_bytes: size of item in bytes
* - type_sym:
* for data item: if != NULL, item is a STRUCT/UNION/RECORD.
* for struct member: if != NULL, item is a STRUCT/UNION/RECORD/TYPEDEF.
*
* - dup: array size if called by DUP operator, otherwise 1
* - inside_struct: TRUE=inside a STRUCT declaration
* - is_float: TRUE=item is float
* - first: TRUE=first item in a line
*
* the symbol will have its 'isarray' flag set if any of the following is true:
* 1. at least 2 items separated by a comma are used for initialization
* 2. the DUP operator occures
* 3. item size is 1 and a quoted string with len > 1 is used as initializer
*/
static ret_code data_item( int *start_pos, struct asm_tok tokenarray[], struct asym *sym, uint_32 no_of_bytes, const struct asym *type_sym, uint_32 dup, bool inside_struct, bool is_float, bool first, int end )
/***************************************************************************************************************************************************************************************************************/
{
int i;
int j;
int string_len;
uint_32 total = 0;
bool initwarn = FALSE;
bool half = FALSE;
//unsigned int count;
uint_8 *pchar,*p;
char tmp;
enum fixup_types fixup_type;
struct fixup *fixup;
struct expr opndx;
uint_16 buff[256];
DebugMsg1(("data_item( idx=%u [%s], label=%s, no_of_bytes=%" I32_SPEC "u, type=%s, dup=%" I32_SPEC "Xh, inside_struct=%u, is_float=%u ) enter\n",
*start_pos, tokenarray[*start_pos].tokpos, sym ? sym->name : "NULL",
no_of_bytes, type_sym ? type_sym->name : "NULL",
dup, inside_struct, is_float ));
for ( ; dup; dup-- ) {
i = *start_pos;
next_item: /* <--- continue scan if a comma has been detected */
/* since v1.94, the expression evaluator won't handle strings
* enclosed in <> or {}. That is, in previous versions syntax
* "mov eax,<1>" was accepted, now it's rejected.
*/
if ( tokenarray[i].token == T_STRING && ( tokenarray[i].string_delim == '<' || tokenarray[i].string_delim == '{' ) ) {
if( type_sym ) {
DebugMsg1(("data_item(%s): literal/brace found: >%s<, inside_struct=%u, no_of_bytes=%" I32_SPEC "u, curr_ofs=%" I32_SPEC "X\n",
type_sym->name, tokenarray[i].string_ptr, inside_struct, no_of_bytes, GetCurrOffset()));
/* it's either a real data item - then inside_struct is FALSE -
* or a structure FIELD of arbitrary type.
*
* v2.10: regression in v2.09: alias types weren't skipped for InitStructuredVar()
*/
while ( type_sym->type ) type_sym = type_sym->type;
if( inside_struct == FALSE ) {
if ( InitStructuredVar( i, tokenarray, (struct dsym *)type_sym, NULL ) == ERROR )
return( ERROR );
} else {
/* v2.09: emit a warning if a TYPEDEF member is a simple type,
* but is initialized with a literal.
* Note: Masm complains about such literals only if the struct is instanced OR -Fl is set.
* fixme: the best solution is to always set type_sym to NULL if
* the type is a TYPEDEF. if the item is a struct member, then
* sym is ALWAYS != NULL and the symbol's type can be gained from there.
* v2.10: aliases are now already skipped here ( see above ).
*/
//while ( type_sym->type ) /* skip alias types */
// type_sym = type_sym->type;
if( type_sym->typekind == TYPE_TYPEDEF && Parse_Pass == PASS_1 )
EmitWarn( 2, COUNT_MUST_BE_POSITIVE_OR_ZERO, tokenarray[i].tokpos );
}
total++;
i++;
goto item_done;
#if 0 /* v2.08: just let EvalOperand() emit 'Unexpected literal...' error */
} else {
DebugMsg(("data_item: invalid string initializer >%s<\n", tokenarray[i].tokpos ));
/* Masm accepts invalid initializer strings if they are actually never used
* for initialization.
*/
if( inside_struct && Parse_Pass == PASS_1 ) {
EmitWarn( 2, UNEXPECTED_LITERAL_FOUND_IN_EXPRESSION, tokenarray[i].tokpos );
tokenarray[i].token = T_QUESTION_MARK;
}
#endif
}
}
if (tokenarray[i].token == T_QUESTION_MARK)
{
opndx.kind = EXPR_EMPTY;
}
else
if ( EvalOperand( &i, tokenarray, end, &opndx, 0 ) == ERROR )
return( ERROR );
//DebugMsg(("data_item, EvalOperand() returned, opndx.kind=%u\n", opndx.kind ));
/* handle DUP operator */
if ( tokenarray[i].token == T_RES_ID && tokenarray[i].tokval == T_DUP ) {
/* v2.03: db 'AB' dup (0) is valid syntax! */
//if ( opndx.kind != EXPR_CONST || opndx.string != NULL ) {
if ( opndx.kind != EXPR_CONST ) {
DebugMsg(("data_item, error, unexpected kind=%u of DUP's first operand\n", opndx.kind ));
/* v2.09: emit a better error msg if a forward ref was used */
if ( opndx.sym && opndx.sym->state == SYM_UNDEFINED )
EmitErr( SYMBOL_NOT_DEFINED, opndx.sym->name );
else
EmitError( CONSTANT_EXPECTED );
return( ERROR );
}
/* max dup is 0x7fffffff */
if ( opndx.value < 0 ) {
return( EmitError( COUNT_MUST_BE_POSITIVE_OR_ZERO ) );
}
i++;
if( tokenarray[i].token != T_OP_BRACKET ) {
DebugMsg(("data_item error, missing '('\n"));
return( EmitErr( EXPECTED, "(" ) );
}
i++;
if ( sym )
sym->isarray = TRUE;
if ( opndx.value == 0 ) {
int level = 1;
for ( ; tokenarray[i].token != T_FINAL; i++ ) {
if ( tokenarray[i].token == T_OP_BRACKET )
level++;
else if ( tokenarray[i].token == T_CL_BRACKET )
level--;
if ( level == 0 )
break;
}
} else {
DebugMsg1(("data_item(%s): op DUP, count=%" I32_SPEC "Xh, calling data_item()\n", sym ? sym->name : "NULL", opndx.uvalue ));
if ( data_item( &i, tokenarray, sym, no_of_bytes, type_sym, opndx.uvalue, inside_struct, is_float, first, end ) == ERROR ) {
DebugMsg(("data_item(%s): op DUP, count=%" I32_SPEC "Xh, returned with error\n", sym ? sym->name : "NULL", opndx.uvalue ));
return( ERROR );
}
}
if( tokenarray[i].token != T_CL_BRACKET ) {
DebugMsg(("data_item: error 'missing ')', exit\n"));
return( EmitErr( EXPECTED, ")" ) );
}
/* v2.09: SIZE and LENGTH actually don't return values for "first initializer, but
* the "first dimension" values
* v2.11: fixme: if the first dimension is 0 ( opndx.value == 0),
* Masm ignores the expression - may be a Masm bug!
*/
if ( sym && first && Parse_Pass == PASS_1 ) {
sym->first_size = opndx.value * no_of_bytes;
sym->first_length = opndx.value;
first = FALSE;
}
i++;
goto item_done;
}
/* a STRUCT/UNION/RECORD data item needs a literal as initializer */
/* v2.06: changed */
//if( type_sym != NULL && inside_struct == FALSE ) {
//if( type_sym != NULL && type_sym->typekind != TYPE_TYPEDEF ) {
if( type_sym ) {
/* v2.10: use a temp symbol, so the original type name can be displayed in the error msg. */
const struct asym *tmp = type_sym;
/* v2.10: check if target type is a struct/union/record; if yes, it's an error. */
while ( tmp->type ) tmp = tmp->type;
if ( tmp->typekind != TYPE_TYPEDEF ) {
DebugMsg1(("data_item: error, type=%s needs literal, >%s<\n", type_sym->name, tokenarray[i].tokpos ));
return( EmitErr( STRUCTURE_IMPROPERLY_INITIALIZED, type_sym->name ) );
}
}
/* handle '?' */
if ( opndx.kind == EXPR_EMPTY && tokenarray[i].token == T_QUESTION_MARK ) {
DebugMsg1(("data_item: ? found, curr_ofs=%X\n", GetCurrOffset()));
opndx.uvalue = no_of_bytes;
/* tiny optimization for uninitialized arrays */
if ( tokenarray[i+1].token != T_COMMA && i == *start_pos ) {
opndx.uvalue *= dup;
total += dup;
dup = 1; /* force loop exit */
} else {
total++;
}
if( !inside_struct ) {
SetCurrOffset( CurrSeg, opndx.uvalue, TRUE, TRUE );
}
i++;
goto item_done;
}
/* warn about initialized data in BSS/AT segments */
if ( Parse_Pass == PASS_2 &&
inside_struct == FALSE &&
// CurrSeg != NULL && /* this is already ensured to be true */
(CurrSeg->e.seginfo->segtype == SEGTYPE_BSS ||
CurrSeg->e.seginfo->segtype == SEGTYPE_ABS) &&
initwarn == FALSE ) {
EmitWarn( 2,
INITIALIZED_DATA_NOT_SUPPORTED_IN_SEGMENT,
(CurrSeg->e.seginfo->segtype == SEGTYPE_BSS) ? "BSS" : "AT" );
initwarn = TRUE;
};
switch( opndx.kind ) {
case EXPR_EMPTY:
DebugMsg(("data_item.EMPTY: idx=%u, tokenarray.token=%X\n", i, tokenarray[i].token));
if ( tokenarray[i].token != T_FINAL )
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos );
else
EmitError( SYNTAX_ERROR );
return( ERROR );
case EXPR_FLOAT:
DebugMsg1(("data_item.FLOAT: >%s<, inside_struct=%u, no_of_bytes=%u, curr_ofs=%X\n",
opndx.float_tok->string_ptr, inside_struct, no_of_bytes, GetCurrOffset()));
if (!inside_struct)
output_float( &opndx, no_of_bytes );
total++;
break;
case EXPR_CONST:
if ( is_float ) {
opndx.fvalue = (float)opndx.value;
opndx.kind = EXPR_FLOATI;
opndx.negative = (opndx.fvalue < 0) ? 1 : 0;
if (opndx.negative)
opndx.fvalue *= -1;
output_float( &opndx, no_of_bytes); /* Coerce const integer value to float in data definitions */
total++;
break;
//return( EmitError( MUST_USE_FLOAT_INITIALIZER ) );
}
/* a string returned by the evaluator (enclosed in quotes!)? */
if ( opndx.quoted_string ) {
DebugMsg1(("data_item.CONST: string found: >%s<, inside_struct=%u, no_of_bytes=%u, curr_ofs=%X\n",
opndx.quoted_string->string_ptr, inside_struct, no_of_bytes, GetCurrOffset()));
pchar = (uint_8 *)opndx.quoted_string->string_ptr + 1;
string_len = opndx.quoted_string->stringlen; /* this is the length without quotes */
/* v2.07: check for empty string for ALL types */
if ( string_len == 0 ) {
if ( inside_struct ) {
/* when the struct is declared, it's no error -
* but won't be accepted when the struct is instanced.
* v2.07: don't modify string_len! Instead
* mark field as array!
*/
//string_len = 1;
sym->isarray = TRUE;
} else {
return( EmitError( EMPTY_STRING ) ); /* MASM doesn't like "" */
}
}
/* a string is only regarded as an array if item size is 1 */
/* else it is regarded as ONE item */
if( no_of_bytes != 1 )
{
if (Options.masm51_compat || Options.strict_masm_compat || !Options.literal_strings)
{
if (string_len > no_of_bytes)
return(EmitError(INITIALIZER_OUT_OF_RANGE));
}
else
{
if (string_len > no_of_bytes && sym && sym->mem_type != MT_WORD)
return(EmitError(INITIALIZER_OUT_OF_RANGE));
/* if characters are not single byte, 2 bytes are used for 1 size v2.38 */
p = pchar;
half = FALSE;
for (j = 0; j < string_len; j++,p++){
if ((unsigned char)*p > 0x7F){
half = TRUE;
break;
}
}
if (half){
memset( &buff, 0, string_len * 2 );
j = UTF8toWideChar(pchar, string_len, NULL,(unsigned short *) &buff, string_len);
opndx.quoted_string->stringlen = j;
string_len = j;
sym->mem_type = MT_BYTE; /* each byte must be stored without zeros between */
/* total has to be devided by 2 for the length */
}
}
}
if( sym && Parse_Pass == PASS_1 && string_len > 0 ) {
total++;
if ( no_of_bytes == 1 && string_len > 1 )
{
total += ( string_len - 1 );
sym->isarray = TRUE; /* v2.07: added */
if ( first ) {
sym->first_length = 1;
sym->first_size = 1;
first = FALSE; /* avoid to touch first_xxx fields below */
}
}
else if (no_of_bytes == 2 && string_len > 1)
{
sym->isarray = TRUE;
total += (string_len - 1);
if (first)
{
sym->first_length = 1;
sym->first_size = 2;
first = FALSE;
}
}
}
if( !inside_struct )
{
/* anything bigger than a byte must be stored in little-endian format -- LSB first */
if (Options.masm51_compat || Options.strict_masm_compat || !Options.literal_strings)
{
if (string_len > 1 && no_of_bytes > 1)
pchar = little_endian( (const char *)pchar, string_len );
OutputDataBytes( pchar, string_len );
if ( no_of_bytes > string_len )
FillDataBytes(0, no_of_bytes - string_len);
}
else
{
if (string_len > 1 && no_of_bytes > 1 && sym && sym->mem_type == MT_WORD)
{
OutputInterleavedDataBytes(pchar, string_len);
}
else
{
if (string_len > 1 && no_of_bytes > 1)
{
if (half) /* no need for little_endian, already converted, v2.38 */
OutputDataBytes((uint_8*)&buff, j * 2);
}
else
{
if(no_of_bytes > 1)
pchar = little_endian((const char *)pchar, string_len);
OutputDataBytes(pchar, string_len);
}
}
if (no_of_bytes > string_len)
FillDataBytes(0, no_of_bytes - string_len);
}
}
} else {
/* it's NOT a string */
DebugMsg1(("data_item.CONST: const found, value=%" I32_SPEC "Xh, no_of_bytes=%u, curr_ofs=%" I32_SPEC "X\n", opndx.value, no_of_bytes, GetCurrOffset()));
if( !inside_struct ) {
/* the evaluator cannot handle types with size > 16.
* so if a (simple) type is larger ( YMMWORD? ),
* clear anything which is above.
*/
if ( no_of_bytes > 16 ) {
OutputDataBytes( opndx.chararray, 16 );
tmp = ( opndx.chararray[15] < 0x80 ? 0 : 0xFF );
FillDataBytes( tmp, no_of_bytes - 16 );
} else {
/* v2.06: TBYTE/OWORD/XMMWORD: extend a negative value to 16-byte */
if ( no_of_bytes > sizeof( int_64 ) ) {
if ( opndx.negative && opndx.value64 < 0 && opndx.hlvalue == 0 )
opndx.hlvalue = -1;
}
OutputDataBytes( opndx.chararray, no_of_bytes );
/* check that there's no significant data left
* which hasn't been emitted.
*/
/* v2.06: rewritten, now more rigid and checks
* 1-8 and 10 bytes instead of just 1-4.
*/
if ( no_of_bytes <= sizeof( int_64 ) ) {
tmp = ( opndx.chararray[7] < 0x80 ? 0 : 0xFF );
memset( opndx.chararray, tmp, no_of_bytes );
if ( opndx.llvalue != 0 && opndx.llvalue != -1 ) {
DebugMsg(("data_item.CONST: error, unhandled data is %" I64_SPEC "X_%016" I64_SPEC "X\n", opndx.hlvalue, opndx.llvalue));
return( EmitErr( INITIALIZER_MAGNITUDE_TOO_LARGE, opndx.sym ? opndx.sym->name : "" ) );
}
} else if ( no_of_bytes == 10 ) {
//if ( opndx.hlvalue > 0xffff ) {
if ( opndx.hlvalue > 0xffff && opndx.hlvalue < -0xffff ) {
return( EmitErr( INITIALIZER_MAGNITUDE_TOO_LARGE, opndx.sym ? opndx.sym->name : "" ) );
}
}
}
}
total++;
}
break;
case EXPR_ADDR:
/* since a fixup will be created, 8 bytes is max.
* there's no way to define an initialized tbyte "far64" address,
* because there's no fixup available for the selector part.
*/
if ( no_of_bytes > sizeof(uint_64) ) {
EmitErr( INVALID_DATA_INITIALIZER, sym ? sym->name : "" );
break;
}
/* indirect addressing (incl. stack variables) is invalid */
if ( opndx.indirect == TRUE ) {
DebugMsg(("data_item.ADDR: error, indirect=%u, sym=%X\n", opndx.indirect, opndx.sym ));
EmitError( INVALID_USE_OF_REGISTER );
break;
}
#if AMD64_SUPPORT
if ( ModuleInfo.Ofssize != USE64 )
#endif
if ( opndx.hvalue && ( opndx.hvalue != -1 || opndx.value >= 0 ) ) {
/* v2.05: compared to Masm, the above is too restrictive.
* the line below might be better.
*/
//if ( opndx.hvalue != 0 && ( opndx.hvalue != -1 || opndx.value == 0 ) ) {
DebugMsg(("data_item.ADDR: displacement doesn't fit in 32 bits: %" I64_SPEC "X\n", opndx.value64 ));
return( EmitConstError( &opndx ) );
}
if ( is_float ) {
DebugMsg(("data_item.ADDR: error, is_float=%u\n", is_float ));
EmitError( MUST_USE_FLOAT_INITIALIZER );
break;
}
total++;
/* for STRUCT fields, don't emit anything! */
if ( inside_struct ) {
break;
}
/* determine what type of fixup is to be created */
switch ( opndx.instr ) {
case T_SEG:
if ( no_of_bytes < 2 ) {
DebugMsg(("data_item.ADDR: error, a SEG wont fit in a BYTE\n" ));
EmitError( MAGNITUDE_TOO_LARGE_FOR_SPECIFIED_SIZE );
}
fixup_type = FIX_SEG;
break;
case T_OFFSET:
switch ( no_of_bytes ) {
case 1:
/* forward reference? */
if ( Parse_Pass == PASS_1 && opndx.sym && opndx.sym->state == SYM_UNDEFINED ) {
DebugMsg(("data_item.ADDR: forward reference + OFFSET operator + DB -> may become error in Pass 2\n" ));
fixup_type = FIX_VOID; /* v2.10: was regression in v2.09 */
} else {
DebugMsg(("data_item.ADDR: error, an offset wont fit in a BYTE\n" ));
EmitError( OFFSET_MAGNITUDE_TOO_LARGE );
fixup_type = FIX_OFF8;
}
break;
case 2:
fixup_type = FIX_OFF16;
break;
#if AMD64_SUPPORT
case 8:
if ( ModuleInfo.Ofssize == USE64 ) {
fixup_type = FIX_OFF64;
break;
}
#endif
default:
if ( opndx.sym && ( GetSymOfssize(opndx.sym) == USE16 ) )
fixup_type = FIX_OFF16;
else
fixup_type = FIX_OFF32;
break;
}
break;
#if IMAGERELSUPP
case T_IMAGEREL:
if ( no_of_bytes < sizeof(uint_32) ) {
DebugMsg(("data_item.ADDR: IMAGEREL, error, size=%u (should be 4)\n", no_of_bytes ));
EmitError( OFFSET_MAGNITUDE_TOO_LARGE );
}
fixup_type = FIX_OFF32_IMGREL;
break;
#endif
#if SECTIONRELSUPP
case T_SECTIONREL:
if ( no_of_bytes < sizeof(uint_32) ) {
DebugMsg(("data_item.ADDR: SECTIONREL, error, size=%u (should be 4)\n", no_of_bytes ));
EmitError( OFFSET_MAGNITUDE_TOO_LARGE );
}
fixup_type = FIX_OFF32_SECREL;
break;
#endif
case T_LOW:
fixup_type = FIX_OFF8; /* OMF, BIN + GNU-ELF only */
break;
case T_HIGH:
DebugMsg(("data_item.ADDR: HIGH detected\n"));
fixup_type = FIX_HIBYTE; /* OMF only */
break;
case T_LOWWORD:
fixup_type = FIX_OFF16;
if ( no_of_bytes < 2 ) {
EmitError( MAGNITUDE_TOO_LARGE_FOR_SPECIFIED_SIZE );
break;
}
break;
#if LOHI32
case T_HIGH32:
/* no break */
#endif
case T_HIGHWORD:
fixup_type = FIX_VOID;
EmitError( CONSTANT_EXPECTED );
break;
#if LOHI32
case T_LOW32:
fixup_type = FIX_OFF32;
if ( no_of_bytes < 4 ) {
EmitError( MAGNITUDE_TOO_LARGE_FOR_SPECIFIED_SIZE );
break;
}
break;
#endif
default:
/* size < 2 can work with T_LOW|T_HIGH operator only */
if ( no_of_bytes < 2 ) {
/* forward reference? */
if ( Parse_Pass == PASS_1 && opndx.sym && opndx.sym->state == SYM_UNDEFINED )
;
else {
/* v2.08: accept 1-byte absolute externals */
if ( opndx.sym && opndx.sym->state == SYM_EXTERNAL && opndx.is_abs == TRUE ) {
} else {
DebugMsg(("data_item.ADDR: error, no of bytes=%u\n", no_of_bytes ));
EmitError( MAGNITUDE_TOO_LARGE_FOR_SPECIFIED_SIZE );
}
fixup_type = FIX_OFF8;
break;
}
}
/* if the symbol references a segment or group,
then generate a segment fixup.
*/
if ( opndx.sym && (opndx.sym->state == SYM_SEG || opndx.sym->state == SYM_GRP ) ) {
fixup_type = FIX_SEG;
break;
}
switch ( no_of_bytes ) {
case 2:
/* accept "near16" override, else complain
* if symbol's offset is 32bit */
/* v2.06: if condition changed */
//if ( opndx.explicit == TRUE && opndx.mem_type == MT_NEAR && opndx.Ofssize == USE16 )
if ( opndx.explicit == TRUE ) {
if ( SizeFromMemtype( opndx.mem_type, opndx.Ofssize, opndx.type ) > no_of_bytes ) {
DebugMsg(("data_item.ADDR: error, memtype %X wont fit in a WORD\n", opndx.mem_type));
EmitErr( INITIALIZER_MAGNITUDE_TOO_LARGE, opndx.sym ? opndx.sym->name : "" );
};
} else if ( opndx.sym && opndx.sym->state == SYM_EXTERNAL && opndx.is_abs == TRUE ) {
/* v2.07a: accept ABSolute externals (regression in v2.07) */
} else if ( opndx.sym &&
opndx.sym->state != SYM_UNDEFINED &&
( GetSymOfssize(opndx.sym) > USE16 ) ) {
DebugMsg(("data_item.ADDR: error, a 32bit offset (%s) wont fit in a WORD\n", opndx.sym->name));
EmitErr( INITIALIZER_MAGNITUDE_TOO_LARGE, opndx.sym ? opndx.sym->name : "" );
}
fixup_type = FIX_OFF16;
break;
case 4:
/* masm generates:
* off32 if curr segment is 32bit,
* ptr16 if curr segment is 16bit,
* and ignores type overrides.
* if it's a NEAR external, size is 16, and
* format isn't OMF, error 'symbol type conflict'
* is displayed
*/
if ( opndx.explicit == TRUE ) {
if ( opndx.mem_type == MT_FAR ) {
if ( opndx.Ofssize != USE_EMPTY && opndx.Ofssize != USE16 ) {
DebugMsg(("data_item.ADDR: error, FAR32 won't fit in a DWORD\n" ));
EmitErr( INITIALIZER_MAGNITUDE_TOO_LARGE, opndx.sym ? opndx.sym->name : "" );
}
fixup_type = FIX_PTR16;
} else if ( opndx.mem_type == MT_NEAR ) {
if ( opndx.Ofssize == USE16 )
fixup_type = FIX_OFF16;
else if ( opndx.sym && ( GetSymOfssize( opndx.sym ) == USE16 ) )
fixup_type = FIX_OFF16;
else
fixup_type = FIX_OFF32;
}
} else {
/* what's done if code size is 16 is Masm-compatible.
* It's not very smart, however.
* A better strategy is to choose fixup type depending
* on the symbol's offset size.
*/
//if ( opndx.sym && ( GetSymOfssize( opndx.sym ) == USE16 ) )
if ( ModuleInfo.Ofssize == USE16 )
#if COFF_SUPPORT || ELF_SUPPORT
if ( opndx.mem_type == MT_NEAR &&
( Options.output_format == OFORMAT_COFF
#if ELF_SUPPORT
|| Options.output_format == OFORMAT_ELF
#endif
)) {
fixup_type = FIX_OFF16;
EmitErr( SYMBOL_TYPE_CONFLICT, sym->name );
} else
#endif
fixup_type = FIX_PTR16;
else
fixup_type = FIX_OFF32;
}
break;
case 6:
/* Masm generates a PTR32 fixup in OMF!
* and a DIR32 fixup in COFF.
*/
/* COFF/ELF has no far fixups */
#if COFF_SUPPORT || ELF_SUPPORT
if ( Options.output_format == OFORMAT_COFF
#if ELF_SUPPORT
|| Options.output_format == OFORMAT_ELF
#endif
) {
fixup_type = FIX_OFF32;
} else
#endif
fixup_type = FIX_PTR32;
break;
default:
/* Masm generates
* off32 if curr segment is 32bit
* ptr16 if curr segment is 16bit
* Uasm additionally accepts a FAR32 PTR override
* and generates a ptr32 fixup then */
if ( opndx.explicit == TRUE && opndx.mem_type == MT_FAR && opndx.Ofssize == USE32 )
fixup_type = FIX_PTR32;
else if( ModuleInfo.Ofssize == USE32 )
fixup_type = FIX_OFF32;
#if AMD64_SUPPORT
else if( ModuleInfo.Ofssize == USE64 )
fixup_type = FIX_OFF64;
#endif
else
fixup_type = FIX_PTR16;
}
break;
} /* end switch ( opndx.instr ) */
/* v2.07: fixup type check moved here */
if ( ( 1 << fixup_type ) & ModuleInfo.fmtopt->invalid_fixup_type ) {
return( EmitErr( UNSUPPORTED_FIXUP_TYPE,
ModuleInfo.fmtopt->formatname,
opndx.sym ? opndx.sym->name : szNull ) );
}
fixup = NULL;
if ( write_to_file ) {
/* there might be a segment override:
* a segment, a group or a segment register.
* Init var SegOverride, it's used inside set_frame()
*/
SegOverride = NULL;
segm_override( &opndx, NULL );
/* set global vars Frame and Frame_Datum */
/* opndx.sym may be NULL, then SegOverride is set. */
if ( ModuleInfo.offsettype == OT_SEGMENT &&
( opndx.instr == T_OFFSET || opndx.instr == T_SEG ))
set_frame2( opndx.sym );
else
set_frame( opndx.sym );
/* uses Frame and Frame_Datum */
fixup = CreateFixup( opndx.sym, fixup_type, OPTJ_NONE );
//store_fixup( fixup, &opndx.value ); /* may fail, but ignore error! */
}
OutputBytes( (unsigned char *)&opndx.value, no_of_bytes, fixup );
break;
case EXPR_REG:
EmitError( INVALID_USE_OF_REGISTER );
break;
default: /* unknown opndx.kind, shouldn't happen */
DebugMsg(("data_item: error, opndx.kind=%u\n", opndx.kind ));
return( EmitError( SYNTAX_ERROR ) );
} /* end switch (opndx.kind) */
item_done:
if( sym && first && Parse_Pass == PASS_1 ) {
sym->first_length = total;
sym->first_size = total * no_of_bytes;
}
if( i < end && tokenarray[i].token == T_COMMA ) {
i++;
if ( tokenarray[i].token != T_FINAL &&
tokenarray[i].token != T_CL_BRACKET ) {
first = FALSE;
if ( sym )
sym->isarray = TRUE;
goto next_item;
}
}
} /* end for */
if( sym && Parse_Pass == PASS_1 ) {
sym->total_length += total; /* we need to know a proper length of characters */
sym->total_size += total * no_of_bytes; /* total_size is needed for proper storing the data */
}
*start_pos = i;
DebugMsg1(("data_item: exit, no error, i=%d\n", i));
return( NOT_ERROR );
}
static ret_code checktypes( const struct asym *sym, enum memtype mem_type, const struct asym *type_sym )
/******************************************************************************************************/
{
/* for EXTERNDEF, check type changes */
if ( sym->mem_type != MT_EMPTY ) {
enum memtype mem_type2 = sym->mem_type;
const struct asym *tmp;
/* skip alias types */
tmp = type_sym;
while ( mem_type == MT_TYPE ) {
mem_type = tmp->mem_type;
tmp = tmp->type;
}
tmp = sym;
while ( mem_type2 == MT_TYPE ) {
mem_type2 = tmp->mem_type;
tmp = tmp->type;
}
if ( mem_type2 != mem_type ) {
DebugMsg(("checktypes: memtype conflict: %u - %u\n", mem_type2, mem_type ));
return( EmitErr( SYMBOL_TYPE_CONFLICT, sym->name ) );
}
}
return( NOT_ERROR );
}
/*
* parse data definition line. Syntax:
* [label] directive|simple type|arbitrary type initializer [,...]
* - directive: DB, DW, ...
* - simple type: BYTE, WORD, ...
* - arbitrary type: struct, union, typedef or record name
* arguments:
* i: index of directive or type
* type_sym: userdef type or NULL
*/
ret_code data_dir( int i, struct asm_tok tokenarray[], struct asym *type_sym )
/****************************************************************************/
{
uint_32 no_of_bytes;
struct asym *sym = NULL;
uint_32 old_offset;
uint_32 currofs; /* for LST output */
enum memtype mem_type;
bool is_float = FALSE;
int idx;
char *name;
struct dsym *symtype = ((struct dsym *)type_sym);
struct sfield *f;
struct sfield *f2;
struct sfield *fPrev;
bool foundSubType = FALSE;
uint_32 subid = 0;
uint_32 subcnt = 0;
uint_32 k = 0;
/* v2.05: the previous test in parser.c wasn't fool-proofed */
if ( i > 1 && ModuleInfo.m510 == FALSE ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr ) );
}
if( tokenarray[i+1].token == T_FINAL ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos ) );
}
/* set values for mem_type and no_of_bytes */
if ( type_sym ) {
mem_type = MT_TYPE;
if ( type_sym->typekind != TYPE_TYPEDEF &&
( type_sym->total_size == 0 || ((struct dsym *)type_sym)->e.structinfo->OrgInside == TRUE ) ) {
return( EmitError( STRUCT_CANNOT_BE_INSTANCED ) );
}
/* v2.09: expand literals inside <> or {}.
* Previously this was done inside InitStructuredVar()
*/
#if FASTPASS
if ( Parse_Pass == PASS_1 || UseSavedState == FALSE )
#endif
ExpandLiterals( i+1, tokenarray );
no_of_bytes = type_sym->total_size;
if ( no_of_bytes == 0 ) {
/* a void type is not valid */
if ( type_sym->typekind == TYPE_TYPEDEF ) {
return( EmitErr( INVALID_TYPE_FOR_DATA_DECLARATION, type_sym->name ) );
}
}
} else {
/* it's either a predefined type or a data directive. For types, the index
into the simpletype table is in <bytval>, for data directives
the index is found in <sflags>.
* v2.06: SimpleType is obsolete. Use token index directly!
*/
if ( tokenarray[i].token == T_STYPE ) {
idx = tokenarray[i].tokval;
} else if ( tokenarray[i].token == T_DIRECTIVE &&
( tokenarray[i].dirtype == DRT_DATADIR )) {
idx = GetSflagsSp( tokenarray[i].tokval );
} else {
return( EmitErr( INVALID_TYPE_FOR_DATA_DECLARATION, tokenarray[i].string_ptr ) );
}
mem_type = GetMemtypeSp( idx );
/* types NEAR[16|32], FAR[16|32] and PROC are invalid here */
if ( ( mem_type & MT_SPECIAL_MASK ) == MT_ADDRESS ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr ) );
}
no_of_bytes = (mem_type & MT_SIZE_MASK) + 1;
if ( mem_type & MT_FLOAT )
is_float = TRUE;
}
/* if i == 1, there's a (data) label at pos 0.
* (note: if -Zm is set, a code label may be at pos 0, and
* i is 2 then.)
*/
name = ( ( i == 1 ) ? tokenarray[0].string_ptr : NULL );
/* in a struct declaration? */
if( CurrStruct ) {
/* structure parsing is done in the first pass only */
if( Parse_Pass == PASS_1 ) {
/* current offset isn't necessarily the fields start offset */
//currofs = CurrStruct->sym.offset;
if (!(sym = CreateStructField( i, tokenarray, name, mem_type, type_sym, no_of_bytes ))) {
return ( ERROR );
}
#if FASTPASS
if ( StoreState ) FStoreLine(0);
#endif
currofs = sym->offset;
sym->isdata = TRUE; /* 'first_size' is valid */
DebugMsg1(("data_dir: %s, CreateStructField called, ofs=%d\n", sym->name, sym->offset ));
} else { /* v2.04: else branch added */
sym = &CurrStruct->e.structinfo->tail->sym;
currofs = sym->offset;
CurrStruct->e.structinfo->tail = CurrStruct->e.structinfo->tail->next;
}
} else {
if( CurrSeg == NULL ) {
return( EmitError( MUST_BE_IN_SEGMENT_BLOCK ) );
}
FStoreLine(0);
if ( ModuleInfo.CommentDataInCode )
omf_OutSelect( TRUE );
if ( ModuleInfo.list ) {
currofs = GetCurrOffset();
}
/* is a label accociated with the data definition? */
if( name ) {
/* get/create the label. */
DebugMsg1(("data_dir: calling SymLookup(%s)\n", name ));
sym = SymLookup( name );
/* v2.11: cannot fail */
//if( sym == NULL ) {
// DebugMsg(("data_dir exit, error: invalid label name\n"));
// return( ERROR );
//}
if( Parse_Pass == PASS_1 ) {
if ( sym->state == SYM_EXTERNAL && sym->weak == TRUE && sym->isproc == FALSE ) { /* EXTERNDEF? */
//if ( checktypes( sym, mem_type, type_sym ) == ERROR )
// return( ERROR );
/* v2.0: display error and continue! */
checktypes( sym, mem_type, type_sym );
/* remove the external info */
sym_ext2int( sym );
sym->total_size = 0;
sym->total_length = 0;
sym->first_length = 0;
} else if( sym->state == SYM_UNDEFINED ) {
sym_remove_table( &SymTables[TAB_UNDEF], (struct dsym *)sym );
sym->state = SYM_INTERNAL;
#if 1
/* v2.11: Set the symbol's langtype. It may have been set
* by a PUBLIC directive, so take care not to overwrite it.
* Problem: Masm doesn't do this - might be a bug.
*/
if ( sym->langtype == LANG_NONE )
sym->langtype = ModuleInfo.langtype;
#endif
} else if ( sym->state == SYM_INTERNAL) {
/* accept a symbol "redefinition" if addresses and types
* do match.
*/
if ( sym->segment != (struct asym *)CurrSeg ||
sym->offset != GetCurrOffset() ) {
return( EmitErr( SYMBOL_ALREADY_DEFINED, name ) );
}
/* check for symbol type conflict */
if ( checktypes( sym, mem_type, type_sym ) == ERROR )
return( ERROR );
/* v2.09: reset size and length ( might have been set by LABEL directive ) */
sym->total_size = 0;
sym->total_length = 0;
goto label_defined; /* don't relink the label */
} else {
DebugMsg(("data_dir(%s): error, symbol redefinition, state=%X\n", sym->name, sym->state ));
return( EmitErr( SYMBOL_REDEFINITION, sym->name ) );
}
/* add the label to the linked list attached to curr segment */
/* this allows to reduce the number of passes (see Fixup.c) */
((struct dsym *)sym)->next = (struct dsym *)CurrSeg->e.seginfo->label_list;
CurrSeg->e.seginfo->label_list = sym;
} else {
old_offset = sym->offset;
}
label_defined:
SetSymSegOfs( sym );
if( Parse_Pass != PASS_1 && sym->offset != old_offset ) {
#ifdef DEBUG_OUT
if ( !ModuleInfo.PhaseError )
DebugMsg(("data_dir: Phase error, pass %u, sym >%s< first time, new=%X != old=%X\n", Parse_Pass+1, sym->name, sym->offset, old_offset));
#endif
ModuleInfo.PhaseError = TRUE;
}
sym->isdefined = TRUE;
sym->isdata = TRUE; /* 'first_size' is valid */
sym->mem_type = mem_type;
sym->type = type_sym;
/* backpatch for data items? Yes, if the item is defined
* in a code segment then its offset may change!
*/
BackPatch( sym );
}
if ( type_sym ) {
while ( type_sym->mem_type == MT_TYPE )
type_sym = type_sym->type;
/* if it is just a type alias, skip the arbitrary type */
if ( type_sym->typekind == TYPE_TYPEDEF )
type_sym = NULL;
}
}
i++;
/* Enhanced Uasm 2.22+ Union initialize */
if (tokenarray[i].token == T_DOT && type_sym->state == SYM_TYPE)
{
i++; // skip dot.
// Check sub type.
symtype = ((struct dsym *)type_sym);
foundSubType = FALSE;
subid = 0;
subcnt = 0;
k = 0;
for (f = symtype->e.structinfo->head; f != NULL; f = f->next)
{
if (strcmp(tokenarray[i].string_ptr, f->sym.name) == 0)
{
f2 = f;
foundSubType = TRUE;
subid = subcnt;
fPrev = symtype->e.structinfo->head;
}
subcnt++;
}
if (!foundSubType)
{
return(EmitErr(INVALID_DATA_INITIALIZER, sym->name));
}
else
{
// sort the union subtypes so the one we want is first (in accordance with what is allowed for initialisation)
if (f2 == symtype->e.structinfo->head)
{
// Do nothing as the required sub type is the first one.
}
else
{
symtype->e.structinfo->head = f2;
}
}
i++; /* skip subtype identifier */
}
if ( data_item( &i, tokenarray, sym, no_of_bytes, type_sym, 1, CurrStruct != NULL, is_float, TRUE, Token_Count ) == ERROR ) {
DebugMsg(("data_dir: error in data_item()\n"));
return( ERROR );
}
if (foundSubType)
symtype->e.structinfo->head = fPrev;
if ( tokenarray[i].token != T_FINAL ) {
return( EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos ) );
}
/* v2.06: update struct size after ALL items have been processed */
if ( CurrStruct )
UpdateStructSize( sym );
if ( ModuleInfo.list )
LstWrite( CurrStruct ? LSTTYPE_STRUCT : LSTTYPE_DATA, currofs, sym );
DebugMsg1(("data_dir: exit, no error, label=%s, is_array=%u Curr%s.ofs=%X\n",
sym ? sym->name : "NULL",
sym ? sym->isarray : 0,
CurrStruct ? "Struct" : "Seg",
CurrStruct ? CurrStruct->sym.offset : CurrSeg->sym.offset ));
return( NOT_ERROR );
}