-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiasm.c
4851 lines (4426 loc) · 144 KB
/
iasm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1992-1999 by Symantec
* Copyright (c) 1999-2011 by Digital Mars
* All Rights Reserved
* http://www.digitalmars.com
* http://www.dsource.org/projects/dmd/browser/branches/dmd-1.x/src/iasm.c
* http://www.dsource.org/projects/dmd/browser/trunk/src/iasm.c
* Written by Mike Cote, John Micco and Walter Bright
* D version by Walter Bright
*
* This source file is made available for personal use
* only. The license is in /dmd/src/dmd/backendlicense.txt
* For any other uses, please contact Digital Mars.
*/
// Inline assembler for the D programming language compiler
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <assert.h>
#include <setjmp.h>
#if __DMC__
#undef setjmp
#include <limits.h>
#endif
// D compiler
#include "mars.h"
#include "lexer.h"
#include "mtype.h"
#include "statement.h"
#include "id.h"
#include "declaration.h"
#include "scope.h"
#include "init.h"
#include "enum.h"
#include "module.h"
// C/C++ compiler
#define SCOPE_H 1 // avoid conflicts with D's Scope
#include "cc.h"
#include "token.h"
#include "global.h"
#include "el.h"
#include "type.h"
#include "oper.h"
#include "code.h"
#include "iasm.h"
#include "xmm.h"
// I32 isn't set correctly yet because this is the front end, and I32
// is a backend flag
#undef I16
#undef I32
#undef I64
#define I16 0
#define I32 (global.params.is64bit == 0)
#define I64 (global.params.is64bit == 1)
//#define EXTRA_DEBUG 1
#undef ADDFWAIT
#define ADDFWAIT() 0
// Error numbers
enum ASMERRMSGS
{
EM_bad_float_op,
EM_bad_addr_mode,
EM_align,
EM_opcode_exp,
EM_prefix,
EM_eol,
EM_bad_operand,
EM_bad_integral_operand,
EM_ident_exp,
EM_not_struct,
EM_nops_expected,
EM_bad_op,
EM_const_init,
EM_undefined,
EM_pointer,
EM_colon,
EM_rbra,
EM_rpar,
EM_ptr_exp,
EM_num,
EM_float,
EM_char,
EM_label_expected,
EM_uplevel,
EM_type_as_operand,
EM_invalid_64bit_opcode,
};
const char *asmerrmsgs[] =
{
"unknown operand for floating point instruction",
"bad addr mode",
"align %d must be a power of 2",
"opcode expected, not %s",
"prefix",
"end of instruction",
"bad operand",
"bad integral operand",
"identifier expected",
"not struct",
"%u operands found for %s instead of the expected %u",
"bad type/size of operands '%s'",
"constant initializer expected",
"undefined identifier '%s'",
"pointer",
"colon",
"] expected instead of '%s'",
") expected instead of '%s'",
"ptr expected",
"integer expected",
"floating point expected",
"character is truncated",
"label expected",
"uplevel nested reference to variable %s",
"cannot use type %s as an operand",
"opcode %s is unavailable in 64bit mode"
};
// Additional tokens for the inline assembler
typedef enum
{
ASMTKlocalsize = TOKMAX + 1,
ASMTKdword,
ASMTKeven,
ASMTKfar,
ASMTKnaked,
ASMTKnear,
ASMTKptr,
ASMTKqword,
ASMTKseg,
ASMTKword,
ASMTKmax = ASMTKword-(TOKMAX+1)+1
} ASMTK;
static const char *apszAsmtk[ASMTKmax] = {
"__LOCAL_SIZE",
"dword",
"even",
"far",
"naked",
"near",
"ptr",
"qword",
"seg",
"word",
};
struct ASM_STATE
{
unsigned char ucItype; // Instruction type
#define ITprefix 0x10 // special prefix
#define ITjump 0x20 // jump instructions CALL, Jxx and LOOPxx
#define ITimmed 0x30 // value of an immediate operand controls
// code generation
#define ITopt 0x40 // not all operands are required
#define ITshift 0x50 // rotate and shift instructions
#define ITfloat 0x60 // floating point coprocessor instructions
#define ITdata 0x70 // DB, DW, DD, DQ, DT pseudo-ops
#define ITaddr 0x80 // DA (define addresss) pseudo-op
#define ITMASK 0xF0
#define ITSIZE 0x0F // mask for size
Loc loc;
unsigned char bInit;
LabelDsymbol *psDollar;
Dsymbol *psLocalsize;
jmp_buf env;
unsigned char bReturnax;
AsmStatement *statement;
Scope *sc;
};
ASM_STATE asmstate;
static Token *asmtok;
static enum TOK tok_value;
//char debuga = 1;
// From ptrntab.c
const char *asm_opstr(OP *pop);
OP *asm_op_lookup(const char *s);
void init_optab();
static unsigned char asm_TKlbra_seen = FALSE;
typedef struct
{
char regstr[6];
unsigned char val;
opflag_t ty;
} REG;
static REG regFp = { "ST", 0, _st };
static REG aregFp[] = {
{ "ST(0)", 0, _sti },
{ "ST(1)", 1, _sti },
{ "ST(2)", 2, _sti },
{ "ST(3)", 3, _sti },
{ "ST(4)", 4, _sti },
{ "ST(5)", 5, _sti },
{ "ST(6)", 6, _sti },
{ "ST(7)", 7, _sti }
};
#define _AL 0
#define _AH 4
#define _AX 0
#define _EAX 0
#define _BL 3
#define _BH 7
#define _BX 3
#define _EBX 3
#define _CL 1
#define _CH 5
#define _CX 1
#define _ECX 1
#define _DL 2
#define _DH 6
#define _DX 2
#define _EDX 2
#define _BP 5
#define _EBP 5
#define _SP 4
#define _ESP 4
#define _DI 7
#define _EDI 7
#define _SI 6
#define _ESI 6
#define _ES 0
#define _CS 1
#define _SS 2
#define _DS 3
#define _GS 5
#define _FS 4
static REG regtab[] =
{
"AL", _AL, _r8 | _al,
"AH", _AH, _r8,
"AX", _AX, _r16 | _ax,
"EAX", _EAX, _r32 | _eax,
"BL", _BL, _r8,
"BH", _BH, _r8,
"BX", _BX, _r16,
"EBX", _EBX, _r32,
"CL", _CL, _r8 | _cl,
"CH", _CH, _r8,
"CX", _CX, _r16,
"ECX", _ECX, _r32,
"DL", _DL, _r8,
"DH", _DH, _r8,
"DX", _DX, _r16 | _dx,
"EDX", _EDX, _r32,
"BP", _BP, _r16,
"EBP", _EBP, _r32,
"SP", _SP, _r16,
"ESP", _ESP, _r32,
"DI", _DI, _r16,
"EDI", _EDI, _r32,
"SI", _SI, _r16,
"ESI", _ESI, _r32,
"ES", _ES, _seg | _es,
"CS", _CS, _seg | _cs,
"SS", _SS, _seg | _ss ,
"DS", _DS, _seg | _ds,
"GS", _GS, _seg | _gs,
"FS", _FS, _seg | _fs,
"CR0", 0, _special | _crn,
"CR2", 2, _special | _crn,
"CR3", 3, _special | _crn,
"CR4", 4, _special | _crn,
"DR0", 0, _special | _drn,
"DR1", 1, _special | _drn,
"DR2", 2, _special | _drn,
"DR3", 3, _special | _drn,
"DR4", 4, _special | _drn,
"DR5", 5, _special | _drn,
"DR6", 6, _special | _drn,
"DR7", 7, _special | _drn,
"TR3", 3, _special | _trn,
"TR4", 4, _special | _trn,
"TR5", 5, _special | _trn,
"TR6", 6, _special | _trn,
"TR7", 7, _special | _trn,
"MM0", 0, _mm,
"MM1", 1, _mm,
"MM2", 2, _mm,
"MM3", 3, _mm,
"MM4", 4, _mm,
"MM5", 5, _mm,
"MM6", 6, _mm,
"MM7", 7, _mm,
"XMM0", 0, _xmm | _xmm0,
"XMM1", 1, _xmm,
"XMM2", 2, _xmm,
"XMM3", 3, _xmm,
"XMM4", 4, _xmm,
"XMM5", 5, _xmm,
"XMM6", 6, _xmm,
"XMM7", 7, _xmm,
};
// 64 bit only registers
#define _RAX 0
#define _RBX 3
#define _RCX 1
#define _RDX 2
#define _RSI 6
#define _RDI 7
#define _RBP 5
#define _RSP 4
#define _R8 8
#define _R9 9
#define _R10 10
#define _R11 11
#define _R12 12
#define _R13 13
#define _R14 14
#define _R15 15
#define _R8D 8
#define _R9D 9
#define _R10D 10
#define _R11D 11
#define _R12D 12
#define _R13D 13
#define _R14D 14
#define _R15D 15
#define _R8W 8
#define _R9W 9
#define _R10W 10
#define _R11W 11
#define _R12W 12
#define _R13W 13
#define _R14W 13
#define _R15W 15
#define _SIL 6
#define _DIL 7
#define _BPL 5
#define _SPL 4
#define _R8B 8
#define _R9B 9
#define _R10B 10
#define _R11B 11
#define _R12B 12
#define _R13B 13
#define _R14B 14
#define _R15B 15
static REG regtab64[] =
{
"RAX", _RAX, _r64 | _rax,
"RBX", _RBX, _r64,
"RCX", _RCX, _r64,
"RDX", _RDX, _r64,
"RSI", _RSI, _r64,
"RDI", _RDI, _r64,
"RBP", _RBP, _r64,
"RSP", _RSP, _r64,
"R8", _R8, _r64,
"R9", _R9, _r64,
"R10", _R10, _r64,
"R11", _R11, _r64,
"R12", _R12, _r64,
"R13", _R13, _r64,
"R14", _R14, _r64,
"R15", _R15, _r64,
"R8D", _R8D, _r32,
"R9D", _R9D, _r32,
"R10D", _R10D, _r32,
"R11D", _R11D, _r32,
"R12D", _R12D, _r32,
"R13D", _R13D, _r32,
"R14D", _R14D, _r32,
"R15D", _R15D, _r32,
"R8W", _R8W, _r16,
"R9W", _R9W, _r16,
"R10W", _R10W, _r16,
"R11W", _R11W, _r16,
"R12W", _R12W, _r16,
"R13W", _R13W, _r16,
"R14W", _R14W, _r16,
"R15W", _R15W, _r16,
"SIL", _SIL, _r8,
"DIL", _DIL, _r8,
"BPL", _BPL, _r8,
"SPL", _SPL, _r8,
"R8B", _R8B, _r8,
"R9B", _R9B, _r8,
"R10B", _R10B, _r8,
"R11B", _R11B, _r8,
"R12B", _R12B, _r8,
"R13B", _R13B, _r8,
"R14B", _R14B, _r8,
"R15B", _R15B, _r8,
"XMM8", 8, _xmm,
"XMM9", 9, _xmm,
"XMM10", 10, _xmm,
"XMM11", 11, _xmm,
"XMM12", 12, _xmm,
"XMM13", 13, _xmm,
"XMM14", 14, _xmm,
"XMM15", 15, _xmm,
"YMM0", 0, _ymm,
"YMM1", 1, _ymm,
"YMM2", 2, _ymm,
"YMM3", 3, _ymm,
"YMM4", 4, _ymm,
"YMM5", 5, _ymm,
"YMM6", 6, _ymm,
"YMM7", 7, _ymm,
"YMM8", 8, _ymm,
"YMM9", 9, _ymm,
"YMM10", 10, _ymm,
"YMM11", 11, _ymm,
"YMM12", 12, _ymm,
"YMM13", 13, _ymm,
"YMM14", 14, _ymm,
"YMM15", 15, _ymm,
};
typedef enum {
ASM_JUMPTYPE_UNSPECIFIED,
ASM_JUMPTYPE_SHORT,
ASM_JUMPTYPE_NEAR,
ASM_JUMPTYPE_FAR
} ASM_JUMPTYPE; // ajt
typedef struct opnd
{
REG *base; // if plain register
REG *pregDisp1; // if [register1]
REG *pregDisp2;
REG *segreg; // if segment override
char indirect; // if had a '*' or '->'
char bOffset; // if 'offset' keyword
char bSeg; // if 'segment' keyword
char bPtr; // if 'ptr' keyword
unsigned uchMultiplier; // register multiplier; valid values are 0,1,2,4,8
opflag_t usFlags;
Dsymbol *s;
targ_llong disp;
long double real;
Type *ptype;
ASM_JUMPTYPE ajt;
} OPND;
//
// Exported functions called from the compiler
//
int asm_state(int iFlags);
void iasm_term();
//
// Local functions defined and only used here
//
STATIC OPND *asm_add_exp();
STATIC OPND *opnd_calloc();
STATIC void opnd_free(OPND *popnd);
STATIC OPND *asm_and_exp();
STATIC OPND *asm_cond_exp();
STATIC opflag_t asm_determine_operand_flags(OPND *popnd);
code *asm_genloc(Loc loc, code *c);
int asm_getnum();
STATIC void asmerr(const char *, ...);
#if __clang__
STATIC void asmerr(int, ...) __attribute__((analyzer_noreturn));
#else
STATIC void asmerr(int, ...);
#if __DMC__
#pragma SC noreturn(asmerr)
#endif
#endif
STATIC OPND *asm_equal_exp();
STATIC OPND *asm_inc_or_exp();
STATIC OPND *asm_log_and_exp();
STATIC OPND *asm_log_or_exp();
STATIC char asm_length_type_size(OPND *popnd);
STATIC void asm_token();
STATIC void asm_token_trans(Token *tok);
STATIC unsigned char asm_match_flags(opflag_t usOp , opflag_t usTable );
STATIC unsigned char asm_match_float_flags(opflag_t usOp, opflag_t usTable);
STATIC void asm_make_modrm_byte(
#ifdef DEBUG
unsigned char *puchOpcode, unsigned *pusIdx,
#endif
code *pc,
unsigned usFlags,
OPND *popnd, OPND *popnd2);
STATIC regm_t asm_modify_regs(PTRNTAB ptb, OPND *popnd1, OPND *popnd2);
STATIC void asm_output_flags(opflag_t usFlags);
STATIC void asm_output_popnd(OPND *popnd);
STATIC unsigned asm_type_size(Type * ptype);
STATIC opflag_t asm_float_type_size(Type * ptype, opflag_t *pusFloat);
STATIC OPND *asm_mul_exp();
STATIC OPND *asm_br_exp();
STATIC OPND *asm_primary_exp();
STATIC OPND *asm_prim_post(OPND *);
STATIC OPND *asm_rel_exp();
STATIC OPND *asm_shift_exp();
STATIC OPND *asm_una_exp();
STATIC OPND *asm_xor_exp();
STATIC void *link_alloc(size_t, void *);
STATIC void asm_chktok(enum TOK toknum, unsigned errnum);
STATIC code *asm_db_parse(OP *pop);
STATIC code *asm_da_parse(OP *pop);
unsigned compute_hashkey(char *);
/*******************************
*/
STATIC OPND *opnd_calloc()
{ OPND *o;
o = new OPND();
memset(o, 0, sizeof(*o));
return o;
}
/*******************************
*/
STATIC void opnd_free(OPND *o)
{
if (o)
{
delete o;
}
}
/*******************************
*/
STATIC void asm_chktok(enum TOK toknum,unsigned errnum)
{
if (tok_value == toknum)
asm_token(); // scan past token
else
/* When we run out of tokens, asmtok is NULL.
* But when this happens when a ';' was hit.
*/
asmerr(errnum, asmtok ? asmtok->toChars() : ";");
}
/*******************************
*/
STATIC PTRNTAB asm_classify(OP *pop, OPND *popnd1, OPND *popnd2,
OPND *popnd3, OPND *popnd4, unsigned *pusNumops)
{
unsigned usNumops;
unsigned usActual;
PTRNTAB ptbRet = { NULL };
opflag_t opflags1 = 0 ;
opflag_t opflags2 = 0;
opflag_t opflags3 = 0;
opflag_t opflags4 = 0;
char bFake = FALSE;
char bInvalid64bit = FALSE;
unsigned char bMatch1, bMatch2, bMatch3, bMatch4, bRetry = FALSE;
// How many arguments are there? the parser is strictly left to right
// so this should work.
if (!popnd1)
usNumops = 0;
else
{
popnd1->usFlags = opflags1 = asm_determine_operand_flags(popnd1);
if (!popnd2)
usNumops = 1;
else
{
popnd2->usFlags = opflags2 = asm_determine_operand_flags(popnd2);
if (!popnd3)
usNumops = 2;
else
{
popnd3->usFlags = opflags3 = asm_determine_operand_flags(popnd3);
if (!popnd4)
usNumops = 3;
else
{
popnd4->usFlags = opflags4 = asm_determine_operand_flags(popnd4);
usNumops = 4;
}
}
}
}
// Now check to insure that the number of operands is correct
usActual = (pop->usNumops & ITSIZE);
if (usActual != usNumops && asmstate.ucItype != ITopt &&
asmstate.ucItype != ITfloat)
{
PARAM_ERROR:
asmerr(EM_nops_expected, usNumops, asm_opstr(pop), usActual);
}
if (usActual < usNumops)
*pusNumops = usActual;
else
*pusNumops = usNumops;
//
// The number of arguments matches, now check to find the opcode
// in the associated opcode table
//
RETRY:
//printf("usActual = %d\n", usActual);
switch (usActual)
{
case 0:
if (I64 && (pop->ptb.pptb0->usFlags & _i64_bit))
asmerr( EM_invalid_64bit_opcode, asm_opstr(pop)); // illegal opcode in 64bit mode
ptbRet = pop->ptb;
goto RETURN_IT;
case 1:
{ //printf("opflags1 = "); asm_output_flags(opflags1); printf("\n");
PTRNTAB1 *table1;
for (table1 = pop->ptb.pptb1; table1->usOpcode != ASM_END;
table1++)
{
//printf("table = "); asm_output_flags(table1->usOp1); printf("\n");
bMatch1 = asm_match_flags(opflags1, table1->usOp1);
//printf("bMatch1 = x%x\n", bMatch1);
if (bMatch1)
{ if (table1->usOpcode == 0x68 &&
!I16 &&
table1->usOp1 == _imm16
)
// Don't match PUSH imm16 in 32 bit code
continue;
// Check if match is invalid in 64bit mode
if (I64 && (table1->usFlags & _i64_bit))
{
bInvalid64bit = TRUE;
continue;
}
break;
}
if ((asmstate.ucItype == ITimmed) &&
asm_match_flags(opflags1,
CONSTRUCT_FLAGS(_8 | _16 | _32, _imm, _normal,
0)) &&
popnd1->disp == table1->usFlags)
break;
if ((asmstate.ucItype == ITopt ||
asmstate.ucItype == ITfloat) &&
!usNumops &&
!table1->usOp1)
{
if (usNumops > 1)
goto PARAM_ERROR;
break;
}
}
if (table1->usOpcode == ASM_END)
{
#ifdef DEBUG
if (debuga)
{ printf("\t%s\t", asm_opstr(pop));
if (popnd1)
asm_output_popnd(popnd1);
if (popnd2) {
printf(",");
asm_output_popnd(popnd2);
}
if (popnd3) {
printf(",");
asm_output_popnd(popnd3);
}
printf("\n");
printf("OPCODE mism = ");
if (popnd1)
asm_output_flags(popnd1->usFlags);
else
printf("NONE");
printf("\n");
}
#endif
TYPE_SIZE_ERROR:
if (popnd1 && ASM_GET_aopty(popnd1->usFlags) != _reg)
{
opflags1 = popnd1->usFlags |= _anysize;
if (asmstate.ucItype == ITjump)
{
if (bRetry && popnd1->s && !popnd1->s->isLabel())
{
asmerr(EM_label_expected, popnd1->s->toChars());
}
popnd1->usFlags |= CONSTRUCT_FLAGS(0, 0, 0,
_fanysize);
}
}
if (popnd2 && ASM_GET_aopty(popnd2->usFlags) != _reg) {
opflags2 = popnd2->usFlags |= (_anysize);
if (asmstate.ucItype == ITjump)
popnd2->usFlags |= CONSTRUCT_FLAGS(0, 0, 0,
_fanysize);
}
if (popnd3 && ASM_GET_aopty(popnd3->usFlags) != _reg) {
opflags3 = popnd3->usFlags |= (_anysize);
if (asmstate.ucItype == ITjump)
popnd3->usFlags |= CONSTRUCT_FLAGS(0, 0, 0,
_fanysize);
}
if (bRetry)
{
if(bInvalid64bit)
asmerr("operand for '%s' invalid in 64bit mode", asm_opstr(pop));
else
asmerr(EM_bad_op, asm_opstr(pop)); // illegal type/size of operands
}
bRetry = TRUE;
goto RETRY;
}
ptbRet.pptb1 = table1;
goto RETURN_IT;
}
case 2:
{ //printf("opflags1 = "); asm_output_flags(opflags1); printf(" ");
//printf("opflags2 = "); asm_output_flags(opflags2); printf("\n");
PTRNTAB2 *table2;
for (table2 = pop->ptb.pptb2;
table2->usOpcode != ASM_END;
table2++)
{
//printf("table1 = "); asm_output_flags(table2->usOp1); printf(" ");
//printf("table2 = "); asm_output_flags(table2->usOp2); printf("\n");
if (I64 && (table2->usFlags & _i64_bit))
asmerr( EM_invalid_64bit_opcode, asm_opstr(pop));
bMatch1 = asm_match_flags(opflags1, table2->usOp1);
bMatch2 = asm_match_flags(opflags2, table2->usOp2);
//printf("match1 = %d, match2 = %d\n",bMatch1,bMatch2);
if (bMatch1 && bMatch2) {
//printf("match\n");
/* If they both match and the first op in the table is not AL
* or size of 8 and the second is immediate 8,
* then check to see if the constant
* is a signed 8 bit constant. If so, then do not match, otherwise match
*/
if (!bRetry &&
!((ASM_GET_uSizemask(table2->usOp1) & _8) ||
(ASM_GET_uRegmask(table2->usOp1) & _al)) &&
(ASM_GET_aopty(table2->usOp2) == _imm) &&
(ASM_GET_uSizemask(table2->usOp2) & _8))
{
if (popnd2->disp <= SCHAR_MAX)
break;
else
bFake = TRUE;
}
else
break;
}
if (asmstate.ucItype == ITopt ||
asmstate.ucItype == ITfloat)
{
switch (usNumops)
{
case 0:
if (!table2->usOp1)
goto Lfound2;
break;
case 1:
if (bMatch1 && !table2->usOp2)
goto Lfound2;
break;
case 2:
break;
default:
goto PARAM_ERROR;
}
}
#if 0
if (asmstate.ucItype == ITshift &&
!table2->usOp2 &&
bMatch1 && popnd2->disp == 1 &&
asm_match_flags(opflags2,
CONSTRUCT_FLAGS(_8|_16|_32, _imm,_normal,0))
)
break;
#endif
}
Lfound2:
if (table2->usOpcode == ASM_END)
{
#ifdef DEBUG
if (debuga)
{ printf("\t%s\t", asm_opstr(pop));
if (popnd1)
asm_output_popnd(popnd1);
if (popnd2) {
printf(",");
asm_output_popnd(popnd2);
}
if (popnd3) {
printf(",");
asm_output_popnd(popnd3);
}
printf("\n");
printf("OPCODE mismatch = ");
if (popnd1)
asm_output_flags(popnd1->usFlags);
else
printf("NONE");
printf( " Op2 = ");
if (popnd2)
asm_output_flags(popnd2->usFlags);
else
printf("NONE");
printf("\n");
}
#endif
goto TYPE_SIZE_ERROR;
}
ptbRet.pptb2 = table2;
goto RETURN_IT;
}
case 3:
{
PTRNTAB3 *table3;
for (table3 = pop->ptb.pptb3;
table3->usOpcode != ASM_END;
table3++)
{
bMatch1 = asm_match_flags(opflags1, table3->usOp1);
bMatch2 = asm_match_flags(opflags2, table3->usOp2);
bMatch3 = asm_match_flags(opflags3, table3->usOp3);
if (bMatch1 && bMatch2 && bMatch3)
goto Lfound3;
if (asmstate.ucItype == ITopt)
{
switch (usNumops)
{
case 0:
if (!table3->usOp1)
goto Lfound3;
break;
case 1:
if (bMatch1 && !table3->usOp2)
goto Lfound3;
break;
case 2:
if (bMatch1 && bMatch2 && !table3->usOp3)
goto Lfound3;
break;
case 3:
break;
default:
goto PARAM_ERROR;
}
}
}
Lfound3:
if (table3->usOpcode == ASM_END)
{
#ifdef DEBUG
if (debuga)
{ printf("\t%s\t", asm_opstr(pop));
if (popnd1)
asm_output_popnd(popnd1);
if (popnd2) {
printf(",");
asm_output_popnd(popnd2);
}
if (popnd3) {
printf(",");
asm_output_popnd(popnd3);
}
printf("\n");
printf("OPCODE mismatch = ");
if (popnd1)
asm_output_flags(popnd1->usFlags);
else
printf("NONE");
printf( " Op2 = ");
if (popnd2)
asm_output_flags(popnd2->usFlags);
else
printf("NONE");
if (popnd3)
asm_output_flags(popnd3->usFlags);
printf("\n");
}
#endif
goto TYPE_SIZE_ERROR;
}
ptbRet.pptb3 = table3;
goto RETURN_IT;
}
case 4:
{
PTRNTAB4 *table4;
for (table4 = pop->ptb.pptb4;
table4->usOpcode != ASM_END;
table4++)
{
bMatch1 = asm_match_flags(opflags1, table4->usOp1);
bMatch2 = asm_match_flags(opflags2, table4->usOp2);
bMatch3 = asm_match_flags(opflags3, table4->usOp3);
bMatch4 = asm_match_flags(opflags4, table4->usOp4);
if (bMatch1 && bMatch2 && bMatch3 && bMatch4)
goto Lfound4;
if (asmstate.ucItype == ITopt)
{
switch (usNumops)
{
case 0:
if (!table4->usOp1)
goto Lfound3;
break;
case 1:
if (bMatch1 && !table4->usOp2)
goto Lfound3;
break;
case 2:
if (bMatch1 && bMatch2 && !table4->usOp3)
goto Lfound3;
break;
case 3:
if (bMatch1 && bMatch2 && bMatch3 && !table4->usOp4)
goto Lfound3;
break;
case 4:
break;
default:
goto PARAM_ERROR;
}
}
}
Lfound4:
if (table4->usOpcode == ASM_END)
{
#ifdef DEBUG
if (debuga)
{ printf("\t%s\t", asm_opstr(pop));
if (popnd1)
asm_output_popnd(popnd1);
if (popnd2) {
printf(",");
asm_output_popnd(popnd2);
}
if (popnd3) {
printf(",");
asm_output_popnd(popnd3);
}
if (popnd4) {
printf(",");
asm_output_popnd(popnd4);
}
printf("\n");
printf("OPCODE mismatch = ");
if (popnd1)
asm_output_flags(popnd1->usFlags);
else
printf("NONE");
printf( " Op2 = ");
if (popnd2)
asm_output_flags(popnd2->usFlags);