forked from AliveToolkit/alive2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalive_parser.cpp
1433 lines (1266 loc) · 35.7 KB
/
alive_parser.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) 2018-present The Alive2 Authors.
// Distributed under the MIT license that can be found in the LICENSE file.
#include "tools/alive_parser.h"
#include "ir/constant.h"
#include "ir/precondition.h"
#include "ir/value.h"
#include "tools/alive_lexer.h"
#include "util/compiler.h"
#include <cassert>
#include <memory>
#include <unordered_map>
#include <vector>
#define YYDEBUG 0
#if YYDEBUG
#include <iostream>
#endif
using namespace IR;
using namespace tools;
using namespace std;
static_assert(LEXER_READ_AHEAD == PARSER_READ_AHEAD);
namespace tools {
static void error(string &&s) {
throw ParseException(std::move(s), yylineno);
}
static void error(const char *s, token t) {
throw ParseException(string(s) + "; got: " + token_name[t], yylineno);
}
static vector<unique_ptr<IntType>> int_types;
static vector<unique_ptr<PtrType>> pointer_types;
static FloatType half_type("half", FloatType::Half);
static FloatType float_type("float", FloatType::Float);
static FloatType double_type("double", FloatType::Double);
static unordered_map<string, Value*> identifiers, identifiers_src;
static Function *fn;
static BasicBlock *bb;
static bool parse_src;
static Type& get_sym_type();
static Value& parse_operand(Type &type);
static Value& get_constant(uint64_t n, Type &t) {
auto c = make_unique<IntConst>(t, n);
auto ret = c.get();
fn->addConstant(std::move(c));
return *ret;
}
static Value& get_num_constant(string_view n, Type &t) {
auto c = make_unique<IntConst>(t, string(n));
auto ret = c.get();
fn->addConstant(std::move(c));
return *ret;
}
static Value& get_constant(string_view name, Type &type) {
string id(name);
if (auto I = identifiers.find(id);
I != identifiers.end())
return *I->second;
auto c = make_unique<ConstantInput>(type, string(id));
auto ret = c.get();
fn->addInput(std::move(c));
identifiers.emplace(std::move(id), ret);
return *ret;
}
static Value& get_fp_constant(string_view n, Type &t) {
auto c = make_unique<FloatConst>(t, string(n), false);
auto ret = c.get();
fn->addConstant(std::move(c));
return *ret;
}
namespace {
struct tokenizer_t {
token last;
// true if token last was 'unget' and should be returned next
bool returned = false;
token operator*() {
if (returned) {
returned = false;
return last;
}
return get_new_token();
}
token peek() {
if (returned)
return last;
returned = true;
return last = get_new_token();
}
bool consumeIf(token expected) {
auto token = peek();
if (token == expected) {
returned = false;
return true;
}
return false;
}
void ensure(token expected) {
auto t = **this;
if (t != expected)
error(string("expected token: ") + token_name[expected] + ", got: " +
token_name[t]);
}
void unget(token t) {
assert(returned == false);
returned = true;
last = t;
}
bool empty() {
return peek() == END;
}
bool isType() {
return isScalarType() || isVectorType() || isArrayType() || isStructType();
}
bool isScalarType() {
return peek() == INT_TYPE || peek() == HALF ||
peek() == FLOAT || peek() == DOUBLE || peek() == STAR;
}
bool isVectorType() {
return peek() == VECTOR_TYPE_PREFIX;
}
bool isArrayType() {
return peek() == ARRAY_TYPE_PREFIX;
}
bool isStructType() {
return peek() == LBRACE;
}
private:
token get_new_token() const {
try {
auto t = yylex();
#if YYDEBUG
cout << "token: " << token_name[t] << '\n';
#endif
return t;
} catch (LexException &e) {
throw ParseException(std::move(e.str), e.lineno);
}
}
};
}
static tokenizer_t tokenizer;
static void parse_name(Transform &t) {
if (tokenizer.consumeIf(NAME))
t.name = yylval.str;
}
static string parse_global_name() {
tokenizer.ensure(GLOBAL_NAME);
return string(yylval.str);
}
static Value& parse_const_expr(Type &type);
static Predicate* parse_predicate(Predicate *last = nullptr,
token last_t = END) {
switch (auto t = *tokenizer) {
case LPAREN: {
auto newpred = parse_predicate();
tokenizer.ensure(RPAREN);
if (last == nullptr) {
parse_more:
switch (auto t = *tokenizer) {
case BAND:
case BOR:
return parse_predicate(newpred, t);
default:
tokenizer.unget(t);
return newpred;
}
}
if (last_t == BAND) {
auto p = make_unique<BoolPred>(*last, *newpred, BoolPred::AND);
newpred = p.get();
fn->addPredicate(std::move(p));
goto parse_more;
}
assert(last_t == BOR);
switch (auto t = *tokenizer) {
case BAND: {
newpred = parse_predicate(newpred, BAND);
auto p = make_unique<BoolPred>(*last, *newpred, BoolPred::OR);
auto ret = p.get();
fn->addPredicate(std::move(p));
return ret;
}
case BOR: {
auto p = make_unique<BoolPred>(*last, *newpred, BoolPred::OR);
auto ret = p.get();
fn->addPredicate(std::move(p));
return parse_predicate(ret, BOR);
}
default: {
tokenizer.unget(t);
auto p = make_unique<BoolPred>(*last, *newpred,
last_t == BAND ? BoolPred::AND :
BoolPred::OR);
auto ret = p.get();
fn->addPredicate(std::move(p));
return ret;
}
}
break;
}
case IDENTIFIER: {
// function: identifier '(' arg0, ... argn ')'
vector<Value*> args;
auto name = yylval.str;
tokenizer.ensure(LPAREN);
while (true) {
args.emplace_back(&parse_operand(get_sym_type()));
if (!tokenizer.consumeIf(COMMA))
break;
}
tokenizer.ensure(RPAREN);
auto p = make_unique<FnPred>(name, std::move(args));
auto ret = p.get();
fn->addPredicate(std::move(p));
return ret;
}
case CONSTANT:
//TODO
parse_const_expr(*int_types[64].get());
break;
default:
error("Expected predicate", t);
}
UNREACHABLE();
}
static void parse_pre(Transform &t) {
if (!tokenizer.consumeIf(PRE))
return;
fn = &t.src;
t.precondition = parse_predicate();
}
static void parse_comma() {
tokenizer.ensure(COMMA);
}
static uint64_t parse_number() {
tokenizer.ensure(NUM);
return yylval.num;
}
static unordered_map<Type*, unique_ptr<StructType>> overflow_aggregate_types;
static vector<unique_ptr<SymbolicType>> sym_types;
static unsigned sym_num;
static unsigned struct_num;
static Type& get_overflow_type(Type &type) {
auto p = overflow_aggregate_types.try_emplace(&type);
auto &st = p.first->second;
if (p.second)
// TODO: Knowing exact layout of { i1, ity } needs data layout.
st = make_unique<StructType>("structty_" + to_string(struct_num++),
initializer_list<Type*>({ &type, int_types[1].get() }),
initializer_list<bool>({ false, false }));
return *st.get();
}
static Type& get_sym_type() {
// NOTE: don't reuse sym_types across transforms or printing is messed up
return *sym_types.emplace_back(
make_unique<SymbolicType>("symty_" + to_string(sym_num++))).get();
}
static Type& get_int_type(unsigned size) {
if (size >= int_types.size())
int_types.resize(size + 1);
if (!int_types[size])
int_types[size] = make_unique<IntType>("i" + to_string(size), size);
return *int_types[size].get();
}
static Type& get_pointer_type(unsigned address_space_number) {
if (address_space_number >= pointer_types.size())
pointer_types.resize(address_space_number + 1);
if (!pointer_types[address_space_number])
pointer_types[address_space_number] = make_unique<PtrType>(
address_space_number);
return *pointer_types[address_space_number].get();
}
static vector<unique_ptr<VectorType>> vector_types;
static vector<unique_ptr<ArrayType>> array_types;
static vector<unique_ptr<StructType>> struct_types;
static Type& parse_scalar_type() {
switch (*tokenizer) {
case INT_TYPE:
if (yylval.num > 4 * 1024)
error("Int type too long: " + to_string(yylval.num));
return get_int_type(yylval.num);
case HALF:
return half_type;
case FLOAT:
return float_type;
case DOUBLE:
return double_type;
case STAR:
// TODO: Pointer type of non-zero address space
return get_pointer_type(0);
default:
UNREACHABLE();
}
}
static Type& parse_vector_type() {
tokenizer.ensure(VECTOR_TYPE_PREFIX);
unsigned elements = yylval.num;
Type &elemTy = parse_scalar_type();
tokenizer.ensure(CSGT);
return *vector_types.emplace_back(
make_unique<VectorType>("vty_" + to_string(vector_types.size()),
elements, elemTy)).get();
}
static Type& parse_array_type();
static Type& parse_struct_type();
static Type& parse_type(bool optional = true) {
if (tokenizer.isScalarType())
return parse_scalar_type();
if (tokenizer.isArrayType())
return parse_array_type();
if (tokenizer.isVectorType())
return parse_vector_type();
if (tokenizer.isStructType())
return parse_struct_type();
if (optional)
return get_sym_type();
else
error("Expecting a type", tokenizer.peek());
UNREACHABLE();
}
static Type& parse_array_type() {
tokenizer.ensure(ARRAY_TYPE_PREFIX);
unsigned elements = yylval.num;
Type &elemTy = parse_type(false);
tokenizer.ensure(RSQBRACKET);
return *array_types.emplace_back(
make_unique<ArrayType>("aty_" + to_string(array_types.size()),
elements, elemTy)).get();
}
static Type& parse_struct_type() {
tokenizer.ensure(LBRACE);
vector<Type*> tys;
vector<bool> padding;
do {
tys.emplace_back(&parse_type(false));
padding.emplace_back(false);
switch (auto t = *tokenizer) {
case COMMA: break;
case RBRACE: goto end;
default:
error("Expected comma or }", t);
}
} while (true);
end:
return *struct_types.emplace_back(
make_unique<StructType>("sty_" + to_string(struct_types.size()),
std::move(tys), std::move(padding))).get();
}
static Type& try_parse_type(Type &default_type) {
if (tokenizer.isType())
return parse_type();
return default_type;
}
static Value& parse_const_expr(Type &type) {
switch (auto t = *tokenizer) {
case LPAREN: {
auto &ret = parse_const_expr(type);
tokenizer.ensure(RPAREN);
return ret;
}
case CONSTANT:
return get_constant(yylval.str, type);
case IDENTIFIER: {
string_view name = yylval.str;
vector<Value*> args;
tokenizer.ensure(LPAREN);
if (!tokenizer.consumeIf(RPAREN)) {
do {
args.push_back(&parse_operand(parse_type()));
} while (tokenizer.consumeIf(COMMA));
tokenizer.ensure(RPAREN);
}
try {
auto f = make_unique<ConstantFn>(type, name, std::move(args));
auto ret = f.get();
fn->addConstant(std::move(f));
return *ret;
} catch (ConstantFnException &e) {
error(std::move(e.str));
}
}
default:
error("Expected constant expression", t);
}
UNREACHABLE();
}
static Value& get_or_copy_instr(const string &name) {
assert(!parse_src);
if (auto I = identifiers.find(name);
I != identifiers.end())
return *I->second;
// we need to either copy instruction(s) from src, or it's an error
auto I_src = identifiers_src.find(name);
if (I_src == identifiers_src.end())
error("Cannot declare an input variable in the target: " + name);
auto val_src = I_src->second;
assert(!dynamic_cast<Input*>(val_src));
auto instr_src = dynamic_cast<Instr*>(val_src);
assert(instr_src);
auto tgt_instr = instr_src->dup(*fn, "");
for (auto &op : instr_src->operands()) {
if (dynamic_cast<Input*>(op)) {
tgt_instr->rauw(*op, *identifiers.at(op->getName()));
} else if (dynamic_cast<UndefValue*>(op)) {
auto newop = make_unique<UndefValue>(op->getType());
tgt_instr->rauw(*op, *newop.get());
fn->addUndef(std::move(newop));
} else if (dynamic_cast<PoisonValue*>(op)) {
auto newop = make_unique<PoisonValue>(op->getType());
tgt_instr->rauw(*op, *newop.get());
fn->addConstant(std::move(newop));
} else if (dynamic_cast<NullPointerValue*>(op)) {
auto newop = make_unique<NullPointerValue>(op->getType());
tgt_instr->rauw(*op, *newop.get());
fn->addConstant(std::move(newop));
} else if (auto c = dynamic_cast<IntConst*>(op)) {
auto newop = make_unique<IntConst>(*c);
tgt_instr->rauw(*op, *newop.get());
fn->addConstant(std::move(newop));
} else if (auto c = dynamic_cast<FloatConst*>(op)) {
auto newop = make_unique<FloatConst>(*c);
tgt_instr->rauw(*op, *newop.get());
fn->addConstant(std::move(newop));
} else if (dynamic_cast<ConstantInput*>(op)) {
assert(0 && "TODO");
} else if (dynamic_cast<ConstantBinOp*>(op)) {
assert(0 && "TODO");
} else if (dynamic_cast<ConstantFn*>(op)) {
assert(0 && "TODO");
} else if (dynamic_cast<Instr*>(op)) {
// FIXME: support for PHI nodes (cyclic graph)
tgt_instr->rauw(*op, get_or_copy_instr(op->getName()));
} else {
UNREACHABLE();
}
}
auto ret = tgt_instr.get();
identifiers.emplace(name, ret);
if (!bb->empty() &&
(dynamic_cast<JumpInstr*>(&bb->back()) ||
dynamic_cast<Return*>(&bb->back())))
bb->addInstrAt(std::move(tgt_instr), &bb->back(), true);
else
bb->addInstr(std::move(tgt_instr));
return *ret;
}
static Value& parse_aggregate_constant(Type &type, token close_tk) {
vector<Value*> vals;
do {
Type &elemTy = parse_scalar_type();
Value *elem = &parse_operand(elemTy);
vals.emplace_back(elem);
} while (tokenizer.consumeIf(COMMA));
tokenizer.ensure(close_tk);
auto c = make_unique<AggregateValue>(type, std::move(vals));
auto ret = c.get();
fn->addConstant(std::move(c));
return *ret;
}
static Value& parse_operand(Type &type) {
switch (auto t = *tokenizer) {
case NUM:
return get_constant(yylval.num, type);
case FP_NUM:
return get_fp_constant(yylval.str, type);
case NUM_STR:
return get_num_constant(yylval.str, type);
case CSLT:
return parse_aggregate_constant(type, CSGT);
case LSQBRACKET:
return parse_aggregate_constant(type, RSQBRACKET);
case TRUE:
return get_constant(1, *int_types[1]);
case FALSE:
return get_constant(0, *int_types[1]);
case UNDEF: {
auto val = make_unique<UndefValue>(type);
auto ret = val.get();
fn->addUndef(std::move(val));
return *ret;
}
case POISON: {
auto val = make_unique<PoisonValue>(type);
auto ret = val.get();
fn->addConstant(std::move(val));
return *ret;
}
case NULLTOKEN: {
auto val = make_unique<NullPointerValue>(type);
auto ret = val.get();
fn->addConstant(std::move(val));
return *ret;
}
case REGISTER: {
string id(yylval.str);
if (auto I = identifiers.find(id);
I != identifiers.end())
return *I->second;
if (parse_src) {
auto input = make_unique<Input>(type, string(id));
auto ret = input.get();
fn->addInput(std::move(input));
identifiers.emplace(std::move(id), ret);
return *ret;
}
return get_or_copy_instr(id);
}
case CONSTANT:
case IDENTIFIER:
case LPAREN:
tokenizer.unget(t);
return parse_const_expr(type);
default:
error("Expected an operand", t);
}
UNREACHABLE();
}
static unsigned parse_nsw_nuw() {
unsigned flags = BinOp::None;
while (true) {
if (tokenizer.consumeIf(NSW)) {
flags |= BinOp::NSW;
} else if (tokenizer.consumeIf(NUW)) {
flags |= BinOp::NUW;
} else {
break;
}
}
return flags;
}
static unsigned parse_exact() {
if (tokenizer.consumeIf(EXACT))
return BinOp::Exact;
return BinOp::None;
}
static FastMathFlags parse_fast_math(token op_token) {
FastMathFlags fmath;
while (true) {
if (tokenizer.consumeIf(NNAN)) {
fmath.flags |= FastMathFlags::NNaN;
} else if (tokenizer.consumeIf(NINF)) {
fmath.flags |= FastMathFlags::NInf;
} else if (tokenizer.consumeIf(NSZ)) {
fmath.flags |= FastMathFlags::NSZ;
} else {
break;
}
}
return fmath;
}
static unsigned parse_binop_flags(token op_token) {
switch (op_token) {
case ADD:
case SUB:
case MUL:
case SHL:
return parse_nsw_nuw();
case SDIV:
case UDIV:
case LSHR:
case ASHR:
return parse_exact();
case FADD:
case FSUB:
case FMUL:
case FDIV:
case FREM:
case FMAX:
case FMIN:
case FMAXIMUM:
case FMINIMUM:
case SREM:
case UREM:
case UADD_SAT:
case SADD_SAT:
case USUB_SAT:
case SSUB_SAT:
case USHL_SAT:
case SSHL_SAT:
case AND:
case OR:
case XOR:
case CTTZ:
case CTLZ:
case SADD_OVERFLOW:
case UADD_OVERFLOW:
case SSUB_OVERFLOW:
case USUB_OVERFLOW:
case SMUL_OVERFLOW:
case UMUL_OVERFLOW:
case UMIN:
case UMAX:
case SMIN:
case SMAX:
case ABS:
return BinOp::None;
default:
UNREACHABLE();
}
}
static unique_ptr<Instr> parse_binop(string_view name, token op_token) {
auto flags = parse_binop_flags(op_token);
auto &type = parse_type();
auto &a = parse_operand(type);
parse_comma();
Type &type_rhs = try_parse_type(type);
auto &b = parse_operand(type_rhs);
Type *rettype = &type;
BinOp::Op op;
switch (op_token) {
case ADD: op = BinOp::Add; break;
case SUB: op = BinOp::Sub; break;
case MUL: op = BinOp::Mul; break;
case SDIV: op = BinOp::SDiv; break;
case UDIV: op = BinOp::UDiv; break;
case SREM: op = BinOp::SRem; break;
case UREM: op = BinOp::URem; break;
case SHL: op = BinOp::Shl; break;
case LSHR: op = BinOp::LShr; break;
case ASHR: op = BinOp::AShr; break;
case AND: op = BinOp::And; break;
case OR: op = BinOp::Or; break;
case XOR: op = BinOp::Xor; break;
case CTTZ: op = BinOp::Cttz; break;
case CTLZ: op = BinOp::Ctlz; break;
case SADD_SAT: op = BinOp::SAdd_Sat; break;
case UADD_SAT: op = BinOp::UAdd_Sat; break;
case SSUB_SAT: op = BinOp::SSub_Sat; break;
case USUB_SAT: op = BinOp::USub_Sat; break;
case SSHL_SAT: op = BinOp::SShl_Sat; break;
case USHL_SAT: op = BinOp::UShl_Sat; break;
case SADD_OVERFLOW:
op = BinOp::SAdd_Overflow;
rettype = &get_overflow_type(type);
break;
case UADD_OVERFLOW:
op = BinOp::UAdd_Overflow;
rettype = &get_overflow_type(type);
break;
case SSUB_OVERFLOW:
op = BinOp::SSub_Overflow;
rettype = &get_overflow_type(type);
break;
case USUB_OVERFLOW:
op = BinOp::USub_Overflow;
rettype = &get_overflow_type(type);
break;
case SMUL_OVERFLOW:
op = BinOp::SMul_Overflow;
rettype = &get_overflow_type(type);
break;
case UMUL_OVERFLOW:
op = BinOp::UMul_Overflow;
rettype = &get_overflow_type(type);
break;
case UMIN: op = BinOp::UMin; break;
case UMAX: op = BinOp::UMax; break;
case SMIN: op = BinOp::SMin; break;
case SMAX: op = BinOp::SMax; break;
case ABS: op = BinOp::Abs; break;
default:
UNREACHABLE();
}
return make_unique<BinOp>(*rettype, string(name), a, b, op, flags);
}
static unique_ptr<Instr> parse_fp_binop(string_view name, token op_token) {
auto fmath = parse_fast_math(op_token);
auto &type = parse_type();
auto &a = parse_operand(type);
parse_comma();
Type &type_rhs = try_parse_type(type);
auto &b = parse_operand(type_rhs);
Type *rettype = &type;
FpBinOp::Op op;
switch (op_token) {
case FADD: op = FpBinOp::FAdd; break;
case FSUB: op = FpBinOp::FSub; break;
case FMUL: op = FpBinOp::FMul; break;
case FDIV: op = FpBinOp::FDiv; break;
case FREM: op = FpBinOp::FRem; break;
case FMAX: op = FpBinOp::FMax; break;
case FMIN: op = FpBinOp::FMin; break;
case FMAXIMUM: op = FpBinOp::FMaximum; break;
case FMINIMUM: op = FpBinOp::FMinimum; break;
default:
UNREACHABLE();
}
return make_unique<FpBinOp>(*rettype, string(name), a, b, op, fmath);
}
static unique_ptr<Instr> parse_unaryop(string_view name, token op_token) {
UnaryOp::Op op;
switch (op_token) {
case BITREVERSE: op = UnaryOp::BitReverse; break;
case BSWAP: op = UnaryOp::BSwap; break;
case CTPOP: op = UnaryOp::Ctpop; break;
case FFS: op = UnaryOp::FFS; break;
default:
UNREACHABLE();
}
auto &ty = parse_type();
auto &a = parse_operand(ty);
return make_unique<UnaryOp>(ty, string(name), a, op);
}
static unique_ptr<Instr> parse_fp_unaryop(string_view name, token op_token) {
auto fmath = parse_fast_math(op_token);
FpUnaryOp::Op op;
switch (op_token) {
case FABS: op = FpUnaryOp::FAbs; break;
case FNEG: op = FpUnaryOp::FNeg; break;
default:
UNREACHABLE();
}
auto &ty = parse_type();
auto &a = parse_operand(ty);
return make_unique<FpUnaryOp>(ty, string(name), a, op, fmath);
}
static unique_ptr<Instr> parse_unary_reduction_op(string_view name,
token op_token) {
UnaryReductionOp::Op op;
switch (op_token) {
case REDUCE_ADD: op = UnaryReductionOp::Add; break;
case REDUCE_MUL: op = UnaryReductionOp::Mul; break;
case REDUCE_AND: op = UnaryReductionOp::And; break;
case REDUCE_OR: op = UnaryReductionOp::Or; break;
case REDUCE_XOR: op = UnaryReductionOp::Xor; break;
case REDUCE_SMAX: op = UnaryReductionOp::SMax; break;
case REDUCE_SMIN: op = UnaryReductionOp::SMin; break;
case REDUCE_UMAX: op = UnaryReductionOp::UMax; break;
case REDUCE_UMIN: op = UnaryReductionOp::UMin; break;
default: UNREACHABLE();
}
auto &op_ty = parse_type();
auto &ty =
op_ty.isVectorType() ? op_ty.getAsAggregateType()->getChild(0) : op_ty;
auto &a = parse_operand(op_ty);
return make_unique<UnaryReductionOp>(ty, string(name), a, op);
}
static unique_ptr<Instr> parse_ternary(string_view name, token op_token) {
TernaryOp::Op op;
switch (op_token) {
case FSHL: op = TernaryOp::FShl; break;
case FSHR: op = TernaryOp::FShr; break;
case SMULFIX: op = TernaryOp::SMulFix; break;
case UMULFIX: op = TernaryOp::UMulFix; break;
case SMULFIXSAT: op = TernaryOp::SMulFixSat; break;
case UMULFIXSAT: op = TernaryOp::UMulFixSat; break;
default:
UNREACHABLE();
}
auto &aty = parse_type();
auto &a = parse_operand(aty);
parse_comma();
auto &bty = parse_type();
auto &b = parse_operand(bty);
parse_comma();
auto &cty = parse_type();
auto &c = parse_operand(cty);
return make_unique<TernaryOp>(aty, string(name), a, b, c, op);
}
static unique_ptr<Instr> parse_fp_ternary(string_view name, token op_token) {
auto fmath = parse_fast_math(op_token);
FpTernaryOp::Op op;
switch (op_token) {
case FMA: op = FpTernaryOp::FMA; break;
default:
UNREACHABLE();
}
auto &aty = parse_type();
auto &a = parse_operand(aty);
parse_comma();
auto &bty = parse_type();
auto &b = parse_operand(bty);
parse_comma();
auto &cty = parse_type();
auto &c = parse_operand(cty);
return make_unique<FpTernaryOp>(aty, string(name), a, b, c, op, fmath);
}
static unique_ptr<Instr> parse_conversionop(string_view name, token op_token) {
// op ty %op to ty2
auto &opty = parse_type();
auto &val = parse_operand(opty);
auto &ty2 = parse_type(/*optional=*/!tokenizer.consumeIf(TO));
ConversionOp::Op op;
switch (op_token) {
case BITCAST: op = ConversionOp::BitCast; break;
case SEXT: op = ConversionOp::SExt; break;
case ZEXT: op = ConversionOp::ZExt; break;
case TRUNC: op = ConversionOp::Trunc; break;
case PTRTOINT: op = ConversionOp::Ptr2Int; break;
default:
UNREACHABLE();
}
return make_unique<ConversionOp>(ty2, string(name), val, op);
}
static unique_ptr<Instr>
parse_fp_conversionop(string_view name, token op_token) {
// op ty %op to ty2
auto &opty = parse_type();
auto &val = parse_operand(opty);
auto &ty2 = parse_type(/*optional=*/!tokenizer.consumeIf(TO));
FpConversionOp::Op op;
switch (op_token) {
case SITOFP: op = FpConversionOp::SIntToFP; break;
case UITOFP: op = FpConversionOp::UIntToFP; break;
case FPTOSI: op = FpConversionOp::FPToSInt; break;
case FPTOUI: op = FpConversionOp::FPToUInt; break;
case FPEXT: op = FpConversionOp::FPExt; break;
case FPTRUNC: op = FpConversionOp::FPTrunc; break;
default:
UNREACHABLE();
}
return make_unique<FpConversionOp>(ty2, string(name), val, op);
}
static unique_ptr<Instr> parse_select(string_view name) {
// select condty %cond, ty %a, ty %b
auto &condty = parse_type();
auto &cond = parse_operand(condty);
parse_comma();
auto &aty = parse_type();
auto &a = parse_operand(aty);
parse_comma();
auto &bty = parse_type();
auto &b = parse_operand(bty);
return make_unique<Select>(aty, string(name), cond, a, b);
}
static unique_ptr<Instr> parse_extractvalue(string_view name) {
auto &type = parse_type();
auto &val = parse_operand(type);
auto instr = make_unique<ExtractValue>(get_sym_type(), string(name), val);
while (true) {
if (!tokenizer.consumeIf(COMMA))
break;
instr->addIdx((unsigned)parse_number());
}
return instr;
}
static unique_ptr<Instr> parse_insertvalue(string_view name) {
auto &type = parse_type();
auto &val = parse_operand(type);
parse_comma();
auto &elt_ty = parse_type();
auto &elt = parse_operand(elt_ty);
auto instr = make_unique<InsertValue>(type, string(name), val, elt);
while (true) {
if (!tokenizer.consumeIf(COMMA))
break;
instr->addIdx((unsigned)parse_number());
}
return instr;
}
static ICmp::Cond parse_icmp_cond() {
switch (auto t = *tokenizer) {
case EQ: return ICmp::EQ;
case NE: return ICmp::NE;
case SLE: return ICmp::SLE;
case SLT: return ICmp::SLT;
case SGE: return ICmp::SGE;
case SGT: return ICmp::SGT;
case ULE: return ICmp::ULE;
case ULT: return ICmp::ULT;
case UGE: return ICmp::UGE;
case UGT: return ICmp::UGT;
default:
tokenizer.unget(t);
return ICmp::Any;
}
}
static unique_ptr<Instr> parse_icmp(string_view name) {
// icmp cond ty %a, &b
auto cond = parse_icmp_cond();
auto &ty = parse_type();
auto &a = parse_operand(ty);
parse_comma();
auto &b = parse_operand(ty);
return make_unique<ICmp>(*int_types[1].get(), string(name), cond, a, b);
}