-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathexpreval.c
4375 lines (4067 loc) · 172 KB
/
expreval.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
*
* 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: expression evaluator.
*
****************************************************************************/
#include <stddef.h>
#include <ctype.h>
#include "globals.h"
#include "parser.h"
#include "reswords.h"
#include "expreval.h"
#include "segment.h"
#include "proc.h"
#include "assume.h"
#include "tokenize.h"
#include "types.h"
#include "label.h"
#include "atofloat.h"
#include "myassert.h"
#include "lqueue.h"
#include "data.h"
#include "symbols.h"
#if defined(WINDOWSDDK)
#define PRIx64 "llx"
#else
#include <inttypes.h>
#endif
#define ALIAS_IN_EXPR 1 /* allow alias names in expression */
#define UNARY_PLUSMINUS 0
#define BINARY_PLUSMINUS 1
/* activate if a detailed error location is needed and -dt cant be used */
#if 0
#define ERRLOC( i ) printf("Error at %s.%u: %u >%s< >%s<\n", __FILE__, __LINE__, i, ModuleInfo.tokenarray[i].string_ptr, ModuleInfo.tokenarray[0].tokpos )
//#undef DebugMsg1
//#define DebugMsg1( x ) printf x
#else
#define ERRLOC( i )
#endif
#if STACKBASESUPP==0
extern enum special_token basereg[];
#else
extern uint_32 StackAdj;
#endif
bool gmaskflag;
#ifdef DEBUG_OUT
static int evallvl = 0;
#endif
extern uint_32 GetCurrOffset( void );
extern void ShiftLeft (uint_64 *dstHi, uint_64 *dstLo,uint_64 num, int pos);
//extern ret_code data_item( int *, struct asm_tok[], struct asym *, uint_32, const struct asym *, uint_32, bool inside_struct, bool, bool, int );
extern ret_code BackPatch( struct asym *sym );
/* the following static variables should be moved to ModuleInfo. */
static struct asym *thissym; /* helper symbol for THIS operator */
static struct asym *nullstruct; /* used for T_DOT if second op is a forward ref */
static struct asym *nullmbr; /* used for T_DOT if "current" struct is a forward ref */
static int (* fnEmitErr)( int, ... );
static int noEmitErr( int msg, ... );
/* code label type values - returned by SIZE and TYPE operators */
enum labelsize {
LS_SHORT = 0xFF01, /* it's documented, but can a label be "short"? */
//LS_NEAR16 = 0xFF02, /* v2.09: the near values are calculated */
//LS_NEAR32 = 0xFF04,
//LS_NEAR64 = 0xFF08,
LS_FAR16 = 0xFF05,
LS_FAR32 = 0xFF06,
};
static void init_expr( struct expr *opnd )
/****************************************/
{
opnd->value = 0;
opnd->hvalue = 0;
opnd->hlvalue = 0;
opnd->quoted_string = NULL;
opnd->base_reg = NULL;
opnd->idx_reg = NULL;
opnd->label_tok = NULL;
opnd->override = NULL;
opnd->instr = EMPTY;
opnd->kind = EXPR_EMPTY;
opnd->mem_type = MT_EMPTY;
opnd->scale = 0;
opnd->Ofssize = USE_EMPTY;
opnd->flags1 = 0;
opnd->sym = NULL;
opnd->mbr = NULL;
opnd->type = NULL;
opnd->isptr = FALSE;
}
static ret_code GetMask128(struct expr *opnd1, int index, struct asm_tok tokenarray[])
{
uint_64 dst128Hi = opnd1->hlvalue;
uint_64 dst128Lo = opnd1->llvalue;
struct asym *lbl = NULL;
char buffer[MAX_LINE_LEN];
char buffer1[MAX_LINE_LEN];
char buff[18];
char *ptr;
int i= Token_Count; /* i must remain the start index */
strcpy( buffer,tokenarray->tokpos);
/* if GTEMP is not created yet do it now */
if (Parse_Pass == PASS_1){
lbl = SymSearch("GMASK");
if (lbl == NULL){
strcpy(buffer1, ".data");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
strcpy(buffer1, "GMASK OWORD 0x");
num2hex64(dst128Hi, buff);
strcat(buffer1, buff);
num2hex64(dst128Lo, buff);
strcat(buffer1, buff);
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
strcpy(buffer1, ".code");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
ptr = tokenarray->tokpos;
while (*ptr != '\0')*(ptr++) = '\0';
ptr = buffer;
while (*ptr != ',')ptr++;
ptr++;
*ptr = '\0';
strcat(ptr, "GMASK");
AddLineQueue(buffer);
strcpy(tokenarray->tokpos, buffer);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
tokenarray[Token_Count+1].token = T_FINAL;
ParseLine(tokenarray);
}
else{ /* global variable GMASK exist, reuse it */
num2hex64(dst128Lo, buff);
strcpy(buffer1, "mov dword ptr ");
ptr = buffer1 + 14;
strcpy(ptr, "GMASK"); /* mov dword ptr rubi.rc */
ptr += 5;
strcpy(ptr, ", LOW32(0x"); /* mov dword ptr rubi.rc, LOW32( */
ptr += 10;
strcat(ptr, buff); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
strcat(ptr, ")");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
/* second DWORD */
ptr = buffer1 + 19;
strcpy(ptr, "+4 ,HIGH32(0x"); /* mov dword ptr rubi.rc, HIGH32( */
ptr += 13;
strcat(ptr, buff); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
strcat(ptr, ")");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
/* third DWORD */
num2hex64(dst128Hi, buff);
ptr = buffer1 + 19;
strcpy(ptr, "+8, LOW32(0x"); /* mov dword ptr rubi.rc, LOW32( */
ptr += 12;
strcat(ptr, buff); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
strcat(ptr, ")");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
/* forth DWORD */
ptr = buffer1 + 19;
strcpy(ptr, "+8+4, HIGH32(0x"); /* mov dword ptr rubi.rc, HIGH32( */
ptr += 15;
strcat(ptr, buff); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
strcat(ptr, ")");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
ptr = tokenarray->tokpos;
while (*ptr != '\0')*(ptr++) = '\0';
ptr = buffer;
while (*ptr != ',')ptr++;
ptr++;
*ptr = '\0';
strcat(ptr, "GMASK");
strcpy(tokenarray->tokpos, buffer);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
tokenarray[Token_Count+1].token = T_FINAL;
ParseLine(tokenarray);
}
}
//strcpy(buffer1, "nop");
strcpy(tokenarray->tokpos, "por ");
strcat(tokenarray->tokpos,tokenarray[1].string_ptr);
strcat(tokenarray->tokpos,", ");
strcat(tokenarray->tokpos,tokenarray[1].string_ptr);
//strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
tokenarray[Token_Count+1].token = T_FINAL;
gmaskflag = TRUE;
DebugMsg1(("GetMask128(%s) exit, current ofs=%" I32_SPEC "X\n", GetCurrOffset() ));
return( NOT_ERROR );
}
/* InitRecordVar is used for inline initialise RECORD to be compatibile with masm, v2.41
* now it can be used as :
* mov al, COLOR<1, 7, 0, 1> ;1 111 0 001 = F1
* mov cobalt.rc, COLOR<1, 7, 0, 1> ;1 111 0 001 = F1
*/
static ret_code InitRecordVar( struct expr *opnd1, int index, struct asm_tok tokenarray[], const struct dsym *symtype )
/****************************************************************************************************************************/
{
char *ptr, *ptr2, *ptr3;
struct sfield *f;
int_32 nextofs;
int i;
int nlabel;
struct asym *sym = NULL;
struct asym *lbl = NULL;
#if AMD64_SUPPORT
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 buffer[MAX_LINE_LEN];
char buffer1[MAX_LINE_LEN];
char buff[16];
int tok_start = index - 1;
int len;
char *oldptr;
/**/myassert( symtype->sym.state == SYM_TYPE && symtype->sym.typekind != TYPE_TYPEDEF );
if ( tokenarray[index].token == T_STRING ) {
if ( tokenarray[index].string_delim != '<' &&
tokenarray[index].string_delim != '{' ) {
return( EmitError( MISSING_ANGLE_BRACKET_OR_BRACE_IN_LITERAL ) );
}
i = Token_Count + 1;
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++;
}
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;
}
/* scan the RECORD's members */
for( f = symtype->e.structinfo->head; f != NULL; f = f->next ) {
DebugMsg1(("InitRecordVar(%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->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 AMD64_SUPPORT
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
dwRecInit |= opndx.value << f->sym.offset;
#endif
}
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++;
}
/* 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(("InitRecordVar: 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 */
opnd1->llvalue = dwRecInit;
if (tokenarray[1].token == T_REG) {
ptr=tokenarray->tokpos + 4;
while (isspace(*ptr))ptr++;
if (*ptr == 'r' || *ptr == 'R')
goto all;
else if (opnd1->llvalue < 0x100000000)
goto all;
else
EmitErr(INITIALIZER_OUT_OF_RANGE);
all:
/* mov dword ptr rubi.rc, LOW32(dst128Lo)
* mov dword ptr rubi.rc+4 ,HIGH32(dst128Lo)
* mov dword ptr rubi.rc+8 , LOW32(dst128Hi)
* mov dword ptr rubi.rc+8+4,HIGH32(dst128Hi) */
if (0 == _stricmp(tokenarray->string_ptr, "movxmmr128")){
strcpy( buffer,tokenarray->tokpos+10);
ptr = buffer;
while (*ptr != ',')ptr++;
*ptr = '\0';
ptr++;
/* if not created global variable GTEMP create it */
lbl = SymSearch("GTEMP");
if (lbl == NULL){
strcpy(buffer1, ".data");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
ParseLine(tokenarray);
sprintf(buff, "GTEMP");
strcpy(buffer1,buff);
strcat(buffer1,ptr);
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
ParseLine(tokenarray);
strcpy(buffer1, ".code");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
ParseLine(tokenarray);
}
else{ /* global variable GTEMP exist, reuse it */
strcpy(buffer1, "mov dword ptr ");
ptr = buffer1 + 14;
strcpy(ptr, "GTEMP"); /* mov dword ptr rubi.rc */
ptr += 5;
strcpy(ptr, ", LOW32("); /* mov dword ptr rubi.rc, LOW32( */
ptr += 8;
sprintf(ptr, "0x%" PRIx64, dst128Lo); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
/* second DWORD */
strcpy(buffer1, "mov dword ptr ");
ptr = buffer1 + 14;
strcpy(ptr, "GTEMP"); /* mov dword ptr rubi.rc */
ptr += 5;
strcpy(ptr, "+4 ,HIGH32("); /* mov dword ptr rubi.rc, HIGH32( */
ptr += 11;
sprintf(ptr, "0x%" PRIx64, dst128Lo); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
/* third DWORD */
strcpy(buffer1, "mov dword ptr ");
ptr = buffer1 + 14;
strcpy(ptr, "GTEMP"); /* mov dword ptr rubi.rc */
ptr += 5;
strcpy(ptr, "+8, LOW32("); /* mov dword ptr rubi.rc, LOW32( */
ptr += 10;
sprintf(ptr, "0x%" PRIx64, dst128Hi); /* mov dword ptr rubi.rc, LOW32(dst128Hi */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
/* forth DWORD */
strcpy(buffer1, "mov dword ptr ");
ptr = buffer1 + 14;
strcpy(ptr, "GTEMP"); /* mov dword ptr rubi.rc */
ptr += 5;
strcpy(ptr, "+8+4, HIGH32("); /* mov dword ptr rubi.rc, HIGH32( */
ptr += 13;
sprintf(ptr, "0x%" PRIx64, dst128Hi); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
ParseLine(tokenarray);
}
/* now write new line with "movups xmm1, GTEMP" */
strcpy(buffer1, MOVE_UNALIGNED_FLOAT());
strcat(buffer1,buffer);
strcat(buffer1," , ");
strcat(buffer1,"GTEMP");
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
goto exit;
}else if (0 == _stricmp(tokenarray->string_ptr, "mov128")){
strcpy( buffer,tokenarray->tokpos+6);
ptr = buffer;
while (*ptr != ',')ptr++;
*ptr = '\0';
strcpy(buffer1, "mov dword ptr " );
ptr = buffer1+14;
strcpy(ptr, buffer); /* mov dword ptr rubi.rc */
ptr += strlen(buffer);
strcpy(ptr, ", LOW32("); /* mov dword ptr rubi.rc, LOW32( */
ptr += 8;
sprintf(ptr, "0x%" PRIx64, dst128Lo); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
ParseLine(tokenarray);
/* second DWORD */
strcpy(buffer1, "mov dword ptr " );
ptr = buffer1+14;
strcpy(ptr, buffer); /* mov dword ptr rubi.rc */
ptr += strlen(buffer);
strcpy(ptr, "+4 ,HIGH32("); /* mov dword ptr rubi.rc, HIGH32( */
ptr += 11;
sprintf(ptr, "0x%" PRIx64, dst128Lo); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
ParseLine(tokenarray);
/* third DWORD */
strcpy(buffer1, "mov dword ptr " );
ptr = buffer1+14;
strcpy(ptr, buffer); /* mov dword ptr rubi.rc */
ptr += strlen(buffer);
strcpy(ptr, "+8, LOW32("); /* mov dword ptr rubi.rc, LOW32( */
ptr += 10;
sprintf(ptr, "0x%" PRIx64, dst128Hi); /* mov dword ptr rubi.rc, LOW32(dst128Hi */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
ParseLine(tokenarray);
/* forth DWORD */
strcpy(buffer1, "mov dword ptr " );
ptr = buffer1+14;
strcpy(ptr, buffer); /* mov dword ptr rubi.rc */
ptr += strlen(buffer);
strcpy(ptr, "+8+4, HIGH32("); /* mov dword ptr rubi.rc, HIGH32( */
ptr += 13;
sprintf(ptr, "0x%" PRIx64, dst128Hi); /* mov dword ptr rubi.rc, LOW32(dst128Lo */
while (*ptr)ptr++;
*ptr = ')';
ptr++;
*ptr = '\0';
strcpy(tokenarray->tokpos, buffer1);
Token_Count = Tokenize( tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT );
goto exit;
}
else{
strcpy(buffer, tokenarray->tokpos);
ptr = strstr(buffer, tokenarray[tok_start].string_ptr);
oldptr = ptr;
len = 0;
while (*ptr != '>')
{
ptr++;
len++;
}
ptr = tokenarray->tokpos + (oldptr - buffer);
for (i = 0; i <= len; i++)
*ptr++ = 0x20;
ptr = buffer;
sprintf(ptr, "0x%" PRIx64, dwRecInit);
oldptr = tokenarray->tokpos + (oldptr - buffer);
len = strlen(buffer);
for (i = 0; i < len; i++)
*oldptr++ = *ptr++;
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);
goto exit;
}
}
else if (opnd1->llvalue < 0x100000000)
goto all;
else
{
goto all;
/* strcpy(buffer, tokenarray->tokpos);
ptr = buffer;
while (*ptr != ',') ptr++;
ptr++;
sprintf(ptr, "0x%" PRIx64, dwRecInit);
strcpy(tokenarray->tokpos, buffer);
Token_Count = Tokenize(tokenarray->tokpos, 0, tokenarray, TOK_DEFAULT);*/
}
exit:
DebugMsg1(("InitRecordVar(%s) exit, current ofs=%" I32_SPEC "X\n", symtype->sym.name, GetCurrOffset() ));
return( NOT_ERROR );
}
//push 253
//push 254
//movq xmm1, [esp]
static void TokenAssign( struct expr *opnd1, const struct expr *opnd2 )
/*********************************************************************/
{
#if 1
/* note that offsetof() is used. This means, don't change position
of field <type> in expr! */
memcpy( opnd1, opnd2, offsetof( struct expr, type ) );
#else
opnd1->llvalue = opnd2->llvalue;
opnd1->hlvalue = opnd2->hlvalue;
opnd1->quoted_string = opnd2->quoted_string; /* probably useless */
opnd1->base_reg = opnd2->base_reg;
opnd1->idx_reg = opnd2->idx_reg;
opnd1->label_tok = opnd2->label_tok;
opnd1->override = opnd2->override;
opnd1->instr = opnd2->instr;
opnd1->kind = opnd2->kind;
opnd1->mem_type = opnd2->mem_type;
opnd1->scale = opnd2->scale;
opnd1->Ofssize = opnd2->Ofssize;
opnd1->flags1 = opnd2->flags1;
opnd1->sym = opnd2->sym;
opnd1->mbr = opnd2->mbr;
// opnd1->type = opnd2->type;
#endif
}
//#define BRACKET_PRECEDENCE 1
//#define PTR_PRECEDENCE 4
//#define PLUS_PRECEDENCE 9
#define CMP_PRECEDENCE 10
static int get_precedence( const struct asm_tok *item )
/*****************************************************/
{
/* The following table is taken verbatim from MASM 6.1 Programmer's Guide,
* page 14, Table 1.3.
* 1 (), []
* 2 LENGTH, SIZE, WIDTH, MASK, LENGTHOF, SIZEOF
* 3 . (structure-field-name operator)
* 4 : (segment override operator), PTR
* 5 LROFFSET, OFFSET, SEG, THIS, TYPE
* 6 HIGH, HIGHWORD, LOW, LOWWORD
* 7 +, - (unary)
* 8 *, /, MOD, SHL, SHR
* 9 +, - (binary)
* 10 EQ, NE, LT, LE, GT, GE
* 11 NOT
* 12 AND
* 13 OR, XOR
* 14 OPATTR, SHORT, .TYPE
* The following table appears in QuickHelp online documentation for
* both MASM 6.0 and 6.1. It's slightly different!
* 1 LENGTH, SIZE, WIDTH, MASK
* 2 (), []
* 3 . (structure-field-name operator)
* 4 : (segment override operator), PTR
* 5 THIS, OFFSET, SEG, TYPE
* 6 HIGH, LOW
* 7 +, - (unary)
* 8 *, /, MOD, SHL, SHR
* 9 +, - (binary)
* 10 EQ, NE, LT, LE, GT, GE
* 11 NOT
* 12 AND
* 13 OR, XOR
* 14 SHORT, OPATTR, .TYPE, ADDR
* japheth: the first table is the prefered one. Reasons:
* - () and [] must be first.
* - it contains operators SIZEOF, LENGTHOF, HIGHWORD, LOWWORD, LROFFSET
* - ADDR is no operator for expressions. It's exclusively used inside
* INVOKE directive.
* However, what's wrong in both tables is the precedence of
* the dot operator: Actually for both Uasm and Wasm the dot precedence
* is 2 and LENGTH, SIZE, ... have precedence 3 instead.
* Precedence of operator TYPE was 5 in original Wasm source. It has
* been changed to 4, as described in the Masm docs. This allows syntax
* "TYPE DWORD ptr xxx"
* v2.02: another case which is problematic:
* mov al,BYTE PTR CS:[]
* Since PTR and ':' have the very same priority, the evaluator will
* first calculate 'BYTE PTR CS'. This is invalid, but didn't matter
* prior to v2.02 because register coercion was never checked for
* plausibility. Solution: priority of ':' is changed from 4 to 3.
*/
switch( item->token ) {
case T_UNARY_OPERATOR:
case T_BINARY_OPERATOR:
return( item->precedence );
case T_OP_BRACKET:
case T_OP_SQ_BRACKET:
/* v2.08: with -Zm, the priority of [] and (), if
* used as binary operator, is 9 (like binary +/-).
* test cases: mov ax,+5[bx]
* mov ax,-5[bx]
*/
//return( 1 );
return( ModuleInfo.m510 ? 9 : 1 );
case T_DOT:
return( 2 );
case T_COLON:
//return( 4 );
return( 3 ); /* changed for v2.02 */
case '*':
case '/':
return( 8 );
case '+':
case '-':
return( item->specval ? 9 : 7 );
}
/* shouldn't happen! */
DebugMsg(("get_precedence: unexpected operator=%s\n", item->string_ptr));
fnEmitErr( SYNTAX_ERROR_EX, item->string_ptr );
return( ERROR );
}
#if 0
static bool is_operator( enum tok_type tt )
/*****************************************/
/* determine if token is an operator */
{
/* T_OP_BRACKET and above: "(,[,],},:,.,+,-,*,/" */
/* rest: T_REG, T_STYPE, T_RES_ID, T_ID, T_STRING,
* T_NUM, T_FLOAT, T_BAD_NUM, T_DBL_COLON, T_PERCENT
*/
return( tt >= T_OP_BRACKET || tt == T_UNARY_OPERATOR || tt == T_BINARY_OPERATOR );
}
static bool is_unary_op( enum tok_type tt )
/*****************************************/
/* determine if token is an unary operator */
{
return( tt == T_OP_BRACKET || tt == T_OP_SQ_BRACKET || tt == '+' || tt == '-' || tt == T_UNARY_OPERATOR );
}
#else
#define is_operator( tt ) ( tt >= T_OP_BRACKET || tt == T_UNARY_OPERATOR || tt == T_BINARY_OPERATOR )
#define is_unary_op( tt ) ( tt == T_OP_BRACKET || tt == T_OP_SQ_BRACKET || tt == '+' || tt == '-' || tt == T_UNARY_OPERATOR )
#endif
/* get value for simple types
* NEAR, FAR and PROC are handled slightly differently:
* the HIBYTE is set to 0xFF, and PROC depends on the memory model
*/
static unsigned int GetTypeSize(enum memtype mem_type, int Ofssize)
/*******************************************************************/
{
if ((mem_type & MT_SPECIAL) == 0) {
#if AVXSUPP
if (mem_type == MT_ZMMWORD)
return (0x40);
else
#endif
return((mem_type & MT_SIZE_MASK) + 1);
}
if (Ofssize == USE_EMPTY)
Ofssize = ModuleInfo.Ofssize;
switch (mem_type) {
case MT_NEAR: return (0xFF00 | (2 << Ofssize));
case MT_FAR: return ((Ofssize == USE16) ? LS_FAR16 : 0xFF00 | ((2 << Ofssize) + 2));
}
/* shouldn't happen */
return(0);
}
#if AMD64_SUPPORT
static uint_64 GetRecordMask( struct dsym *record )
#else
static uint_32 GetRecordMask( struct dsym *record )
#endif
/*************************************************/
{
#if AMD64_SUPPORT
uint_64 mask = 0;
#else
uint_32 mask = 0;
#endif
int i;
struct sfield *fl;
for ( fl = record->e.structinfo->head; fl; fl = fl->next ) {
struct asym *sym = &fl->sym;
for ( i = sym->offset ;i < sym->offset + sym->total_size; i++ )
#if AMD64_SUPPORT
mask |= (1LL << (uint_64)i);
#else
mask |= (1 << i);
#endif
}
return( mask );
}
/* v2.06: the value of number strings is now evaluated here.
* Prior to v2.06, it was evaluated in the tokenizer and the
* value was stored in the token string buffer. Since the content
* of the token buffer is no longer destroyed when macros or
* generated code is run, the old strategy needed too much space.
*/
void myatoi128( const char *src, uint_64 dst[], int base, int size )
/******************************************************************/
{
uint_32 val;
unsigned len;
const char *end = src + size;
uint_16 *px;
dst[0] = 0;
dst[1] = 0;
#if CHEXPREFIX
if (((src[1] | 0x20) == 'x') && (*src == '0')){
src += 2;
end += 2;
}
#endif
do {
val = ( *src <= '9' ? *src - '0' : ( *src | 0x20 ) - 'a' + 10 );
px = (uint_16 *)dst;
for ( len = ( 2 * sizeof( uint_64 ) ) >> 1; len; len-- ) {
val += (uint_32)*px * base;
*(px++) = val;
val >>= 16;
};
//myassert( val == 0 ); /* if number doesn't fit in 128 bits */
src++;
} while( src < end );
return;
}
/* get an operand. operands are:
* - integer constant : EXPR_CONST
* - quoted string : EXPR_CONST
* - register : EXPR_REG (indirect = 1/0)
* - user identifier (T_ID): EXPR_ADDR | EXPR_CONST
* - reserved ID (T_RES_ID): EXPR_CONST ( EXPR_ADDR if id=FLAT )
* - float constant : EXPR_FLOAT
*
* valid user identifiers are
* - TYPE ( struct/union, typedef, record )
* - STRUCT FIELD (also bitfield)
* - variable (internal, external, stack ) or constant (EQU, '=')
* valid reserved IDs are types (BYTE, WORD, ... ) and FLAT
*/
static ret_code get_operand( struct expr *opnd, int *idx, struct asm_tok tokenarray[], const uint_8 flags )
/*********************************************************************************************************/
{
char *tmp;
struct asym *sym;
int i = *idx;
int j;
char labelbuff[16];/* for anonymous labels */
int cnt;
char *p;
char clabel[100];
struct asym *labelsym;
struct asym *labelsym2;
struct asm_tok tok;
DebugMsg1(("%u get_operand(idx=%u >%s<) enter [memtype=%Xh]\n", evallvl, i, tokenarray[i].tokpos, opnd->mem_type ));
switch( tokenarray[i].token ) {
case T_DOT:
/* Allow .labelname to be used as an operand */
if ((tokenarray[*idx].token == T_DOT && tokenarray[(*idx) + 1].token == T_ID))
{
// check that T_ID is a label
sprintf(clabel, "%s%s", ".", tokenarray[(*idx) + 1].string_ptr);
labelsym = SymFind(clabel);
labelsym2 = SymFind(tokenarray[(*idx) + 1].string_ptr);
if ((*idx) > 0)
{
tok = tokenarray[(*idx) - 1];
}
if (labelsym != NULL ||
(labelsym == NULL && tok.token != T_ID && tok.token != T_CL_SQ_BRACKET && tok.token != T_CL_BRACKET) ||
(labelsym != NULL && labelsym->label))
{
(*idx)++;
strcpy(clabel, tokenarray[(*idx)].string_ptr);
sprintf(tokenarray[(*idx)].string_ptr, "%s%s", ".", &clabel);
}
else if (labelsym == NULL && labelsym2 == NULL)
{
}
i++;
goto isNowID;
}
case T_NUM:
DebugMsg1(("%u get_operand: T_NUM, %s, base=%u, len=%u\n", evallvl, tokenarray[i].string_ptr, tokenarray[i].numbase, tokenarray[i].itemlen ));
opnd->kind = EXPR_CONST;
myatoi128( tokenarray[i].string_ptr, &opnd->llvalue, tokenarray[i].numbase, tokenarray[i].itemlen );
//opnd->llvalue = tokenarray[i].value64;
//opnd->hlvalue = ( tokenarray[i].numflg == NF_NULL ? 0 : *(uint_64 *)( tokenarray[i].string_ptr - sizeof(uint_64) ) );
break;
case T_STRING:
DebugMsg1(("%u get_operand: T_STRING, %s, size=%u\n", evallvl, tokenarray[i].string_ptr, tokenarray[i].stringlen ));
/* string enclosed in <> or {} are rejected since v1.94! */
if (tokenarray[i].string_delim != '"' && tokenarray[i].string_delim != '\'') {
/* here is handled EVEX Static Rounding Mode
* {sae}, {rn-sae},{rd-sae},{ru-sae} {rz-sae}
* ZLLBVAAA ZLLBVAAA ZLLBVAAA ZLLBVAAA ZLLBVAAA
* 00010000 00010000 00110000 01010000 01110000
* to destinguish between SAE and RN I added 0x10
* to all 4 other decorators
* which will be subtracted in codegen.c
*/
/* optimized and alowed white space after '{' and before '}', v2.38 */
if (tokenarray[i].string_delim == '{' && evex) {
p = tokenarray[i].string_ptr;
while (isspace(*p)) p++; /* skip white spaces*/
if (memcmp(p, "rn-sae", 6) == 0) {
opnd->kind = EXPR_DECORATOR;
opnd->saeflags = 0x20;
break;
}
else if (memcmp(p, "rd-sae", 6) == 0) {
opnd->kind = EXPR_DECORATOR;
opnd->saeflags = 0x40;
break;
}
else if (memcmp(p, "ru-sae", 6) == 0) {
opnd->kind = EXPR_DECORATOR;
opnd->saeflags = 0x60;
break;
}
else if (memcmp(p, "rz-sae", 6) == 0) {
opnd->kind = EXPR_DECORATOR;
opnd->saeflags = 0x80;
break;
}
else if (memcmp(p, "sae", 3) == 0) {
opnd->kind = EXPR_DECORATOR;
opnd->saeflags = 0x10;
break;
}
//EmitError(UNAUTHORISED_USE_OF_EVEX_ENCODING);
}
else if (opnd->is_opattr) /* OPATTR operator accepts anything! */
break;
/* v2.0: display a comprehensible error msg if a quote is missing */
if (tokenarray[i].string_delim == NULLC &&
(*tokenarray[i].string_ptr == '"' || *tokenarray[i].string_ptr == '\''))
fnEmitErr(MISSING_QUOTATION_MARK_IN_STRING);
else
fnEmitErr(MISSING_QUOTATION_MARK_IN_STRING, tokenarray[i].tokpos);
return(ERROR);
}
opnd->kind = EXPR_CONST;
opnd->quoted_string = &tokenarray[i];
//opnd->value = 0;
tmp = tokenarray[i].string_ptr + 1; /* skip the quote */
/* v2.06: use max. 16 bytes to create the "value".
* Prior to 2.06, max 8 bytes were used for 64-bit and
* max 4 bytes were used for 16-/32-bit.
*/
j = ( tokenarray[i].stringlen > sizeof( opnd->chararray ) ? sizeof( opnd->chararray ) : tokenarray[i].stringlen );
for( ; j; j-- )
opnd->chararray[j-1] = *tmp++;
break;
case T_REG:
DebugMsg1(( "%u get_operand: T_REG, string=%s, tokval=%u, regno=%u\n", evallvl, tokenarray[i].string_ptr, tokenarray[i].tokval, tokenarray[i].bytval ));
opnd->kind = EXPR_REG;
opnd->base_reg = &tokenarray[i];
j = tokenarray[i].tokval;
/* check if cpu is sufficient for register */
if( ( ( GetCpuSp( j ) & P_EXT_MASK ) &&
(( GetCpuSp( j ) & ModuleInfo.curr_cpu & P_EXT_MASK) == 0) ||
( ModuleInfo.curr_cpu & P_CPU_MASK ) < ( GetCpuSp( j ) & P_CPU_MASK ) ) ) {
/* v2.11: do not exit in indirect mode; avoids additional syntax error caused by ']' */
if ( flags & EXPF_IN_SQBR ) {
opnd->kind = EXPR_ERROR;
fnEmitErr( INSTRUCTION_OR_REGISTER_NOT_ACCEPTED_IN_CURRENT_CPU_MODE );
} else
return( fnEmitErr( INSTRUCTION_OR_REGISTER_NOT_ACCEPTED_IN_CURRENT_CPU_MODE ) );
}
if ((i > 0 && tokenarray[i - 1].tokval == T_TYPE) ||
(i > 1 && tokenarray[i - 1].token == T_OP_BRACKET
&& tokenarray[i - 2].tokval == T_TYPE))
; /* v2.26 [reg + type reg] | [reg + type(reg)] */
else if (flags & EXPF_IN_SQBR) {
//if( flags & EXPF_IN_SQBR && (i == 0 || tokenarray[i - 1].tokval != T_TYPE) )
//{
/* a valid index register? */
if ( GetSflagsSp( j ) & SFR_IREG ) {
opnd->indirect = TRUE;
opnd->assumecheck = TRUE;
} else if ( GetValueSp( j ) & OP_SR ) {
/* a segment register inside square brackets is only