-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpreval.c
3298 lines (3068 loc) · 127 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 "globals.h"
#include "parser.h"
#include "reswords.h"
#include "expreval.h"
#include "segment.h"
#include "proc.h"
#include "assume.h"
#include "input.h"
#include "tokenize.h"
#include "types.h"
#include "labels.h"
#include "data.h"
#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 -d6 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
static int op_sq_bracket_level;
#ifdef DEBUG_OUT
static int evallvl = 0;
#endif
/* error msg flag.
* 1=display error messages.
* for historical reasons, it's also used to detect
* "EQU mode" (error_msg = 0).
*/
static char error_msg;
static char eflags;
static struct asym *thissym; /* helper symbol for THIS operator */
static struct asym *nullstruct; /* used for T_DOT if there's no current type */
static struct asym *nullmbr; /* used for T_DOT if struct is a forward ref */
enum process_flag {
PROC_BRACKET, /* evaluate until the end or a closing bracket is found */
PROC_OPERAND /* stop if an operator with lower precedence is found */
};
enum labelsize {
LS_SHORT = 0xFF01, /* it's documented, but can a label be "short"? */
LS_NEAR16 = 0xFF02,
LS_NEAR32 = 0xFF04,
LS_FAR16 = 0xFF05,
LS_FAR32 = 0xFF06
};
static ret_code evaluate( struct expr *, int *, struct asm_tok[], int, enum process_flag );
void EmitConstError( const struct expr *opnd )
/********************************************/
{
if ( opnd->hlvalue != 0 )
EmitErr( CONSTANT_VALUE_TOO_LARGE_EX, opnd->hlvalue, opnd->value64 );
else
EmitErr( CONSTANT_VALUE_TOO_LARGE, opnd->value64 );
return;
}
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;
}
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 JWasm 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));
EmitErr( 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 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 )
return( ( mem_type & MT_SIZE_MASK ) + 1 );
switch ( mem_type ) {
case MT_NEAR:
switch ( Ofssize ) {
case USE16: return( 2 );
case USE32: return( 4 );
}
return (0xFF00 | ( 2 << ModuleInfo.Ofssize ) ) ;
case MT_FAR:
switch ( Ofssize ) {
case USE16: return( 4 );
case USE32: return( 6 );
}
return (0xFF00 | ( ( 2 << ModuleInfo.Ofssize ) + 2 ) );
}
/* shouldn't happen */
return( 0 );
}
static void CEmitError( int msg )
/*******************************/
{
if ( error_msg )
EmitError( msg );
}
#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 field_item *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++ )
mask |= 1 << i;
}
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;
while( src < end ) {
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;
};
src++;
}
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[] )
/*************************************************************************************/
{
char *tmp;
struct asym *sym;
int i = *idx;
int j;
char labelbuff[16];/* for anonymous labels */
DebugMsg1(("get_operand(start=%u >%s<) enter, memtype=%Xh\n", i, tokenarray[i].tokpos, opnd->mem_type ));
switch( tokenarray[i].token ) {
case T_NUM:
DebugMsg1(("get_operand: T_NUM (%s, base=%u, len=%u)\n", 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(("get_operand: T_STRING (%s), size=%u\n", 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 != '\'') {
if ( opnd->is_opattr ) /* OPATTR operator accepts anything! */
break;
if ( error_msg )
/* 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 == '\'' ))
EmitErr( MISSING_QUOTATION_MARK_IN_STRING );
else
EmitErr( UNEXPECTED_LITERAL_FOUND_IN_EXPRESSION, 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(( "get_operand: T_REG (%s)\n", tokenarray[i].string_ptr ));
opnd->kind = EXPR_REG;
opnd->base_reg = &tokenarray[i];
j = tokenarray[i].tokval;
#if 1
/* this check was previously done in the parser.
Check if the register needs an extensions (which are bit masks).
If no, then check if the cpu is sufficient.
*/
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 ) ) ) {
CEmitError( INSTRUCTION_OR_REGISTER_NOT_ACCEPTED_IN_CURRENT_CPU_MODE );
return( ERROR );
}
#endif
if( op_sq_bracket_level > 0 ) {
/* a valid index register? */
if ( GetSflagsSp( j ) & SFR_IREG ) {
opnd->indirect = TRUE;
opnd->assumecheck = TRUE;
/* v2.06: in strict Masm mode, a base/index register must not
* be preceded by "<segreg|segment>:" INSIDE the brackets!
*/
/* v2.08: test moved to colon_op() */
//if ( Options.strict_masm_compat && tokenarray[i-1].token == T_COLON )
// CEmitError( INVALID_USE_OF_REGISTER );
} else if ( GetValueSp( j ) & OP_SR ) {
/* a segment register inside square brackets is only
* accepted by Masm if it is the segment part of an
* address (mov ax,[bx+cs:label])!
*/
if( tokenarray[i+1].token != T_COLON ) {
CEmitError( INVALID_USE_OF_REGISTER );
return( ERROR );
}
} else {
CEmitError( MUST_BE_INDEX_OR_BASE_REGISTER );
return( ERROR );
}
#if 0 /* v2.08: now done in calculate() for [] and () op */
} else if( j == T_ST ) {
struct expr sti;
char cl_bracket;
/* read st(i), put i into idx_reg */
i++;
switch( tokenarray[i].token ) {
case T_OP_BRACKET: /* syntax st(0) */
case T_OP_SQ_BRACKET: /* syntax st[0] */
/* v2.06: added: skip the opening bracket! This ensures that
* evaluate() will definitely stop at the closing bracket.
* The type of the closing bracket must be checked then! */
*start = i+1;
init_expr( &sti );
if( evaluate( &sti, start, tokenarray, end, PROC_BRACKET ) == ERROR ) {
return( ERROR );
}
cl_bracket = ( tokenarray[i].token == T_OP_BRACKET ? T_CL_BRACKET : T_CL_SQ_BRACKET );
if( tokenarray[*start].token != cl_bracket ) {
CEmitError( MISSING_RIGHT_PARENTHESIS_IN_EXPRESSION );
return( ERROR );
}
if( sti.kind != EXPR_CONST ) {
CEmitError( CONSTANT_OPERAND_EXPECTED );
return( ERROR );
}
opnd->st_idx = sti.value;
break;
default:
opnd->st_idx = 0; /* st = st(0) */
break;
}
#endif
}
break;
case T_ID:
tmp = tokenarray[i].string_ptr;
if ( opnd->type ) {
DebugMsg1(("get_operand(%s): ID, type=%s\n", tokenarray[i].string_ptr, opnd->type->name ));
opnd->value = 0;
if ( opnd->type == nullstruct )
sym = NULL;
else {
sym = SearchNameInStruct( opnd->type, tmp, &opnd->uvalue, 0 );
DebugMsg1(("get_operand(%s): SearchNameInStruct(%s)=%X, offset=%u\n",
tokenarray[i].string_ptr, opnd->type->name, sym, opnd->uvalue));
}
if ( sym == NULL ) {
sym = SymSearch( tmp );
if ( sym ) {
/*
* skip a type specifier matching the data item's type
* that's something like "<item>.<type>.<member>"
*/
if ( sym->state == SYM_TYPE ) {
/* superfluous type spec? */
/* v2.7: "if" added.
* Masm accepts a different type spec if the "assumed"
* type is undefined */
if ( sym == opnd->type || opnd->type->isdefined == FALSE )
; //opnd->sym = sym;
else
sym = NULL;
} else if ( ModuleInfo.oldstructs &&
( sym->state == SYM_STRUCT_FIELD ||
sym->state == SYM_EXTERNAL || /* v2.01: added */
/* v2.05: changed */
//( sym->state == SYM_INTERNAL && sym->mem_type == MT_ABS ) ) )
sym->state == SYM_INTERNAL ) )
//opnd->sym = sym;
;
else {
/* fixme: clear sym?
* if the symbol is not a type, it's an error which can
* be detected in pass 1 already. dot_op() will emit
* 'struct field expected' if sym isn't cleared.
*/
if ( opnd->type != nullstruct )
sym = NULL;
}
}
}
} else {
DebugMsg1(("get_operand(%s): ID\n", tokenarray[i].string_ptr ));
/* ensure anonym labels are uppercase */
/* v2.06: changed. Previously member 'string_ptr' was used to
* store the anonymous label, but one cannot safely assume that
* there's enough free space for a larger symbol name! It (partly)
* worked by accident, because @F/@B usually are the last tokens
* in a line [ but see: .if ( eax == @F && ecx == 2 ) ].
*/
if ( *tmp == '@' && *(tmp+2 ) == NULLC ) {
if ( *(tmp+1) == 'b' || *(tmp+1 ) == 'B' )
tmp = GetAnonymousLabel( labelbuff, 0 );
else if (*(tmp+1) == 'f' || *(tmp+1 ) == 'F' )
tmp = GetAnonymousLabel( labelbuff, 1 );
}
sym = SymSearch( tmp );
}
if ( sym == NULL ||
sym->state == SYM_UNDEFINED ||
#if ALIAS_IN_EXPR == 0
sym->state == SYM_ALIAS || /* v2.04: added */
#endif
sym->state == SYM_MACRO ||
sym->state == SYM_TMACRO ) {
/* for OPATTR, anything is ok */
if ( opnd->is_opattr ) {
DebugMsg1(( "get_operand(%s): OPATTR, symbol invalid\n", tokenarray[i].string_ptr ));
opnd->kind = EXPR_UNDEF;
break;
}
/* if it is EQU (then error_msg is FALSE), don't display an error,
but return ERROR */
if ( error_msg == FALSE ) {
DebugMsg1(("get_operand(%s): EQU, symbol invalid\n", tokenarray[i].string_ptr));
return( ERROR );
}
if ( sym && ( sym->state == SYM_MACRO ||
#if ALIAS_IN_EXPR == 0
sym->state == SYM_ALIAS || /* v2.04: added */
#endif
sym->state == SYM_TMACRO ) ) {
DebugMsg1(("get_operand(%s): symbol is macro/textmacro/alias!\n", tokenarray[i].string_ptr));
EmitErr( INVALID_SYMBOL_TYPE_IN_EXPRESSION, sym->name );
return( ERROR );
}
//if( Parse_Pass == PASS_1 ) {
if( Parse_Pass == PASS_1 ) {
/* if symbol wasn't found, assume it is a forward ref! */
if ( sym == NULL ) {
if ( opnd->type == NULL && !( eflags & EXPF_NOLCREATE ) ) { /* added v1.95 */
sym = SymLookup( tmp );
sym->state = SYM_UNDEFINED;
sym_add_table( &SymTables[TAB_UNDEF], (struct dsym *)sym ); /* add UNDEFINED */
DebugMsg1(("get_operand(%s): symbol not (yet) defined, CurrProc=%s\n", tmp, CurrProc ? CurrProc->sym.name : "NULL" ));
/* v2.08: if changed */
// } else if ( opnd->type == NULL || opnd->type != nullstruct ) {
} else if ( opnd->type == NULL || opnd->type->typekind != TYPE_NONE ) {
/* no struct or struct is known and defined */
DebugMsg(("get_operand(%s): symbol error (type=%s kind=%u)\n", tmp, opnd->type ? opnd->type->name : "NULL", opnd->type ? opnd->type->typekind : 0 ));
if ( opnd->type && *opnd->type->name )
EmitErr( MEMBER_NOT_DEFINED, opnd->type->name, tmp );
else
EmitErr( SYMBOL_NOT_DEFINED, tmp );
return( ERROR );
} else {
/* forward reference to a struct.
* In these cases, assume everything is ok.
*/
if ( !nullmbr ) {
nullmbr = SymAlloc( "" );
}
DebugMsg(("get_operand(%s): forward reference to a struct (using nullmbr)\n", tmp ));
/* "break" because nullmbr has state SYM_UNDEFINED */
opnd->mbr = nullmbr;
opnd->kind = EXPR_CONST;
break;
}
}
} else {
DebugMsg1(("get_operand(%s): symbol %s not defined, pass > 1, curr proc=>%s<, \n", tokenarray[i].string_ptr, tmp, CurrProc ? CurrProc->sym.name : "NULL" ));
if ( opnd->type && *opnd->type->name ) {
EmitErr( MEMBER_NOT_DEFINED, opnd->type->name, tmp );
} else {
EmitErr( SYMBOL_NOT_DEFINED, *(tmp+1) == '&' ? "@@" : tmp );
}
return( ERROR );
}
#if ALIAS_IN_EXPR /* v2.04b: added */
} else if ( sym->state == SYM_ALIAS ) {
/* ALIAS symbols are not really useable in expressions.
* The alias' substitute symbol is, however.
*/
sym = sym->substitute; /* can't be NULL */
#endif
}
/* set default values */
sym->used = TRUE;
DebugMsg1(("get_operand(%s): sym->state=%u type=%X ofs=%X memtype=%Xh total_size=%u defined=%u\n",
tokenarray[i].string_ptr, sym->state, sym->type, sym->offset, sym->mem_type, sym->total_size, sym->isdefined ));
switch ( sym->state ) {
case SYM_TYPE: /* STRUCT, UNION, RECORD, TYPEDEF */
if ( ((struct dsym *)sym)->e.structinfo->isOpen == FALSE ) {
opnd->kind = EXPR_CONST;
//opnd->mem_type = MT_ABS;
opnd->mem_type = sym->mem_type;
opnd->is_type = TRUE;
DebugMsg1(("get_operand(%s): symbol.kind=%u (STRUCT/UNION/TYPEDEF/RECORD)\n", sym->name, sym->typekind ));
/* v2.08: if() removed. This was an old hack. */
//if ( tokenarray[i-1].token != T_DOT && tokenarray[i+1].token != T_DOT )
/* v2.06: the default value for RECORD types is the mask value */
if ( sym->typekind == TYPE_RECORD ) {
#if AMD64_SUPPORT
opnd->llvalue = GetRecordMask( (struct dsym *)sym );
#else
opnd->value = GetRecordMask( (struct dsym *)sym );
#endif
} else
opnd->value = sym->total_size;
} else {
DebugMsg1(("get_operand(%s): struct/union definition isn't closed!\n", sym->name ));
/* a valid constant should be returned if
1. the struct is open AND
2. it's not an EQU operand
the number isn't used then (except if it's the first DUP operand)
*/
if ( error_msg == FALSE )
opnd->kind = EXPR_UNDEF;
else {
opnd->kind = EXPR_CONST;
/* v2.07: MT_ABS is obsolete */
//opnd->mem_type = MT_ABS;
opnd->value = -1;
opnd->is_type = TRUE;
}
}
/* skip "alias" types. Obsolete? */
for ( ; sym->type; sym = sym->type );
opnd->type = sym;
break;
case SYM_STRUCT_FIELD:
DebugMsg1(("get_operand(%s): structure field, ofs=%Xh\n", sym->name, sym->offset ));
/* opnd->value might have been set by SearchNameInStruct() already! */
opnd->value += sym->offset;
opnd->kind = EXPR_CONST;
opnd->mbr = sym;
/* skip "alias" types (probably obsolete by now!) */
for ( ; sym->type; sym = sym->type );
opnd->mem_type = sym->mem_type;
/*
* check if the member field has arbitrary type.
* If yes, set the <type> member!
* It's probably better to handle this case in PrepareOp()
* or in dot_op().
*/
if ( sym->state == SYM_TYPE && sym->typekind != TYPE_TYPEDEF ) {
opnd->type = sym;
} else {
opnd->type = NULL; /* added v1.96 */
}
DebugMsg1(("get_operand: mem_type=%Xh type=%s\n", opnd->mem_type, opnd->type ? opnd->type->name : "NULL" ));
break;
default: /* SYM_INTERNAL, SYM_EXTERNAL, SYM_SEG, SYM_GRP, SYM_STACK */
opnd->kind = EXPR_ADDR;
/* call internal function (@Line, ... ) */
if ( sym->predefined && sym->sfunc_ptr )
sym->sfunc_ptr( sym );
//if( opnd->sym->mem_type == MT_ABS ) {
if( sym->state == SYM_INTERNAL && sym->segment == NULL ) {
opnd->kind = EXPR_CONST;
opnd->uvalue = sym->uvalue;
opnd->hvalue = sym->value3264;
DebugMsg1(("get_operand(%s): equate hval=%Xh, lval=%Xh\n", sym->name, opnd->hvalue, opnd->uvalue ));
opnd->mem_type = sym->mem_type;
/* don't set the symbol reference, it isn't a label */
} else if( sym->state == SYM_EXTERNAL &&
sym->mem_type == MT_EMPTY &&
sym->iscomm == FALSE ) {
/* type remains EXPR_ADDR, to force fixup creation */
opnd->mem_type = sym->mem_type;
opnd->abs = TRUE;
opnd->sym = sym;
} else {
opnd->label_tok = &tokenarray[i];
/* a variable with arbitrary type? */
/* v2.05: added check for MT_EMPTY */
//if( opnd->sym->type ) {
if( sym->type && sym->type->mem_type != MT_EMPTY ) {
/* skip "alias" types */
/* v2.05: obsolete */
//for ( sym2 = opnd->sym; sym2->type; sym2 = sym2->type );
//opnd->mem_type = sym2->mem_type;
opnd->mem_type = sym->type->mem_type;
} else {
opnd->mem_type = sym->mem_type;
}
/* since there is no fixup for auto variables, the "offset"
must be stored in the <value> field */
if ( sym->state == SYM_STACK ) {
opnd->llvalue = sym->offset;
opnd->indirect = TRUE;
}
opnd->sym = sym;
}
break;
}
break;
case T_STYPE:
DebugMsg1(("get_operand: T_STYPE, >%s<, value=%X\n", tokenarray[i].string_ptr, tokenarray[i].tokval));
opnd->kind = EXPR_CONST;
/* for types, return the size as numeric constant */
/* fixme: mem_type should be set only when used as first arg of PTR op! */
opnd->mem_type = GetMemtypeSp( tokenarray[i].tokval );
opnd->Ofssize = GetSflagsSp( tokenarray[i].tokval );
opnd->value = GetTypeSize( opnd->mem_type, opnd->Ofssize );
opnd->is_type = TRUE;
opnd->type = NULL; /* v2.08: added */
break;
case T_RES_ID:
DebugMsg1(("get_operand: T_RES_ID, >%s<, value=%X\n", tokenarray[i].string_ptr, tokenarray[i].tokval));
if ( tokenarray[i].tokval == T_FLAT ) {
if ( error_msg ) { /* don't define FLAT group in EQU expression! */
/* v2.08 cpu check added */
if( ( ModuleInfo.curr_cpu & P_CPU_MASK ) < P_386 ) {
EmitError( INSTRUCTION_OR_REGISTER_NOT_ACCEPTED_IN_CURRENT_CPU_MODE );
return( ERROR );
}
DefineFlatGroup();
}
if ( !( opnd->sym = &ModuleInfo.flat_grp->sym ) )
return( ERROR );
opnd->label_tok = &tokenarray[i];
opnd->kind = EXPR_ADDR;
} else {
if( error_msg )
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].string_ptr );
return( ERROR );
}
break;
case T_FLOAT: /* v2.05 */
opnd->kind = EXPR_FLOAT;
opnd->float_tok = &tokenarray[i];
//opnd->ftype = ( tokenarray[i].floattype != 0 );
break;
//case T_CL_BRACKET:
//case T_CL_SQ_BRACKET:
default:
DebugMsg1(("get_operand, default: i=%d, string=%s\n", i, tokenarray[i].string_ptr));
if ( opnd->is_opattr ) { /* for OPATTR, allow any operand */
if ( tokenarray[i].token == T_FINAL ||
tokenarray[i].token == T_CL_BRACKET ||
tokenarray[i].token == T_CL_SQ_BRACKET ) /* don't go beyond T_FINAL, ) or ] ! */
return( NOT_ERROR );
break;
}
if( error_msg ) {
if ( tokenarray[i].token == T_BAD_NUM )
/* Masm complains even if in EQU-mode */
EmitErr( NONDIGIT_IN_NUMBER, tokenarray[i].string_ptr );
else if ( tokenarray[i].token == T_COLON )
EmitError( SYNTAX_ERROR_UNEXPECTED_COLON );
else if ( is_operator( tokenarray[i].token ) ) {
EmitError( OPERAND_EXPECTED ); ERRLOC(*i);
} else
EmitErr( SYNTAX_ERROR_EX, tokenarray[i].tokpos );
}
return( ERROR );
}
(*idx)++;
DebugMsg1(("get_operand exit, ok, value=%" I64X_SPEC " hvalue=%" I64X_SPEC " mem_type=%Xh abs=%u string=%s type=>%s<\n",
opnd->llvalue, opnd->hlvalue, opnd->mem_type, opnd->abs, opnd->quoted_string ? opnd->quoted_string->string_ptr : "NULL", opnd->type ? opnd->type->name : "NULL" ));
return( NOT_ERROR );
}
#if 0
static bool check_same( struct expr *opnd1, struct expr *opnd2, enum exprtype kind )
/**********************************************************************************/
/* Check if both tok_1 and tok_2 equal type */
{
if( opnd1->kind == kind &&
opnd2->kind == kind ) {
return( TRUE );
} else {
return( FALSE );
}
}
#else
#define check_same( first, second, KIND ) (first->kind == KIND && second->kind == KIND )
#endif
static bool check_both( const struct expr *opnd1, const struct expr *opnd2, enum exprtype type1, enum exprtype type2 )
/********************************************************************************************************************/
/* Check if tok_1 == type1 and tok_2 == type2 or vice versa */
{
if( opnd1->kind == type1 && opnd2->kind == type2 )
return( TRUE );
if( opnd1->kind == type2 && opnd2->kind == type1 )
return( TRUE );
return( FALSE );
}
static ret_code index_connect( struct expr *opnd1, const struct expr *opnd2 )
/***************************************************************************/
/* Connects the register lists. called by plus_op() and dot_op() */
{
/* move opnd2.base to either opnd1.base or opnd1.idx */
if ( opnd2->base_reg != NULL ) {
if ( opnd1->base_reg == NULL )
opnd1->base_reg = opnd2->base_reg;
else if ( opnd1->idx_reg == NULL ) {
opnd1->idx_reg = opnd2->base_reg;
} else {
CEmitError( MULTIPLE_INDEX_REGISTERS_NOT_ALLOWED );
return( ERROR );
}
opnd1->indirect = 1;
}
/* move opnd2.idx to either opnd1.base or opnd1.index */
if( opnd2->idx_reg != NULL ) {
//if ( opnd2->scale == 0 && opnd1->base_reg == NULL ) {
// opnd1->base_reg = opnd2->idx_reg;
//} else if ( opnd1->idx_reg == NULL ) {
if ( opnd1->idx_reg == NULL ) {
opnd1->idx_reg = opnd2->idx_reg;
opnd1->scale = opnd2->scale;
} else {
CEmitError( MULTIPLE_INDEX_REGISTERS_NOT_ALLOWED );
return( ERROR );
}
opnd1->indirect = 1;
}
return( NOT_ERROR );
}
/* convert an address operand to a const operand if possible.
* called for '*', '/', '+', '-' operators.
*/
static void MakeConst( struct expr *opnd )
/****************************************/
{
if( opnd->kind != EXPR_ADDR )
return;
if( opnd->sym ) {
if ( Parse_Pass > PASS_1 )
return;
/* added for v1.94: if the evaluator assumed an address because
* the label wasn't defined yet, then negate this. Also, an
* EXTERNDEF:ABS is to be accepted.
* v2.07: if the "not yet defined" label was an argument of
* an (OFFSET) operator, do NOT change the type!
*/
if ( ( opnd->sym->state == SYM_UNDEFINED && opnd->instr == EMPTY ) ||
( opnd->sym->state == SYM_EXTERNAL && opnd->sym->weak == TRUE && opnd->abs == TRUE ) )
;
else
return;
/* assume a value != 0 to avoid problems with div */
opnd->value = 1;
}
opnd->label_tok = NULL;
if( opnd->mbr != NULL ) {
if( opnd->mbr->state == SYM_STRUCT_FIELD ) {
} else if( opnd->mbr->state == SYM_TYPE ) {
opnd->value += opnd->mbr->total_size;
opnd->mbr = NULL;
} else {
return;
}
}
if( opnd->base_reg != NULL )
return;
if( opnd->idx_reg != NULL )
return;
if( opnd->override != NULL )
return;
opnd->instr = EMPTY;
opnd->kind = EXPR_CONST;
opnd->indirect = FALSE;
opnd->explicit = FALSE;
opnd->mem_type = MT_EMPTY;
}
/* used by EQ, NE, GT, GE, LE, LT if item is a direct address
*/
static ret_code MakeConst2( struct expr *opnd1, struct expr *opnd2 )
/******************************************************************/
{
if ( opnd1->sym->state == SYM_EXTERNAL ) {
if ( error_msg )
EmitErr( INVALID_USE_OF_EXTERNAL_SYMBOL, opnd1->sym->name );
return( ERROR );
} else if ( ( opnd1->sym->segment != opnd2->sym->segment &&
/* v2.07: ignore segments if at least one label is a fwd ref */
opnd1->sym->state != SYM_UNDEFINED &&
opnd2->sym->state != SYM_UNDEFINED ) ||
opnd2->sym->state == SYM_EXTERNAL ) {
CEmitError( OPERANDS_MUST_BE_IN_SAME_SEGMENT );
return( ERROR );
}
opnd1->kind = EXPR_CONST;
opnd1->value += opnd1->sym->offset;
opnd2->kind = EXPR_CONST;
opnd2->value += opnd2->sym->offset;
return( NOT_ERROR );
}
static ret_code ConstError( struct expr *opnd1, struct expr *opnd2 )
/******************************************************************/
{
if ( opnd1->is_opattr )
return( NOT_ERROR );
if ( opnd1->kind == EXPR_FLOAT || opnd2->kind == EXPR_FLOAT )
CEmitError( REAL_OR_BCD_NUMBER_NOT_ALLOWED );
else
CEmitError( CONSTANT_EXPECTED );
return( ERROR );
}
/* used by + and - binary operators */
static void fix_struct_value( struct expr *opnd )
/***********************************************/
{
if( opnd->mbr && ( opnd->mbr->state == SYM_TYPE ) ) {
opnd->value += opnd->mbr->total_size;
opnd->mbr = NULL;
}
}
static int check_direct_reg( const struct expr *opnd1, const struct expr *opnd2 )
/*******************************************************************************/
{
if( ( opnd1->kind == EXPR_REG ) && ( opnd1->indirect == FALSE )
|| ( opnd2->kind == EXPR_REG ) && ( opnd2->indirect == FALSE ) ) {
return( ERROR );
}
return( NOT_ERROR );
}
static unsigned GetSizeValue( struct asym *sym )
/**********************************************/
{
if ( sym->mem_type == MT_PTR )
return( SizeFromMemtype( sym->isfar ? MT_FAR : MT_NEAR, sym->Ofssize, sym->type ) );
return( SizeFromMemtype( sym->mem_type, sym->Ofssize, sym->type ) );