forked from rochus-keller/Micron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicEvaluator.cpp
1529 lines (1440 loc) · 42 KB
/
MicEvaluator.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) 2024 Rochus Keller (me@rochus-keller.ch)
**
** This file is part of the Micron language project.
**
**
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file. Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
*/
#include "MicEvaluator.h"
#include "MicMilEmitter.h"
#include "MicBuiltins.h"
#include "MicToken.h"
#include <bitset>
#include <QtDebug>
using namespace Mic;
bool Evaluator::evaluate(Expression* e, bool assureOnMilStack)
{
err.clear();
if( e == 0 )
return false;
recursiveRun(e);
if(assureOnMilStack)
assureTopOnMilStack();
return err.isEmpty();
}
bool Evaluator::unaryOp(quint8 op)
{
err.clear();
if( stack.isEmpty() )
{
err = "nothing on the stack";
return false;
}
Value& v = stack.back();
switch( op )
{
case Expression::Not:
notOp(v);
break;
case Expression::Plus:
unaryPlusOp(v);
break;
case Expression::Minus:
unaryMinusOp(v);
break;
default:
Q_ASSERT(false);
// NOTE: @ is directly handled in the parser
break;
}
return err.isEmpty();
}
Type*Evaluator::smallestUIntType(const QVariant& v) const
{
const quint64 u = v.toULongLong();
if( u <= BasicType::getMax(BasicType::UINT8).toULongLong() )
return mdl->getType(BasicType::UINT8);
else if( u <= BasicType::getMax(BasicType::UINT16).toULongLong() )
return mdl->getType(BasicType::UINT16);
else if( u <= BasicType::getMax(BasicType::UINT32).toULongLong() )
return mdl->getType(BasicType::UINT32);
else
return mdl->getType(BasicType::UINT64);
}
Type*Evaluator::smallestIntType(const QVariant& v) const
{
const qint64 i = v.toLongLong();
if( i >= BasicType::getMin(BasicType::INT8).toLongLong() && i <= BasicType::getMax(BasicType::INT8).toLongLong() )
return mdl->getType(BasicType::INT8);
else if( i >= BasicType::getMin(BasicType::INT16).toLongLong() && i <= BasicType::getMax(BasicType::INT16).toLongLong() )
return mdl->getType(BasicType::INT16);
else if( i >= BasicType::getMin(BasicType::INT32).toLongLong() && i <= BasicType::getMax(BasicType::INT32).toLongLong() )
return mdl->getType(BasicType::INT32);
else
return mdl->getType(BasicType::INT64);
}
bool Evaluator::binaryOp(quint8 op)
{
err.clear();
if( stack.size() < 2 )
{
err = "expecting two values on the stack";
return false;
}
Value rhs = stack.takeLast();
Value lhs = stack.takeLast();
switch( op )
{
// Arith
case Expression::Mul:
case Expression::Fdiv:
case Expression::Div:
case Expression::Mod:
case Expression::Add:
case Expression::Sub:
stack.push_back(arithOp(op,lhs,rhs));
break;
// Logic
case Expression::And:
case Expression::Or:
stack.push_back(logicOp(op,lhs,rhs));
break;
// Relation
case Expression::Eq:
case Expression::Neq:
case Expression::Lt:
case Expression::Leq:
case Expression::Gt:
case Expression::Geq:
stack.push_back(relationOp(op, lhs,rhs));
break;
case Expression::In:
stack.push_back(inOp(lhs,rhs));
break;
}
return err.isEmpty();
}
bool Evaluator::prepareRhs(Type* lhs)
{
err.clear();
if( stack.size() < 1 )
{
err = "expecting a value on the stack";
return false;
}
const Value& rhs = stack.back();
if( rhs.mode == Value::TypeDecl )
{
err = "a type declaration cannot be used as a value";
return false;
}
// make sure also a string literal is put on the stack by value
if( lhs && lhs->form == Type::Array && lhs->base->form == BasicType::CHAR &&
rhs.type->form == BasicType::String )
{ // NOTE: already checked that lhs is large enough for rhs
Q_ASSERT( lhs->len >= quint32(dequote(rhs.val.toByteArray()).size()) );
assureTopOnMilStack();
out->ldobj_(MilQuali());
}else if( lhs && lhs->form == BasicType::CHAR &&
rhs.type->form == BasicType::String )
out->ldc_i4(quint8(dequote(rhs.val.toByteArray())[0]));
else if( lhs && lhs->form == Type::Proc &&
rhs.mode == Value::Procedure )
out->ldproc_(toQuali(rhs.val.value<Declaration*>()));
else
assureTopOnMilStack();
return true;
}
static inline MilQuali coreName(const QByteArray& proc)
{
MilQuali res;
res.first = Token::getSymbol("$MIC");
res.second = Token::getSymbol(proc);
return res;
}
bool Evaluator::assign()
{
err.clear();
if( stack.size() < 2 )
{
err = "expecting two values on the stack";
return false;
}
if( !prepareRhs( stack[stack.size()-2].type ) )
return false;
Value rhs = stack.takeLast();
Value lhs = stack.takeLast();
if( !lhs.ref )
return false; // error reported elsewhere
adjustNumType(rhs.type,lhs.type);
if( lhs.type->form == Type::Array )
{
if( lhs.type->base->form == BasicType::CHAR )
{
// special case both sides char arrays, just copy up to and including zero
if( rhs.type->form == Type::Array && rhs.type->base->form == BasicType::CHAR )
{
Q_ASSERT(rhs.ref);
out->call_(coreName("strcopy"),2);
return err.isEmpty();
}
} // else copy memory block TODO
}
switch(lhs.type->form)
{
case BasicType::BOOLEAN:
case BasicType::CHAR:
case BasicType::UINT8:
case BasicType::INT8:
out->stind_(MilEmitter::I1);
break;
case BasicType::UINT16:
case BasicType::INT16:
out->stind_(MilEmitter::I2);
break;
case BasicType::UINT32:
case BasicType::INT32:
case BasicType::SET:
case Type::ConstEnum:
out->stind_(MilEmitter::I4);
break;
case BasicType::UINT64:
case BasicType::INT64:
out->stind_(MilEmitter::I8);
break;
case BasicType::REAL:
out->stind_(MilEmitter::R4);
break;
case BasicType::LONGREAL:
out->stind_(MilEmitter::R8);
break;
case BasicType::Nil:
case BasicType::String:
case Type::Pointer:
case Type::Proc:
out->stind_(MilEmitter::IntPtr);
break;
case Type::Record:
case Type::Array:
case Type::Generic:
out->stobj_(toQuali(lhs.type));
break;
default:
Q_ASSERT( false );
}
return err.isEmpty();
}
bool Evaluator::derefPointer()
{
err.clear();
if( stack.isEmpty() )
{
err = "expecting a value on the stack";
return false;
}
assureTopIsValue();
Value v = stack.takeLast();
Q_ASSERT( v.type && v.type->form == Type::Pointer && !v.ref );
v.type = v.type->base;
v.ref = true;
stack.push_back(v);
return err.isEmpty();
}
bool Evaluator::derefValue()
{
err.clear();
if( stack.isEmpty() )
{
err = "expecting a value on the stack";
return false;
}
Value v = stack.takeLast();
if( v.isConst() || !v.ref || v.type == 0 )
return false;
Q_ASSERT(!v.isConst());
Q_ASSERT(v.ref && v.type);
v.ref = false;
switch(v.type->form)
{
case BasicType::BOOLEAN:
case BasicType::CHAR:
case BasicType::UINT8:
out->ldind_(MilEmitter::U1);
break;
case BasicType::UINT16:
out->ldind_(MilEmitter::U2);
break;
case BasicType::UINT32:
case BasicType::SET:
out->ldind_(MilEmitter::U4);
break;
case BasicType::UINT64:
out->ldind_(MilEmitter::U8);
break;
case BasicType::INT8:
out->ldind_(MilEmitter::I1);
break;
case BasicType::INT16:
out->ldind_(MilEmitter::I2);
break;
case BasicType::INT32:
case Type::ConstEnum:
out->ldind_(MilEmitter::I4);
break;
case BasicType::INT64:
out->ldind_(MilEmitter::I8);
break;
case BasicType::REAL:
out->ldind_(MilEmitter::R4);
break;
case BasicType::LONGREAL:
out->ldind_(MilEmitter::R8);
break;
case Type::Pointer:
case Type::Proc:
out->ldind_(MilEmitter::IntPtr);
break;
case Type::Record:
case Type::Array:
case Type::Generic:
out->ldobj_(toQuali(v.type));
break;
default:
return false;
}
stack.push_back(v);
return err.isEmpty();
}
void Evaluator::assureTopIsValue()
{
if( stack.isEmpty() )
return;
Value& v = stack.back();
if( v.ref )
derefValue();
}
bool Evaluator::desigField(Declaration* field, bool byVal)
{
err.clear();
if( stack.size() < 1 )
{
err = "expecting a value on the stack";
return false;
}
Value lhs = stack.takeLast(); // record reference
// TODO: desig const record
Q_ASSERT(lhs.ref && lhs.type && lhs.type->form == Type::Record);
Q_ASSERT(field);
const MilTrident desig = qMakePair(toQuali(lhs.type),field->name);
if( byVal )
out->ldfld_(desig);
else
out->ldflda_(desig);
Value res;
res.ref = !byVal;
res.type = field->type;
res.visi = 0;
res.mode = Value::Val;
stack.push_back(res);
return err.isEmpty();
}
bool Evaluator::desigVar(bool byVal)
{
err.clear();
if( stack.isEmpty() )
{
err = "expecting a value on the stack";
return false;
}
Value v = stack.takeLast();
if( byVal )
switch( v.mode )
{
case Value::VarDecl:
out->ldvar_(v.val.value<Qualident>());
break;
case Value::LocalDecl:
out->ldloc_(v.val.toInt());
break;
case Value::ParamDecl:
out->ldarg_(v.val.toInt());
break;
default:
Q_ASSERT(false);
}
else
switch( v.mode )
{
case Value::VarDecl:
out->ldvara_(v.val.value<Qualident>());
break;
case Value::LocalDecl:
out->ldloca_(v.val.toInt());
break;
case Value::ParamDecl:
out->ldarga_(v.val.toInt());
break;
default:
Q_ASSERT(false);
}
v.ref = !byVal;
stack.push_back(v);
return err.isEmpty();
}
bool Evaluator::desigIndex(bool byVal)
{
err.clear();
Value rhs = stack.takeLast(); // index
Value lhs = stack.takeLast(); // array reference
if( !lhs.ref || lhs.type == 0 || lhs.type->form != Type::Array)
return false;
Q_ASSERT(lhs.ref && lhs.type && lhs.type->form == Type::Array);
if( rhs.isConst() )
{
const qint64 idx = rhs.val.toLongLong();
if( idx < 0 || rhs.type->len && rhs.type->len <= idx )
{
err = "index out of range";
return false;
}
pushMilStack(rhs);
}
const Qualident elemType = toQuali(lhs.type->base);
if( byVal )
out->ldelem_(elemType);
else
out->ldelema_(elemType);
Value res;
res.ref = !byVal;
res.type = lhs.type->base;
res.visi = 0;
res.mode = Value::Val;
stack.push_back(res);
return err.isEmpty();
}
bool Evaluator::call(int nArgs)
{
err.clear();
if( stack.size() < nArgs + 1 )
{
err = QString("expecting %1 values on the stack").arg(nArgs+1);
return false;
}
Value callee = stack.takeLast();
if( callee.type == 0 )
callee.type = mdl->getType(BasicType::NoType);
Type* ret = 0;
switch( callee.mode )
{
case Value::Builtin:
{
Builtins bi(this);
bi.callBuiltin(callee.val.toInt(),nArgs);
return err.isEmpty();
}
case Value::Procedure:
{
Declaration* proc = callee.val.value<Declaration*>();
Q_ASSERT(proc);
ret = proc->type;
out->call_(toQuali(proc),nArgs, ret != 0); // TODO: desig in imported module
}
break;
case Value::VarDecl:
case Value::LocalDecl:
case Value::ParamDecl:
case Value::Val:
ret = callee.type->base;
if( callee.type->form == Type::Proc )
{
out->calli_(toQuali(callee.type), nArgs, ret != 0);
break;
}
// else fall through
default:
err = "this expression cannot be called";
break;
}
for( int i = 0; i < nArgs; i++ )
stack.pop_back();
Value tmp;
tmp.mode = Value::Val;
if( ret )
tmp.type = ret;
else
tmp.type = mdl->getType(BasicType::NoType);
stack.push_back(tmp);
return err.isEmpty();
}
bool Evaluator::castPtr(Type* to)
{
err.clear();
if( stack.size() < 1 )
{
err = "expecting a value on the stack";
return false;
}
Value lhs = stack.takeLast(); // object
// TODO
Q_ASSERT(to && to->form == Type::Pointer);
out->castptr_(toQuali(to->base));
// TODO restrict to pointers, add to MIL
lhs.type = to;
stack.push_back(lhs);
return err.isEmpty();
}
bool Evaluator::castNum(Type* to)
{
Q_ASSERT( to && to->isNumber() );
err.clear();
if( stack.size() < 1 )
{
err = "expecting a value on the stack";
return false;
}
if( stack.back().isConst() )
stack.back().type = to;
else
{
MilEmitter::Type tt;
switch(to->form)
{
case BasicType::BOOLEAN:
case BasicType::CHAR:
case BasicType::UINT8:
tt = MilEmitter::U1;
break;
case BasicType::UINT16:
tt = MilEmitter::U2;
break;
case BasicType::UINT32:
case BasicType::SET:
tt = MilEmitter::U4;
break;
case BasicType::UINT64:
tt = MilEmitter::U8;
break;
case BasicType::INT8:
tt = MilEmitter::I1;
break;
case BasicType::INT16:
tt = MilEmitter::I2;
break;
case BasicType::INT32:
tt = MilEmitter::I4;
break;
case BasicType::INT64:
tt = MilEmitter::I8;
break;
case BasicType::REAL:
tt = MilEmitter::R4;
break;
case BasicType::LONGREAL:
tt = MilEmitter::R8;
break;
default:
Q_ASSERT(false);
}
out->conv_(tt);
}
return err.isEmpty();
}
Value Evaluator::pop()
{
Value res;
if( !stack.isEmpty() )
res = stack.takeLast();
return res;
}
Value Evaluator::top()
{
Value res;
if( !stack.isEmpty() )
res = stack.last();
return res;
}
void Evaluator::assureTopOnMilStack(bool pop)
{
if( top().isConst() )
{
Value v = stack.takeLast();
pushMilStack(v);
v.mode = Value::Val;
stack.push_back(v);
}
if(pop)
this->pop();
}
bool Evaluator::pushMilStack(const Value& v)
{
err.clear();
switch( v.mode )
{
case Value::Const:
if( v.type->isSimple() || v.type->form == Type::ConstEnum )
{
switch( v.type->form )
{
case BasicType::String:
out->ldstr_( v.val.toByteArray() );
break;
case BasicType::Nil:
out->ldnull_();
break;
case BasicType::BOOLEAN:
case BasicType::CHAR:
case BasicType::UINT8:
case BasicType::UINT16:
case BasicType::UINT32:
case BasicType::SET:
out->ldc_i4(v.val.toUInt());
break;
case BasicType::UINT64:
out->ldc_i8(v.val.toULongLong());
break;
case BasicType::INT8:
case BasicType::INT16:
case BasicType::INT32:
case Type::ConstEnum:
out->ldc_i4(v.val.toInt());
break;
case BasicType::INT64:
out->ldc_i8(v.val.toLongLong());
break;
case BasicType::REAL:
out->ldc_r4(v.val.toFloat());
break;
case BasicType::LONGREAL:
out->ldc_r8(v.val.toDouble());
break;
default:
Q_ASSERT(false);
}
}else
{
MilObject obj;
obj.typeRef = toQuali(v.type);
obj.data = v.val;
out->ldc_obj(obj);
}
break;
case Value::VarDecl:
case Value::LocalDecl:
{
// TODO
}
break;
case Value::ParamDecl:
{
// TODO
}
break;
default:
err = "not supported for this kind of declaration";
return false;
}
return true;
}
void Evaluator::emitRelOp(quint8 op, bool unsig)
{
switch(op)
{
case Expression::Eq:
out->ceq_();
break;
case Expression::Neq: // not eq
out->ceq_();
out->ldc_i4(0);
out->ceq_();
break;
case Expression::Geq: // not lt
out->clt_(unsig);
out->ldc_i4(0);
out->ceq_();
break;
case Expression::Gt:
out->cgt_(unsig);
break;
case Expression::Leq: // not gt
out->cgt_(unsig);
out->ldc_i4(0);
out->ceq_();
break;
case Expression::Lt:
out->clt_(unsig);
break;
default:
Q_ASSERT(false);
break;
}
}
void Evaluator::emitArithOp(quint8 op, bool unsig, bool i64)
{
switch(op)
{
case Expression::Mul:
out->mul_();
break;
case Expression::Fdiv:
out->div_();
break;
case Expression::Div:
out->div_(unsig);
break;
case Expression::Mod:
out->rem_(unsig);
break;
case Expression::Add:
out->add_();
break;
case Expression::Sub:
out->sub_();
break;
default:
Q_ASSERT(false);
}
}
void Evaluator::adjustNumType(Type* me, Type* other)
{
if( me->isNumber() && other->isNumber() )
{
if( me->isInt() && other->isInt() &&
other->form == BasicType::INT64 && me->form < BasicType::INT64 )
out->conv_(MilEmitter::I8);
else if( me->isUInt() && other->isUInt() &&
other->form == BasicType::UINT64 && me->form < BasicType::UINT64 )
out->conv_(MilEmitter::U8);
else if( me->isReal() && other->isReal() &&
other->form == BasicType::LONGREAL && me->form < BasicType::LONGREAL )
out->conv_(MilEmitter::R8);
}
}
void Evaluator::notOp(Value& v)
{
Q_ASSERT( v.type->isBoolean() );
if( v.isConst() )
v.val = !v.val.toBool();
else
{
// logic not
out->ldc_i4(0);
out->ceq_();
}
}
Value Evaluator::logicOp(quint8 op, const Value& lhs, const Value& rhs)
{
Value res;
if( lhs.type->isBoolean() && rhs.type->isBoolean() )
{
if( lhs.isConst() && rhs.isConst() )
{
res.mode = lhs.mode;
res.type = lhs.type;
if( op == Expression::And )
res.val = lhs.val.toBool() && rhs.val.toBool();
else
res.val = lhs.val.toBool() || rhs.val.toBool();
}else
{
res.mode = Value::Val;
res.type = lhs.type;
// order is irrelevant for logic ops
if( op == Expression::And)
out->and_();
else
out->or_();
}
}else
Q_ASSERT(false);
return res;
}
static inline Type* maxType(Type* lhs, Type* rhs)
{
if( lhs->form >= rhs->form )
return lhs;
else
return rhs;
}
Value Evaluator::arithOp(quint8 op, const Value& lhs, const Value& rhs)
{
Value res;
res.mode = Value::Val;
res.type = lhs.type;
if( lhs.type->isNumber() && rhs.type->isNumber() )
{
res.type = maxType(lhs.type,rhs.type);
if( lhs.type->isInt() && rhs.type->isInt() )
{
if( lhs.isConst() && rhs.isConst() )
{
res.mode = Value::Const;
const qint64 a = lhs.val.toLongLong();
const qint64 b = rhs.val.toLongLong();
switch(op)
{
case Expression::Mul:
res.val = a * b;
break;
case Expression::Div:
res.val = (qint64)(a < 0 ? (a - b + 1) / b : a / b);
break;
case Expression::Mod:
res.val = a < 0 ? (b - 1) + ((a - b + 1)) % b : a % b;
break;
case Expression::Add:
res.val = a + b;
break;
case Expression::Sub:
res.val = a - b;
break;
default:
Q_ASSERT(false);
break;
}
}else
{
emitArithOp(op,false, res.type->form == BasicType::INT64 );
}
}else if( lhs.type->isUInt() && rhs.type->isUInt() )
{
if( lhs.isConst() && rhs.isConst() )
{
res.mode = Value::Const;
const quint64 a = lhs.val.toULongLong();
const quint64 b = rhs.val.toULongLong();
switch(op)
{
case Expression::Mul:
res.val = (quint64)(a * b);
break;
case Expression::Div:
res.val = (quint64)(a / b);
break;
case Expression::Mod:
res.val = (quint64)(a % b);
break;
case Expression::Add:
res.val = (quint64)(a + b);
break;
case Expression::Sub:
res.val = (quint64)(a - b);
break;
default:
Q_ASSERT(false);
break;
}
}else
{
emitArithOp(op,true, res.type->form == BasicType::UINT64);
}
}else if( lhs.type->isReal() && rhs.type->isReal() )
{
if( lhs.isConst() && rhs.isConst() )
{
res.mode = Value::Const;
switch(op)
{
case Expression::Mul:
res.val = lhs.val.toDouble() * rhs.val.toDouble();
break;
case Expression::Fdiv:
res.val = lhs.val.toDouble() / rhs.val.toDouble();
break;
case Expression::Add:
res.val = lhs.val.toDouble() + rhs.val.toDouble();
break;
case Expression::Sub:
res.val = lhs.val.toDouble() - rhs.val.toDouble();
break;
default:
Q_ASSERT(false);
break;
}
}else
{
emitArithOp(op);
}
}else
err = "operation not supported";
}else if( lhs.type->isSet() && rhs.type->isSet() )
{
// + - * /
if( lhs.isConst() && rhs.isConst() )
{
res.mode = Value::Const;
switch(op)
{
case Expression::Mul:
res.val = lhs.val.toUInt() & rhs.val.toUInt();
break;
case Expression::Fdiv:
res.val = ~(lhs.val.toUInt() & rhs.val.toUInt()) & (lhs.val.toUInt() | rhs.val.toUInt());
break;
case Expression::Add:
res.val = lhs.val.toUInt() | rhs.val.toUInt();
break;
case Expression::Sub:
res.val = lhs.val.toUInt() & ~rhs.val.toUInt();
break;
default:
Q_ASSERT(false);
break;
}
}else
{
switch(op)
{
case Expression::Mul:
out->and_();
break;
case Expression::Fdiv:
out->call_(coreName("SetDiv"),2,1);
break;
case Expression::Add:
out->or_();
break;
case Expression::Sub:
out->not_();
out->and_();
break;
default:
Q_ASSERT(false);
break;
}
}
}else if( (lhs.type->form == BasicType::String || lhs.type->form == BasicType::CHAR) &&
(rhs.type->form == BasicType::String || rhs.type->form == BasicType::CHAR) )
{
// + only
res.type = mdl->getType(BasicType::String);
Q_ASSERT( op == Expression::Add );
Q_ASSERT( lhs.isConst() && rhs.isConst() );
res.val = lhs.val.toByteArray() + rhs.val.toByteArray();
}else
err = "operation not supported";
return res;
}
Value Evaluator::relationOp(quint8 op, const Value& lhs, const Value& rhs)
{
Value res;
res.mode = Value::Val;
res.type = mdl->getType(BasicType::BOOLEAN);