-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMilCilAsmGen.cpp
More file actions
1754 lines (1546 loc) · 53.1 KB
/
MilCilAsmGen.cpp
File metadata and controls
1754 lines (1546 loc) · 53.1 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
/*
* Copyright 2025 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the Micron language project.
*
* The following is the license that applies to this copy of the
* file. For a license to use the file under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
// derived from CeeGen; same logic; generates ECMA 335 IL asm text instead
#include "MilCilAsmGen.h"
#include "MilValidator.h"
#include "MilProject.h"
#include <QDateTime>
#include <QCoreApplication>
#include <QtDebug>
using namespace Mil;
CilAsmGen::CilAsmGen(AstModel* mdl) : mdl(mdl), labelCounter(0), maxStack(0), currentStack(0)
{
Q_ASSERT(mdl);
}
static QByteArray indent(int level)
{
return QByteArray(level * 4, ' ');
}
QByteArray CilAsmGen::declRef(Declaration * d)
{
// remember MIL has no nested types or procedures!
if( d == 0 )
return QByteArray();
switch(d->kind)
{
case Declaration::Module:
if( d != curMod )
return "['" + d->name + "']'" + d->name + "'";
else
return "'" + d->name + "'"; // since the MIL module is a CIL class, this name always remains.
case Declaration::TypeDecl:
Q_ASSERT(d->outer && d->outer->kind == Declaration::Module && d->getType() && d->getType()->isSUOA() );
return declRef(d->outer) + "/'" + d->name + "'";
case Declaration::Field:
Q_ASSERT(d->outer && d->outer->kind == Declaration::TypeDecl);
return declRef(d->outer) + "::'" + d->name + "'";
case Declaration::VarDecl:
Q_ASSERT(d->outer && d->outer->kind == Declaration::Module);
return declRef(d->outer) + "::'" + d->name + "'";
case Declaration::LocalDecl:
case Declaration::ParamDecl:
return "'" + d->name + "'";
case Declaration::Procedure:
Q_ASSERT(d->outer && d->outer->kind == Declaration::Module);
return declRef(d->outer) + "::'" + d->forwardToProc()->name + "'";
default:
Q_ASSERT(false);
}
}
QByteArray CilAsmGen::declName(Declaration * d)
{
if( d == 0 )
return QByteArray();
switch(d->kind)
{
case Declaration::Field:
case Declaration::VarDecl:
case Declaration::LocalDecl:
case Declaration::ParamDecl:
case Declaration::ConstDecl:
case Declaration::Module:
return "'" + d->name + "'";
case Declaration::Procedure:
case Declaration::TypeDecl:
if (d->outer && d->outer->kind != Declaration::Module)
return declName(d->outer) + "$" + d->forwardToProc()->name;
else
return "'" + d->forwardToProc()->name + "'";
default:
Q_ASSERT(false);
}
}
bool CilAsmGen::generate(Declaration* module, QIODevice* device)
{
Q_ASSERT(module && device);
curMod = module;
out.setDevice(device);
const QString dedication = genDedication();
out << "// " << declName(module) << ".il" << endl;
out << dedication << endl << endl;
out << ".assembly " << declName(module) << " {}" << endl;
out << ".module '" << module->name << ".dll'" << endl << endl;
out << "// imports" << endl;
out << ".assembly extern mscorlib {}" << endl;
out << ".assembly extern MIC$ {}" << endl;
visitModule();
return true;
}
bool CilAsmGen::generateMain(QIODevice *device, const QSet<Declaration *> &used)
{
Q_ASSERT(device);
out.setDevice(device);
const QString dedication = genDedication();
out << "// Main$.il" << endl;
out << dedication << endl << endl;
out << ".assembly Main$ {}" << endl;
out << ".module 'Main$.exe'" << endl << endl;
out << "// imports" << endl;
foreach( Mil::Declaration* module, mdl->getModules() )
{
// if a module is not in "used", it is never imported and thus a root module
if( !used.contains(module) && !module->nobody && !module->generic )
out << ".assembly extern " << declName(module) << " {}" << endl;
}
out << endl;
out << ".class public auto ansi sealed Main$ extends [mscorlib]System.Object {" << endl;
out << ".method public static void Main() cil managed {" << endl;
out << " .entrypoint" << endl;
foreach( Mil::Declaration* module, mdl->getModules() )
{
// if a module is not in "used", it is never imported and thus a root module
if( !used.contains(module) && !module->nobody && !module->generic )
out << " call void ['" << module->name << "']'" << module->name << "'::begin$()" << endl;
}
out << " ret" << endl;
out << "}}" << endl;
return true;
}
bool CilAsmGen::generateConfig(QIODevice *device)
{
Q_ASSERT(device);
out.setDevice(device);
out << "{\"runtimeOptions\": {" << endl;
out << "\"framework\": {" << endl;
out << "\"name\": \"Microsoft.NETCore.App\"," << endl;
out << "\"version\": \"3.0.0\"," << endl;
out << "\"rollForward\": \"LatestMajor\"" << endl;
out << "}}}" << endl;
return true;
}
QString CilAsmGen::genDedication()
{
return "// This file was generated by " + QCoreApplication::applicationName() + " "
+ QCoreApplication::applicationVersion() + " on " + QDateTime::currentDateTime().toString();
}
void CilAsmGen::visitModule()
{
// Import external assemblies for imported modules
Declaration* sub = curMod->subs;
while (sub)
{
if (sub->kind == Declaration::Import)
{
out << ".assembly extern '" << sub->name << "' {}" << endl;
}
sub = sub->next;
}
out << endl;
out << ".class public auto ansi sealed " << declName(curMod) << " extends [mscorlib]System.Object {" << endl;
out << ".field static int32 initialized$" << endl;
sub = curMod->subs;
while (sub)
{
if (sub->kind == Declaration::TypeDecl)
{
typeDecl(sub);
}
sub = sub->next;
}
// Module-level static fields for variables
sub = curMod->subs;
while (sub)
{
if (sub->kind == Declaration::VarDecl)
{
out << ".field public static " << typeRef(sub->getType()) << " " << declName(sub) << endl;
}
sub = sub->next;
}
out << endl;
// Process procedures and type methods
bool initFound = false;
sub = curMod->subs;
while (sub)
{
switch (sub->kind)
{
case Declaration::TypeDecl:
{
Type* t = deref(sub->getType());
if (t && t->kind == Type::Object)
{
foreach (Declaration* p, t->subs)
{
if (p->kind == Declaration::Procedure)
visitProcedure(p);
}
visitMetaDecl(sub);
}
if (t && t->objectInit && t->isSOA())
emitInitializer(t);
}
break;
case Declaration::Procedure:
visitProcedure(sub);
if (sub->entryPoint)
initFound = true;
break;
}
sub = sub->next;
}
// create module initializer if not present
if (!initFound)
{
Declaration proc;
proc.kind = Declaration::Procedure;
proc.entryPoint = true;
proc.outer = curMod;
proc.name = "begin$";
visitProcedure(&proc);
}
out << "}" << endl; // end module class
}
void CilAsmGen::visitProcedure(Declaration* proc)
{
curProc = proc;
labelCounter = 0;
maxStack = 0;
currentStack = 0;
procHeader(proc);
out << "{" << endl;
if (proc->entryPoint && !curMod->nobody)
{
// Module initialization code
out << indent(1) << ".maxstack 2" << endl;
out << indent(1) << "ldsfld int32 " << declRef(curMod) << "::initialized$" << endl;
out << indent(1) << "brtrue.s IL_INIT_DONE" << endl;
out << indent(1) << "ldc.i4.1" << endl;
out << indent(1) << "stsfld int32 " << declRef(curMod) << "::initialized$" << endl;
Declaration* sub = curMod->subs;
while (sub)
{
if (sub->kind == Declaration::Import && !sub->imported->nobody)
{
out << indent(1) << "call void ['" << sub->imported->name << "']'" << sub->imported->name << "'::begin$()" << endl;
}
else if (sub->kind == Declaration::VarDecl)
{
emitSoapInit(mangledName(sub), sub->getType(), true);
}
sub = sub->next;
}
out << indent(1) << "IL_INIT_DONE:" << endl;
}
if (!proc->forward && !proc->extern_ && !proc->foreign_)
{
// Local variables
if (proc->subs)
{
out << indent(1) << ".locals init (" << endl;
Declaration* sub = proc->subs;
int localIdx = 0;
bool first = true;
while (sub)
{
if (sub->kind == Declaration::LocalDecl)
{
if (!first)
out << "," << endl;
out << indent(2) << "[" << localIdx++ << "] " << typeRef(sub->getType()) << " '" << sub->name << "'";
first = false;
}
sub = sub->next;
}
out << endl << indent(1) << ")" << endl;
}
emitMaxStack();
// Generate body
statementSeq(proc->body);
if (proc->getType() == 0 || deref(proc->getType())->kind == Type::Any)
{
out << indent(1) << "ret" << endl;
}
}
out << "}" << endl << endl;
curProc = 0;
}
void CilAsmGen::procHeader(Declaration* proc)
{
out << ".method static ";
if( proc->public_ || proc->entryPoint )
out << "public ";
out << typeRef(proc->getType()) << " " << declName(proc);
out << "(";
DeclList params = proc->getParams();
for (int i = 0; i < params.size(); i++)
{
if (i != 0)
out << ", ";
parameter(params[i], i);
}
out << ") cil managed" << endl;
}
void CilAsmGen::parameter(Declaration* param, int index)
{
out << typeRef(param->getType()) << " " << declName(param);
}
void CilAsmGen::emitField(Declaration *field, bool union_)
{
out << indent(1) << ".field public ";
if( union_ )
out << "[0] ";
out << typeRef(field->getType()) << " '" << field->name << "'" << endl;
// TODO: bitfields?
}
void CilAsmGen::typeDecl(Declaration* d)
{
Type* t = deref(d->getType());
if (t == 0)
{
out << "// Undeclared type " << d->name << endl;
return;
}
switch (t->kind)
{
case Type::Object:
case Type::Struct: {
out << ".class nested public sequential ansi sealed " << declName(d) << endl;
out << " extends [mscorlib]System.ValueType {" << endl;
QList<Declaration*> fields;
if( t->kind == Type::Object )
{
// Objects as structs with vtable pointer
out << indent(1) << ".field public native int class$" << endl; // vtable pointer
fields = t->getFieldList(true);
}else
fields = t->subs;
// For structs, fields are sequential
foreach (Declaration* field, fields)
{
if (field->kind == Declaration::Field)
emitField(field, false);
}
out << "}" << endl << endl;
} break;
case Type::Union: {
out << ".class nested public explicit ansi sealed " << declName(d) << endl;
out << " extends [mscorlib]System.ValueType {" << endl;
// For unions, all fields start at offset 0
foreach (Declaration* field, t->subs)
{
if (field->kind == Declaration::Field)
emitField(field, true);
}
out << "}" << endl << endl;
} break;
case Type::Array:
if( t->len )
{
const QByteArray et = typeRef(t->getType());
out << ".class nested public sequential ansi sealed " << declName(d) << endl;
out << " extends [mscorlib]System.ValueType {" << endl;
for( int i = 0; i < t->len; i++ )
{
out << indent(1) << ".field public ";
out << et << " '#" << i << "'" << endl;
}
out << "}" << endl << endl;
} // else
// open arrays are just pointers to the first element, no type declaration needed
break;
case Type::Pointer:
// Pointers are native int
// No type declaration needed
break;
case Type::Proc:
// Function pointers
if (t->typebound)
{
out << ".class value explicit ansi sealed " << declName(d) << endl;
out << " extends [mscorlib]System.ValueType" << endl;
out << "{" << endl;
out << indent(1) << ".field native int self" << endl;
out << indent(1) << ".field native int proc" << endl;
out << "}" << endl << endl;
} // else
// plain function pointers are just native int, no type declaration needed
break;
}
}
QByteArray CilAsmGen::typeRef(Type* t)
{
if (t == 0)
return "void";
t = t->deref();
switch (t->kind)
{
case Type::BOOL:
return "bool";
case Type::CHAR:
return "uint8"; // the CIL char has 16 bits
case Type::INT8:
return "int8";
case Type::INT16:
return "int16";
case Type::INT32:
return "int32";
case Type::INT64:
return "int64";
case Type::UINT8:
return "uint8";
case Type::UINT16:
return "uint16";
case Type::UINT32:
return "uint32";
case Type::UINT64:
return "uint64";
case Type::FLOAT32:
return "float32";
case Type::FLOAT64:
return "float64";
case Type::INTPTR:
return "native int";
case Type::DBLINTPTR:
return "native int"; // TODO: handle double pointer size
case Type::Pointer: {
Type* tt = deref(t->getType());
if (tt->kind == Type::Array)
tt = deref(tt->getType());
return typeRef(tt) + "*";
}
case Type::Array:
if( t->len )
return "valuetype " + declRef(t->decl);
else
{
Type* tt = deref(t->getType());
return typeRef(tt) + "*"; // TODO
}
case Type::Struct:
case Type::Object:
case Type::Union:
return "valuetype " + declRef(t->decl);
case Type::Proc:
if( t->typebound )
return declRef(t->decl);
else
return "native int";
default:
Q_ASSERT(false);
return "object"; // TODO
}
}
QByteArray CilAsmGen::typeToken(Type* t)
{
t = deref(t);
switch (t->kind)
{
case Type::INT8:
return "i1";
case Type::INT16:
return "i2";
case Type::INT32:
return "i4";
case Type::INT64:
return "i8";
case Type::UINT8:
return "u1";
case Type::UINT16:
return "u2";
case Type::UINT32:
return "u4";
case Type::UINT64:
return "u8";
case Type::FLOAT32:
return "r4";
case Type::FLOAT64:
return "r8";
case Type::INTPTR:
default:
return "i";
}
}
void CilAsmGen::statementSeq(Statement* s)
{
while (s)
{
switch (s->kind)
{
case Statement::ExprStat:
if (s->args)
{
expression(s->args);
// Pop result if not used
if (currentStack > 0)
out << indent(1) << "pop" << endl;
}
break;
case IL_if:
{
QByteArray elseLabel = "IL_ELSE_" + QByteArray::number(labelCounter++);
QByteArray endLabel = "IL_ENDIF_" + QByteArray::number(labelCounter++);
expression(s->args);
out << indent(1) << "brfalse.s " << elseLabel << endl;
statementSeq(s->body);
out << indent(1) << "br.s " << endLabel << endl;
out << indent(1) << elseLabel << ":" << endl;
if (s->next && s->next->kind == IL_else)
{
s = s->next;
statementSeq(s->body);
}
out << indent(1) << endLabel << ":" << endl;
}
break;
case IL_loop:
{
QByteArray loopLabel = "IL_LOOP_" + QByteArray::number(labelCounter++);
out << indent(1) << loopLabel << ":" << endl;
statementSeq(s->body);
out << indent(1) << "br.s " << loopLabel << endl;
}
break;
case IL_repeat:
{
QByteArray loopLabel = "IL_REPEAT_" + QByteArray::number(labelCounter++);
out << indent(1) << loopLabel << ":" << endl;
statementSeq(s->body);
expression(s->args);
out << indent(1) << "brfalse.s " << loopLabel << endl;
}
break;
case IL_switch:
{
expression(s->args);
// Count cases
int caseCount = 0;
Statement* cs = s->next;
while (cs && cs->kind == IL_case)
{
caseCount++;
cs = cs->next;
}
// Generate jump table
out << indent(1) << "switch (";
for (int i = 0; i < caseCount; i++)
{
if (i > 0)
out << ", ";
out << "IL_CASE_" << i;
}
out << ")" << endl;
// Default case
QByteArray defaultLabel = "IL_DEFAULT_" + QByteArray::number(labelCounter++);
out << indent(1) << "br.s " << defaultLabel << endl;
// Generate cases
int caseIdx = 0;
while (s->next && s->next->kind == IL_case)
{
s = s->next;
out << indent(1) << "IL_CASE_" << caseIdx++ << ":" << endl;
statementSeq(s->body);
out << indent(1) << "br.s IL_ENDSWITCH" << endl;
}
out << indent(1) << defaultLabel << ":" << endl;
if (s->next && s->next->kind == IL_else)
{
s = s->next;
statementSeq(s->body);
}
out << indent(1) << "IL_ENDSWITCH:" << endl;
}
break;
case IL_while:
{
QByteArray testLabel = "IL_WHILE_TEST_" + QByteArray::number(labelCounter++);
QByteArray loopLabel = "IL_WHILE_LOOP_" + QByteArray::number(labelCounter++);
out << indent(1) << "br.s " << testLabel << endl;
out << indent(1) << loopLabel << ":" << endl;
statementSeq(s->body);
out << indent(1) << testLabel << ":" << endl;
expression(s->args);
out << indent(1) << "brtrue.s " << loopLabel << endl;
}
break;
case IL_exit:
out << indent(1) << "br IL_ENDLOOP" << endl; // Needs proper loop tracking
break;
case IL_stloc:
case IL_stloc_s:
case IL_stloc_0:
case IL_stloc_1:
case IL_stloc_2:
case IL_stloc_3:
expression(s->args);
if (s->kind >= IL_stloc_0 && s->kind <= IL_stloc_3)
out << indent(1) << "stloc." << (s->kind - IL_stloc_0) << endl;
else
out << indent(1) << "stloc " << s->id << endl;
break;
case IL_starg:
expression(s->args);
out << indent(1) << "starg " << s->id << endl;
break;
case IL_stind:
case IL_stind_i1:
case IL_stind_i4:
case IL_stind_i8:
case IL_stind_r4:
case IL_stind_r8:
case IL_stind_ip:
Q_ASSERT(s->args && s->args->kind == Expression::Argument);
expression(s->args->lhs); // Address
expression(s->args->rhs); // Value
out << indent(1) << "stind." << typeToken(s->args->rhs->getType()) << endl;
break;
case IL_stelem:
case IL_stelem_i1:
case IL_stelem_i2:
case IL_stelem_i4:
case IL_stelem_i8:
case IL_stelem_r4:
case IL_stelem_r8:
case IL_stelem_ip:
{
Q_ASSERT(s->args && s->args->kind == Expression::Argument);
expression(s->args->next->rhs); // Array pointer
expression(s->args->lhs); // Index
out << indent(1) << "conv.i" << endl;
// Calculate element size based on type
Type* elemType = deref(s->args->rhs->getType());
int elemSize = 0;
const char* storeOp = "stind.i";
switch(s->kind) {
case IL_stelem_i1:
elemSize = 1;
storeOp = "stind.i1";
break;
case IL_stelem_i2:
elemSize = 2;
storeOp = "stind.i2";
break;
case IL_stelem_i4:
elemSize = 4;
storeOp = "stind.i4";
break;
case IL_stelem_i8:
elemSize = 8;
storeOp = "stind.i8";
break;
case IL_stelem_r4:
elemSize = 4;
storeOp = "stind.r4";
break;
case IL_stelem_r8:
elemSize = 8;
storeOp = "stind.r8";
break;
case IL_stelem_ip:
out << indent(1) << "sizeof native int" << endl;
storeOp = "stind.i";
break;
case IL_stelem:
out << indent(1) << "sizeof " << typeRef(elemType) << endl;
storeOp = elemType->isSUOA() ? "stobj" : "stind.i";
break;
}
if (elemSize > 0)
out << indent(1) << "ldc.i4." << elemSize << endl;
out << indent(1) << "mul" << endl;
out << indent(1) << "add" << endl; // Calculate address
expression(s->args->rhs); // Value
if (s->kind == IL_stelem && elemType->isSUO())
out << indent(1) << storeOp << " " << typeRef(elemType) << endl;
else
out << indent(1) << storeOp << endl;
}
break;
case IL_stfld:
Q_ASSERT(s->args && s->args->kind == Expression::Argument);
expression(s->args->lhs); // Object
expression(s->args->rhs); // Value
out << indent(1) << "stfld " << typeRef(s->args->rhs->getType()) << " ";
out << mangledName(s->d->outer) << "::'" << s->d->name << "'" << endl;
break;
case IL_stvar:
expression(s->args);
// TODO out << indent(1) << "stsfld " << typeRef(s->args->getType()) << " ";
out << indent(1) << "stsfld " << typeRef(s->d->getType()) << " ";
out << mangledName(s->d) << endl;
break;
case IL_ret:
if (s->args)
expression(s->args);
out << indent(1) << "ret" << endl;
break;
case IL_pop:
expression(s->args);
out << indent(1) << "pop" << endl;
break;
case IL_free:
expression(s->args);
out << indent(1) << "call void [mscorlib]System.Runtime.InteropServices.Marshal::FreeHGlobal(native int)" << endl;
break;
case IL_label:
out << indent(1) << s->name << ":" << endl;
break;
case IL_goto:
out << indent(1) << "br " << s->name << endl;
break;
case IL_line:
// Line debug information
break;
case IL_strcpy:
Q_ASSERT(s->args && s->args->kind == Expression::Argument);
expression(s->args->lhs); // to
expression(s->args->rhs); // from
out << indent(1) << "call void [MIC$]MIC$::strcpy(uint8*,uint8*)" << endl;
break;
default:
Q_ASSERT(false);
}
s = s->next;
}
}
static void collectArgs(Expression* e, QList<Expression*>& args)
{
if (e && e->kind == Expression::Argument)
{
if (e->next)
collectArgs(e->next, args);
if (e->lhs)
args << e->lhs;
args << e->rhs;
}
else if (e)
args << e;
}
void CilAsmGen::expression(Expression* e)
{
switch (e->kind)
{
case IL_add:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "add" << endl;
break;
case IL_div:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "div" << endl;
break;
case IL_div_un:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "div.un" << endl;
break;
case IL_mul:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "mul" << endl;
break;
case IL_rem:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "rem" << endl;
break;
case IL_rem_un:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "rem.un" << endl;
break;
case IL_sub:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "sub" << endl;
break;
case IL_and:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "and" << endl;
break;
case IL_or:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "or" << endl;
break;
case IL_xor:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "xor" << endl;
break;
case IL_shl:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "shl" << endl;
break;
case IL_shr:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "shr" << endl;
break;
case IL_shr_un:
expression(e->lhs);
expression(e->rhs);
out << indent(1) << "shr.un" << endl;
break;
case IL_neg:
expression(e->lhs);
out << indent(1) << "neg" << endl;
break;
case IL_not:
expression(e->lhs);
out << indent(1) << "not" << endl;
break;
case IL_ldc_i4_0:
out << indent(1) << "ldc.i4.0" << endl;
break;
case IL_ldc_i4_1:
out << indent(1) << "ldc.i4.1" << endl;
break;
case IL_ldc_i4_2:
out << indent(1) << "ldc.i4.2" << endl;
break;
case IL_ldc_i4_3:
out << indent(1) << "ldc.i4.3" << endl;
break;
case IL_ldc_i4_4:
out << indent(1) << "ldc.i4.4" << endl;
break;
case IL_ldc_i4_5:
out << indent(1) << "ldc.i4.5" << endl;
break;
case IL_ldc_i4_6:
out << indent(1) << "ldc.i4.6" << endl;
break;
case IL_ldc_i4_7:
out << indent(1) << "ldc.i4.7" << endl;
break;
case IL_ldc_i4_8:
out << indent(1) << "ldc.i4.8" << endl;
break;
case IL_ldc_i4_m1:
out << indent(1) << "ldc.i4.m1" << endl;
break;
case IL_ldc_i4_s:
out << indent(1) << "ldc.i4.s " << e->i << endl;
break;
case IL_ldc_i4:
out << indent(1) << "ldc.i4 " << e->i << endl;
break;
case IL_ldc_i8:
out << indent(1) << "ldc.i8 " << e->i << endl;
break;
case IL_ldc_r4:
out << indent(1) << "ldc.r4 " << QByteArray::number(e->f, 'e', 16) << endl;
break;
case IL_ldc_r8:
out << indent(1) << "ldc.r8 " << QByteArray::number(e->f, 'e', 16) << endl;
break;
case IL_ldnull: