-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathILBlock.cpp
More file actions
1474 lines (1287 loc) · 40.5 KB
/
Copy pathILBlock.cpp
File metadata and controls
1474 lines (1287 loc) · 40.5 KB
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
#include <stdio.h>
#include <stdlib.h>
#include "ILBlock.h"
#include "Variable.h"
#include "Function.h"
#include "Struct.h"
#include "Output.h"
using namespace std;
map<size_t, ILBlock*> ILBlock::m_serializationMapping;
stack< map<size_t, ILBlock*> > ILBlock::m_savedSerializationMappings;
ILParameter::ILParameter()
{
cls = ILPARAM_VOID;
integerValue = 0;
type = ILTYPE_VOID;
}
ILParameter::ILParameter(bool b)
{
cls = ILPARAM_BOOL;
boolValue = b;
type = ILTYPE_INT8;
}
ILParameter::ILParameter(ILParameterType t, int64_t i)
{
cls = ILPARAM_INT;
integerValue = i;
type = t;
}
ILParameter::ILParameter(Type* t, int64_t i)
{
cls = ILPARAM_INT;
integerValue = i;
type = ReduceType(t);
}
ILParameter::ILParameter(ILParameterType t, double f)
{
cls = ILPARAM_FLOAT;
floatValue = f;
type = t;
}
ILParameter::ILParameter(Type* t, double f)
{
cls = ILPARAM_FLOAT;
floatValue = f;
type = ReduceType(t);
}
ILParameter::ILParameter(const string& str)
{
cls = ILPARAM_STRING;
stringValue = str;
type = (GetTargetPointerSize() == 4) ? ILTYPE_INT32 : ILTYPE_INT64;
}
ILParameter::ILParameter(Struct* s, const string& name)
{
cls = ILPARAM_FIELD;
structure = s;
stringValue = name;
type = ILTYPE_VOID;
}
ILParameter::ILParameter(Variable* var)
{
cls = ILPARAM_VAR;
variable = var;
type = ReduceType(var->GetType());
}
ILParameter::ILParameter(Function* func)
{
cls = ILPARAM_FUNC;
function = func;
type = (GetTargetPointerSize() == 4) ? ILTYPE_INT32 : ILTYPE_INT64;
}
ILParameter::ILParameter(ILBlock* b)
{
cls = ILPARAM_BLOCK;
block = b;
type = (GetTargetPointerSize() == 4) ? ILTYPE_INT32 : ILTYPE_INT64;
}
ILParameter::ILParameter(const ILParameter& obj, Type* objType, const string& str)
{
cls = ILPARAM_MEMBER;
parent = new ILParameter(obj);
structure = objType->GetStruct();
stringValue = str;
}
ILParameter::ILParameter(const ILParameter& param)
{
cls = param.cls;
type = param.type;
stringValue = param.stringValue;
structure = param.structure;
variable = param.variable;
function = param.function;
if (cls == ILPARAM_MEMBER)
parent = new ILParameter(*param.parent);
else if (cls == ILPARAM_BLOCK)
block = param.block;
else
integerValue = param.integerValue;
}
ILParameter::~ILParameter()
{
if (cls == ILPARAM_MEMBER)
delete parent;
}
ILParameter& ILParameter::operator=(const ILParameter& param)
{
if (cls == ILPARAM_MEMBER)
delete parent;
cls = param.cls;
type = param.type;
stringValue = param.stringValue;
structure = param.structure;
variable = param.variable;
function = param.function;
if (cls == ILPARAM_MEMBER)
parent = new ILParameter(*param.parent);
else if (cls == ILPARAM_BLOCK)
block = param.block;
else
integerValue = param.integerValue;
return *this;
}
ILParameterType ILParameter::ReduceType(Type* t)
{
switch (t->GetClass())
{
case TYPE_BOOL:
return ILTYPE_INT8;
case TYPE_INT:
switch (t->GetWidth())
{
case 1:
return ILTYPE_INT8;
case 2:
return ILTYPE_INT16;
case 4:
return ILTYPE_INT32;
case 8:
return ILTYPE_INT64;
default:
return ILTYPE_VOID;
}
case TYPE_FLOAT:
return (t->GetWidth() == 4) ? ILTYPE_FLOAT : ILTYPE_DOUBLE;
case TYPE_POINTER:
case TYPE_ARRAY:
case TYPE_FUNCTION:
return (GetTargetPointerSize() == 4) ? ILTYPE_INT32 : ILTYPE_INT64;
default:
return ILTYPE_VOID;
}
}
size_t ILParameter::GetWidth() const
{
switch (type)
{
case ILTYPE_INT8:
return 1;
case ILTYPE_INT16:
return 2;
case ILTYPE_INT32:
case ILTYPE_FLOAT:
return 4;
case ILTYPE_INT64:
case ILTYPE_DOUBLE:
return 8;
default:
return 0;
}
}
void ILParameter::ReplaceFunction(Function* from, Function* to)
{
if (cls == ILPARAM_MEMBER)
parent->ReplaceFunction(from, to);
if (function == from)
function = to;
}
void ILParameter::ReplaceVariable(Variable* from, Variable* to)
{
if (cls == ILPARAM_MEMBER)
parent->ReplaceVariable(from, to);
if (variable == from)
variable = to;
}
void ILParameter::CheckForUndefinedReferences(size_t& errors)
{
if (cls == ILPARAM_MEMBER)
parent->CheckForUndefinedReferences(errors);
// All prototypes should have been resolved by the linker
if (function && (!function->IsFullyDefined()))
{
// No location information here, but there error should have been already generated in the
// parse tree, so we shouldn't get here unless something else is wrong. Print the error
// anyway to avoid missing errors
errors++;
fprintf(stderr, "error: undefined reference to '%s'\n", function->GetName().c_str());
}
// All variables marked 'extern' should have been resolved by the linker
if (variable && variable->IsExternal())
{
// No location information here, but there error should have been already generated in the
// parse tree, so we shouldn't get here unless something else is wrong. Print the error
// anyway to avoid missing errors
errors++;
fprintf(stderr, "error: undefined reference to '%s'\n", variable->GetName().c_str());
}
}
void ILParameter::ConvertStringsToVariables(map< string, Ref<Variable> >& stringMap)
{
if (cls != ILPARAM_STRING)
return;
map< string, Ref<Variable> >::iterator i = stringMap.find(stringValue);
if (i != stringMap.end())
{
cls = ILPARAM_VAR;
variable = i->second;
return;
}
Type* t = Type::ArrayType(Type::IntType(1, true), stringValue.size() + 1);
t->SetConst(true);
Variable* var = new Variable(VAR_GLOBAL, t, string("$str$" + stringValue));
var->GetData().Write(stringValue.c_str(), stringValue.size() + 1);
stringMap[stringValue] = var;
cls = ILPARAM_VAR;
variable = var;
}
void ILParameter::ResolveImportedFunction(Function* from, Variable* importTable)
{
if (cls != ILPARAM_FUNC)
return;
if (function != from)
return;
cls = ILPARAM_MEMBER;
function = NULL;
parent = new ILParameter(importTable);
structure = importTable->GetType()->GetStruct();
stringValue = from->GetName();
}
bool ILParameter::IsConstant() const
{
switch (cls)
{
case ILPARAM_INT:
case ILPARAM_FLOAT:
case ILPARAM_STRING:
case ILPARAM_BOOL:
return true;
default:
return false;
}
}
void ILParameter::TagReferences()
{
switch (cls)
{
case ILPARAM_FUNC:
function->TagReferences();
break;
case ILPARAM_VAR:
variable->TagReference();
break;
case ILPARAM_MEMBER:
parent->TagReferences();
break;
default:
break;
}
}
void ILParameter::Serialize(OutputBlock* output)
{
output->WriteInteger(cls);
output->WriteInteger(type);
switch (cls)
{
case ILPARAM_INT:
output->WriteInteger(integerValue);
break;
case ILPARAM_FLOAT:
output->Write(&floatValue, sizeof(floatValue));
break;
case ILPARAM_STRING:
output->WriteString(stringValue);
break;
case ILPARAM_FIELD:
structure->Serialize(output);
output->WriteString(stringValue);
break;
case ILPARAM_BOOL:
output->WriteInteger(boolValue ? 1 : 0);
break;
case ILPARAM_VAR:
variable->Serialize(output);
break;
case ILPARAM_FUNC:
function->Serialize(output);
break;
case ILPARAM_BLOCK:
output->WriteInteger(block->GetIndex());
break;
case ILPARAM_MEMBER:
parent->Serialize(output);
structure->Serialize(output);
output->WriteString(stringValue);
break;
default:
break;
}
}
bool ILParameter::Deserialize(InputBlock* input)
{
uint32_t clsInt;
if (!input->ReadUInt32(clsInt))
return false;
cls = (ILParameterClass)clsInt;
uint32_t typeInt;
if (!input->ReadUInt32(typeInt))
return false;
type = (ILParameterType)typeInt;
int64_t objectIndex;
switch (cls)
{
case ILPARAM_INT:
if (!input->ReadInt64(integerValue))
return false;
break;
case ILPARAM_FLOAT:
if (!input->Read(&floatValue, sizeof(floatValue)))
return false;
break;
case ILPARAM_STRING:
if (!input->ReadString(stringValue))
return false;
break;
case ILPARAM_FIELD:
structure = Struct::Deserialize(input);
if (!structure)
return false;
if (!input->ReadString(stringValue))
return false;
break;
case ILPARAM_BOOL:
if (!input->ReadBool(boolValue))
return false;
break;
case ILPARAM_VAR:
variable = Variable::Deserialize(input);
if (!variable)
return false;
break;
case ILPARAM_FUNC:
function = Function::Deserialize(input);
if (!function)
return false;
break;
case ILPARAM_BLOCK:
if (!input->ReadInt64(objectIndex))
return false;
block = ILBlock::GetSerializationMapping((size_t)objectIndex);
if (!block)
return false;
break;
case ILPARAM_MEMBER:
parent = new ILParameter();
if (!parent->Deserialize(input))
return false;
structure = Struct::Deserialize(input);
if (!structure)
return false;
if (!input->ReadString(stringValue))
return false;
break;
default:
break;
}
return true;
}
void ILParameter::Print() const
{
switch (cls)
{
case ILPARAM_VOID: fprintf(stderr, "void"); break;
#ifdef WIN32
case ILPARAM_INT: fprintf(stderr, "%.I64d {%d}", (long long)integerValue, (int)GetWidth()); break;
#else
case ILPARAM_INT: fprintf(stderr, "%lld {%d}", (long long)integerValue, (int)GetWidth()); break;
#endif
case ILPARAM_FLOAT: fprintf(stderr, "%f", floatValue); break;
case ILPARAM_STRING: fprintf(stderr, "\"%s\"", stringValue.c_str()); break;
case ILPARAM_FIELD: fprintf(stderr, "%s::%s", structure->GetName().c_str(), stringValue.c_str()); break;
case ILPARAM_BOOL: fprintf(stderr, "%s", boolValue ? "true" : "false"); break;
case ILPARAM_VAR: fprintf(stderr, "var<%s>", variable->GetName().c_str()); break;
case ILPARAM_FUNC: fprintf(stderr, "func<%s>", function->GetName().c_str()); break;
case ILPARAM_BLOCK: fprintf(stderr, "block<%d>", (int)block->GetIndex()); break;
case ILPARAM_MEMBER: parent->Print(); fprintf(stderr, ".%s::%s", structure->GetName().c_str(), stringValue.c_str()); break;
case ILPARAM_UNDEFINED: fprintf(stderr, "__undefined"); break;
default: fprintf(stderr, "<invalid>"); break;
}
}
ILInstruction::ILInstruction()
{
operation = ILOP_RETURN_VOID;
}
ILInstruction::ILInstruction(ILOperation op)
{
operation = op;
}
ILInstruction::ILInstruction(ILOperation op, const ILParameter& a)
{
operation = op;
params.push_back(a);
}
ILInstruction::ILInstruction(ILOperation op, const ILParameter& a, const ILParameter& b)
{
operation = op;
params.push_back(a);
params.push_back(b);
}
ILInstruction::ILInstruction(ILOperation op, const ILParameter& a, const ILParameter& b, const ILParameter& c)
{
operation = op;
params.push_back(a);
params.push_back(b);
params.push_back(c);
}
ILInstruction::ILInstruction(ILOperation op, const ILParameter& a, const ILParameter& b, const ILParameter& c, const ILParameter& d)
{
operation = op;
params.push_back(a);
params.push_back(b);
params.push_back(c);
params.push_back(d);
}
ILInstruction::ILInstruction(ILOperation op, const ILParameter& a, const ILParameter& b, const ILParameter& c,
const ILParameter& d, const ILParameter& e)
{
operation = op;
params.push_back(a);
params.push_back(b);
params.push_back(c);
params.push_back(d);
params.push_back(e);
}
ILInstruction::ILInstruction(ILOperation op, const ILParameter& a, const ILParameter& b, const ILParameter& c,
const ILParameter& d, const ILParameter& e, const ILParameter& f)
{
operation = op;
params.push_back(a);
params.push_back(b);
params.push_back(c);
params.push_back(d);
params.push_back(e);
params.push_back(f);
}
ILInstruction::ILInstruction(ILOperation op, const ILParameter& a, const ILParameter& b, const ILParameter& c,
const ILParameter& d, const ILParameter& e, const ILParameter& f, const ILParameter& g)
{
operation = op;
params.push_back(a);
params.push_back(b);
params.push_back(c);
params.push_back(d);
params.push_back(e);
params.push_back(f);
params.push_back(g);
}
ILInstruction::ILInstruction(ILOperation op, const vector<ILParameter>& list)
{
operation = op;
params = list;
}
ILInstruction::ILInstruction(const ILInstruction& instr)
{
operation = instr.operation;
params = instr.params;
}
ILInstruction& ILInstruction::operator=(const ILInstruction& instr)
{
operation = instr.operation;
params = instr.params;
return *this;
}
void ILInstruction::ReplaceFunction(Function* from, Function* to)
{
for (vector<ILParameter>::iterator i = params.begin(); i != params.end(); i++)
i->ReplaceFunction(from, to);
}
void ILInstruction::ReplaceVariable(Variable* from, Variable* to)
{
for (vector<ILParameter>::iterator i = params.begin(); i != params.end(); i++)
i->ReplaceVariable(from, to);
}
void ILInstruction::CheckForUndefinedReferences(size_t& errors)
{
for (vector<ILParameter>::iterator i = params.begin(); i != params.end(); i++)
i->CheckForUndefinedReferences(errors);
}
void ILInstruction::ConvertStringsToVariables(map< string, Ref<Variable> >& stringMap)
{
for (vector<ILParameter>::iterator i = params.begin(); i != params.end(); i++)
i->ConvertStringsToVariables(stringMap);
}
void ILInstruction::ResolveImportedFunction(Function* from, Variable* importTable)
{
for (vector<ILParameter>::iterator i = params.begin(); i != params.end(); i++)
i->ResolveImportedFunction(from, importTable);
}
void ILInstruction::TagReferences()
{
for (vector<ILParameter>::iterator i = params.begin(); i != params.end(); i++)
i->TagReferences();
}
bool ILInstruction::WritesToFirstParameter() const
{
switch (operation)
{
case ILOP_ASSIGN:
case ILOP_ADDRESS_OF:
case ILOP_ADDRESS_OF_MEMBER:
case ILOP_DEREF:
case ILOP_DEREF_MEMBER:
case ILOP_ARRAY_INDEX:
case ILOP_ARRAY_INDEX_ASSIGN:
case ILOP_PTR_ADD:
case ILOP_PTR_SUB:
case ILOP_PTR_DIFF:
case ILOP_ADD:
case ILOP_SUB:
case ILOP_SMULT:
case ILOP_UMULT:
case ILOP_SDIV:
case ILOP_UDIV:
case ILOP_SMOD:
case ILOP_UMOD:
case ILOP_AND:
case ILOP_OR:
case ILOP_XOR:
case ILOP_SHL:
case ILOP_SHR:
case ILOP_SAR:
case ILOP_NEG:
case ILOP_NOT:
case ILOP_CALL:
case ILOP_SCONVERT:
case ILOP_UCONVERT:
case ILOP_ALLOCA:
case ILOP_STRLEN:
case ILOP_SYSCALL:
case ILOP_SYSCALL2:
case ILOP_RDTSC:
case ILOP_RDTSC_LOW:
case ILOP_RDTSC_HIGH:
case ILOP_PEB:
case ILOP_TEB:
case ILOP_INITIAL_VARARG:
case ILOP_NEXT_ARG:
case ILOP_PREV_ARG:
case ILOP_BYTESWAP:
case ILOP_POW:
case ILOP_FLOOR:
case ILOP_CEIL:
case ILOP_SQRT:
case ILOP_SIN:
case ILOP_COS:
case ILOP_TAN:
case ILOP_ASIN:
case ILOP_ACOS:
case ILOP_ATAN:
// These instructions write to the first parameter
return true;
case ILOP_DEREF_ASSIGN:
case ILOP_DEREF_MEMBER_ASSIGN:
case ILOP_IF_TRUE:
case ILOP_IF_LESS_THAN:
case ILOP_IF_LESS_EQUAL:
case ILOP_IF_BELOW:
case ILOP_IF_BELOW_EQUAL:
case ILOP_IF_EQUAL:
case ILOP_GOTO:
case ILOP_NORETURN:
case ILOP_RETURN:
case ILOP_RETURN_VOID:
case ILOP_MEMCPY:
case ILOP_MEMSET:
case ILOP_BREAKPOINT:
// These instructions do not write to any variables, default is avoided here
// to make the compiler spit out warnings if instructions are added but they
// aren't handled here (which could lead to well hidden bugs)
break;
}
return false;
}
void ILInstruction::MarkWrittenVariables()
{
if (operation == ILOP_ADDRESS_OF)
{
// This technically isn't a write to the source parameter itself, but this
// creates an alias, which could lead to an indirect write. Proper alias analysis
// could resolve some of these and determine if there is actually a write. Since
// the primary use of this function is to do simple optimizations on the inlining
// process, we can optimize any unnecessary aliased copies later.
if (params[0].cls == ILPARAM_VAR)
params[0].variable->SetWritten(true);
if (params[1].cls == ILPARAM_VAR)
params[1].variable->SetWritten(true);
return;
}
if (WritesToFirstParameter())
{
if (params[0].cls == ILPARAM_VAR)
params[0].variable->SetWritten(true);
}
}
void ILInstruction::Serialize(OutputBlock* output)
{
output->WriteInteger(operation);
output->WriteInteger(params.size());
for (vector<ILParameter>::iterator i = params.begin(); i != params.end(); i++)
i->Serialize(output);
}
bool ILInstruction::Deserialize(InputBlock* input)
{
uint32_t op;
if (!input->ReadUInt32(op))
return false;
operation = (ILOperation)op;
size_t paramCount;
if (!input->ReadNativeInteger(paramCount))
return false;
for (size_t i = 0; i < paramCount; i++)
{
ILParameter param;
if (!param.Deserialize(input))
return false;
params.push_back(param);
}
return true;
}
void ILInstruction::Print() const
{
switch (operation)
{
case ILOP_ASSIGN: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); break;
case ILOP_ADDRESS_OF: params[0].Print(); fprintf(stderr, " = &"); params[1].Print(); break;
case ILOP_ADDRESS_OF_MEMBER: params[0].Print(); fprintf(stderr, " = &"); params[1].Print(); fprintf(stderr, "->"); params[2].Print(); break;
case ILOP_DEREF: params[0].Print(); fprintf(stderr, " = *"); params[1].Print(); break;
case ILOP_DEREF_MEMBER: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, "->"); params[2].Print(); break;
case ILOP_DEREF_ASSIGN: fprintf(stderr, "*"); params[0].Print(); fprintf(stderr, " = "); params[1].Print(); break;
case ILOP_DEREF_MEMBER_ASSIGN: params[0].Print(); fprintf(stderr, "->"); params[1].Print(); fprintf(stderr, " = "); params[2].Print(); break;
case ILOP_ARRAY_INDEX: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, "["); params[2].Print(); fprintf(stderr, "]{%d}", (int)params[3].integerValue); break;
case ILOP_ARRAY_INDEX_ASSIGN: params[0].Print(); fprintf(stderr, "["); params[1].Print(); fprintf(stderr, "]{%d} = ", (int)params[2].integerValue); params[3].Print(); break;
case ILOP_PTR_ADD: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " ptr+ "); params[2].Print(); fprintf(stderr, " {%d}", (int)params[3].integerValue); break;
case ILOP_PTR_SUB: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " ptr- "); params[2].Print(); fprintf(stderr, " {%d}", (int)params[3].integerValue); break;
case ILOP_PTR_DIFF: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " ptrdiff "); params[2].Print(); fprintf(stderr, " {%d}", (int)params[3].integerValue); break;
case ILOP_ADD: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " + "); params[2].Print(); break;
case ILOP_SUB: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " - "); params[2].Print(); break;
case ILOP_SMULT: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " smult "); params[2].Print(); break;
case ILOP_UMULT: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " umult "); params[2].Print(); break;
case ILOP_SDIV: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " sdiv "); params[2].Print(); break;
case ILOP_UDIV: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " udiv "); params[2].Print(); break;
case ILOP_SMOD: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " smod "); params[2].Print(); break;
case ILOP_UMOD: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " umod "); params[2].Print(); break;
case ILOP_AND: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " & "); params[2].Print(); break;
case ILOP_OR: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " | "); params[2].Print(); break;
case ILOP_XOR: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " ^ "); params[2].Print(); break;
case ILOP_SHL: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " shl "); params[2].Print(); break;
case ILOP_SHR: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " shr "); params[2].Print(); break;
case ILOP_SAR: params[0].Print(); fprintf(stderr, " = "); params[1].Print(); fprintf(stderr, " sar "); params[2].Print(); break;
case ILOP_NEG: params[0].Print(); fprintf(stderr, " = -"); params[1].Print(); break;
case ILOP_NOT: params[0].Print(); fprintf(stderr, " = ~"); params[1].Print(); break;
case ILOP_IF_TRUE: fprintf(stderr, "if "); params[0].Print(); fprintf(stderr, " then "); params[1].Print(); fprintf(stderr, " else "); params[2].Print(); break;
case ILOP_IF_LESS_THAN: fprintf(stderr, "if signed "); params[0].Print(); fprintf(stderr, " < "); params[1].Print(); fprintf(stderr, " then "); params[2].Print(); fprintf(stderr, " else "); params[3].Print(); break;
case ILOP_IF_LESS_EQUAL: fprintf(stderr, "if signed "); params[0].Print(); fprintf(stderr, " <= "); params[1].Print(); fprintf(stderr, " then "); params[2].Print(); fprintf(stderr, " else "); params[3].Print(); break;
case ILOP_IF_BELOW: fprintf(stderr, "if unsigned "); params[0].Print(); fprintf(stderr, " < "); params[1].Print(); fprintf(stderr, " then "); params[2].Print(); fprintf(stderr, " else "); params[3].Print(); break;
case ILOP_IF_BELOW_EQUAL: fprintf(stderr, "if unsigned "); params[0].Print(); fprintf(stderr, " <= "); params[1].Print(); fprintf(stderr, " then "); params[2].Print(); fprintf(stderr, " else "); params[3].Print(); break;
case ILOP_IF_EQUAL: fprintf(stderr, "if "); params[0].Print(); fprintf(stderr, " == "); params[1].Print(); fprintf(stderr, " then "); params[2].Print(); fprintf(stderr, " else "); params[3].Print(); break;
case ILOP_GOTO: fprintf(stderr, "goto "); params[0].Print(); break;
case ILOP_NORETURN: fprintf(stderr, "noreturn"); break;
case ILOP_SCONVERT: params[0].Print(); fprintf(stderr, " = sconvert "); params[1].Print(); break;
case ILOP_UCONVERT: params[0].Print(); fprintf(stderr, " = uconvert "); params[1].Print(); break;
case ILOP_RETURN: fprintf(stderr, "return "); params[0].Print(); break;
case ILOP_RETURN_VOID: fprintf(stderr, "return"); break;
case ILOP_ALLOCA: params[0].Print(); fprintf(stderr, " = alloca "); params[1].Print(); break;
case ILOP_MEMCPY: fprintf(stderr, "memcpy "); params[0].Print(); fprintf(stderr, ", "); params[1].Print(); fprintf(stderr, ", "); params[2].Print(); break;
case ILOP_MEMSET: fprintf(stderr, "memset "); params[0].Print(); fprintf(stderr, ", "); params[1].Print(); fprintf(stderr, ", "); params[2].Print(); break;
case ILOP_STRLEN: params[0].Print(); fprintf(stderr, " = strlen "); params[1].Print(); break;
case ILOP_RDTSC: params[0].Print(); fprintf(stderr, " = rdtsc"); break;
case ILOP_RDTSC_LOW: params[0].Print(); fprintf(stderr, " = rdtsc_low"); break;
case ILOP_RDTSC_HIGH: params[0].Print(); fprintf(stderr, " = rdtsc_high"); break;
case ILOP_PEB: params[0].Print(); fprintf(stderr, " = peb"); break;
case ILOP_TEB: params[0].Print(); fprintf(stderr, " = teb"); break;
case ILOP_INITIAL_VARARG: params[0].Print(); fprintf(stderr, " = initial_vararg"); break;
case ILOP_NEXT_ARG: params[0].Print(); fprintf(stderr, " = next_arg "); params[1].Print(); fprintf(stderr, ", "); params[2].Print(); break;
case ILOP_PREV_ARG: params[0].Print(); fprintf(stderr, " = prev_arg "); params[1].Print(); fprintf(stderr, ", "); params[2].Print(); break;
case ILOP_BYTESWAP: params[0].Print(); fprintf(stderr, " = byteswap "); params[1].Print(); break;
case ILOP_BREAKPOINT: fprintf(stderr, "breakpoint"); break;
case ILOP_POW: params[0].Print(); fprintf(stderr, " = pow "); params[1].Print(); fprintf(stderr, ", "); params[2].Print(); break;
case ILOP_FLOOR: params[0].Print(); fprintf(stderr, " = floor "); params[1].Print(); break;
case ILOP_CEIL: params[0].Print(); fprintf(stderr, " = ceil "); params[1].Print(); break;
case ILOP_SQRT: params[0].Print(); fprintf(stderr, " = sqrt "); params[1].Print(); break;
case ILOP_SIN: params[0].Print(); fprintf(stderr, " = sin "); params[1].Print(); break;
case ILOP_COS: params[0].Print(); fprintf(stderr, " = cos "); params[1].Print(); break;
case ILOP_TAN: params[0].Print(); fprintf(stderr, " = tan "); params[1].Print(); break;
case ILOP_ASIN: params[0].Print(); fprintf(stderr, " = asin "); params[1].Print(); break;
case ILOP_ACOS: params[0].Print(); fprintf(stderr, " = acos "); params[1].Print(); break;
case ILOP_ATAN: params[0].Print(); fprintf(stderr, " = atan "); params[1].Print(); break;
case ILOP_CALL:
params[0].Print();
fprintf(stderr, " = ");
params[1].Print();
fprintf(stderr, "<");
switch (params[2].integerValue)
{
case CALLING_CONVENTION_CDECL:
fprintf(stderr, "cdecl");
break;
case CALLING_CONVENTION_STDCALL:
fprintf(stderr, "stdcall");
break;
case CALLING_CONVENTION_FASTCALL:
fprintf(stderr, "fastcall");
break;
default:
break;
}
fprintf(stderr, ",%d>(", (int)params[3].integerValue);
for (size_t i = 3; i < params.size(); i++)
{
if (i > 3)
fprintf(stderr, ", ");
params[i].Print();
}
fprintf(stderr, ")");
break;
case ILOP_SYSCALL:
params[0].Print();
fprintf(stderr, " = __syscall");
fprintf(stderr, "(");
for (size_t i = 1; i < params.size(); i++)
{
if (i > 1)
fprintf(stderr, ", ");
params[i].Print();
}
fprintf(stderr, ")");
break;
case ILOP_SYSCALL2:
params[0].Print();
fprintf(stderr, ", ");
params[1].Print();
fprintf(stderr, " = __syscall2");
fprintf(stderr, "(");
for (size_t i = 2; i < params.size(); i++)
{
if (i > 2)
fprintf(stderr, ", ");
params[i].Print();
}
fprintf(stderr, ")");
break;
default:
fprintf(stderr, "<invalid>");
break;
}
}
ILBlock::ILBlock()
{
m_index = 0;
m_output = NULL;
m_blockEnded = false;
}
ILBlock::ILBlock(size_t i)
{
m_index = i;
m_output = NULL;
m_blockEnded = false;
}
ILBlock::~ILBlock()
{
if (m_output)
{
free(m_output->code);
delete m_output;
}
}
void ILBlock::AddInstruction(const ILInstruction& instr)
{
if (m_blockEnded)
{
// A block ending instruction has already been emitted, just ignore anything after it
return;
}
// For instructions that use the special __undefined value, the output is considered
// a "don't care". For conditions, always take the false case. For returns, convert
// to a return of void (which ignores the return value). For calls, leave the
// parameter so that it can be skipped during parameter passing. For others, omit the
// instruction entirely.
bool hasUndefinedResults = false;
for (vector<ILParameter>::const_iterator i = instr.params.begin(); i != instr.params.end(); i++)
{
if (i->cls == ILPARAM_UNDEFINED)
{
hasUndefinedResults = true;
break;
}
}
if (hasUndefinedResults)
{
// Undefined output
if (instr.operation == ILOP_RETURN)
AddInstruction(ILOP_RETURN_VOID);
else if (instr.operation == ILOP_CALL)
m_instrs.push_back(instr);
else if ((instr.operation == ILOP_SYSCALL) || (instr.operation == ILOP_SYSCALL2))
m_instrs.push_back(instr);
else if ((instr.operation == ILOP_IF_TRUE) || (instr.operation == ILOP_IF_LESS_THAN) ||
(instr.operation == ILOP_IF_LESS_EQUAL) || (instr.operation == ILOP_IF_BELOW) ||
(instr.operation == ILOP_IF_BELOW_EQUAL) || (instr.operation == ILOP_IF_EQUAL))
AddInstruction(ILOP_GOTO, instr.params[2]);
}
else
{
// Normal instruction, add to list
m_instrs.push_back(instr);
}
// Check for block ending instructions
switch (instr.operation)
{
case ILOP_IF_TRUE:
case ILOP_IF_LESS_THAN:
case ILOP_IF_LESS_EQUAL:
case ILOP_IF_BELOW:
case ILOP_IF_BELOW_EQUAL:
case ILOP_IF_EQUAL:
case ILOP_GOTO:
case ILOP_NORETURN:
case ILOP_RETURN:
case ILOP_RETURN_VOID:
m_blockEnded = true;
break;
default:
break;
}
}
void ILBlock::RemoveLastInstruction()
{
m_instrs.erase(m_instrs.end() - 1);
m_blockEnded = false;
}
void ILBlock::SplitBlock(size_t firstToMove, ILBlock* target)
{
if (target)
target->m_instrs.insert(target->m_instrs.begin(), m_instrs.begin() + firstToMove, m_instrs.end());
m_instrs.erase(m_instrs.begin() + firstToMove, m_instrs.end());
if (target)
target->m_blockEnded = m_blockEnded;
m_blockEnded = false;
}
void ILBlock::ReplaceFunction(Function* from, Function* to)
{
for (vector<ILInstruction>::iterator i = m_instrs.begin(); i != m_instrs.end(); i++)
i->ReplaceFunction(from, to);
}