-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkaleidoscope.cpp
1061 lines (819 loc) · 25.7 KB
/
kaleidoscope.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
#include "KaleidoscopeJIT.h"
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/InstCombine/InstCombine.h"
#include "llvm/Transforms/Scalar.h"
#include "llvm/Transforms/Scalar/GVN.h"
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include <map>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <unordered_map>
using namespace llvm;
using namespace llvm::orc;
#define IRGEN true
// ---Lexer---
typedef enum {
// EOF
tok_eof = -1,
// keywords
tok_def = -2,
tok_extern = -3,
// things
tok_number = -4,
tok_identifier = -5,
tok_error = -6
}Token_t;
static std::string IdentifierString;
static double NumValue;
static int gettok(void){
static char LastChar = ' ';
while(isspace(LastChar)){
LastChar = getchar();
}
if (isalpha(LastChar)){
IdentifierString = "";
while(isalnum(LastChar) || LastChar == '_'){
IdentifierString += LastChar;
LastChar = getchar();
}
if (IdentifierString == "def"){
return tok_def;
}
else if (IdentifierString == "extern"){
return tok_extern;
}
else{
return tok_identifier;
}
}
else if (isdigit(LastChar) || LastChar == '.'){
std::string NumStr = "";
bool decimal = false;
while(isdigit(LastChar) || (!decimal && (LastChar == '.'))){
NumStr += LastChar;
if (LastChar == '.') {
decimal = true;
}
LastChar = getchar();
}
if (decimal && LastChar == '.'){
return tok_error;
}
NumValue = strtod(NumStr.c_str(), nullptr);
return tok_number;
}
else if (LastChar == '#'){
while(LastChar != EOF && LastChar != '\n' && LastChar != '\r') {
LastChar = getchar();
}
if (LastChar != EOF){
return gettok(); // LOL, cheap trick, but well played
}
}
else if (LastChar == EOF){
return tok_eof;
}
else {
int ThisChar = LastChar;
LastChar = getchar();
return ThisChar;
}
return tok_error;
}
// State variables
static std::unique_ptr<LLVMContext> TheContext;
static std::unique_ptr<Module> TheModule; // to hold blocks, definitions? (TODO), TODO: why does this have to be a pointer?
static std::unique_ptr<IRBuilder<>> Builder; // for creating instructions, constants, etc
static std::unique_ptr<legacy::FunctionPassManager> TheFPM; // Function pass manager
static std::unordered_map<std::string, Value *> Symbols; // Maps names inside function context to LLVM "values"
static std::unique_ptr<KaleidoscopeJIT> TheJIT; // JIT engine for Kaleidoscope
// Prototypes will be codegened in _each_ module, again and again? TODO: check
static ExitOnError ExitOnErr;
// The different types of expressions:
//
// ExprAST an expression a + f(b) + 5
//
// NumExprAST a number 5
//
// VariableExprAST an identifier `a`
//
// CallExprAST a function call `f(b)
//
// BinaryExprAST a binary expression a + b
//
// PrototypeExprAST a function prototype f(a, b, c, d, e)
//
// FunctionExprAST a function declaration prototype
// - f(a, b)
// a + f(b) + 5
// --- AST ---
class ASTVisitor;
class ExprAST {
public:
virtual ~ExprAST() {} // if a base class pointer points to a derived class object
// when it goes out of scope, it should be
// deallocated properly (TODO: understand properly)
// TODO: throws "undefined reference to vtable for ExprAST"
// if accept is not declared as pure virtual
// Why?
// Accept function for visitor pattern
virtual void accept(ASTVisitor& visitor) = 0;
// Generate code for sub-AST
virtual Value* codegen() = 0;
};
class NumExprAST;
class VariableExprAST;
class BinaryExprAST;
class CallExprAST;
class PrototypeAST;
class FunctionAST;
// Visitor class for ExprAST
class ASTVisitor {
public:
virtual void visit(NumExprAST *p_obj) = 0;
virtual void visit(VariableExprAST *p_obj) = 0;
virtual void visit(CallExprAST *p_obj) = 0;
virtual void visit(FunctionAST *p_obj) = 0;
virtual void visit(PrototypeAST *p_obj) = 0;
virtual void visit(BinaryExprAST *p_obj) = 0;
};
std::unique_ptr<ExprAST> LogError(const char *Str) {
fprintf(stderr, "LogError: %s\n", Str);
return nullptr;
}
// TODO: what is this for?
std::unique_ptr<PrototypeAST> LogErrorP(const char *Str) {
LogError(Str);
return nullptr;
}
// For logging errors while doing codegeneration- returns a `null` value, and prints error
Value *LogErrorV(const char *Str) {
LogError(Str);
return nullptr;
}
class NumExprAST: public ExprAST {
private: // default access is private, be explicit
double Val;
public:
NumExprAST(double Val): Val(Val) {}
Value* codegen();
void accept(ASTVisitor& visitor) { visitor.visit(this); }
double GetVal() { return Val; }
};
class VariableExprAST: public ExprAST {
private:
std::string Name;
public:
VariableExprAST(const std::string &Name): Name(Name) {};
Value *codegen();
void accept(ASTVisitor& visitor) { visitor.visit(this); }
// Reference used because the string is not going to be used later
// again, so why waste space? (and it's not going to be modified, so
// const
std::string& GetName() { return Name; }
};
class CallExprAST: public ExprAST {
private:
std::string Callee;
public:
// TODO: figure out a way to keep this private
std::vector<std::unique_ptr <ExprAST> > Args;
CallExprAST(std::string &Callee,
std::vector< std::unique_ptr <ExprAST> > Args_):
Callee(Callee), Args(std::move(Args_)) {}
// TODO: figure out why I need to use move here (because vector itself
// is not a unique_ptr!, to get deleted before use)
// probably so because moving a vector does not move it's contents, just
// it's meta information, while moving a string moves its contents
Value *codegen();
void accept(ASTVisitor& visitor) { visitor.visit(this); }
std::string& GetCallee() { return Callee; }
};
class BinaryExprAST: public ExprAST {
private:
char Op;
public:
// TODO: figure out a way to keep these private
std::unique_ptr<ExprAST> LHS, RHS;
BinaryExprAST(char Op, std::unique_ptr<ExprAST> LHS,
std::unique_ptr<ExprAST> RHS):
Op(Op), LHS(std::move(LHS)), RHS(std::move(RHS)) {}
Value *codegen();
void accept(ASTVisitor& visitor) { visitor.visit(this); }
char GetOp() { return Op; }
};
// Neither a prototype, nor a function is an "expression"
class PrototypeAST {
private:
std::string Name;
std::vector< std::string > Args;
public:
PrototypeAST(const std::string &Name,
std::vector< std::string> Args):
Name(Name), Args(std::move(Args)) {}
Function* codegen();
void accept(ASTVisitor& visitor) { visitor.visit(this); }
std::string& GetName() { return Name; }
std::vector<std::string>& GetArgs() { return Args; }
};
class FunctionAST {
private:
// this is a tree! these should be pointers to members!
// (a function should be an object pointing to these things, not an
// object *containing* these things
public:
// TODO: Figure out a way to make these
// unique_ptr members private, and still
// access them from the visitor
std::unique_ptr<PrototypeAST> Proto;
std::unique_ptr<ExprAST> Body;
FunctionAST(std::unique_ptr<PrototypeAST> Proto,
std::unique_ptr<ExprAST> Body):
Proto(std::move(Proto)), Body(std::move(Body)) {}
// probably when I am getting passed a unique_ptr, the compiler sees
// that it will get deleted when it goes out of scope- *move* is used to
// indicate that I want to be a cannibal
Function* codegen();
void accept(ASTVisitor& visitor) { visitor.visit(this); }
};
static int CurTok;
static void print_tok() {
switch(CurTok) {
case tok_number:
std::cout << "(" << CurTok << ", " << NumValue << ")" << std::endl;
break;
case tok_identifier:
std::cout << "(" << CurTok << ", " << IdentifierString << ")" << std::endl;
break;
case tok_def: case tok_extern:
std::cout << "(" << CurTok << ", " << IdentifierString << ")" << std::endl;
break;
case tok_eof:
std::cout << "(" << "End" << "," << 0 << ")" << std::endl;
break;
case tok_error:
std::cout << "(" << "Error" << "," << 0 << ")" << std::endl;
break;
default:
std::cout << "(" << (char)CurTok << "," << 0 << ")" << std::endl;
break;
}
}
// -- Parser --
int getNextToken() {
CurTok = gettok();
//print_tok();
return CurTok;
}
// LISP-like pretty-printer
class LispPrintVisitor : public ASTVisitor {
public:
LispPrintVisitor(): nesting_depth(0) {}
void visit(NumExprAST *p_obj) {
std::cout << std::string(2 * nesting_depth, ' ') << p_obj->GetVal();
}
void visit(VariableExprAST *p_obj) {
std::cout << std::string(2 * nesting_depth, ' ') << p_obj->GetName();
}
void visit(CallExprAST *p_obj) {
std::cout << std::string(2 * nesting_depth, ' ') << '(' << p_obj->GetCallee();
++nesting_depth;
for (const auto& arg: p_obj->Args) {
std::cout << std::endl;
arg->accept(*this);
}
--nesting_depth;
std::cout << ')';
}
void visit(FunctionAST *p_obj) {
p_obj->Proto->accept(*this);
std::cout << std::endl;
++nesting_depth;
p_obj->Body->accept(*this);
--nesting_depth;
}
void visit(PrototypeAST *p_obj) {
std::cout << "(def (" << p_obj->GetName();
for (auto arg: p_obj->GetArgs()) {
std::cout << ' ' << arg;
}
std::cout << ')';
}
void visit(BinaryExprAST *p_obj) {
std::cout << std::string(2 * nesting_depth, ' ') << '(' << p_obj->GetOp() << std::endl;
++nesting_depth;
p_obj->LHS->accept(*this);
std::cout << std::endl;
p_obj->RHS->accept(*this);
--nesting_depth;
std::cout << ")";
}
private:
int nesting_depth;
};
static std::unique_ptr<ExprAST> ParseNumberExpr();
static std::unique_ptr<ExprAST> ParseParenExpr();
static std::unique_ptr<ExprAST> ParseIdentifierExpr();
static std::unique_ptr<ExprAST> ParsePrimary();
static std::unique_ptr<ExprAST> ParseExpression();
static std::unique_ptr<ExprAST> ParseBinOpRHS(int, std::unique_ptr<ExprAST>);
// numberexpr ::= number
// the number has already been detected in gettok() and is present in
static std::unique_ptr<ExprAST> ParseNumberExpr() {
auto numberExpr = std::make_unique<NumExprAST>(NumValue);
getNextToken();
//fprintf(stderr, "debug: numberexpr\n");
return std::move(numberExpr);
}
// parenexpr:: '(' expression ')'
static std::unique_ptr<ExprAST> ParseParenExpr() {
getNextToken();
auto v = ParseExpression();
if (CurTok != ')') {
return LogError("expected: ')'");
}
//fprintf(stderr, "debug: parenexpr\n");
getNextToken();
return v;
}
// identifierexpr:
// ::= identifier
// ::= identifier '(' e + expression
static std::unique_ptr<ExprAST> ParseIdentifierExpr() {
std::string IdName = IdentifierString; // produced by the tokenizer
getNextToken(); // MUST EAT UP TOKEN BEFORE RETURNING, CURRENT TOKEN IS ID, GET NEXT TOKEN
if (CurTok != '(') {
//fprintf(stderr, "debug: identifier single\n");
return std::make_unique<VariableExprAST>(IdName);
}
getNextToken();
std::vector< std::unique_ptr<ExprAST> > Args;
if (CurTok != ')') {
while (1) {
if (auto v = ParseExpression())
Args.push_back(std::move(v)); // reuse v
else
return nullptr; // there should be an expression if parentheses don't close immediately
if (CurTok == ')')
break;
if (CurTok != ',')
return LogError("expected ',' or ')' in argument list");
getNextToken();
}
}
getNextToken(); // eat ')'
//fprintf(stderr, "debug: identifier two\n");
return std::make_unique<CallExprAST>(IdName, std::move(Args));
}
// primary:
// ::= identifier
// ::= numberexpr
// ::= parenexpr
static std::unique_ptr<ExprAST> ParsePrimary() {
// lookahead?
switch(CurTok) {
case tok_number:
return ParseNumberExpr();
break;
case tok_identifier:
return ParseIdentifierExpr();
break;
case '(':
return ParseParenExpr();
break;
default:
return LogError("unknown token while trying to parse expression");
break;
}
//fprintf(stderr, "debug: primary\n");
return nullptr;
}
static std::map<char, int> BinopPrecedence;
static int getTokPrecedence() {
//printf("debug: getting precedence of: ");
if (!isascii(CurTok)) // not an operator, stop parsing expression
return -1;
int TokPrec = BinopPrecedence[CurTok];
if (TokPrec <= 0) return -1;
return TokPrec;
}
// TODO: install precedence in main()
// BinopPrecedence['<'] = 10;
// BinopPrecedence['+'] = 20;
// BinopPrecedence['-'] = 20;
// BinopPrecedence['*'] = 40;
// expression =
// ::= primary binoprhs
static std::unique_ptr<ExprAST> ParseExpression() {
auto LHS = ParsePrimary();
////fprintf(stderr, "debug: Parsed LHS\n");
if (!LHS)
return nullptr;
////fprintf(stderr, "debug: Calling ParseBinOpRHS\n");
//fprintf(stderr, "debug: expression\n");
return ParseBinOpRHS(0, std::move(LHS));
}
// binoprhs =
// ::= (op binoprhs)*
static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS) {
while (1) { // parses (op binoprhs)
int TokPrec = getTokPrecedence();
if (TokPrec < ExprPrec) {
//fprintf(stderr, "debug: less precedence\n");
return LHS;
}
int BinOp = CurTok;
getNextToken(); // ate op
//fprintf(stderr, "debug: Parsing RHS");
auto RHS = ParsePrimary(); // ate binoprhs
int NextOpPrec = getTokPrecedence();
if (!RHS)
return nullptr;
// peek at next operator
if (NextOpPrec > TokPrec) {
// has larger precedence, eat all the large precedence
// operators first
RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS));
if (!RHS)
return nullptr;
}
// now the precedence of the next operator is less than or equal
// to tokprec, so the previous expressions can be safely
// combined
//
//std::cerr << "(" << "LHS" << " " << BinOp << " " << "RHS" << std::endl;
LHS = std::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS));
// we have our new LHS, on to the next!
}
}
// prototype:
// ::= identifier '(' identifier* ')'
static std::unique_ptr<PrototypeAST> ParsePrototype() {
if (CurTok != tok_identifier)
return LogErrorP("Expected function name in prototype");
std::string FunctionName = std::move(IdentifierString);
getNextToken();
if (CurTok != '(')
return LogErrorP("Expected '(' in prototype");
std::vector<std::string> Args;
while (getNextToken() == tok_identifier)
Args.push_back(std::move(IdentifierString));
if (CurTok != ')')
LogErrorP("Expected ',' in prototype");
getNextToken(); // after parsing is done, fetch next token
auto prot = std::make_unique<PrototypeAST>(FunctionName, std::move(Args));
//fprintf(stderr, "debug: prototype\n");
return prot;
}
// definition:
// ::= 'def' prototype expression
static std::unique_ptr<FunctionAST> ParseDefinition() {
// eat up "def"
getNextToken();
auto Proto = ParsePrototype();
if (!Proto) {
return nullptr;
}
auto E = ParseExpression();
if (!E) {
return nullptr;
}else {
//fprintf(stderr, "debug: definition\n");
return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
}
}
// extern:
// ::= 'extern' prototype
static std::unique_ptr<PrototypeAST> ParseExtern() {
getNextToken();
return ParsePrototype();
}
// toplevelexpr:
// ::= expr
static std::unique_ptr<FunctionAST> ParseTopLevelExpr() {
if (auto E = ParseExpression()) {
auto Proto = std::make_unique<PrototypeAST>("__anon_expr", std::vector<std::string>());
//fprintf(stderr, "debug: toplevelexpr\n");
return std::make_unique<FunctionAST>(std::move(Proto), std::move(E));
}
return nullptr;
}
static std::unordered_map<std::string, std::unique_ptr<PrototypeAST>> FunctionProtos; // Function Name -> PrototypeAST Node map
// -- Code Generator --
static void InitializeModuleAndPassManager() {
TheContext = std::make_unique<LLVMContext>();
TheModule = std::make_unique<Module>("kaleidoscope", *TheContext);
TheModule->setDataLayout(TheJIT->getDataLayout());
Builder = std::make_unique<IRBuilder<>>(*TheContext);
// Why .get? Ahh- I want to pass a pointer. What about uniqueness?
TheFPM = std::make_unique<legacy::FunctionPassManager>(TheModule.get());
// Peephole optimizations
TheFPM->add(createInstructionCombiningPass());
// ?
TheFPM->add(createReassociatePass());
// Global value numbering-> common subexpression elimination. Global is actually per-function
TheFPM->add(createGVNPass());
// Dead code elimination pass;
TheFPM->add(createCFGSimplificationPass());
// Run initalizers for all passes added to pass manager
TheFPM->doInitialization();
}
Function *getOrCreateFunction(const std::string& Name) {
// Check whether declaration is present in current module
if (auto *F = TheModule->getFunction(Name)) {
// Hypothesis: When each function is created in a new module, this will never happen
return F;
}
// Check whether this function has been declared previously
auto F_itr = FunctionProtos.find(Name);
if (F_itr != FunctionProtos.end()) {
// If yes, codegen declaration to _this module_.
return F_itr->second->codegen();
}
return nullptr;
}
// Create a new constant of type "double"
Value* NumExprAST::codegen() {
return ConstantFP::get(Builder->getDoubleTy(), Val);
}
// Return a pointer to the value that this variable refers to
Value* VariableExprAST::codegen() {
Value *varval = Symbols[Name];
if (!varval) {
return LogErrorV((std::string("Undefined reference: ") + Name).c_str());
}
return varval;
}
// Generates code for function call, returns `Value` of function call
Value* CallExprAST::codegen() {
// Obtain function with name `Callee` from Module
Function *func = getOrCreateFunction(Callee);
if (!func) {
return LogErrorV((std::string("undefined function: ") + Callee).c_str());
}
// "Type Check" call
if (Args.size() != func->arg_size()) {
return LogErrorV((
std::string("Invalid number of arguments in function call to function")
+ Callee).c_str()
);
}
// Generate code for arguments, and get their values
std::vector<Value *> Argvec;
for (const auto& arg: Args) {
Argvec.push_back(arg->codegen());
}
// TODO: why do I need to provide TheContext to getDoubleTy?
//std::vector<Type *> ArgTypes = std::vector<Type *>(Args.size(), Builder->getDoubleTy());
// Create type for call
// TODO: this is not needed: CreateCall can be called without a type
// Why is this so? Is it because the function does not take variable arguments?
//FunctionType *func_type = FunctionType::get(Builder->getDoubleTy(), ArgTypes, false);
// Create call
return Builder->CreateCall(func, Argvec, "call");
}
Value* BinaryExprAST::codegen() {
Value *L = LHS->codegen();
Value *R = RHS->codegen();
if (!L || !R) {
return nullptr;
}
switch(Op) {
case '+':
return Builder->CreateFAdd(L, R, "add");
break;
case '-':
return Builder->CreateFSub(L, R, "sub");
break;
case '*':
return Builder->CreateFMul(L, R, "mul");
break;
case '/':
// TODO: do static analysis to ensure that RHS is not a 0?
return Builder->CreateFDiv(L, R, "div");
break;
case '<':
L = Builder->CreateFCmp(CmpInst::FCMP_OLT, L, R, "lessthan");
return Builder->CreateUIToFP(L, Builder->getDoubleTy(), "booltofp");
break;
case '>':
L = Builder->CreateFCmp(CmpInst::FCMP_UGT, L, R, "greaterthan");
return Builder->CreateUIToFP(L, Builder->getDoubleTy(), "booltofp");
default:
return LogErrorV("Invalid Operator");
break;
}
}
Function* PrototypeAST::codegen() {
std::vector<Type *> Argtypes = std::vector<Type *>(Args.size(), Builder->getDoubleTy());
FunctionType *func_type = FunctionType::get(Builder->getDoubleTy(), Argtypes, false);
// TODO: why do I use TheModule.get() here? Why not *TheModule? how will things change due to this?
Function *func = Function::Create(func_type, Function::ExternalLinkage, Name, TheModule.get());
unsigned Idx = 0;
for (Argument& x: func->args()) {
x.setName(Args[Idx++]);
}
return func;
}
Function* FunctionAST::codegen() {
// TODO: why are we doing this? This codegen method will never be called
// for an extern function, right? Why else do I need to check?
const std::string& func_name = Proto->GetName();
// Make global FunctionProto map the owner of function prototype node
// This ensures that declaration can be codegened in different modules
FunctionProtos[func_name] = std::move(Proto);
Function *func = getOrCreateFunction(func_name);
if (!func) {
return nullptr;
}
BasicBlock *BB = BasicBlock::Create(*TheContext, "entry", func);
Builder->SetInsertPoint(BB);
Symbols.clear();
for (auto& arg: func->args()) {
// I could have used the AST to find the names-
// but I've already stored this information in the function
// prototype
Symbols[std::string(arg.getName())] = &arg;
}
Value *retval = Body->codegen();
if (retval) {
Builder->CreateRet(retval);
// TODO: Does this mean that my "write head" is at the end of the function-
// but I do not need to move it immediately, because the only place where
// writes will happen will be while generating code for another function,
// and I _will_ call SetInsertPoint in that function anyway?
// TODO: What if this check fails? Do I still continue?
verifyFunction(*func);
// Run passes on function
TheFPM->run(*func);
return func;
}
// For recovering from errors- improperly defined functions should not persist.
func->eraseFromParent();
return nullptr;
}
// --- Driver ---
static void HandleDefinition() {
if (auto def = ParseDefinition()) {
#if DEBUGPARSE
LispPrintVisitor lvt;
def->accept(lvt);
#endif
#if IRGEN
if (Function *func = def->codegen()) {
func->print(errs());
ExitOnErr(TheJIT->addModule(
ThreadSafeModule(std::move(TheModule), std::move(TheContext))
));
InitializeModuleAndPassManager();
fprintf(stderr, "\n");
fprintf(stderr, "Read a function definition\n");
}
#endif
}else {
getNextToken(); // skip token
}
}
static void HandleExtern() {
if (auto extn = ParseExtern()) {
#if DEBUGPARSE
LispPrintVisitor lvt;
extn->accept(lvt);
#endif
#if IRGEN
if (Function *func = extn->codegen()) {
func->print(errs());
fprintf(stderr, "\n");
fprintf(stderr, "Read an extern\n");
FunctionProtos[extn->GetName()] = std::move(extn);
}
#endif
}else {
getNextToken();
}
}
static void HandleTopLevelExpression() {
if (const auto tle = ParseTopLevelExpr()) {
#if DEBUGPARSE
LispPrintVisitor lvt;
tle->accept(lvt);
#endif
#if IRGEN
if (Function *func = tle->codegen()) {
func->print(errs());
fprintf(stderr, "\n");
fprintf(stderr, "Parsed a top level expression\n");
// TODO: how do I know which functions to call? In this case, I have the
// tutorial for reference. What if I don't know what does what?
auto RT = TheJIT->getMainJITDylib().createResourceTracker();
// TODO: wasn't the context supposed to be unique for the
// program? If this context is now owned by the JIT, then
// will each top level expression (and even each function)
// be created in a new context?
auto TSM = ThreadSafeModule(std::move(TheModule), std::move(TheContext));
ExitOnErr(TheJIT->addModule(std::move(TSM), RT));
// Now, the next function will be placed in a new Module?
InitializeModuleAndPassManager();
auto ExprSymbol = ExitOnErr(TheJIT->lookup("__anon_expr"));
// TODO: why do I need intptr_t
double (*Fn)() = (double(*)())(intptr_t)ExprSymbol.getAddress();
fprintf(stderr, "Evaluated to %lf\n", Fn());
ExitOnErr(RT->remove());
}
#endif
} else {
getNextToken();
}
}
// top = definition | expression | external | ;
static void MainLoop() {
while(true) {
fprintf(stderr, "ready>");
switch (CurTok) {
case tok_eof:
return;
break;
case tok_def:
HandleDefinition();
break;
case tok_extern:
HandleExtern();
break;
case ';':
getNextToken();
break;
default:
HandleTopLevelExpression();
break;
}
}
}
int oldmain(void) {
int Token;