forked from dlang/dmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bc.d
2950 lines (2626 loc) · 89.7 KB
/
bc.d
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
module ddmd.ctfe.bc;
import ddmd.ctfe.bc_common;
import ddmd.ctfe.bc_limits;
import core.stdc.stdio;
import std.conv;
/**
* Written By Stefan Koch in 2016/17
*/
debug = 0;
enum InstKind
{
ShortInst,
CndJumpInst,
LongInst2Stack,
LongInstImm32,
StackInst,
}
/+ We don't need this right now ... maybe later
auto instKind(LongInst i)
{
final switch (i)
{
case /*LongInst.Prt,*/ LongInst.RelJmp, LongInst.Ret, LongInstc,
LongInst.Flg, LongInst.Mod4:
{
return InstKind.ShortInst;
}
case LongInst.Jmp, LongInst.JmpFalse, LongInst.JmpTrue, LongInst.JmpZ, LongInst.JmpNZ:
{
return InstKind.CndJumpInst;
}
case LongInst.Add, LongInst.Sub, LongInst.Div, LongInst.Mul, LongInst.Eq, LongInst.Neq,
LongInst.Lt, LongInst.Le, LongInst.Gt, LongInst.Ge, LongInst.Set, LongInst.And, LongInst.Or,
LongInst.Xor, LongInst.Lsh, LongInst.Rsh, LongInst.Mod, // loadOps begin
LongInst.HeapLoad32, LongInst.HeapStore32, LongInst.ExB, LongInst.Alloc:
{
return InstKind.LongInst2Stack;
}
case LongInst.ImmAdd, LongInst.ImmSub, LongInst.ImmDiv, LongInst.ImmMul,
LongInst.ImmEq, LongInst.ImmNeq, LongInst.ImmLt, LongInst.ImmLe, LongInst.ImmGt, LongInst.ImmGe, LongInst.ImmSet,
LongInst.ImmAnd, LongInst.ImmOr, LongInst.ImmXor, LongInst.ImmLsh,
LongInst.ImmRsh, LongInst.ImmMod, LongInst.Call, LongInst.BuiltinCall,
LongInst.SetHighImm:
{
return InstKind.LongInstImm32;
}
}
} +/
struct RetainedCall
{
import ddmd.globals : Loc;
BCValue fn;
BCValue[] args;
uint callerId;
BCAddr callerIp;
StackAddr callerSp;
Loc loc;
}
enum LongInst : ushort
{
//Former ShortInst
//Prt,
RelJmp,
Ret32,
Ret64,
Not,
Flg, // writes the conditionFlag into [lw >> 16]
//End Former ShortInst
Jmp,
JmpFalse,
JmpTrue,
JmpZ,
JmpNZ,
// 2 StackOperands
Add,
Sub,
Div,
Mul,
Mod,
Eq, //sets condflags
Neq, //sets condflag
Lt, //sets condflags
Le,
Gt, //sets condflags
Ge,
Set,
And,
And32,
Or,
Xor,
Xor32,
Lsh,
Rsh,
StrEq,
Assert,
// Immedate operand
ImmAdd,
ImmSub,
ImmDiv,
ImmMul,
ImmMod,
ImmEq,
ImmNeq,
ImmLt,
ImmLe,
ImmGt,
ImmGe,
ImmSet,
ImmAnd,
ImmAnd32,
ImmOr,
ImmXor,
ImmXor32,
ImmLsh,
ImmRsh,
FAdd32,
FSub32,
FDiv32,
FMul32,
FMod32,
FEq32,
FNeq32,
FLt32,
FLe32,
FGt32,
FGe32,
F32ToF64,
F32ToI,
IToF32,
FAdd64,
FSub64,
FDiv64,
FMul64,
FMod64,
FEq64,
FNeq64,
FLt64,
FLe64,
FGt64,
FGe64,
F64ToF32,
F64ToI,
IToF64,
SetHighImm,
Call,
HeapLoad32, ///SP[hi & 0xFFFF] = Heap[align4(SP[hi >> 16])]
HeapStore32, ///Heap[align4(SP[hi & 0xFFFF)] = SP[hi >> 16]]
HeapLoad64,
HeapStore64,
Alloc, /// SP[hi & 0xFFFF] = heapSize; heapSize += SP[hi >> 16]
MemCpy,
BuiltinCall, // call a builtin.
Comment,
Line,
}
//Imm-Intructuins and corrospinding 2Operand instructions have to be in the same order
static immutable bc_order_errors = () {
string result;
auto members = [__traits(allMembers, LongInst)];
auto d1 = LongInst.ImmAdd - LongInst.Add;
auto d2 = LongInst.ImmMod - LongInst.Mod;
if (d1 != d2)
{
result ~= "mismatch between ImmAdd - Add and ImmMod-Mod\nThis indicates Imm insts that do not corrospond to 2stack insts";
}
foreach (i, member; members)
{
if (member.length > 3 && member[0 .. 3] == "Imm" && members[i - d1] != member[3 .. $])
{
result ~= "\nError: " ~ member ~ " should match to: " ~ member[3 .. $]
~ "; but it matches to: " ~ members[i - d1];
}
}
return result;
} ();
static assert(!bc_order_errors, bc_order_errors);
pragma(msg, 2 ^^ 7 - LongInst.max, " opcodes remaining");
static assert(LongInst.ImmAdd - LongInst.Add == LongInst.ImmRsh - LongInst.Rsh);
static assert(LongInst.ImmAnd - LongInst.And == LongInst.ImmMod - LongInst.Mod);
enum InstMask = ubyte(0x7F); // mask for bit 0-6
//enum CondFlagMask = ~ushort(0x2FF); // mask for 8-10th bit
enum CondFlagMask = 0b11_0000_0000;
/** 2StackInst Layout :
* [0-6] Instruction
* [6-8] Flags
* -----------------
* [8-12] CondFlag (or Padding)
* [12-32] Padding
* [32-48] StackOffset (lhs)
* [48-64] StackOffset (rhs)
* *************************
* ImmInstructions Layout :
* [0-7] Instruction
* [7-8] Flags
* ------------------------
* [8-12] CondFlag (or Padding)
* [12-16] Padding
* [16-32] StackOffset (lhs)
* [32-64] Imm32 (rhs)
*/
struct LongInst64
{
uint lw;
uint hi;
@safe pure const nothrow:
this(const LongInst i, const BCAddr targetAddr)
{
lw = i;
hi = targetAddr.addr;
}
this(const LongInst i, const StackAddr stackAddrLhs, const BCAddr targetAddr)
{
lw = i | stackAddrLhs.addr << 16;
hi = targetAddr.addr;
}
this(const LongInst i, const StackAddr stackAddrLhs,
const StackAddr stackAddrRhs)
{
lw = i;
hi = stackAddrLhs.addr | stackAddrRhs.addr << 16;
}
this(const LongInst i, const StackAddr stackAddrLhs, const Imm32 rhs)
{
lw = i | stackAddrLhs.addr << 16;
hi = rhs.imm32;
}
this(const LongInst i, const StackAddr stackAddrLhs,
const StackAddr stackAddrRhs, StackAddr stackAddrOp)
{
lw = i | stackAddrOp.addr << 16;
hi = stackAddrLhs.addr | stackAddrRhs.addr << 16;
}
}
static assert(LongInst.max < 0x7F, "Instruction do not fit in 7 bit anymore");
static short isShortJump(const int offset) pure @safe
{
assert(offset != 0, "An Jump to the Jump itself is invalid");
const bool wasNegative = (offset < 0);
int abs_offset = wasNegative ? offset * -1 : offset;
if (abs_offset < (1 << 15))
{
return (cast(ushort)(wasNegative ? abs_offset *= -1 : abs_offset));
}
else
{
return 0;
}
}
auto ShortInst16(const LongInst i, const int _imm) pure @safe
{
short imm = cast(short) _imm;
return i | imm << 16;
}
auto ShortInst16Ex(const LongInst i, ubyte ex, const short imm) pure @safe
{
return i | ex << 8 | imm << 16;
}
enum BCFunctionTypeEnum : byte
{
undef,
Builtin,
Bytecode,
Compiled,
}
//static if (is(typeof(() { import ddmd.declaration : FuncDeclaration; })))
//{
// import ddmd.declaration : FuncDeclaration;
// alias FT = FuncDeclaration;
//}
//else
//{
// alias FT = void*;
//}
struct BCFunction
{
void* funcDecl;
uint fn;
BCFunctionTypeEnum type;
ushort nArgs;
ushort maxStackUsed;
immutable(int)[] byteCode;
// this(void* fd, BCFunctionTypeEnum type, int nr, const int[] byteCode, uint nArgs) pure
// {
// this.funcDecl = fd;
// this.nr = nr;
// this.type = BCFunctionTypeEnum.Builtin;
// this.byteCode = cast(immutable(int)[]) byteCode;
// this.nArgs = nArgs;
// }
//
// this(int nr, BCValue function(const BCValue[] arguments, uint[] heapPtr) _fBuiltin,
// uint nArgs) pure
// {
// this.nr = nr;
// this.type = BCFunctionTypeEnum.Builtin;
// this._fBuiltin = _fBuiltin;
// this.nArgs = nArgs;
// }
//
}
struct BCGen
{
int[ushort.max] byteCodeArray;
/// ip starts at 4 because 0 should be an invalid address;
BCAddr ip = BCAddr(4);
StackAddr sp = StackAddr(4);
ubyte parameterCount;
ushort localCount;
ushort temporaryCount;
uint functionId;
void* fd;
bool insideFunction;
BCLocal[256] locals;
RetainedCall[ubyte.max * 6] calls;
uint callCount;
auto interpret(BCValue[] args, BCHeap* heapPtr = null) const
{
return interpret_(cast(const) byteCodeArray[0 .. ip], args, heapPtr, null);
}
@safe:
string[ushort] stackMap()
{
string[ushort] result;
foreach(local;locals[0 .. localCount])
{
result[local.addr] = local.name;
}
return result;
}
void beginFunction(uint fnId = 0, void* fd = null)
{
import ddmd.declaration : FuncDeclaration;
import std.string;
ip = BCAddr(4);
//() @trusted { assert(!insideFunction, fd ? (cast(FuncDeclaration)fd).toChars.fromStringz : "fd:null"); } ();
//TODO figure out why the above assert cannot always be true ... see issue 7667
insideFunction = true;
functionId = fnId;
}
pure:
/// The emitLongInst functions have be kept up to date if
/// LongInst64 is changed.
void emitLongInst(const LongInst i, const BCAddr targetAddr)
{
byteCodeArray[ip] = i;
byteCodeArray[ip + 1] = targetAddr.addr;
ip += 2;
}
void emitLongInst(const LongInst i, const StackAddr stackAddrLhs, const BCAddr targetAddr)
{
byteCodeArray[ip] = i | stackAddrLhs.addr << 16;
byteCodeArray[ip + 1] = targetAddr.addr;
ip += 2;
}
void emitLongInst(const LongInst i, const StackAddr stackAddrLhs,
const StackAddr stackAddrRhs)
{
byteCodeArray[ip] = i;
byteCodeArray[ip + 1] = stackAddrLhs.addr | stackAddrRhs.addr << 16;
ip += 2;
}
void emitLongInst(const LongInst i, const StackAddr stackAddrLhs, const Imm32 rhs)
{
byteCodeArray[ip] = i | stackAddrLhs.addr << 16;
byteCodeArray[ip + 1] = rhs.imm32;
ip += 2;
}
void emitLongInst(const LongInst i, const StackAddr stackAddrLhs,
const StackAddr stackAddrRhs, StackAddr stackAddrOp)
{
byteCodeArray[ip] = i | stackAddrOp.addr << 16;
byteCodeArray[ip + 1] = stackAddrLhs.addr | stackAddrRhs.addr << 16;
ip += 2;
}
BCValue genTemporary(BCType bct)
{
auto tmpAddr = sp.addr;
if (isBasicBCType(bct))
{
sp += align4(basicTypeSize(bct.type));
}
else
{
sp += 4;
}
return BCValue(StackAddr(tmpAddr), bct, ++temporaryCount);
}
extern (D) BCValue genLocal(BCType bct, string name)
{
auto localAddr = sp.addr;
ushort localIdx = ++localCount;
if (isBasicBCType(bct))
{
sp += align4(basicTypeSize(bct.type));
}
else
{
sp += 4;
}
string localName = name ? name : null;
locals[localIdx - 1] = BCLocal(localIdx, bct, StackAddr(localAddr), localName);
return BCValue(StackAddr(localAddr), bct, localIdx, localName);
}
void Initialize()
{
callCount = 0;
parameterCount = 0;
temporaryCount = 0;
localCount = 0;
byteCodeArray[0] = 0;
byteCodeArray[1] = 0;
byteCodeArray[2] = 0;
byteCodeArray[3] = 0;
ip = BCAddr(4);
sp = StackAddr(4);
}
void Finalize()
{
callCount = 0;
//the [ip-1] may be wrong in some cases ?
/* byteCodeArray[ip - 1] = 0;
byteCodeArray[ip] = 0;
byteCodeArray[ip + 1] = 0;
*/
}
BCFunction endFunction()
{
//assert(insideFunction);
//I have no idea how this can fail ...
insideFunction = false;
BCFunction result;
result.type = BCFunctionTypeEnum.Bytecode;
result.maxStackUsed = sp;
result.fn = functionId;
{
// MUTEX BEGIN
// result.byteCode = byteCodeArray[4 .. ip];
// MUTEX END
}
sp = StackAddr(4);
return result;
}
BCValue genParameter(BCType bct, string name = null)
{
auto p = BCValue(BCParameter(++parameterCount, bct, sp));
p.name = name;
sp += 4;
return p;
}
BCAddr beginJmp()
{
BCAddr atIp = ip;
ip += 2;
return atIp;
}
void incSp()
{
sp += 4;
}
StackAddr currSp()
{
return sp;
}
void endJmp(BCAddr atIp, BCLabel target)
{
auto offset = isShortJump(target.addr - atIp);
if (offset)
{
byteCodeArray[atIp] = ShortInst16(LongInst.RelJmp, offset);
}
else
{
byteCodeArray[atIp] = LongInst.Jmp;
byteCodeArray[atIp + 1] = target.addr;
}
}
BCLabel genLabel()
{
return BCLabel(ip);
}
CndJmpBegin beginCndJmp(BCValue cond = BCValue.init, bool ifTrue = false)
{
auto result = CndJmpBegin(ip, cond, ifTrue);
ip += 2;
return result;
}
void endCndJmp(CndJmpBegin jmp, BCLabel target)
{
auto atIp = jmp.at;
auto cond = jmp.cond;
auto ifTrue = jmp.ifTrue;
LongInst64 lj;
if (isStackValueOrParameter(cond) && cond.type.type == BCTypeEnum.i32)
{
lj = (ifTrue ? LongInst64(LongInst.JmpNZ, cond.stackAddr,
target.addr) : LongInst64(LongInst.JmpZ, cond.stackAddr, target.addr));
}
else
{
lj = (ifTrue ? LongInst64(LongInst.JmpTrue,
target.addr) : LongInst64(LongInst.JmpFalse, target.addr));
}
byteCodeArray[atIp] = lj.lw;
byteCodeArray[atIp + 1] = lj.hi;
}
void genJump(BCLabel target)
{
assert(target.addr);
if (ip != target.addr)
{
auto at = beginJmp();
endJmp(at, target);
}
}
void emitFlg(BCValue lhs)
{
assert(isStackValueOrParameter(lhs), "Can only store flags in Stack Values");
byteCodeArray[ip] = ShortInst16(LongInst.Flg, lhs.stackAddr.addr);
byteCodeArray[ip + 1] = 0;
ip += 2;
}
void Alloc(BCValue heapPtr, BCValue size)
{
assert(size.type.type == BCTypeEnum.i32, "Size for alloc needs to be an i32");
if (size.vType == BCValueType.Immediate)
{
size = pushOntoStack(size);
}
assert(isStackValueOrParameter(size));
assert(isStackValueOrParameter(heapPtr));
emitLongInst(LongInst.Alloc, heapPtr.stackAddr, size.stackAddr);
}
void Assert(BCValue value, BCValue err)
{
BCValue _msg;
if (isStackValueOrParameter(err))
{
assert(0, "err.vType is not Error but: " ~ err.vType.to!string);
}
if (value)
{
emitLongInst(LongInst.Assert, pushOntoStack(value).stackAddr, err.imm32);
}
else
{
assert(0, "BCValue.init is no longer a valid value for assert");
}
}
void MemCpy(BCValue dst, BCValue src, BCValue size)
{
size = pushOntoStack(size);
src = pushOntoStack(src);
dst = pushOntoStack(dst);
emitLongInst(LongInst.MemCpy, dst.stackAddr, src.stackAddr, size.stackAddr);
}
void Line(uint line)
{
emitLongInst(LongInst.Line, StackAddr(0), Imm32(line));
}
void Comment(string comment)
{
debug
{
uint alignedLength = align4(cast(uint) comment.length) / 4;
uint commentLength = cast(uint) comment.length;
emitLongInst(LongInst.Comment, StackAddr.init, Imm32(commentLength));
uint idx;
while(commentLength >= 4)
{
byteCodeArray[ip++] = comment[idx] | comment[idx+1] << 8 | comment[idx+2] << 16 | comment[idx+3] << 24;
idx += 4;
commentLength -= 4;
}
switch(commentLength)
{
case 3 :
byteCodeArray[ip] |= comment[idx+2] << 24;
goto case;
case 2 :
byteCodeArray[ip] |= comment[idx+1] << 16;
goto case;
case 1 :
byteCodeArray[ip++] |= comment[idx] << 8;
goto case;
case 0 :
break;
default : assert(0);
}
}
}
void Not(BCValue result, BCValue val)
{
if (result != val)
{
Set(result, val);
val = result;
}
if (val.vType == BCValueType.Immediate)
val = pushOntoStack(val);
byteCodeArray[ip] = ShortInst16(LongInst.Not, val.stackAddr);
byteCodeArray[ip + 1] = 0;
ip += 2;
}
void emitArithInstruction(LongInst inst, BCValue lhs, BCValue rhs, BCTypeEnum* resultTypeEnum = null)
{
assert(inst >= LongInst.Add && inst < LongInst.ImmAdd,
"Instruction is not in Range for Arith Instructions");
assert(lhs.vType.StackValue, "only StackValues are supported as lhs");
// FIXME remove the lhs.type == BCTypeEnum.Char as soon as we convert correctly.
assert(lhs.type.type == BCTypeEnum.i32 || lhs.type.type == BCTypeEnum.i64
|| lhs.type.type == BCTypeEnum.f23 || lhs.type.type == BCTypeEnum.Char
|| lhs.type.type == BCTypeEnum.c8 || lhs.type.type == BCTypeEnum.f52,
"only i32, i64, f23, f52, is supported for now not: " ~ to!string(lhs.type.type));
//assert(lhs.type.type == rhs.type.type, lhs.type.type.to!string ~ " != " ~ rhs.type.type.to!string);
if (lhs.vType == BCValueType.Immediate)
{
lhs = pushOntoStack(lhs);
}
BCTypeEnum commonType = commonTypeEnum(lhs.type.type, rhs.type.type);
if (resultTypeEnum !is null)
*resultTypeEnum = commonType;
if (lhs.type.type == BCTypeEnum.f23)
{
if(rhs.type.type == BCTypeEnum.i32)
{
if (rhs.vType == BCValueType.Immediate)
() @trusted {
float frhs = float(rhs.imm32);
rhs = imm32(*cast(int*)&frhs);
} ();
else
rhs = castTo(rhs, BCTypeEnum.f23);
}
else if (rhs.type.type == BCTypeEnum.f23)
{
rhs = pushOntoStack(rhs);
}
else
assert(0, "did not expecte type " ~ to!string(rhs.type.type) ~ "to be used in a float expression");
if (inst != LongInst.Set)
inst += (LongInst.FAdd32 - LongInst.Add);
}
else if (lhs.type.type == BCTypeEnum.f52)
{
assert(rhs.type.type == BCTypeEnum.f52);
rhs = pushOntoStack(rhs);
if (inst != LongInst.Set)
inst += (LongInst.FAdd64 - LongInst.Add);
}
else if (rhs.vType == BCValueType.Immediate)
{
if (basicTypeSize(rhs.type) <= 4)
{
//Change the instruction into the corrosponding Imm Instruction;
inst += (LongInst.ImmAdd - LongInst.Add);
emitLongInst(inst, lhs.stackAddr, rhs.imm32);
return ;
}
else
{
rhs = pushOntoStack(rhs);
}
}
if (isStackValueOrParameter(rhs))
{
emitLongInst(inst, lhs.stackAddr, rhs.stackAddr);
}
else
{
assert(0, "Cannot handle: " ~ to!string(rhs.vType));
}
}
void Set(BCValue lhs, BCValue rhs)
{
assert(isStackValueOrParameter(lhs), "Set lhs is has to be a StackValue");
assert(rhs.vType == BCValueType.Immediate || isStackValueOrParameter(rhs), "Set rhs is has to be a StackValue or Imm not: " ~ rhs.vType.to!string);
if (rhs.vType == BCValueType.Immediate && (rhs.type.type == BCTypeEnum.i64 || rhs.type.type == BCTypeEnum.f52))
{
emitLongInst(LongInst.ImmSet, lhs.stackAddr, rhs.imm32);
if (rhs.type.type != BCTypeEnum.i64 || rhs.imm64 > uint.max) // if there are high bits
emitLongInst(LongInst.SetHighImm, lhs.stackAddr, Imm32(rhs.imm64 >> 32));
}
else if (lhs != rhs) // do not emit self asignments;
{
emitArithInstruction(LongInst.Set, lhs, rhs);
}
}
void SetHigh(BCValue lhs, BCValue rhs)
{
assert(isStackValueOrParameter(lhs), "SeHigt lhs is has to be a StackValue");
assert(rhs.vType == BCValueType.Immediate || isStackValueOrParameter(rhs), "SetHigh rhs is has to be a StackValue or Imm");
//two cases :
// lhs.type.size == 4 && rhs.type.size == 8
// OR
// lhs.type.size == 8 && rhs.type.size == 4
}
void Lt3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType == BCValueType.Unknown
|| isStackValueOrParameter(result),
"The result for this must be Empty or a StackValue not: " ~ to!string(result.vType));
emitArithInstruction(LongInst.Lt, lhs, rhs);
if (isStackValueOrParameter(result))
{
emitFlg(result);
}
}
void Le3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType == BCValueType.Unknown
|| isStackValueOrParameter(result),
"The result for this must be Empty or a StackValue not: " ~ to!string(result.vType));
emitArithInstruction(LongInst.Le, lhs, rhs);
if (isStackValueOrParameter(result))
{
emitFlg(result);
}
}
void Gt3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType == BCValueType.Unknown
|| isStackValueOrParameter(result),
"The result for this must be Empty or a StackValue not: " ~ to!string(result.vType));
emitArithInstruction(LongInst.Gt, lhs, rhs);
if (isStackValueOrParameter(result))
{
emitFlg(result);
}
}
void Ge3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType == BCValueType.Unknown
|| isStackValueOrParameter(result),
"The result for this must be Empty or a StackValue not " ~ to!string(result.vType) );
emitArithInstruction(LongInst.Ge, lhs, rhs);
if (isStackValueOrParameter(result))
{
emitFlg(result);
}
}
void Eq3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType == BCValueType.Unknown
|| isStackValueOrParameter(result),
"The result for this must be Empty or a StackValue not " ~ to!string(result.vType) );
emitArithInstruction(LongInst.Eq, lhs, rhs);
if (isStackValueOrParameter(result))
{
emitFlg(result);
}
}
void Neq3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType == BCValueType.Unknown
|| isStackValueOrParameter(result),
"The result for this must be Empty or a StackValue not: " ~ to!string(result.vType));
emitArithInstruction(LongInst.Neq, lhs, rhs);
if (isStackValueOrParameter(result))
{
emitFlg(result);
}
}
void Add3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot add to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
emitArithInstruction(LongInst.Add, result, rhs, &result.type.type);
}
void Sub3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot sub to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
emitArithInstruction(LongInst.Sub, result, rhs, &result.type.type);
}
void Mul3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot mul to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
emitArithInstruction(LongInst.Mul, result, rhs, &result.type.type);
}
void Div3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot div to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
emitArithInstruction(LongInst.Div, result, rhs, &result.type.type);
}
void And3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot and to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
if (lhs.type.type == BCTypeEnum.i32 && rhs.type.type == BCTypeEnum.i32)
emitArithInstruction(LongInst.And32, result, rhs);
else
emitArithInstruction(LongInst.And, result, rhs);
}
void Or3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot or to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
emitArithInstruction(LongInst.Or, result, rhs);
}
void Xor3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot or to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
if (lhs.type.type == BCTypeEnum.i32 && rhs.type.type == BCTypeEnum.i32)
emitArithInstruction(LongInst.Xor32, result, rhs);
else
emitArithInstruction(LongInst.Xor, result, rhs);
}
void Lsh3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot lsh to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}
emitArithInstruction(LongInst.Lsh, result, rhs);
}
void Rsh3(BCValue result, BCValue lhs, BCValue rhs)
{
assert(result.vType != BCValueType.Immediate, "Cannot rsh to Immediate");
result = (result ? result : lhs);
if (lhs != result)
{
Set(result, lhs);
}