llvm.org GIT mirror llvm / 545a2be
Update tutorial to reflect the current APIs. Also correct a small omission in LangImpl6.html (it needed to defined the 'binary :' operator). PR9052 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@142123 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 7 years ago
6 changed file(s) with 240 addition(s) and 215 deletion(s). Raw diff Collapse all Expand all
800800
801801
802802

                  
                
803 # Compile
804 g++ -g -O3 toy.cpp
805 # Run
806 ./a.out
803 # Compile
804 clang++ -g -O3 toy.cpp
805 # Run
806 ./a.out
807807
808808
809809
265265 if (ArgsV.back() == 0) return 0;
266266 }
267267
268 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
268 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
269269 }
270270
271271
307307

                  
                
308308 Function *PrototypeAST::Codegen() {
309309 // Make the function type: double(double,double) etc.
310 std::vector<const Type*> Doubles(Args.size(),
311 Type::getDoubleTy(getGlobalContext()));
310 std::vector<Type*> Doubles(Args.size(),
311 Type::getDoubleTy(getGlobalContext()));
312312 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
313313 Doubles, false);
314
314
315315 Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
316316
317317
531531

                  
                
532532 ready> 4+5;
533533 Read top-level expression:
534 define double @""() {
534 define double @0() {
535535 entry:
536 ret double 9.000000e+00
536 ret double 9.000000e+00
537537 }
538538
539539
552552 Read function definition:
553553 define double @foo(double %a, double %b) {
554554 entry:
555 %multmp = fmul double %a, %a
556 %multmp1 = fmul double 2.000000e+00, %a
557 %multmp2 = fmul double %multmp1, %b
558 %addtmp = fadd double %multmp, %multmp2
559 %multmp3 = fmul double %b, %b
560 %addtmp4 = fadd double %addtmp, %multmp3
561 ret double %addtmp4
555 %multmp = fmul double %a, %a
556 %multmp1 = fmul double 2.000000e+00, %a
557 %multmp2 = fmul double %multmp1, %b
558 %addtmp = fadd double %multmp, %multmp2
559 %multmp3 = fmul double %b, %b
560 %addtmp4 = fadd double %addtmp, %multmp3
561 ret double %addtmp4
562562 }
563563
564564
572572 Read function definition:
573573 define double @bar(double %a) {
574574 entry:
575 %calltmp = call double @foo(double %a, double 4.000000e+00)
576 %calltmp1 = call double @bar(double 3.133700e+04)
577 %addtmp = fadd double %calltmp, %calltmp1
578 ret double %addtmp
575 %calltmp = call double @foo(double %a, double 4.000000e+00)
576 %calltmp1 = call double @bar(double 3.133700e+04)
577 %addtmp = fadd double %calltmp, %calltmp1
578 ret double %addtmp
579579 }
580580
581581
592592
593593 ready> cos(1.234);
594594 Read top-level expression:
595 define double @""() {
595 define double @1() {
596596 entry:
597 %calltmp = call double @cos(double 1.234000e+00)
598 ret double %calltmp
597 %calltmp = call double @cos(double 1.234000e+00)
598 ret double %calltmp
599599 }
600600
601601
608608 ready> ^D
609609 ; ModuleID = 'my cool jit'
610610
611 define double @""() {
611 define double @0() {
612612 entry:
613 %addtmp = fadd double 4.000000e+00, 5.000000e+00
614 ret double %addtmp
613 %addtmp = fadd double 4.000000e+00, 5.000000e+00
614 ret double %addtmp
615615 }
616616
617617 define double @foo(double %a, double %b) {
618618 entry:
619 %multmp = fmul double %a, %a
620 %multmp1 = fmul double 2.000000e+00, %a
621 %multmp2 = fmul double %multmp1, %b
622 %addtmp = fadd double %multmp, %multmp2
623 %multmp3 = fmul double %b, %b
624 %addtmp4 = fadd double %addtmp, %multmp3
625 ret double %addtmp4
619 %multmp = fmul double %a, %a
620 %multmp1 = fmul double 2.000000e+00, %a
621 %multmp2 = fmul double %multmp1, %b
622 %addtmp = fadd double %multmp, %multmp2
623 %multmp3 = fmul double %b, %b
624 %addtmp4 = fadd double %addtmp, %multmp3
625 ret double %addtmp4
626626 }
627627
628628 define double @bar(double %a) {
629629 entry:
630 %calltmp = call double @foo(double %a, double 4.000000e+00)
631 %calltmp1 = call double @bar(double 3.133700e+04)
632 %addtmp = fadd double %calltmp, %calltmp1
633 ret double %addtmp
630 %calltmp = call double @foo(double %a, double 4.000000e+00)
631 %calltmp1 = call double @bar(double 3.133700e+04)
632 %addtmp = fadd double %calltmp, %calltmp1
633 ret double %addtmp
634634 }
635635
636636 declare double @cos(double)
637637
638 define double @""() {
638 define double @1() {
639639 entry:
640 %calltmp = call double @cos(double 1.234000e+00)
641 ret double %calltmp
640 %calltmp = call double @cos(double 1.234000e+00)
641 ret double %calltmp
642642 }
643643
644644
669669
670670
671671

                  
                
672 # Compile
673 g++ -g -O3 toy.cpp `llvm-config --cppflags --ldflags --libs core` -o toy
674 # Run
675 ./toy
672 # Compile
673 clang++ -g -O3 toy.cpp `llvm-config --cppflags --ldflags --libs core` -o toy
674 # Run
675 ./toy
676676
677677
678678
10801080 if (ArgsV.back() == 0) return 0;
10811081 }
10821082
1083 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
1083 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
10841084 }
10851085
10861086 Function *PrototypeAST::Codegen() {
10871087 // Make the function type: double(double,double) etc.
1088 std::vector<const Type*> Doubles(Args.size(),
1089 Type::getDoubleTy(getGlobalContext()));
1088 std::vector<Type*> Doubles(Args.size(),
1089 Type::getDoubleTy(getGlobalContext()));
10901090 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
10911091 Doubles, false);
10921092
342342
343343

                  
                
344344 ready> 4+5;
345 define double @""() {
345 Read top-level expression:
346 define double @0() {
346347 entry:
347 ret double 9.000000e+00
348 ret double 9.000000e+00
348349 }
349350
350351 Evaluated to 9.000000
362363 Read function definition:
363364 define double @testfunc(double %x, double %y) {
364365 entry:
365 %multmp = fmul double %y, 2.000000e+00
366 %addtmp = fadd double %multmp, %x
367 ret double %addtmp
366 %multmp = fmul double %y, 2.000000e+00
367 %addtmp = fadd double %multmp, %x
368 ret double %addtmp
368369 }
369370
370371 ready> testfunc(4, 10);
371 define double @""() {
372 Read top-level expression:
373 define double @1() {
372374 entry:
373 %calltmp = call double @testfunc(double 4.000000e+00, double 1.000000e+01)
374 ret double %calltmp
375 %calltmp = call double @testfunc(double 4.000000e+00, double 1.000000e+01)
376 ret double %calltmp
375377 }
376378
377379 Evaluated to 24.000000
403405 declare double @cos(double)
404406
405407 ready> sin(1.0);
408 Read top-level expression:
409 define double @2() {
410 entry:
411 ret double 0x3FEAED548F090CEE
412 }
413
406414 Evaluated to 0.841471
407415
408416 ready> def foo(x) sin(x)*sin(x) + cos(x)*cos(x);
409417 Read function definition:
410418 define double @foo(double %x) {
411419 entry:
412 %calltmp = call double @sin(double %x)
413 %multmp = fmul double %calltmp, %calltmp
414 %calltmp2 = call double @cos(double %x)
415 %multmp4 = fmul double %calltmp2, %calltmp2
416 %addtmp = fadd double %multmp, %multmp4
417 ret double %addtmp
420 %calltmp = call double @sin(double %x)
421 %multmp = fmul double %calltmp, %calltmp
422 %calltmp2 = call double @cos(double %x)
423 %multmp4 = fmul double %calltmp2, %calltmp2
424 %addtmp = fadd double %multmp, %multmp4
425 ret double %addtmp
418426 }
419427
420428 ready> foo(4.0);
429 Read top-level expression:
430 define double @3() {
431 entry:
432 %calltmp = call double @foo(double 4.000000e+00)
433 ret double %calltmp
434 }
435
421436 Evaluated to 1.000000
422437
423438
483498
484499
485500

                  
                
486 # Compile
487 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
488 # Run
489 ./toy
501 # Compile
502 clang++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
503 # Run
504 ./toy
490505
491506
492507
508523 #include "llvm/Analysis/Verifier.h"
509524 #include "llvm/Analysis/Passes.h"
510525 #include "llvm/Target/TargetData.h"
511 #include "llvm/Target/TargetSelect.h"
512526 #include "llvm/Transforms/Scalar.h"
513527 #include "llvm/Support/IRBuilder.h"
528 #include "llvm/Support/TargetSelect.h"
514529 #include <cstdio>
515530 #include <string>
516531 #include <map>
904919 if (ArgsV.back() == 0) return 0;
905920 }
906921
907 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
922 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
908923 }
909924
910925 Function *PrototypeAST::Codegen() {
911926 // Make the function type: double(double,double) etc.
912 std::vector<const Type*> Doubles(Args.size(),
913 Type::getDoubleTy(getGlobalContext()));
927 std::vector<Type*> Doubles(Args.size(),
928 Type::getDoubleTy(getGlobalContext()));
914929 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
915930 Doubles, false);
916931
10121027 // Evaluate a top-level expression into an anonymous function.
10131028 if (FunctionAST *F = ParseTopLevelExpr()) {
10141029 if (Function *LF = F->Codegen()) {
1030 fprintf(stderr, "Read top-level expression:");
1031 LF->dump();
1032
10151033 // JIT the function, returning a function pointer.
10161034 void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
10171035
10751093
10761094 // Create the JIT. This takes ownership of the module.
10771095 std::string ErrStr;
1078 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
1096 TheExecutionEngine = EngineBuilder(TheModule).setErrorStr(&ErrStr).create();
10791097 if (!TheExecutionEngine) {
10801098 fprintf(stderr, "Could not create ExecutionEngine: %s\n", ErrStr.c_str());
10811099 exit(1);
258258
259259 define double @baz(double %x) {
260260 entry:
261 %ifcond = fcmp one double %x, 0.000000e+00
262 br i1 %ifcond, label %then, label %else
261 %ifcond = fcmp one double %x, 0.000000e+00
262 br i1 %ifcond, label %then, label %else
263263
264264 then: ; preds = %entry
265 %calltmp = call double @foo()
266 br label %ifcont
265 %calltmp = call double @foo()
266 br label %ifcont
267267
268268 else: ; preds = %entry
269 %calltmp1 = call double @bar()
270 br label %ifcont
269 %calltmp1 = call double @bar()
270 br label %ifcont
271271
272272 ifcont: ; preds = %else, %then
273 %iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ]
274 ret double %iftmp
273 %iftmp = phi double [ %calltmp, %then ], [ %calltmp1, %else ]
274 ret double %iftmp
275275 }
276276
277277
659659
660660 define double @printstar(double %n) {
661661 entry:
662 ; initial value = 1.0 (inlined into phi)
663 br label %loop
662 ; initial value = 1.0 (inlined into phi)
663 br label %loop
664664
665665 loop: ; preds = %loop, %entry
666 %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ]
667 ; body
668 %calltmp = call double @putchard(double 4.200000e+01)
669 ; increment
670 %nextvar = fadd double %i, 1.000000e+00
671
672 ; termination test
673 %cmptmp = fcmp ult double %i, %n
674 %booltmp = uitofp i1 %cmptmp to double
675 %loopcond = fcmp one double %booltmp, 0.000000e+00
676 br i1 %loopcond, label %loop, label %afterloop
666 %i = phi double [ 1.000000e+00, %entry ], [ %nextvar, %loop ]
667 ; body
668 %calltmp = call double @putchard(double 4.200000e+01)
669 ; increment
670 %nextvar = fadd double %i, 1.000000e+00
671
672 ; termination test
673 %cmptmp = fcmp ult double %i, %n
674 %booltmp = uitofp i1 %cmptmp to double
675 %loopcond = fcmp one double %booltmp, 0.000000e+00
676 br i1 %loopcond, label %loop, label %afterloop
677677
678678 afterloop: ; preds = %loop
679 ; loop always returns 0.0
680 ret double 0.000000e+00
679 ; loop always returns 0.0
680 ret double 0.000000e+00
681681 }
682682
683683
828828
829829
830830

With the code for the body of the loop complete, we just need to finish up

831 the control flow for it. This code remembers the end block (for the phi node), then creates the block for the loop exit ("afterloop"). Based on the value of the
832 exit condition, it creates a conditional branch that chooses between executing
833 the loop again and exiting the loop. Any future code is emitted in the
834 "afterloop" block, so it sets the insertion position to it.

831 the control flow for it. This code remembers the end block (for the phi node),
832 then creates the block for the loop exit ("afterloop"). Based on the value of
833 the exit condition, it creates a conditional branch that chooses between
834 executing the loop again and exiting the loop. Any future code is emitted in
835 the "afterloop" block, so it sets the insertion position to it.

835836
836837
837838

                  
                
879880
880881
881882

                  
                
882 # Compile
883 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
884 # Run
885 ./toy
883 # Compile
884 clang++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
885 # Run
886 ./toy
886887
887888
888889
899900 #include "llvm/Analysis/Verifier.h"
900901 #include "llvm/Analysis/Passes.h"
901902 #include "llvm/Target/TargetData.h"
902 #include "llvm/Target/TargetSelect.h"
903903 #include "llvm/Transforms/Scalar.h"
904904 #include "llvm/Support/IRBuilder.h"
905 #include "llvm/Support/TargetSelect.h"
905906 #include <cstdio>
906907 #include <string>
907908 #include <map>
13961397 if (ArgsV.back() == 0) return 0;
13971398 }
13981399
1399 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
1400 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
14001401 }
14011402
14021403 Value *IfExprAST::Codegen() {
15451546
15461547 Function *PrototypeAST::Codegen() {
15471548 // Make the function type: double(double,double) etc.
1548 std::vector<const Type*> Doubles(Args.size(),
1549 Type::getDoubleTy(getGlobalContext()));
1549 std::vector<Type*> Doubles(Args.size(),
1550 Type::getDoubleTy(getGlobalContext()));
15501551 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
15511552 Doubles, false);
15521553
292292 Function *F = TheModule->getFunction(std::string("binary")+Op);
293293 assert(F && "binary operator not found!");
294294
295 Value *Ops[] = { L, R };
296 return Builder.CreateCall(F, Ops, Ops+2, "binop");
295 Value *Ops[2] = { L, R };
296 return Builder.CreateCall(F, Ops, "binop");
297297 }
298298
299299
504504
505505

                  
                
506506 ready> extern printd(x);
507 Read extern: declare double @printd(double)
507 Read extern:
508 declare double @printd(double)
509
508510 ready> def binary : 1 (x y) 0; # Low-precedence operator that ignores operands.
509511 ..
510512 ready> printd(123) : printd(456) : printd(789);
554556 def binary = 9 (LHS RHS)
555557 !(LHS < RHS | LHS > RHS);
556558
559 # Define ':' for sequencing: as a low-precedence operator that ignores operands
560 # and just returns the RHS.
561 def binary : 1 (x y) y;
557562
558563
559564
578583 else
579584 putchard(42); # '*'
580585 ...
581 ready> printdensity(1): printdensity(2): printdensity(3) :
582 printdensity(4): printdensity(5): printdensity(9): putchard(10);
583 *++..
586 ready> printdensity(1): printdensity(2): printdensity(3):
587 printdensity(4): printdensity(5): printdensity(9):
588 putchard(10);
589 **++.
584590 Evaluated to 0.000000
585591
586592
592598
593599
594600

                  
                
595 # determine whether the specific location diverges.
601 # Determine whether the specific location diverges.
596602 # Solve for z = z^2 + c in the complex plane.
597603 def mandleconverger(real imag iters creal cimag)
598604 if iters > 255 | (real*real + imag*imag > 4) then
602608 2*real*imag + cimag,
603609 iters+1, creal, cimag);
604610
605 # return the number of iterations required for the iteration to escape
611 # Return the number of iterations required for the iteration to escape
606612 def mandleconverge(real imag)
607613 mandleconverger(real, imag, 0, real, imag);
608614
609615
610616
611

This "z = z2 + c" function is a beautiful little creature that is the basis

612 for computation of the
613 href="http://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot Set. Our
614 mandelconverge function returns the number of iterations that it takes
615 for a complex orbit to escape, saturating to 255. This is not a very useful
616 function by itself, but if you plot its value over a two-dimensional plane,
617 you can see the Mandelbrot set. Given that we are limited to using putchard
618 here, our amazing graphical output is limited, but we can whip together
617

This "z = z2 + c" function is a beautiful little

618 creature that is the basis for computation of
619 the Mandelbrot Set.
620 Our mandelconverge function returns the number of iterations that it
621 takes for a complex orbit to escape, saturating to 255. This is not a very
622 useful function by itself, but if you plot its value over a two-dimensional
623 plane, you can see the Mandelbrot set. Given that we are limited to using
624 putchard here, our amazing graphical output is limited, but we can whip together
619625 something using the density plotter above:

620626
621627
622628

                  
                
623 # compute and plot the mandlebrot set with the specified 2 dimensional range
629 # Compute and plot the mandlebrot set with the specified 2 dimensional range
624630 # info.
625631 def mandelhelp(xmin xmax xstep ymin ymax ystep)
626632 for y = ymin, y < ymax, ystep in (
807813
808814
809815

                  
                
810 # Compile
811 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
812 # Run
813 ./toy
816 # Compile
817 clang++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
818 # Run
819 ./toy
814820
815821
816822
833839 #include "llvm/Analysis/Verifier.h"
834840 #include "llvm/Analysis/Passes.h"
835841 #include "llvm/Target/TargetData.h"
836 #include "llvm/Target/TargetSelect.h"
837842 #include "llvm/Transforms/Scalar.h"
838843 #include "llvm/Support/IRBuilder.h"
844 #include "llvm/Support/TargetSelect.h"
839845 #include <cstdio>
840846 #include <string>
841847 #include <map>
14141420 Function *F = TheModule->getFunction(std::string("binary")+Op);
14151421 assert(F && "binary operator not found!");
14161422
1417 Value *Ops[] = { L, R };
1418 return Builder.CreateCall(F, Ops, Ops+2, "binop");
1423 Value *Ops[2] = { L, R };
1424 return Builder.CreateCall(F, Ops, "binop");
14191425 }
14201426
14211427 Value *CallExprAST::Codegen() {
14341440 if (ArgsV.back() == 0) return 0;
14351441 }
14361442
1437 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
1443 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
14381444 }
14391445
14401446 Value *IfExprAST::Codegen() {
15831589
15841590 Function *PrototypeAST::Codegen() {
15851591 // Make the function type: double(double,double) etc.
1586 std::vector<const Type*> Doubles(Args.size(),
1587 Type::getDoubleTy(getGlobalContext()));
1592 std::vector<Type*> Doubles(Args.size(),
1593 Type::getDoubleTy(getGlobalContext()));
15881594 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
15891595 Doubles, false);
15901596
101101
102102 define i32 @test(i1 %Condition) {
103103 entry:
104 br i1 %Condition, label %cond_true, label %cond_false
104 br i1 %Condition, label %cond_true, label %cond_false
105105
106106 cond_true:
107 %X.0 = load i32* @G
108 br label %cond_next
107 %X.0 = load i32* @G
108 br label %cond_next
109109
110110 cond_false:
111 %X.1 = load i32* @H
112 br label %cond_next
111 %X.1 = load i32* @H
112 br label %cond_next
113113
114114 cond_next:
115 %X.2 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
116 ret i32 %X.2
115 %X.2 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
116 ret i32 %X.2
117117 }
118118
119119
173173

                  
                
174174 define i32 @example() {
175175 entry:
176 %X = alloca i32 ; type of %X is i32*.
177 ...
178 %tmp = load i32* %X ; load the stack value %X from the stack.
179 %tmp2 = add i32 %tmp, 1 ; increment it
180 store i32 %tmp2, i32* %X ; store it back
181 ...
176 %X = alloca i32 ; type of %X is i32*.
177 ...
178 %tmp = load i32* %X ; load the stack value %X from the stack.
179 %tmp2 = add i32 %tmp, 1 ; increment it
180 store i32 %tmp2, i32* %X ; store it back
181 ...
182182
183183
184184
195195
196196 define i32 @test(i1 %Condition) {
197197 entry:
198 %X = alloca i32 ; type of %X is i32*.
199 br i1 %Condition, label %cond_true, label %cond_false
198 %X = alloca i32 ; type of %X is i32*.
199 br i1 %Condition, label %cond_true, label %cond_false
200200
201201 cond_true:
202 %X.0 = load i32* @G
203 store i32 %X.0, i32* %X ; Update X
204 br label %cond_next
202 %X.0 = load i32* @G
203 store i32 %X.0, i32* %X ; Update X
204 br label %cond_next
205205
206206 cond_false:
207 %X.1 = load i32* @H
208 store i32 %X.1, i32* %X ; Update X
209 br label %cond_next
207 %X.1 = load i32* @H
208 store i32 %X.1, i32* %X ; Update X
209 br label %cond_next
210210
211211 cond_next:
212 %X.2 = load i32* %X ; Read X
213 ret i32 %X.2
212 %X.2 = load i32* %X ; Read X
213 ret i32 %X.2
214214 }
215215
216216
241241
242242 define i32 @test(i1 %Condition) {
243243 entry:
244 br i1 %Condition, label %cond_true, label %cond_false
244 br i1 %Condition, label %cond_true, label %cond_false
245245
246246 cond_true:
247 %X.0 = load i32* @G
248 br label %cond_next
247 %X.0 = load i32* @G
248 br label %cond_next
249249
250250 cond_false:
251 %X.1 = load i32* @H
252 br label %cond_next
251 %X.1 = load i32* @H
252 br label %cond_next
253253
254254 cond_next:
255 %X.01 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
256 ret i32 %X.01
255 %X.01 = phi i32 [ %X.1, %cond_false ], [ %X.0, %cond_true ]
256 ret i32 %X.01
257257 }
258258
259259
541541

                  
                
542542 define double @fib(double %x) {
543543 entry:
544 %x1 = alloca double
545 store double %x, double* %x1
546 %x2 = load double* %x1
547 %cmptmp = fcmp ult double %x2, 3.000000e+00
548 %booltmp = uitofp i1 %cmptmp to double
549 %ifcond = fcmp one double %booltmp, 0.000000e+00
550 br i1 %ifcond, label %then, label %else
544 %x1 = alloca double
545 store double %x, double* %x1
546 %x2 = load double* %x1
547 %cmptmp = fcmp ult double %x2, 3.000000e+00
548 %booltmp = uitofp i1 %cmptmp to double
549 %ifcond = fcmp one double %booltmp, 0.000000e+00
550 br i1 %ifcond, label %then, label %else
551551
552552 then: ; preds = %entry
553 br label %ifcont
553 br label %ifcont
554554
555555 else: ; preds = %entry
556 %x3 = load double* %x1
557 %subtmp = fsub double %x3, 1.000000e+00
558 %calltmp = call double @fib(double %subtmp)
559 %x4 = load double* %x1
560 %subtmp5 = fsub double %x4, 2.000000e+00
561 %calltmp6 = call double @fib(double %subtmp5)
562 %addtmp = fadd double %calltmp, %calltmp6
563 br label %ifcont
556 %x3 = load double* %x1
557 %subtmp = fsub double %x3, 1.000000e+00
558 %calltmp = call double @fib(double %subtmp)
559 %x4 = load double* %x1
560 %subtmp5 = fsub double %x4, 2.000000e+00
561 %calltmp6 = call double @fib(double %subtmp5)
562 %addtmp = fadd double %calltmp, %calltmp6
563 br label %ifcont
564564
565565 ifcont: ; preds = %else, %then
566 %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
567 ret double %iftmp
566 %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
567 ret double %iftmp
568568 }
569569
570570
583583

                  
                
584584 define double @fib(double %x) {
585585 entry:
586 %cmptmp = fcmp ult double %x, 3.000000e+00
587 %booltmp = uitofp i1 %cmptmp to double
588 %ifcond = fcmp one double %booltmp, 0.000000e+00
589 br i1 %ifcond, label %then, label %else
586 %cmptmp = fcmp ult double %x, 3.000000e+00
587 %booltmp = uitofp i1 %cmptmp to double
588 %ifcond = fcmp one double %booltmp, 0.000000e+00
589 br i1 %ifcond, label %then, label %else
590590
591591 then:
592 br label %ifcont
592 br label %ifcont
593593
594594 else:
595 %subtmp = fsub double %x, 1.000000e+00
596 %calltmp = call double @fib(double %subtmp)
597 %subtmp5 = fsub double %x, 2.000000e+00
598 %calltmp6 = call double @fib(double %subtmp5)
599 %addtmp = fadd double %calltmp, %calltmp6
600 br label %ifcont
595 %subtmp = fsub double %x, 1.000000e+00
596 %calltmp = call double @fib(double %subtmp)
597 %subtmp5 = fsub double %x, 2.000000e+00
598 %calltmp6 = call double @fib(double %subtmp5)
599 %addtmp = fadd double %calltmp, %calltmp6
600 br label %ifcont
601601
602602 ifcont: ; preds = %else, %then
603 %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
604 ret double %iftmp
603 %iftmp = phi double [ 1.000000e+00, %then ], [ %addtmp, %else ]
604 ret double %iftmp
605605 }
606606
607607
616616

                  
                
617617 define double @fib(double %x) {
618618 entry:
619 %cmptmp = fcmp ult double %x, 3.000000e+00
620 %booltmp = uitofp i1 %cmptmp to double
621 %ifcond = fcmp ueq double %booltmp, 0.000000e+00
622 br i1 %ifcond, label %else, label %ifcont
619 %cmptmp = fcmp ult double %x, 3.000000e+00
620 %booltmp = uitofp i1 %cmptmp to double
621 %ifcond = fcmp ueq double %booltmp, 0.000000e+00
622 br i1 %ifcond, label %else, label %ifcont
623623
624624 else:
625 %subtmp = fsub double %x, 1.000000e+00
626 %calltmp = call double @fib(double %subtmp)
627 %subtmp5 = fsub double %x, 2.000000e+00
628 %calltmp6 = call double @fib(double %subtmp5)
629 %addtmp = fadd double %calltmp, %calltmp6
630 ret double %addtmp
625 %subtmp = fsub double %x, 1.000000e+00
626 %calltmp = call double @fib(double %subtmp)
627 %subtmp5 = fsub double %x, 2.000000e+00
628 %calltmp6 = call double @fib(double %subtmp5)
629 %addtmp = fadd double %calltmp, %calltmp6
630 ret double %addtmp
631631
632632 ifcont:
633 ret double 1.000000e+00
633 ret double 1.000000e+00
634634 }
635635
636636
987987
988988
989989

                  
                
990 # Compile
991 g++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
992 # Run
993 ./toy
990 # Compile
991 clang++ -g toy.cpp `llvm-config --cppflags --ldflags --libs core jit native` -O3 -o toy
992 # Run
993 ./toy
994994
995995
996996
10071007 #include "llvm/Analysis/Verifier.h"
10081008 #include "llvm/Analysis/Passes.h"
10091009 #include "llvm/Target/TargetData.h"
1010 #include "llvm/Target/TargetSelect.h"
10111010 #include "llvm/Transforms/Scalar.h"
10121011 #include "llvm/Support/IRBuilder.h"
1012 #include "llvm/Support/TargetSelect.h"
10131013 #include <cstdio>
10141014 #include <string>
10151015 #include <map>
16851685 Function *F = TheModule->getFunction(std::string("binary")+Op);
16861686 assert(F && "binary operator not found!");
16871687
1688 Value *Ops[] = { L, R };
1689 return Builder.CreateCall(F, Ops, Ops+2, "binop");
1688 Value *Ops[2] = { L, R };
1689 return Builder.CreateCall(F, Ops, "binop");
16901690 }
16911691
16921692 Value *CallExprAST::Codegen() {
17051705 if (ArgsV.back() == 0) return 0;
17061706 }
17071707
1708 return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
1708 return Builder.CreateCall(CalleeF, ArgsV, "calltmp");
17091709 }
17101710
17111711 Value *IfExprAST::Codegen() {
19061906
19071907 Function *PrototypeAST::Codegen() {
19081908 // Make the function type: double(double,double) etc.
1909 std::vector<const Type*> Doubles(Args.size(),
1910 Type::getDoubleTy(getGlobalContext()));
1909 std::vector<Type*> Doubles(Args.size(),
1910 Type::getDoubleTy(getGlobalContext()));
19111911 FunctionType *FT = FunctionType::get(Type::getDoubleTy(getGlobalContext()),
19121912 Doubles, false);
19131913