-
Notifications
You must be signed in to change notification settings - Fork 6
/
lra86.cpp
2212 lines (2022 loc) · 60.3 KB
/
lra86.cpp
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) 2021-2023, Alexey Frunze
2-clause BSD license.
*/
#include <string>
#include <cstring>
#include <vector>
#include <algorithm>
#include <iostream>
#include "check.h"
#define OCO << ", " <<
// Lower this from 6 to 4 to see occasional spills.
#define USE_REGS 6
enum HReg : int
{
// Specific regs begin
HReg0, HRegAX = HReg0,
HReg1, HRegCX = HReg1,
HReg2, HRegDX = HReg2,
HReg3, HRegBX = HReg3,
#if USE_REGS >= 5
HReg4, HRegSI = HReg4,
#endif
#if USE_REGS >= 6
HReg5, HRegDI = HReg5,
#endif
// Specific regs end
HRegCnt,
// Constants representing multiple-choice desired regs:
HRegAny = HRegCnt, // unspecified, any reg at all is OK
HRegNotCX, // prefer regs other than cx (for shifts)
HRegNotDXNotAX, // prefer regs other than dx and ax (for (i)div)
HRegNotDXNotCXNotAX, // prefer regs other than dx, cx and ax
HRegByte, // prefer those with individual byte components: ax, cx, dx, bx
HRegByteNotCX, // prefer those with individual byte components except cx: ax, dx, bx
HRegAddr, // prefer those that can be a memory operand: bx, si, di
};
struct Node;
// The only global variables of the allocator.
Node* NodeFromHReg[HRegCnt];
Node* CurrentOutputNode;
// For HRegAny, HRegByte
static const HReg alloc_order_normal[HRegCnt] =
{
HRegAX, HRegCX, HRegDX, HRegBX,
#if USE_REGS >= 5
HRegSI,
#endif
#if USE_REGS >= 6
HRegDI,
#endif
};
// For HRegNotCX
static const HReg alloc_order_not_cx[HRegCnt] =
{
HRegAX, HRegDX, HRegBX,
#if USE_REGS >= 5
HRegSI,
#endif
#if USE_REGS >= 6
HRegDI,
#endif
HRegCX,
};
// For HRegNotDXNotAX
static const HReg alloc_order_not_dx_not_ax[HRegCnt] =
{
HRegCX, HRegBX,
#if USE_REGS >= 5
HRegSI,
#endif
#if USE_REGS >= 6
HRegDI,
#endif
HRegAX, HRegDX,
};
// For HRegNotDXNotCXNotAX
static const HReg alloc_order_not_dx_not_cx_not_ax[HRegCnt] =
{
HRegBX,
#if USE_REGS >= 5
HRegSI,
#endif
#if USE_REGS >= 6
HRegDI,
#endif
HRegAX, HRegCX, HRegDX,
};
// For HRegByteNotCX
static const HReg alloc_order_byte_not_cx[HRegCnt] =
{
HRegAX, HRegDX, HRegBX, HRegCX,
#if USE_REGS >= 5
HRegSI,
#endif
#if USE_REGS >= 6
HRegDI,
#endif
};
// For HRegAddr
static const HReg alloc_order_addr[HRegCnt] =
{
HRegBX,
#if USE_REGS >= 5
HRegSI,
#endif
#if USE_REGS >= 6
HRegDI,
#endif
HRegAX, HRegCX, HRegDX,
};
const HReg* RegsOrderedForAllocSearch(HReg desired)
{
const HReg* ordered_regs;
switch (desired)
{
case HRegNotCX:
ordered_regs = alloc_order_not_cx;
break;
case HRegNotDXNotAX:
ordered_regs = alloc_order_not_dx_not_ax;
break;
case HRegNotDXNotCXNotAX:
ordered_regs = alloc_order_not_dx_not_cx_not_ax;
break;
case HRegByteNotCX:
ordered_regs = alloc_order_byte_not_cx;
break;
case HRegAddr:
ordered_regs = alloc_order_addr;
break;
default:
ordered_regs = alloc_order_normal;
}
return ordered_regs;
}
bool SpecificHReg(HReg hr)
{
return (hr >= HReg0) && (hr < HRegCnt);
}
// Whether the register has 8-bit subregisters.
bool ByteHReg(HReg hr)
{
switch(hr)
{
case HRegAX: case HRegCX: case HRegDX: case HRegBX:
return true;
default:
return false;
}
}
// Whether the register can be used as a memory address for a load or store.
bool AddrHReg(HReg hr)
{
switch(hr)
{
case HRegBX:
#if USE_REGS >= 5
case HRegSI:
#endif
#if USE_REGS >= 6
case HRegDI:
#endif
return true;
default:
return false;
}
}
std::ostream &operator<<(std::ostream &os, HReg hr)
{
switch(hr)
{
case HRegAX:
return os << "ax";
case HRegCX:
return os << "cx";
case HRegDX:
return os << "dx";
case HRegBX:
return os << "bx";
#if USE_REGS >= 5
case HRegSI:
return os << "si";
#endif
#if USE_REGS >= 6
case HRegDI:
return os << "di";
#endif
default:
CHECK(0);
return os << "r?";
}
}
const char* HRegHi(HReg hr)
{
switch(hr)
{
case HRegAX:
return "ah";
case HRegCX:
return "ch";
case HRegDX:
return "dh";
case HRegBX:
return "bh";
default:
CHECK(0);
return "?h";
}
}
const char* HRegLo(HReg hr)
{
switch(hr)
{
case HRegAX:
return "al";
case HRegCX:
return "cl";
case HRegDX:
return "dl";
case HRegBX:
return "bl";
default:
CHECK(0);
return "?l";
}
}
struct Node
{
Node() : Node(nullptr, nullptr) {}
Node(Node* left) : Node(left, nullptr) {}
Node(Node* left, Node* right) :
child_ { left, right },
first_(-1),
vr_(-1),
user_vr_(-1),
loc_(LocNowhere),
loc_hr_(HRegAny),
hr_ { HRegAny, HRegAny, HRegAny },
val_(0) {}
virtual ~Node() { delete child_[0]; delete child_[1]; }
virtual bool IsSymmetric() const { return false; } // some override
virtual bool LeftFirst() const { return false; }
virtual bool RightFirst() const { return false; }
virtual bool PrefersRight() const { return false; } // shifts override
virtual int SameRegCntAddend() const { return 1; }
virtual int MinRegCnt() const { return 0; } // (i)div, (i)rem override
virtual std::string PrintOperation() const = 0; // all override
virtual std::string PrintInstruction() const // (i)rem, loads, stores override
{
return PrintOperation();
}
virtual int SelectFirst(); // args, calls override
void AssignVReg(int vr) { vr_ = vr; }
void SetUserVReg(int user_vr) { user_vr_ = user_vr; }
void AssignVRegs(int* p_vr_cnt = nullptr);
virtual void Eval() = 0; // all override
virtual void GenMemValue() {} // loads override
void GenMemValues();
virtual void GenMemCheck() {} // stores override
void GenMemChecks();
void GenRegCheck();
void SetLocNowhere() { loc_ = LocNowhere; loc_hr_ = HRegAny; }
void SetLocReg(HReg hr)
{
CHECK(SpecificHReg(hr));
loc_ = LocReg; loc_hr_ = hr;
}
void SetLocSpilled() { loc_ = LocSpilled; loc_hr_ = HRegAny; }
bool IsNowhere() const { return loc_ == LocNowhere; }
bool InReg() const { return loc_ == LocReg; }
bool IsSpilled() const { return loc_ == LocSpilled; }
virtual void AllocHRegs(HReg desired = HRegAny); // some override
void PrintExprTree(std::ostream& os, int level = 0);
void PrintInstructions(std::ostream& os);
Node* child_[2];
int first_; // which child_[] to handle first to minimize register usage
int vr_; // "virtual" register holding the output value from this node
int user_vr_; // vr_ of the parent node that uses this node as input
enum : int { LocNowhere, LocReg, LocSpilled } loc_; // location of vr_
HReg loc_hr_; // Register for LocReg
HReg hr_[3]; // AllocHRegs() sets these
unsigned short val_; // Expected value in test
std::vector<std::string> instructions; // Generated instructions
};
// Recursively selects which node to generate code for first
// (sets this->first_ to 0 (left's 1st), 1 (right's 1st) or
// -1 (either can be 1st)), returns the number of registers
// needed for the node.
// This number is known as Ershov number.
// See also Strahler number and Sethi-Ullman algorithm.
int Node::SelectFirst()
{
first_ = 0;
if (!child_[0])
{
// Leaf.
return 1;
}
int nl = child_[0]->SelectFirst();
if (!child_[1])
{
// Unary.
return nl;
}
// Binary.
int nr = child_[1]->SelectFirst();
if (RightFirst())
first_ = 1;
else if (!LeftFirst())
first_ = (nl == nr) ? -1 : ((nl > nr) ? 0 : 1);
// else `first_ = 0;` by default above.
int lrmax = (nl == nr) ? (nl + SameRegCntAddend()) : ((nl > nr) ? nl : nr);
// (i)div uses ax, another register for the divisor and also clobbers dx
// (not just by way of producing a remainder in dx, but also by requiring
// the dividend zero- or sign-extended from ax into dx before (i)div,
// which bars any input (dividend or divisor) from being in dx).
// That is, (i)div needs at least 3 registers to compute a quotient or
// remainder regardless of how few registers are needed to compute its
// dividend and divisor. Otherwise (i)div needs the same number of
// registers as e.g. add, sub, etc.
int rmin = MinRegCnt();
return (lrmax < rmin) ? rmin : lrmax;
}
// Recursively assigns virtual register numbers to this->vr_
// in every node (from the leaves towards the root).
// this->user_vr_ is assigned parent node's vr_.
void Node::AssignVRegs(int* p_vr_cnt)
{
int vr_cnt = 0;
p_vr_cnt = p_vr_cnt ? p_vr_cnt : &vr_cnt;
// Every node gets its own output VReg and
// that's also the order of evaluation / code generation.
// Each VReg is used as an input only once.
if (child_[1])
{
// Generally prefer recursing left if all else is equal.
// For shifts prefer recursing right if all else is equal.
int first_idx = (PrefersRight()
? (first_ == 0)
: (first_ <= 0)) ? 0 : 1;
child_[first_idx]->AssignVRegs(p_vr_cnt);
child_[!first_idx]->AssignVRegs(p_vr_cnt);
// Record where (at which node) inputs are used/consumed.
child_[first_idx]->SetUserVReg(*p_vr_cnt);
child_[!first_idx]->SetUserVReg(*p_vr_cnt);
}
else if (child_[0])
{
child_[0]->AssignVRegs(p_vr_cnt);
// Record where (at which node) input is used/consumed.
child_[0]->SetUserVReg(*p_vr_cnt);
}
AssignVReg((*p_vr_cnt)++);
// Unknown location for this VReg for now.
// Also don't know yet if this output is going to be used/consumed.
// That is, root's user_vr_ will remain -1.
}
// Free the given hardware register with or without spilling.
void Free(HReg hr, bool spill = false)
{
CHECK(SpecificHReg(hr));
Node* n = NodeFromHReg[hr];
CHECK(n);
if (spill)
{
// N.B. It should be safe to push/pop instead of storing/loading at
// bp+offset.
// Reasons:
// - furthest used VReg won't be needed earlier and won't have a chance to
// be mistakenly used earlier
// - there will not be 2 VRegs spilled that are used by the same
// node/instruction because if one VReg has been spilled, the other VReg
// at that node/instruction is still being computed in the sibling branch;
// this is true for unary and binary operators.
n->SetLocSpilled();
std::ostringstream oss;
oss << "push " << hr;
CurrentOutputNode->instructions.push_back(oss.str());
}
else
{
n->SetLocNowhere();
}
NodeFromHReg[hr] = nullptr;
}
// Allocate a hardware register to hold the result of the given node.
// May be different from the desired register.
HReg Allocate(Node* n, HReg desired = HRegAny)
{
CHECK(n);
int furthest = -1;
bool spill_needed = true;
#if 01
for (int hr = 0; hr < HRegCnt; hr++)
CHECK(NodeFromHReg[hr] != n);
#endif
if (SpecificHReg(desired) && !NodeFromHReg[desired])
{
furthest = desired;
spill_needed = false;
}
else
{
const HReg* order = RegsOrderedForAllocSearch(desired);
for (int i = 0; i < HRegCnt; i++)
{
HReg hr = order[i];
if (!NodeFromHReg[hr])
{
furthest = hr;
spill_needed = false;
break;
}
if (furthest == -1)
furthest = hr;
CHECK(NodeFromHReg[furthest]->user_vr_ != -1);
CHECK(NodeFromHReg[hr]->user_vr_ != -1);
if (NodeFromHReg[furthest]->user_vr_ < NodeFromHReg[hr]->user_vr_)
furthest = hr;
}
}
CHECK(furthest != -1);
HReg hr = HReg(furthest);
if (spill_needed)
Free(hr, /*spill*/true);
n->SetLocReg(hr);
NodeFromHReg[hr] = n;
CHECK(SpecificHReg(hr));
return hr;
}
// Make sure the value of the given node is in a hardware register.
// May be different from the desired register.
HReg Ensure(Node* n, HReg desired = HRegAny)
{
CHECK(n);
HReg hr = HRegAny;
if (n->IsNowhere())
{
hr = Allocate(n, desired);
}
else if (n->InReg())
{
hr = n->loc_hr_;
}
else
{
CHECK(n->IsSpilled());
hr = Allocate(n, desired);
// N.B. It should be safe to push/pop instead of storing/loading at
// bp+offset.
// Reasons:
// - furthest used VReg won't be needed earlier and won't have a chance to
// be mistakenly used earlier
// - there will not be 2 VRegs spilled that are used by the same
// node/instruction because if one VReg has been spilled, the other VReg
// at that node/instruction is still being computed in the sibling branch;
// this is true for unary and binary operators.
std::ostringstream oss;
oss << "pop " << hr;
CurrentOutputNode->instructions.push_back(oss.str());
}
CHECK(SpecificHReg(hr));
return hr;
}
// When something isn't in the desired HReg,
// move there or exchange.
void MoveToDesired(HReg hr_desired, HReg hr_actual)
{
CHECK(SpecificHReg(hr_desired));
CHECK(SpecificHReg(hr_actual));
CHECK(hr_desired != hr_actual);
Node* n_desired = NodeFromHReg[hr_desired];
Node* n_actual = NodeFromHReg[hr_actual];
CHECK(n_actual);
std::ostringstream oss;
if (!n_desired)
{
Free(hr_actual);
HReg hr = Allocate(n_actual, hr_desired);
CHECK(hr == hr_desired);
oss << "mov " << hr_desired OCO hr_actual;
}
else
{
NodeFromHReg[hr_desired] = n_actual;
NodeFromHReg[hr_actual] = n_desired;
n_desired->SetLocReg(hr_actual);
n_actual->SetLocReg(hr_desired);
oss << "xchg " << hr_desired OCO hr_actual;
}
CurrentOutputNode->instructions.push_back(oss.str());
}
// Commonly used helper for Node::AllocHRegs().
void RecurseToAndEnsureIns(Node* n, const HReg desired[/*1 or 2*/])
{
CHECK(n->child_[0]);
bool binary = n->child_[1] != nullptr;
int first_idx =
binary ? ((n->child_[0]->vr_ <= n->child_[1]->vr_) ? 0 : 1) : 0;
n->child_[first_idx]->AllocHRegs(desired[first_idx]);
if (binary)
n->child_[!first_idx]->AllocHRegs(desired[!first_idx]);
CurrentOutputNode = n; // Associate generated instructions with this node.
n->hr_[first_idx] = Ensure(n->child_[first_idx], desired[first_idx]);
if (binary)
n->hr_[!first_idx] = Ensure(n->child_[!first_idx], desired[!first_idx]);
}
// Commonly used helper for Node::AllocHRegs().
void FreeInsAllocOut(Node* n)
{
CHECK(n->child_[0]);
bool binary = n->child_[1] != nullptr;
Free(n->hr_[0]);
if (binary)
Free(n->hr_[1]);
n->hr_[2] = Allocate(n, n->hr_[0]);
CHECK(n->hr_[2] == n->hr_[0]);
if (binary)
CHECK(n->hr_[2] != n->hr_[1]);
}
// Generic implementation of the register allocator.
void Node::AllocHRegs(HReg desired)
{
HReg desired2[2] = { desired, desired };
if (child_[1])
{
// Binary.
RecurseToAndEnsureIns(this, desired2);
// If the operation is symmetric and
// the desired out register is hr_[1],
// swap it with hr_[0] so that hr_[0] is in/out.
if (IsSymmetric() && (hr_[1] == desired))
{
hr_[1] = hr_[0];
hr_[0] = desired;
}
FreeInsAllocOut(this);
}
else if (child_[0])
{
// Unary.
RecurseToAndEnsureIns(this, desired2);
FreeInsAllocOut(this);
}
else
{
// Leaf.
CurrentOutputNode = this; // Associate generated instructions with this node.
hr_[2] = Allocate(this, desired);
}
std::ostringstream oss;
if (child_[1])
oss << PrintInstruction() << ' ' << hr_[2] OCO hr_[1];
else if (child_[0])
oss << PrintInstruction() << ' ' << hr_[2];
else
oss << "mov " << hr_[2] OCO PrintInstruction(); // NodeInt
CurrentOutputNode->instructions.push_back(oss.str());
}
// Generates instructions to set the memory values
// that the expression code will load.
void Node::GenMemValues()
{
CHECK(vr_ != -1);
if (child_[1])
{
int first_idx = (child_[0]->vr_ <= child_[1]->vr_) ? 0 : 1;
child_[first_idx]->GenMemValues();
child_[!first_idx]->GenMemValues();
}
else if (child_[0])
{
child_[0]->GenMemValues();
}
if (!vr_)
CurrentOutputNode = this;
CHECK(CurrentOutputNode);
GenMemValue();
}
// Generates instructions to check the memory values
// that the expression code will store.
void Node::GenMemChecks()
{
CHECK(vr_ != -1);
if (child_[1])
{
int first_idx = (child_[0]->vr_ <= child_[1]->vr_) ? 0 : 1;
child_[first_idx]->GenMemChecks();
child_[!first_idx]->GenMemChecks();
}
else if (child_[0])
{
child_[0]->GenMemChecks();
}
CHECK(CurrentOutputNode);
CHECK(vr_ <= CurrentOutputNode->vr_);
GenMemCheck();
}
// Generates instructions to check the register value
// that the expression code will produce for the root node.
void Node::GenRegCheck()
{
CHECK(CurrentOutputNode);
std::ostringstream oss, oss2;
oss << "cmp " << hr_[2] OCO val_;
CurrentOutputNode->instructions.push_back(oss.str());
oss2 << "jne failure";
CurrentOutputNode->instructions.push_back(oss2.str());
}
// Prints the expression tree.
void Node::PrintExprTree(std::ostream& os, int level)
{
if (child_[1])
child_[1]->PrintExprTree(os, level + 4);
os << "; " << std::string(level, ' ') << PrintOperation();
if (vr_ >= 0)
{
os << " (vr" << vr_;
#if 0
os << "; u@" << user_vr_;
#endif
os << ")";
}
os << '\n';
if (child_[0])
child_[0]->PrintExprTree(os, level + 4);
}
// Prints all generated instructions.
void Node::PrintInstructions(std::ostream& os)
{
CHECK(vr_ != -1);
if (child_[1])
{
int first_idx = (child_[0]->vr_ <= child_[1]->vr_) ? 0 : 1;
child_[first_idx]->PrintInstructions(os);
child_[!first_idx]->PrintInstructions(os);
}
else if (child_[0])
{
child_[0]->PrintInstructions(os);
}
for (const auto& s : instructions)
{
os << " ";
os << s;
os << std::string(28 - s.length(), ' ');
os << "; vr" << vr_;
os << '\n';
}
}
// 16-bit integer (leaf node).
struct NodeInt : Node
{
NodeInt() = delete;
NodeInt(unsigned short val) { val_ = val; }
virtual std::string PrintOperation() const
{
std::ostringstream oss;
oss << val_;
return oss.str();
}
virtual void Eval() {}
};
// label (leaf node).
struct NodeLabel : Node
{
NodeLabel() = delete;
NodeLabel(const std::string& label) { label_ = label; }
virtual std::string PrintOperation() const
{
return label_;
}
virtual void Eval() {}
std::string label_;
};
struct NodeNeg : Node
{
NodeNeg() = delete;
NodeNeg(Node* left) : Node(left) {}
virtual std::string PrintOperation() const { return "neg "; }
virtual void Eval()
{
child_[0]->Eval();
val_ = -child_[0]->val_;
}
};
struct NodeNot : Node
{
NodeNot() = delete;
NodeNot(Node* left) : Node(left) {}
virtual std::string PrintOperation() const { return "not "; }
virtual void Eval()
{
child_[0]->Eval();
val_ = ~child_[0]->val_;
}
};
// Zero extension from 8 to 16 bits.
struct NodeZext : Node
{
NodeZext() = delete;
NodeZext(Node* left) : Node(left) {}
virtual std::string PrintOperation() const { return "zext "; }
virtual void Eval()
{
child_[0]->Eval();
val_ = child_[0]->val_ & 0xFF;
}
virtual void AllocHRegs(HReg desired)
{
child_[0]->AllocHRegs(desired);
CurrentOutputNode = this; // Associate generated instructions with this node.
hr_[0] = Ensure(child_[0], desired);
Free(hr_[0]);
hr_[2] = Allocate(this, hr_[0]);
std::ostringstream oss;
if (ByteHReg(hr_[2]))
oss << "xor " << HRegHi(hr_[2]) OCO HRegHi(hr_[2]);
else
oss << "and " << hr_[2] OCO 255;
CurrentOutputNode->instructions.push_back(oss.str());
}
};
// Sign extension from 8 to 16 bits.
struct NodeSext : Node
{
NodeSext() = delete;
NodeSext(Node* left) : Node(left) {}
virtual std::string PrintOperation() const { return "sext "; }
virtual void Eval()
{
child_[0]->Eval();
val_ = child_[0]->val_ & 0xFF;
val_ -= (val_ & 0x80) << 1;
}
virtual void AllocHRegs(HReg desired)
{
(void)desired;
HReg desired2 = HRegAX;
child_[0]->AllocHRegs(desired2);
CurrentOutputNode = this; // Associate generated instructions with this node.
hr_[0] = Ensure(child_[0], desired2);
if (hr_[0] != desired2)
{
MoveToDesired(desired2, hr_[0]);
hr_[0] = desired2;
}
Free(hr_[0]);
hr_[2] = Allocate(this, hr_[0]);
CurrentOutputNode->instructions.push_back("cbw");
}
};
struct NodeAnd : Node
{
NodeAnd() = delete;
NodeAnd(Node* left, Node* right) : Node(left, right) {}
virtual bool IsSymmetric() const { return true; }
virtual std::string PrintOperation() const { return "and "; }
virtual void Eval()
{
child_[0]->Eval();
child_[1]->Eval();
val_ = child_[0]->val_ & child_[1]->val_;
}
};
struct NodeOr : Node
{
NodeOr() = delete;
NodeOr(Node* left, Node* right) : Node(left, right) {}
virtual bool IsSymmetric() const { return true; }
virtual std::string PrintOperation() const { return "or "; }
virtual void Eval()
{
child_[0]->Eval();
child_[1]->Eval();
val_ = child_[0]->val_ | child_[1]->val_;
}
};
struct NodeXor : Node
{
NodeXor() = delete;
NodeXor(Node* left, Node* right) : Node(left, right) {}
virtual bool IsSymmetric() const { return true; }
virtual std::string PrintOperation() const { return "xor "; }
virtual void Eval()
{
child_[0]->Eval();
child_[1]->Eval();
val_ = child_[0]->val_ ^ child_[1]->val_;
}
};
struct NodeAdd : Node
{
NodeAdd() = delete;
NodeAdd(Node* left, Node* right) : Node(left, right) {}
virtual bool IsSymmetric() const { return true; }
virtual std::string PrintOperation() const { return "add "; }
virtual void Eval()
{
child_[0]->Eval();
child_[1]->Eval();
val_ = child_[0]->val_ + child_[1]->val_;
}
};
struct NodeSub : Node
{
NodeSub() = delete;
NodeSub(Node* minuend, Node* subtrahend) : Node(minuend, subtrahend) {}
virtual std::string PrintOperation() const { return "sub "; }
virtual void Eval()
{
child_[0]->Eval();
child_[1]->Eval();
val_ = child_[0]->val_ - child_[1]->val_;
}
};
void AllocHRegs_ShiftX(Node* n, HReg desired)
{
HReg (&hr_)[3] = n->hr_;
// Simulate the shl instruction requiring
// its right input in HRegCX.
HReg desired2[2] = { desired, HRegCX };
switch (desired)
{
case HRegAX:
case HRegDX:
case HRegBX:
#if USE_REGS >= 5
case HRegSI:
#endif
#if USE_REGS >= 6
case HRegDI:
#endif
case HRegNotCX:
case HRegNotDXNotCXNotAX:
case HRegByteNotCX:
case HRegAddr:
break;
case HRegCX:
case HRegAny:
desired2[0] = HRegNotCX;
break;
case HRegNotDXNotAX:
desired2[0] = HRegNotDXNotCXNotAX;
break;
case HRegByte:
desired2[0] = HRegByteNotCX;
break;
default:
CHECK(0);
break;
}
// N.B. For shifts we prefer recursing right if all else is equal.
// The idea is to get HRegCX for the count operand ASAP.
RecurseToAndEnsureIns(n, desired2);
if (hr_[1] != desired2[1])
{
MoveToDesired(desired2[1], hr_[1]);
if (hr_[0] == desired2[1])
hr_[0] = hr_[1];
hr_[1] = desired2[1];
}
FreeInsAllocOut(n);
std::ostringstream oss;
oss << n->PrintInstruction() << ' ' << hr_[2] OCO HRegLo(hr_[1]);
CurrentOutputNode->instructions.push_back(oss.str());
}
// Shift left.
struct NodeShLft : Node
{
NodeShLft() = delete;
NodeShLft(Node* value, Node* count) : Node(value, count) {}
virtual bool PrefersRight() const { return true; }
virtual std::string PrintOperation() const { return "shl "; }
virtual void AllocHRegs(HReg desired) { AllocHRegs_ShiftX(this, desired); }
virtual void Eval()
{
child_[0]->Eval();
child_[1]->Eval();
val_ = child_[0]->val_ << (child_[1]->val_ & 0xF);
}
};
// Logical shift right.
struct NodeShRht : Node
{
NodeShRht() = delete;
NodeShRht(Node* value, Node* count) : Node(value, count) {}
virtual bool PrefersRight() const { return true; }
virtual std::string PrintOperation() const { return "shr "; }
virtual void AllocHRegs(HReg desired) { AllocHRegs_ShiftX(this, desired); }
virtual void Eval()
{
child_[0]->Eval();
child_[1]->Eval();
val_ = child_[0]->val_ >> (child_[1]->val_ & 0xF);
}
};
// Arithmetic/sign-extending shift right.
struct NodeShArRht : Node