-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMilValidator.cpp
More file actions
1818 lines (1729 loc) · 59.8 KB
/
MilValidator.cpp
File metadata and controls
1818 lines (1729 loc) · 59.8 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.
*/
#include "MilValidator.h"
#include <QtDebug>
#include <limits>
#include <float.h>
#include <math.h>
using namespace Mil;
// NOTE: removed all INTPTR from the checks. There should never be an actual INTPTR type on stack since there is no conv_ip
Validator::Validator(AstModel* m):mdl(m),curMod(0),curProc(0),needsPointerInit(true)
{
}
bool Validator::validate(Declaration* module)
{
curMod = module;
Declaration* sub = curMod->subs;
while( sub )
{
Type* t = deref(sub->getType());
if( !sub->forward )
{
Declaration* d = module->findSubByName(sub->name);
if( d && d != sub )
{
if( module->generic && d->kind == Declaration::TypeDecl && d->getType()->kind == Type::Generic )
{
if( d->getType()->getType() )
error(sub, "the generic type with the same name already has a constraining type");
else
{
d->name.clear();
Type* ref = d->getType();
ref->kind = Type::NameRef;
ref->quali = new Quali();
ref->quali->second = sub->name;
ref->setType(t);
}
}else
error(sub, "duplicate name");
}
}
switch(sub->kind)
{
case Declaration::TypeDecl:
if( module->generic )
{
}
if( t->typebound && (t->kind == Type::Object || t->kind == Type::Struct || t->kind == Type::Interface) )
{
Type* base = deref(t->getType());
int id = base->numOfNonFwdNonOverrideProcs();
foreach( Declaration* method, t->subs )
{
if( method->kind == Declaration::Procedure && !method->forward )
{
Declaration* super = base->findSubByName(method->name, true);
if( super && super->pd )
{
method->getPd()->slot = super->pd->slot;
method->override_ = true;
// TODO: check param equality with super
}else
method->getPd()->slot = id++;
visitProcedure(method);
}
}
}else if( t->kind == Type::Union )
foreach( Declaration* d, t->subs )
{
if( d->kind == Declaration::Field )
if( deref(d->getType())->kind == Type::Object )
error(d, "union fields cannot be of object type");
}
t->objectInit = checkIfObjectInit(t);
t->pointerInit = checkIfPointerInit(t);
break;
case Declaration::ConstDecl:
if( sub->c )
sub->setType(toType(sub->c));
break;
case Declaration::Procedure:
visitProcedure(sub);
break;
}
sub = sub->next;
}
foreach( Expression* e, newExprs )
{
if( module->md == 0 )
module->md = new ModuleData();
if( module->md->toDelete == 0 )
module->md->toDelete = new ToDelete(e);
else
module->md->toDelete->append(new ToDelete(e));
}
newExprs.clear();
module->validated = errors.isEmpty();
return errors.isEmpty();
}
Type*Validator::deref(Type* t)
{
if( t && t->kind == Type::NameRef )
{
Type* t2 = t->getType();
if( t == t2 )
return t;
return deref(t2);
}else if( t )
return t;
else
return mdl->getBasicType(Type::Undefined);
}
Type *Validator::deref(Declaration * d)
{
return deref(d ? d->getType() : 0);
}
void Validator::visitProcedure(Declaration* proc)
{
if( proc->forward || proc->extern_ || proc->foreign_ )
{
return; // TODO: check param compat
}
curProc = proc;
stack.clear();
if( proc->typebound )
{
DeclList params = proc->getParams();
Type* firstType = params.isEmpty() ? 0 : deref(params.first()->getType());
const bool firstIsSelf = !params.isEmpty() && params.first()->typebound && firstType &&
( firstType->kind == Type::Interface ||
(firstType->kind == Type::Pointer && firstType->getType() &&
(deref(firstType->getType())->kind == Type::Object ||
deref(firstType->getType())->kind == Type::Struct)) );
if( !firstIsSelf )
error(proc, "the 'self' parameter must point to the receiver");
}
// TODO: check that
gotos.clear(); labels.clear();
visitStatSeq(proc->body);
if( !stack.isEmpty() )
error(proc, "stack not empty at the end of the procedure");
foreach( const NamePos& np, gotos )
{
const int label = findLabel(np.name->name);
if( label == -1 )
error(proc, QString("cannot find label: '%1'").arg(np.name->name));
else if( !np.pos.contains( labels[label].pos.last() ) )
error(proc, QString("cannot cannot jump to this label: '%1'").arg(np.name->name));
}
curProc = 0;
}
static inline bool isInt32(Type* t)
{
if( 0 )
return false;
else
return t->isInt32OnStack();
}
void Validator::visitStatSeq(Statement* stat)
{
blockStack.push_back(stat);
while(stat)
{
switch(stat->kind)
{
case Statement::ExprStat:
{
expectN(0, stat);
Expression* lastExprInSeq = visitExpr(stat->e);
if( stack.isEmpty() )
{
Q_ASSERT( stat->args == 0 );
stat->args = lastExprInSeq;
if( lastExprInSeq && lastExprInSeq->next )
{
// split ExprStat
Statement* s = new Statement();
s->kind = (IL_op)Statement::ExprStat;
s->pos = lastExprInSeq->next->pos;
s->e = lastExprInSeq->next;
lastExprInSeq->next = 0;
s->next = stat->next;
stat->next = s;
}
}else if( lastExprInSeq != 0 && lastExprInSeq->next != 0 )
{
// visitExpr returned after non-function call and we would expect the stack to be empty
error(stat,QString("stack is not empty (%1 elements on stack)").arg(stack.size()));
}
}
break;
case IL_goto:
gotos << NamePos(stat, blockStack);
break;
case IL_label:
labels << NamePos(stat, blockStack);
break;
case IL_line:
expectN(0, stat); // TODO
break;
case IL_exit:
if( loopStack.isEmpty() )
error(stat, "exit requires enclosing loop");
break;
case IL_pop:
expectN(1, stat); // we have no reference type information to check here
break;
case IL_free:
if( expectN(1, stat) )
{
Type* t = deref(stat->args->getType());
if( t->kind != Type::Pointer )
{
error(stat, "expecting a pointer argument");
break;
}
}
break;
case IL_starg:
case IL_starg_s:
if( expectN(1, stat) )
{
DeclList params = curProc->getParams();
if( stat->id >= params.size() )
{
error(stat, "the referenced parameter does not exist");
break;
}
Type* lhsT = deref(params[stat->id]->getType());
if( !assigCompat(lhsT,stat->args) )
error(stat, "argument type not compatible with value on stack");
}
break;
case IL_stloc:
case IL_stloc_s:
case IL_stloc_0:
case IL_stloc_1:
case IL_stloc_2:
case IL_stloc_3:
if( expectN(1, stat) )
{
DeclList locals = curProc->getLocals();
if( stat->id >= locals.size() )
{
error(stat, "the referenced local does not exist");
break;
}
Type* lhsT = deref(locals[stat->id]->getType());
if( !assigCompat(lhsT,stat->args) )
{
//assigCompat(lhsT,stat->args); // TEST
error(stat, "local variable type not compatible with value on stack");
}
}
break;
case IL_stvar:
if( expectN(1, stat) )
{
Type* lhsT = deref(stat->d->getType());
if( !assigCompat(lhsT,stat->args) )
{
//assigCompat(lhsT,stat->args); // TEST
error(stat, "module variable type not compatible with value on stack");
}
}
break;
case IL_if:
expectN(0, stat);
stat = visitIfThenElse(stat);
break;
case IL_loop:
expectN(0, stat);
visitLoop(stat);
break;
case IL_repeat:
expectN(0, stat);
visitRepeat(stat);
break;
case IL_ret:
if( curProc && curProc->getType() )
{
if( expectN(1, stat) && !assigCompat(deref(curProc->getType()), stat->args) )
error(stat,"return type incompatible with function type");
}else
{
expectN(0, stat);
if( curProc && curProc->getType() )
error(stat,"return requires a value");
}
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:
case IL_stelem_ipp:
if( expectN(3, stat) )
{
Type* aptr = deref(stat->args->next->rhs->getType());
if( aptr->kind != Type::Pointer )
error(stat,"first argument must be a pointer");
Type* aos = deref(aptr->getType()); // array actually on stack
if( aos->kind != Type::Array )
error(stat,"first argument must be a pointer to array");
Type* index = deref(stat->args->lhs->getType());
if( !isInt32(index) )
error(stat,"second argument must be a 32 bit integer");
Type* etOs = deref(aos->getType()); // element type on stack
Type* refT = tokToBasicType(mdl, stat->kind);
if( stat->kind == IL_stelem )
refT = deref(stat->d);
if( etOs && !equal(etOs,refT) )
error(stat,"the element type on stack is not compatible with the required type");
if( etOs == 0 )
etOs = refT;
if( !assigCompat(etOs, stat->args->rhs) )
error(stat,"value on stack is not compatible with the element type");
}
break;
case IL_stfld:
if( expectN(2, stat) )
{
Type* objptr = deref(stat->args->lhs->getType());
if( objptr->kind != Type::Pointer )
error(stat,"first argument must be a pointer");
Type* objOs = deref(objptr->getType()); // object actually on stack
if( objOs->kind != Type::Struct && objOs->kind != Type::Union && objOs->kind != Type::Object )
error(stat,"first argument must be a pointer to struct, union or object");
Type* refObj = deref(stat->d->outer->getType());
if( objOs && !equal(objOs, refObj) && !Type::isA(objOs,refObj) )
error(stat,"the pointer base type on stack is not compatible with the required type");
Type* refFld = deref(stat->d->getType());
if( !assigCompat(refFld, stat->args->rhs) )
error(stat,"value on stack is not compatible with the pointer base type");
}
break;
case IL_strcpy:
if( expectN(2, stat) )
{
Type* lhsT = deref(stat->args->lhs->getType());
if( lhsT->kind != Type::Pointer )
error(stat,"first argument must be a pointer");
Type* a = deref(lhsT->getType());
if( a->kind != Type::Array || a->len == 0 || deref(a->getType())->kind != Type::CHAR )
error(stat,"first argument must be a pointer to an non-open char array");
Type* rhsT = deref(stat->args->rhs->getType());
if( rhsT->kind == Type::Pointer )
{
a = deref(rhsT->getType());
if( a->kind != Type::Array || a->len != 0 || deref(a->getType())->kind != Type::CHAR )
error(stat,"second argument must be a pointer to an open char array or string literal");
}else if( rhsT->kind != Type::StringLit )
error(stat,"second argument must be a pointer to an open char array or string literal");
}
break;
case IL_stind:
case IL_stind_i1:
case IL_stind_i2:
case IL_stind_i4:
case IL_stind_i8:
case IL_stind_r4:
case IL_stind_r8:
case IL_stind_ip:
case IL_stind_ipp:
if( expectN(2, stat) )
{
Type* ptrT = deref(stat->args->lhs->getType());
if( ptrT->kind != Type::Pointer )
error(stat,"first argument must be a pointer");
Type* baseOs = deref(ptrT->getType()); // pointer basetype on stack
Type* refT = tokToBasicType(mdl, stat->kind);
if( stat->kind == IL_stind )
refT = deref(stat->d->getType());
if( baseOs && !equal(baseOs,refT) )
error(stat,"the pointer base type on stack is not compatible with the required type");
if( baseOs == 0 )
baseOs = refT;
if( !assigCompat(baseOs, stat->args->rhs) )
{
//assigCompat(baseOs, stat->args->rhs); // TEST
error(stat,"value on stack is not compatible with the pointer base type");
}
}
break;
case IL_switch:
expectN(0, stat);
stat = visitSwitch(stat);
break;
case IL_while:
expectN(0, stat);
visitWhile(stat);
break;
default:
error(stat, QString("unexpected statement operator '%1'").arg(s_opName[stat->kind]));
break;
}
#if 0
// this assertion is not generally valid. E.g. "inc(i,off)" generates the following valid MIL:
// ldvara i, ldvar off, stloc_0; after stloc_0 the stack is not expected to be empty!
if( stat->kind != IL_pop // pop is a special statement after which the stack might not be empty
&& stat->kind != Statement::ExprStat && stack.size() != 0 )
error(stat,QString("evaluation stack not empty after statement"));
#endif
stat = nextStat(stat);
}
blockStack.pop_back();
}
Statement* Validator::visitIfThenElse(Statement* stat)
{
Q_ASSERT( stat && stat->kind == IL_if );
visitExpr(stat->e);
expectN(1, stat); // IF args points to the boolean expression,
if( stat->args == 0 || !isInt32(deref(stat->args->getType())) )
error(stat,"expecting a 32 bit result of boolean expression");
visitStatSeq(stat->body);
if( stat->next && stat->next->kind == IL_else )
{
stat = nextStat(stat);
visitStatSeq(stat->body);
}
return stat;
}
void Validator::visitLoop(Statement* stat)
{
Q_ASSERT( stat && stat->kind == IL_loop );
loopStack.push_back(stat);
visitStatSeq(stat->body);
loopStack.pop_back();
}
void Validator::visitRepeat(Statement* stat)
{
Q_ASSERT( stat && stat->kind == IL_repeat );
visitStatSeq(stat->body);
visitExpr(stat->e);
expectN(1, stat);
if( stat->args && !isInt32(deref(stat->args->getType())) )
error(stat,"expecting a 32 bit result of boolean expression");
}
Statement*Validator::visitSwitch(Statement* stat)
{
Q_ASSERT( stat && stat->kind == IL_switch );
visitExpr(stat->e);
expectN(1, stat);
Type* t = deref(stat->args->getType());
if( !isInt32(t) && !t->isInt64() )
error(stat,"expecting a 32 or 64 bit integer expression");
while( stat->next && stat->next->kind == IL_case )
{
stat = nextStat(stat);
Expression* e = stat->e;
while( e )
{
Q_ASSERT( e->kind == IL_case );
// stat->e is a list of integers, each as an Expression
#if 0
// there is no type information
Type* tt = deref(e->getType());
if( !isInt32(tt) && !tt->isInt64() )
error(s,"expecting a 32 or 64 bit integer expression");
#endif
e = e->next;
}
visitStatSeq(stat->body);
}
if( stat->next && stat->next->kind == IL_else )
{
stat = nextStat(stat);
visitStatSeq(stat->body);
}
return stat;
}
void Validator::visitWhile(Statement* stat)
{
Q_ASSERT( stat && stat->kind == IL_while );
visitExpr(stat->e);
expectN(1, stat);
if( stat->args && !isInt32(deref(stat->args->getType())) )
error(stat,"expecting a 32 bit result of boolean expression");
visitStatSeq(stat->body);
}
Statement*Validator::nextStat(Statement* stat)
{
return stat->next;
}
static bool isPointer(Type* t)
{
if( t == 0 )
return false;
else
return t->isPointer();
}
static bool isFunc(Type* t)
{
if( t == 0 )
return false;
else
return t->isFuncOnStack();
}
static bool isMeth(Type* t)
{
if( t == 0 )
return false;
else
return t->isMethOnStack();
}
Expression* Validator::visitExpr(Expression* e)
{
while(e)
{
switch( e->kind )
{
case IL_add:
case IL_div_un:
case IL_div:
case IL_mul:
case IL_sub:
if( expectN(2,e) )
{
e->lhs = stackAt(-2);
e->rhs = stackAt(-1);
Type* lhs = deref(e->lhs->getType());
Type* rhs = deref(e->rhs->getType());
Type* t = 0;
if( lhs->isInt64() && rhs->isInt64() )
t = mdl->getBasicType(Type::INT64);
else if( isInt32(lhs) && isInt32(rhs) )
t = mdl->getBasicType(Type::INT32);
else if( lhs->kind == Type::FLOAT32 && rhs->kind == Type::FLOAT32 )
t = mdl->getBasicType(Type::FLOAT32);
else if( lhs->kind == Type::FLOAT64 && rhs->kind == Type::FLOAT64 )
t = mdl->getBasicType(Type::FLOAT64);
else if( lhs->isFloat() && rhs->isFloat() )
t = mdl->getBasicType(Type::FLOAT64);
else
error(e, QString("the values on the stack are not compatible with the operation '%1'").arg(s_opName[e->kind]));
e->setType(t);
stack.pop_back();
stack.last() = e;
}
break;
case IL_and:
case IL_or:
case IL_xor:
case IL_rem:
case IL_rem_un:
if( expectN(2,e) )
{
e->lhs = stackAt(-2);
e->rhs = stackAt(-1);
Type* lhs = deref(e->lhs->getType());
Type* rhs = deref(e->rhs->getType());
Type* t = 0;
if( lhs->isInt64() && rhs->isInt64() )
t = mdl->getBasicType(Type::INT64);
else if( isInt32(lhs) && isInt32(rhs) )
t = mdl->getBasicType(Type::INT32);
else
error(e, QString("the values on the stack are not compatible with the operation '%1'").arg(s_opName[e->kind]));
e->setType(t);
stack.pop_back();
stack.last() = e;
}
break;
case IL_shl:
case IL_shr_un:
case IL_shr:
if( expectN(2,e) )
{
e->lhs = stackAt(-2); // value
e->rhs = stackAt(-1); // shift amount
Type* lhs = deref(e->lhs->getType());
Type* rhs = deref(e->rhs->getType());
Type* t = 0;
if( lhs->isInt64() && rhs->isInt64() )
t = mdl->getBasicType(Type::INT64);
else if( isInt32(lhs) && isInt32(rhs) )
t = mdl->getBasicType(Type::INT32);
else
error(e, QString("the values on the stack are not compatible with the operation '%1'").arg(s_opName[e->kind]));
e->setType(t);
stack.pop_back();
stack.last() = e;
}
break;
case IL_not:
if( expectN(1,e) )
{
e->lhs = stackAt(-1);
Type* lhs = deref(e->lhs->getType());
Type* t = 0;
if( lhs->isInt64() )
t = mdl->getBasicType(Type::INT64);
else if( isInt32(lhs) )
t = mdl->getBasicType(Type::INT32);
else
error(e, QString("the values on the stack are not compatible with the operation '%1'").arg(s_opName[e->kind]));
e->setType(t);
stack.last() = e;
}
break;
case IL_neg:
case IL_abs:
if( expectN(1,e) )
{
e->lhs = stackAt(-1);
Type* lhs = deref(e->lhs->getType());
Type* t = 0;
if( lhs->isInt64() )
t = mdl->getBasicType(Type::INT64);
else if( isInt32(lhs) )
t = mdl->getBasicType(Type::INT32);
else if( lhs->kind == Type::FLOAT32 )
t = mdl->getBasicType(Type::FLOAT32);
else if( lhs->kind == Type::FLOAT64 )
t = mdl->getBasicType(Type::FLOAT64);
else
error(e, QString("the values on the stack are not compatible with the operation '%1'").arg(s_opName[e->kind]));
e->setType(t);
stack.last() = e;
}
break;
case IL_ldc_i4_0:
case IL_ldc_i4_1:
case IL_ldc_i4_2:
case IL_ldc_i4_3:
case IL_ldc_i4_4:
case IL_ldc_i4_5:
case IL_ldc_i4_6:
case IL_ldc_i4_7:
case IL_ldc_i4_8:
case IL_ldc_i4_m1:
case IL_ldc_i4_s:
case IL_ldc_i4:
e->setType(mdl->getBasicType(Type::INT32));
stack.push_back(e);
break;
case IL_ldc_i8:
e->setType(mdl->getBasicType(Type::INT64));
stack.push_back(e);
break;
case IL_ldc_r4:
e->setType(mdl->getBasicType(Type::FLOAT32));
stack.push_back(e);
break;
case IL_ldc_r8:
e->setType(mdl->getBasicType(Type::FLOAT64));
stack.push_back(e);
break;
// TODO: LDC_ip
case IL_ldnull:
e->setType(mdl->getBasicType(Type::NIL));
stack.push_back(e);
break;
case IL_ldstr:
e->setType(mdl->getBasicType(Type::StringLit));
stack.push_back(e);
break;
case IL_ldc_obj:
e->setType(toType(e->c));
stack.push_back(e);
break;
case IL_ldproc:
if( e->d && (e->d->kind != Declaration::Procedure || e->d->typebound) )
error(e, "expecting an unbound procedure");
if( e->d && e->d->inline_ )
error(e, "cannot take address of inline procedure");
else if(e->d)
{
Type* pt = new Type();
pt->kind = Type::Proc;
pt->setType(e->d->getType());
QList<Declaration*> params = e->d->getParams();
for( int i = 0; i < params.size(); i++ )
{
Declaration* p2 = new Declaration();
*p2 = *params[i];
p2->outer = 0;
p2->ownstype = false;
p2->next = 0;
pt->subs.append(p2);
}
e->setType(pt);
}
stack.push_back(e);
break;
case IL_ldmeth:
{
// expect the object instance pointer on the stack
e->lhs = stackAt(-1); // ptr
Type* lhsT = deref(e->lhs->getType());
Type* ot1 = deref(lhsT->getType());
if( !((lhsT->kind == Type::Pointer && (ot1->kind == Type::Object || ot1->kind == Type::Struct)) || lhsT->kind == Type::Interface) )
{
error(e, "expecting a pointer to object or interface on the stack");
break;
}
if( e->d && (e->d->kind != Declaration::Procedure || !e->d->typebound) )
error(e, "expecting a bound procedure");
// TODO: check whether object on stack is compat with e->d
if( e->d )
{
Type* pt = new Type();
pt->kind = Type::Proc;
pt->typebound = true;
pt->subsborrowed = true;
pt->decl = e->d;
pt->setType(e->d->getType());
QList<Declaration*> params = e->d->getParams(false);
for( int i = 0; i < params.size(); i++ )
pt->subs.append(params[i]); // borrow params
e->setType(pt);
} // else already reported
stack.back() = e; // replace the stack top with methref
}
break;
case IL_ldiface:
{
// expect the object instance pointer on the stack
e->lhs = stackAt(-1); // ptr
Type* lhsT = deref(e->lhs->getType());
Type* ot1 = deref(lhsT->getType());
if( !(lhsT->kind == Type::Pointer && (ot1->kind == Type::Object || ot1->kind == Type::Struct)) )
{
error(e, "expecting a pointer to object or record on the stack");
break;
}
if( e->d && (e->d->kind != Declaration::TypeDecl || deref(e->d->getType())->kind != Type::Interface) )
error(e, "expecting an interface");
e->setType(e->d->getType());
stack.back() = e; // replace the stack top with ldiface
}
break;
case IL_sizeof:
e->setType(mdl->getBasicType(Type::INT32));
stack.push_back(e);
break;
case IL_conv_i1:
case IL_conv_i2:
case IL_conv_i4:
case IL_conv_u1:
case IL_conv_u2:
case IL_conv_u4:
case IL_cast_i4:
if( !expectN(1,e) )
break;
e->lhs = stackAt(-1);
e->setType(mdl->getBasicType(Type::INT32));
stack.back() = e;
break;
case IL_conv_i8:
case IL_conv_u8:
case IL_cast_i8:
if( !expectN(1,e) )
break;
e->lhs = stackAt(-1);
e->setType(mdl->getBasicType(Type::INT64));
stack.back() = e;
break;
case IL_conv_r4:
case IL_cast_r4:
if( !expectN(1,e) )
break;
e->lhs = stackAt(-1);
e->setType(mdl->getBasicType(Type::FLOAT32));
stack.back() = e;
break;
case IL_conv_r8:
case IL_cast_r8:
if( !expectN(1,e) )
break;
e->lhs = stackAt(-1);
e->setType(mdl->getBasicType(Type::FLOAT64));
stack.back() = e;
break;
case IL_castptr:
if( expectN(1,e) )
{
e->lhs = stackAt(-1); // ptr
Type* lhsT = deref(e->lhs->getType());
if( lhsT->kind != Type::Pointer )
{
error(e, "expecting a pointer on the stack");
break;
}
Type* ptr = new Type();
ptr->kind = Type::Pointer;
if( e->d )
ptr->setType(e->d->getType());
e->setType(ptr);
stack.back() = e;
}
break;
case IL_initobj:
if( expectN(1,e) )
{
e->lhs = stackAt(-1); // ptr
Type* lhsT = deref(e->lhs->getType());
Type* t1 = deref(lhsT->getType());
if( lhsT->kind != Type::Pointer ||
!(t1->kind == Type::Struct || t1->kind == Type::Union ||
t1->kind != Type::Object || t1->kind != Type::Array) ||
(t1->kind == Type::Array && t1->len == 0) )
{
error(e, "expecting a pointer to struct, union, object or fixed size array on the stack");
break;
}
Type* t2 = deref(e->d);
if( !equal(t2, t1) )
{
error(e, "initobj type not compatible with type on the stack");
break;
}
stack.pop_back();
}
break;
case IL_ceq:
case IL_cgt_un:
case IL_cgt:
case IL_clt_un:
case IL_clt:
if( expectN(2,e) )
{
e->lhs = stackAt(-2);
e->rhs = stackAt(-1);
Type* lhs = deref(e->lhs->getType());
Type* rhs = deref(e->rhs->getType());
Type* t = 0;
if( (lhs->isInt64() && rhs->isInt64()) ||
(isInt32(lhs) && isInt32(rhs)) ||
(lhs->kind == Type::FLOAT32 && rhs->kind == Type::FLOAT32) ||
(lhs->kind == Type::FLOAT64 && rhs->kind == Type::FLOAT64) ||
(isInt32(lhs) && isPointer(rhs)) ||
(isPointer(lhs) && isInt32(rhs)) ||
(isPointer(lhs) && isPointer(rhs)) ||
(isFunc(lhs) && isFunc(rhs)) ||
(isMeth(lhs) && isMeth(rhs)))
t = mdl->getBasicType(Type::INT32);
else
error(e, QString("the values on the stack are not compatible with the operation '%1'").arg(s_opName[e->kind]));
e->setType(t);
stack.pop_back();
stack.last() = e;
}
break;
case IL_ldarg_0:
case IL_ldarg_1:
case IL_ldarg_2:
case IL_ldarg_3:
case IL_ldarg_s:
case IL_ldarg:
case IL_ldarga_s:
case IL_ldarga:
{
DeclList params = curProc->getParams();
if( e->id >= params.size() )
{
error(e, "the referenced parameter does not exist");
break;
}
Type* paramType = params[e->id]->getType();
if( e->kind == IL_ldarga_s || e->kind == IL_ldarga )
{
Type* ptr = new Type();
ptr->kind = Type::Pointer;
ptr->setType(paramType);
e->setType(ptr);
}else
e->setType(paramType);
stack.push_back(e);
}
break;
case IL_ldloc_0:
case IL_ldloc_1:
case IL_ldloc_2:
case IL_ldloc_3:
case IL_ldloc_s:
case IL_ldloc:
case IL_ldloca_s:
case IL_ldloca:
{
DeclList locals = curProc->getLocals();
if( e->id >= locals.size() )
{
error(e, "the referenced local variable does not exist");
break;
}
if( e->kind == IL_ldloca_s || e->kind == IL_ldloca )
{
Type* ptr = new Type();
ptr->kind = Type::Pointer;
ptr->setType(locals[e->id]->getType());
e->setType(ptr);
}else
e->setType(locals[e->id]->getType());
stack.push_back(e);
}
break;
case IL_ldelem_i1:
case IL_ldelem_i2:
case IL_ldelem_i4:
case IL_ldelem_i8:
case IL_ldelem_ip:
case IL_ldelem_ipp:
case IL_ldelem_r4:
case IL_ldelem_r8:
case IL_ldelem_u1:
case IL_ldelem_u2:
case IL_ldelem_u4:
case IL_ldelem_u8:
case IL_ldelem:
case IL_ldelema:
if( expectN(2,e) )
{
e->lhs = stackAt(-2); // pointer to array
e->rhs = stackAt(-1); // index
Type* lhsT = deref(e->lhs->getType());
Type* rhsT = deref(e->rhs->getType());
Type* array = deref(lhsT->getType());
if( lhsT->kind != Type::Pointer || array->kind != Type::Array || !rhsT->isInteger() )
{
error(e, "expecting a pointer to array and an integer index on the stack");
break;
}
Type* et1 = tokToBasicType(mdl, e->kind);
if( e->kind == IL_ldelem )
et1 = deref(e->d);
Type* et2 = deref(array->getType());
if( et1 && !equal(et1,et2) )
{
error(e, "ldelem type not compatible with array element type on the stack");
break;
}
if( e->kind == IL_ldelema )
{
Type* ptr = new Type();
ptr->kind = Type::Pointer;