summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authored <ed@FreeBSD.org>2009-06-02 17:52:33 +0000
committered <ed@FreeBSD.org>2009-06-02 17:52:33 +0000
commit3277b69d734b9c90b44ebde4ede005717e2c3b2e (patch)
tree64ba909838c23261cace781ece27d106134ea451 /examples
downloadFreeBSD-src-3277b69d734b9c90b44ebde4ede005717e2c3b2e.zip
FreeBSD-src-3277b69d734b9c90b44ebde4ede005717e2c3b2e.tar.gz
Import LLVM, at r72732.
Diffstat (limited to 'examples')
-rw-r--r--examples/BrainF/BrainF.cpp458
-rw-r--r--examples/BrainF/BrainF.h91
-rw-r--r--examples/BrainF/BrainFDriver.cpp155
-rw-r--r--examples/BrainF/CMakeLists.txt6
-rw-r--r--examples/BrainF/Makefile15
-rw-r--r--examples/CMakeLists.txt12
-rw-r--r--examples/Fibonacci/CMakeLists.txt5
-rw-r--r--examples/Fibonacci/Makefile17
-rw-r--r--examples/Fibonacci/fibonacci.cpp121
-rw-r--r--examples/HowToUseJIT/CMakeLists.txt5
-rw-r--r--examples/HowToUseJIT/HowToUseJIT.cpp117
-rw-r--r--examples/HowToUseJIT/Makefile15
-rw-r--r--examples/Kaleidoscope/CMakeLists.txt5
-rw-r--r--examples/Kaleidoscope/Makefile15
-rw-r--r--examples/Kaleidoscope/toy.cpp1135
-rw-r--r--examples/Makefile19
-rw-r--r--examples/ModuleMaker/CMakeLists.txt5
-rw-r--r--examples/ModuleMaker/Makefile14
-rw-r--r--examples/ModuleMaker/ModuleMaker.cpp61
-rw-r--r--examples/ModuleMaker/README.txt8
-rw-r--r--examples/ParallelJIT/CMakeLists.txt7
-rw-r--r--examples/ParallelJIT/Makefile17
-rw-r--r--examples/ParallelJIT/ParallelJIT.cpp298
23 files changed, 2601 insertions, 0 deletions
diff --git a/examples/BrainF/BrainF.cpp b/examples/BrainF/BrainF.cpp
new file mode 100644
index 0000000..32a14c4
--- /dev/null
+++ b/examples/BrainF/BrainF.cpp
@@ -0,0 +1,458 @@
+//===-- BrainF.cpp - BrainF compiler example ----------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--------------------------------------------------------------------===//
+//
+// This class compiles the BrainF language into LLVM assembly.
+//
+// The BrainF language has 8 commands:
+// Command Equivalent C Action
+// ------- ------------ ------
+// , *h=getchar(); Read a character from stdin, 255 on EOF
+// . putchar(*h); Write a character to stdout
+// - --*h; Decrement tape
+// + ++*h; Increment tape
+// < --h; Move head left
+// > ++h; Move head right
+// [ while(*h) { Start loop
+// ] } End loop
+//
+//===--------------------------------------------------------------------===//
+
+#include "BrainF.h"
+#include "llvm/Constants.h"
+#include "llvm/Intrinsics.h"
+#include "llvm/ADT/STLExtras.h"
+#include <iostream>
+using namespace llvm;
+
+//Set the constants for naming
+const char *BrainF::tapereg = "tape";
+const char *BrainF::headreg = "head";
+const char *BrainF::label = "brainf";
+const char *BrainF::testreg = "test";
+
+Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf) {
+ in = in1;
+ memtotal = mem;
+ comflag = cf;
+
+ header();
+ readloop(0, 0, 0);
+ delete builder;
+ return module;
+}
+
+void BrainF::header() {
+ module = new Module("BrainF");
+
+ //Function prototypes
+
+ //declare void @llvm.memset.i32(i8 *, i8, i32, i32)
+ const Type *Tys[] = { Type::Int32Ty };
+ Function *memset_func = Intrinsic::getDeclaration(module, Intrinsic::memset,
+ Tys, 1);
+
+ //declare i32 @getchar()
+ getchar_func = cast<Function>(module->
+ getOrInsertFunction("getchar", IntegerType::Int32Ty, NULL));
+
+ //declare i32 @putchar(i32)
+ putchar_func = cast<Function>(module->
+ getOrInsertFunction("putchar", IntegerType::Int32Ty,
+ IntegerType::Int32Ty, NULL));
+
+
+ //Function header
+
+ //define void @brainf()
+ brainf_func = cast<Function>(module->
+ getOrInsertFunction("brainf", Type::VoidTy, NULL));
+
+ builder = new IRBuilder<>(BasicBlock::Create(label, brainf_func));
+
+ //%arr = malloc i8, i32 %d
+ ConstantInt *val_mem = ConstantInt::get(APInt(32, memtotal));
+ ptr_arr = builder->CreateMalloc(IntegerType::Int8Ty, val_mem, "arr");
+
+ //call void @llvm.memset.i32(i8 *%arr, i8 0, i32 %d, i32 1)
+ {
+ Value *memset_params[] = {
+ ptr_arr,
+ ConstantInt::get(APInt(8, 0)),
+ val_mem,
+ ConstantInt::get(APInt(32, 1))
+ };
+
+ CallInst *memset_call = builder->
+ CreateCall(memset_func, memset_params, array_endof(memset_params));
+ memset_call->setTailCall(false);
+ }
+
+ //%arrmax = getelementptr i8 *%arr, i32 %d
+ if (comflag & flag_arraybounds) {
+ ptr_arrmax = builder->
+ CreateGEP(ptr_arr, ConstantInt::get(APInt(32, memtotal)), "arrmax");
+ }
+
+ //%head.%d = getelementptr i8 *%arr, i32 %d
+ curhead = builder->CreateGEP(ptr_arr,
+ ConstantInt::get(APInt(32, memtotal/2)),
+ headreg);
+
+
+
+ //Function footer
+
+ //brainf.end:
+ endbb = BasicBlock::Create(label, brainf_func);
+
+ //free i8 *%arr
+ new FreeInst(ptr_arr, endbb);
+
+ //ret void
+ ReturnInst::Create(endbb);
+
+
+
+ //Error block for array out of bounds
+ if (comflag & flag_arraybounds)
+ {
+ //@aberrormsg = internal constant [%d x i8] c"\00"
+ Constant *msg_0 = ConstantArray::
+ get("Error: The head has left the tape.", true);
+
+ GlobalVariable *aberrormsg = new GlobalVariable(
+ msg_0->getType(),
+ true,
+ GlobalValue::InternalLinkage,
+ msg_0,
+ "aberrormsg",
+ module);
+
+ //declare i32 @puts(i8 *)
+ Function *puts_func = cast<Function>(module->
+ getOrInsertFunction("puts", IntegerType::Int32Ty,
+ PointerType::getUnqual(IntegerType::Int8Ty), NULL));
+
+ //brainf.aberror:
+ aberrorbb = BasicBlock::Create(label, brainf_func);
+
+ //call i32 @puts(i8 *getelementptr([%d x i8] *@aberrormsg, i32 0, i32 0))
+ {
+ Constant *zero_32 = Constant::getNullValue(IntegerType::Int32Ty);
+
+ Constant *gep_params[] = {
+ zero_32,
+ zero_32
+ };
+
+ Constant *msgptr = ConstantExpr::
+ getGetElementPtr(aberrormsg, gep_params,
+ array_lengthof(gep_params));
+
+ Value *puts_params[] = {
+ msgptr
+ };
+
+ CallInst *puts_call =
+ CallInst::Create(puts_func,
+ puts_params, array_endof(puts_params),
+ "", aberrorbb);
+ puts_call->setTailCall(false);
+ }
+
+ //br label %brainf.end
+ BranchInst::Create(endbb, aberrorbb);
+ }
+}
+
+void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb) {
+ Symbol cursym = SYM_NONE;
+ int curvalue = 0;
+ Symbol nextsym = SYM_NONE;
+ int nextvalue = 0;
+ char c;
+ int loop;
+ int direction;
+
+ while(cursym != SYM_EOF && cursym != SYM_ENDLOOP) {
+ // Write out commands
+ switch(cursym) {
+ case SYM_NONE:
+ // Do nothing
+ break;
+
+ case SYM_READ:
+ {
+ //%tape.%d = call i32 @getchar()
+ CallInst *getchar_call = builder->CreateCall(getchar_func, tapereg);
+ getchar_call->setTailCall(false);
+ Value *tape_0 = getchar_call;
+
+ //%tape.%d = trunc i32 %tape.%d to i8
+ Value *tape_1 = builder->
+ CreateTrunc(tape_0, IntegerType::Int8Ty, tapereg);
+
+ //store i8 %tape.%d, i8 *%head.%d
+ builder->CreateStore(tape_1, curhead);
+ }
+ break;
+
+ case SYM_WRITE:
+ {
+ //%tape.%d = load i8 *%head.%d
+ LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
+
+ //%tape.%d = sext i8 %tape.%d to i32
+ Value *tape_1 = builder->
+ CreateSExt(tape_0, IntegerType::Int32Ty, tapereg);
+
+ //call i32 @putchar(i32 %tape.%d)
+ Value *putchar_params[] = {
+ tape_1
+ };
+ CallInst *putchar_call = builder->
+ CreateCall(putchar_func,
+ putchar_params, array_endof(putchar_params));
+ putchar_call->setTailCall(false);
+ }
+ break;
+
+ case SYM_MOVE:
+ {
+ //%head.%d = getelementptr i8 *%head.%d, i32 %d
+ curhead = builder->
+ CreateGEP(curhead, ConstantInt::get(APInt(32, curvalue)),
+ headreg);
+
+ //Error block for array out of bounds
+ if (comflag & flag_arraybounds)
+ {
+ //%test.%d = icmp uge i8 *%head.%d, %arrmax
+ Value *test_0 = builder->
+ CreateICmpUGE(curhead, ptr_arrmax, testreg);
+
+ //%test.%d = icmp ult i8 *%head.%d, %arr
+ Value *test_1 = builder->
+ CreateICmpULT(curhead, ptr_arr, testreg);
+
+ //%test.%d = or i1 %test.%d, %test.%d
+ Value *test_2 = builder->
+ CreateOr(test_0, test_1, testreg);
+
+ //br i1 %test.%d, label %main.%d, label %main.%d
+ BasicBlock *nextbb = BasicBlock::Create(label, brainf_func);
+ builder->CreateCondBr(test_2, aberrorbb, nextbb);
+
+ //main.%d:
+ builder->SetInsertPoint(nextbb);
+ }
+ }
+ break;
+
+ case SYM_CHANGE:
+ {
+ //%tape.%d = load i8 *%head.%d
+ LoadInst *tape_0 = builder->CreateLoad(curhead, tapereg);
+
+ //%tape.%d = add i8 %tape.%d, %d
+ Value *tape_1 = builder->
+ CreateAdd(tape_0, ConstantInt::get(APInt(8, curvalue)), tapereg);
+
+ //store i8 %tape.%d, i8 *%head.%d\n"
+ builder->CreateStore(tape_1, curhead);
+ }
+ break;
+
+ case SYM_LOOP:
+ {
+ //br label %main.%d
+ BasicBlock *testbb = BasicBlock::Create(label, brainf_func);
+ builder->CreateBr(testbb);
+
+ //main.%d:
+ BasicBlock *bb_0 = builder->GetInsertBlock();
+ BasicBlock *bb_1 = BasicBlock::Create(label, brainf_func);
+ builder->SetInsertPoint(bb_1);
+
+ // Make part of PHI instruction now, wait until end of loop to finish
+ PHINode *phi_0 =
+ PHINode::Create(PointerType::getUnqual(IntegerType::Int8Ty),
+ headreg, testbb);
+ phi_0->reserveOperandSpace(2);
+ phi_0->addIncoming(curhead, bb_0);
+ curhead = phi_0;
+
+ readloop(phi_0, bb_1, testbb);
+ }
+ break;
+
+ default:
+ std::cerr << "Error: Unknown symbol.\n";
+ abort();
+ break;
+ }
+
+ cursym = nextsym;
+ curvalue = nextvalue;
+ nextsym = SYM_NONE;
+
+ // Reading stdin loop
+ loop = (cursym == SYM_NONE)
+ || (cursym == SYM_MOVE)
+ || (cursym == SYM_CHANGE);
+ while(loop) {
+ *in>>c;
+ if (in->eof()) {
+ if (cursym == SYM_NONE) {
+ cursym = SYM_EOF;
+ } else {
+ nextsym = SYM_EOF;
+ }
+ loop = 0;
+ } else {
+ direction = 1;
+ switch(c) {
+ case '-':
+ direction = -1;
+ // Fall through
+
+ case '+':
+ if (cursym == SYM_CHANGE) {
+ curvalue += direction;
+ // loop = 1
+ } else {
+ if (cursym == SYM_NONE) {
+ cursym = SYM_CHANGE;
+ curvalue = direction;
+ // loop = 1
+ } else {
+ nextsym = SYM_CHANGE;
+ nextvalue = direction;
+ loop = 0;
+ }
+ }
+ break;
+
+ case '<':
+ direction = -1;
+ // Fall through
+
+ case '>':
+ if (cursym == SYM_MOVE) {
+ curvalue += direction;
+ // loop = 1
+ } else {
+ if (cursym == SYM_NONE) {
+ cursym = SYM_MOVE;
+ curvalue = direction;
+ // loop = 1
+ } else {
+ nextsym = SYM_MOVE;
+ nextvalue = direction;
+ loop = 0;
+ }
+ }
+ break;
+
+ case ',':
+ if (cursym == SYM_NONE) {
+ cursym = SYM_READ;
+ } else {
+ nextsym = SYM_READ;
+ }
+ loop = 0;
+ break;
+
+ case '.':
+ if (cursym == SYM_NONE) {
+ cursym = SYM_WRITE;
+ } else {
+ nextsym = SYM_WRITE;
+ }
+ loop = 0;
+ break;
+
+ case '[':
+ if (cursym == SYM_NONE) {
+ cursym = SYM_LOOP;
+ } else {
+ nextsym = SYM_LOOP;
+ }
+ loop = 0;
+ break;
+
+ case ']':
+ if (cursym == SYM_NONE) {
+ cursym = SYM_ENDLOOP;
+ } else {
+ nextsym = SYM_ENDLOOP;
+ }
+ loop = 0;
+ break;
+
+ // Ignore other characters
+ default:
+ break;
+ }
+ }
+ }
+ }
+
+ if (cursym == SYM_ENDLOOP) {
+ if (!phi) {
+ std::cerr << "Error: Extra ']'\n";
+ abort();
+ }
+
+ // Write loop test
+ {
+ //br label %main.%d
+ builder->CreateBr(testbb);
+
+ //main.%d:
+
+ //%head.%d = phi i8 *[%head.%d, %main.%d], [%head.%d, %main.%d]
+ //Finish phi made at beginning of loop
+ phi->addIncoming(curhead, builder->GetInsertBlock());
+ Value *head_0 = phi;
+
+ //%tape.%d = load i8 *%head.%d
+ LoadInst *tape_0 = new LoadInst(head_0, tapereg, testbb);
+
+ //%test.%d = icmp eq i8 %tape.%d, 0
+ ICmpInst *test_0 = new ICmpInst(ICmpInst::ICMP_EQ, tape_0,
+ ConstantInt::get(APInt(8, 0)), testreg,
+ testbb);
+
+ //br i1 %test.%d, label %main.%d, label %main.%d
+ BasicBlock *bb_0 = BasicBlock::Create(label, brainf_func);
+ BranchInst::Create(bb_0, oldbb, test_0, testbb);
+
+ //main.%d:
+ builder->SetInsertPoint(bb_0);
+
+ //%head.%d = phi i8 *[%head.%d, %main.%d]
+ PHINode *phi_1 = builder->
+ CreatePHI(PointerType::getUnqual(IntegerType::Int8Ty), headreg);
+ phi_1->reserveOperandSpace(1);
+ phi_1->addIncoming(head_0, testbb);
+ curhead = phi_1;
+ }
+
+ return;
+ }
+
+ //End of the program, so go to return block
+ builder->CreateBr(endbb);
+
+ if (phi) {
+ std::cerr << "Error: Missing ']'\n";
+ abort();
+ }
+}
diff --git a/examples/BrainF/BrainF.h b/examples/BrainF/BrainF.h
new file mode 100644
index 0000000..06c00ae
--- /dev/null
+++ b/examples/BrainF/BrainF.h
@@ -0,0 +1,91 @@
+//===-- BrainF.h - BrainF compiler class ----------------------*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--------------------------------------------------------------------===//
+//
+// This class stores the data for the BrainF compiler so it doesn't have
+// to pass all of it around. The main method is parse.
+//
+//===--------------------------------------------------------------------===//
+
+#ifndef BRAINF_H
+#define BRAINF_H
+
+#include "llvm/Module.h"
+#include "llvm/Support/IRBuilder.h"
+
+using namespace llvm;
+
+/// This class provides a parser for the BrainF language.
+/// The class itself is made to store values during
+/// parsing so they don't have to be passed around
+/// as much.
+class BrainF {
+ public:
+ /// Options for how BrainF should compile
+ enum CompileFlags {
+ flag_off = 0,
+ flag_arraybounds = 1
+ };
+
+ /// This is the main method. It parses BrainF from in1
+ /// and returns the module with a function
+ /// void brainf()
+ /// containing the resulting code.
+ /// On error, it calls abort.
+ /// The caller must delete the returned module.
+ Module *parse(std::istream *in1, int mem, CompileFlags cf);
+
+ protected:
+ /// The different symbols in the BrainF language
+ enum Symbol {
+ SYM_NONE,
+ SYM_READ,
+ SYM_WRITE,
+ SYM_MOVE,
+ SYM_CHANGE,
+ SYM_LOOP,
+ SYM_ENDLOOP,
+ SYM_EOF
+ };
+
+ /// Names of the different parts of the language.
+ /// Tape is used for reading and writing the tape.
+ /// headreg is used for the position of the head.
+ /// label is used for the labels for the BasicBlocks.
+ /// testreg is used for testing the loop exit condition.
+ static const char *tapereg;
+ static const char *headreg;
+ static const char *label;
+ static const char *testreg;
+
+ /// Put the brainf function preamble and other fixed pieces of code
+ void header();
+
+ /// The main loop for parsing. It calls itself recursively
+ /// to handle the depth of nesting of "[]".
+ void readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb);
+
+ /// Constants during parsing
+ int memtotal;
+ CompileFlags comflag;
+ std::istream *in;
+ Module *module;
+ Function *brainf_func;
+ Function *getchar_func;
+ Function *putchar_func;
+ Value *ptr_arr;
+ Value *ptr_arrmax;
+ BasicBlock *endbb;
+ BasicBlock *aberrorbb;
+
+ /// Variables
+ IRBuilder<> *builder;
+ Value *curhead;
+};
+
+#endif
diff --git a/examples/BrainF/BrainFDriver.cpp b/examples/BrainF/BrainFDriver.cpp
new file mode 100644
index 0000000..34fb806
--- /dev/null
+++ b/examples/BrainF/BrainFDriver.cpp
@@ -0,0 +1,155 @@
+//===-- BrainFDriver.cpp - BrainF compiler driver -----------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--------------------------------------------------------------------===//
+//
+// This program converts the BrainF language into LLVM assembly,
+// which it can then run using the JIT or output as BitCode.
+//
+// This implementation has a tape of 65536 bytes,
+// with the head starting in the middle.
+// Range checking is off by default, so be careful.
+// It can be enabled with -abc.
+//
+// Use:
+// ./BrainF -jit prog.bf #Run program now
+// ./BrainF -jit -abc prog.bf #Run program now safely
+// ./BrainF prog.bf #Write as BitCode
+//
+// lli prog.bf.bc #Run generated BitCode
+// llvm-ld -native -o=prog prog.bf.bc #Compile BitCode into native executable
+//
+//===--------------------------------------------------------------------===//
+
+#include "BrainF.h"
+#include "llvm/Constants.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/Analysis/Verifier.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/Support/ManagedStatic.h"
+#include <fstream>
+#include <iostream>
+using namespace llvm;
+
+//Command line options
+
+static cl::opt<std::string>
+InputFilename(cl::Positional, cl::desc("<input brainf>"));
+
+static cl::opt<std::string>
+OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"));
+
+static cl::opt<bool>
+ArrayBoundsChecking("abc", cl::desc("Enable array bounds checking"));
+
+static cl::opt<bool>
+JIT("jit", cl::desc("Run program Just-In-Time"));
+
+
+//Add main function so can be fully compiled
+void addMainFunction(Module *mod) {
+ //define i32 @main(i32 %argc, i8 **%argv)
+ Function *main_func = cast<Function>(mod->
+ getOrInsertFunction("main", IntegerType::Int32Ty, IntegerType::Int32Ty,
+ PointerType::getUnqual(PointerType::getUnqual(
+ IntegerType::Int8Ty)), NULL));
+ {
+ Function::arg_iterator args = main_func->arg_begin();
+ Value *arg_0 = args++;
+ arg_0->setName("argc");
+ Value *arg_1 = args++;
+ arg_1->setName("argv");
+ }
+
+ //main.0:
+ BasicBlock *bb = BasicBlock::Create("main.0", main_func);
+
+ //call void @brainf()
+ {
+ CallInst *brainf_call = CallInst::Create(mod->getFunction("brainf"),
+ "", bb);
+ brainf_call->setTailCall(false);
+ }
+
+ //ret i32 0
+ ReturnInst::Create(ConstantInt::get(APInt(32, 0)), bb);
+}
+
+int main(int argc, char **argv) {
+ cl::ParseCommandLineOptions(argc, argv, " BrainF compiler\n");
+
+ if (InputFilename == "") {
+ std::cerr<<"Error: You must specify the filename of the program to "
+ "be compiled. Use --help to see the options.\n";
+ abort();
+ }
+
+ //Get the output stream
+ std::ostream *out = &std::cout;
+ if (!JIT) {
+ if (OutputFilename == "") {
+ std::string base = InputFilename;
+ if (InputFilename == "-") {base = "a";}
+
+ //Use default filename
+ const char *suffix = ".bc";
+ OutputFilename = base+suffix;
+ }
+ if (OutputFilename != "-") {
+ out = new std::
+ ofstream(OutputFilename.c_str(),
+ std::ios::out | std::ios::trunc | std::ios::binary);
+ }
+ }
+
+ //Get the input stream
+ std::istream *in = &std::cin;
+ if (InputFilename != "-") {
+ in = new std::ifstream(InputFilename.c_str());
+ }
+
+ //Gather the compile flags
+ BrainF::CompileFlags cf = BrainF::flag_off;
+ if (ArrayBoundsChecking) {
+ cf = BrainF::CompileFlags(cf | BrainF::flag_arraybounds);
+ }
+
+ //Read the BrainF program
+ BrainF bf;
+ Module *mod = bf.parse(in, 65536, cf); //64 KiB
+ if (in != &std::cin) {delete in;}
+ addMainFunction(mod);
+
+ //Verify generated code
+ if (verifyModule(*mod)) {
+ std::cerr<<"Error: module failed verification. This shouldn't happen.\n";
+ abort();
+ }
+
+ //Write it out
+ if (JIT) {
+ std::cout << "------- Running JIT -------\n";
+ ExistingModuleProvider *mp = new ExistingModuleProvider(mod);
+ ExecutionEngine *ee = ExecutionEngine::create(mp, false);
+ std::vector<GenericValue> args;
+ Function *brainf_func = mod->getFunction("brainf");
+ GenericValue gv = ee->runFunction(brainf_func, args);
+ } else {
+ WriteBitcodeToFile(mod, *out);
+ }
+
+ //Clean up
+ if (out != &std::cout) {delete out;}
+ delete mod;
+
+ llvm_shutdown();
+
+ return 0;
+}
diff --git a/examples/BrainF/CMakeLists.txt b/examples/BrainF/CMakeLists.txt
new file mode 100644
index 0000000..7bec105
--- /dev/null
+++ b/examples/BrainF/CMakeLists.txt
@@ -0,0 +1,6 @@
+set(LLVM_LINK_COMPONENTS jit bitwriter nativecodegen interpreter)
+
+add_llvm_example(BrainF
+ BrainF.cpp
+ BrainFDriver.cpp
+ )
diff --git a/examples/BrainF/Makefile b/examples/BrainF/Makefile
new file mode 100644
index 0000000..2c3e066
--- /dev/null
+++ b/examples/BrainF/Makefile
@@ -0,0 +1,15 @@
+##===- examples/BrainF/Makefile ----------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL = ../..
+TOOLNAME = BrainF
+EXAMPLE_TOOL = 1
+
+LINK_COMPONENTS := jit bitwriter nativecodegen interpreter
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
new file mode 100644
index 0000000..c5b8079
--- /dev/null
+++ b/examples/CMakeLists.txt
@@ -0,0 +1,12 @@
+add_subdirectory(BrainF)
+add_subdirectory(Fibonacci)
+add_subdirectory(HowToUseJIT)
+add_subdirectory(Kaleidoscope)
+add_subdirectory(ModuleMaker)
+
+include(CheckIncludeFile)
+check_include_file(pthread.h HAVE_PTHREAD_H)
+
+if( HAVE_PTHREAD_H )
+ add_subdirectory(ParallelJIT)
+endif( HAVE_PTHREAD_H )
diff --git a/examples/Fibonacci/CMakeLists.txt b/examples/Fibonacci/CMakeLists.txt
new file mode 100644
index 0000000..6937612
--- /dev/null
+++ b/examples/Fibonacci/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(LLVM_LINK_COMPONENTS jit interpreter nativecodegen)
+
+add_llvm_example(Fibonacci
+ fibonacci.cpp
+ )
diff --git a/examples/Fibonacci/Makefile b/examples/Fibonacci/Makefile
new file mode 100644
index 0000000..71f6ba0
--- /dev/null
+++ b/examples/Fibonacci/Makefile
@@ -0,0 +1,17 @@
+##===- examples/Fibonacci/Makefile -------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+
+LEVEL = ../..
+TOOLNAME = Fibonacci
+EXAMPLE_TOOL = 1
+
+# Link in JIT support
+LINK_COMPONENTS := jit interpreter nativecodegen
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/Fibonacci/fibonacci.cpp b/examples/Fibonacci/fibonacci.cpp
new file mode 100644
index 0000000..09f2203
--- /dev/null
+++ b/examples/Fibonacci/fibonacci.cpp
@@ -0,0 +1,121 @@
+//===--- examples/Fibonacci/fibonacci.cpp - An example use of the JIT -----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This small program provides an example of how to build quickly a small module
+// with function Fibonacci and execute it with the JIT.
+//
+// The goal of this snippet is to create in the memory the LLVM module
+// consisting of one function as follow:
+//
+// int fib(int x) {
+// if(x<=2) return 1;
+// return fib(x-1)+fib(x-2);
+// }
+//
+// Once we have this, we compile the module via JIT, then execute the `fib'
+// function and return result to a driver, i.e. to a "host program".
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Module.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Constants.h"
+#include "llvm/Instructions.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/Analysis/Verifier.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/Interpreter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/Support/raw_ostream.h"
+using namespace llvm;
+
+static Function *CreateFibFunction(Module *M) {
+ // Create the fib function and insert it into module M. This function is said
+ // to return an int and take an int parameter.
+ Function *FibF =
+ cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function.
+ BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
+
+ // Get pointers to the constants.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+ Value *Two = ConstantInt::get(Type::Int32Ty, 2);
+
+ // Get pointer to the integer argument of the add1 function...
+ Argument *ArgX = FibF->arg_begin(); // Get the arg.
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the true_block.
+ BasicBlock *RetBB = BasicBlock::Create("return", FibF);
+ // Create an exit block.
+ BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF);
+
+ // Create the "if (arg <= 2) goto exitbb"
+ Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
+ BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
+
+ // Create: ret int 1
+ ReturnInst::Create(One, RetBB);
+
+ // create fib(x-1)
+ Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
+ CallInst *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
+ CallFibX1->setTailCall();
+
+ // create fib(x-2)
+ Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
+ CallInst *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
+ CallFibX2->setTailCall();
+
+
+ // fib(x-1)+fib(x-2)
+ Value *Sum = BinaryOperator::CreateAdd(CallFibX1, CallFibX2,
+ "addresult", RecurseBB);
+
+ // Create the return instruction and add it to the basic block
+ ReturnInst::Create(Sum, RecurseBB);
+
+ return FibF;
+}
+
+
+int main(int argc, char **argv) {
+ int n = argc > 1 ? atol(argv[1]) : 24;
+
+ // Create some module to put our function into it.
+ Module *M = new Module("test");
+
+ // We are about to create the "fib" function:
+ Function *FibF = CreateFibFunction(M);
+
+ // Now we going to create JIT
+ ExistingModuleProvider *MP = new ExistingModuleProvider(M);
+ ExecutionEngine *EE = ExecutionEngine::create(MP, false);
+
+ errs() << "verifying... ";
+ if (verifyModule(*M)) {
+ errs() << argv[0] << ": Error constructing function!\n";
+ return 1;
+ }
+
+ errs() << "OK\n";
+ errs() << "We just constructed this LLVM module:\n\n---------\n" << *M;
+ errs() << "---------\nstarting fibonacci(" << n << ") with JIT...\n";
+
+ // Call the Fibonacci function with argument n:
+ std::vector<GenericValue> Args(1);
+ Args[0].IntVal = APInt(32, n);
+ GenericValue GV = EE->runFunction(FibF, Args);
+
+ // import result of execution
+ outs() << "Result: " << GV.IntVal << "\n";
+ return 0;
+}
diff --git a/examples/HowToUseJIT/CMakeLists.txt b/examples/HowToUseJIT/CMakeLists.txt
new file mode 100644
index 0000000..428b53f
--- /dev/null
+++ b/examples/HowToUseJIT/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(LLVM_LINK_COMPONENTS jit interpreter nativecodegen)
+
+add_llvm_example(HowToUseJIT
+ HowToUseJIT.cpp
+ )
diff --git a/examples/HowToUseJIT/HowToUseJIT.cpp b/examples/HowToUseJIT/HowToUseJIT.cpp
new file mode 100644
index 0000000..b5c6d11
--- /dev/null
+++ b/examples/HowToUseJIT/HowToUseJIT.cpp
@@ -0,0 +1,117 @@
+//===-- examples/HowToUseJIT/HowToUseJIT.cpp - An example use of the JIT --===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This small program provides an example of how to quickly build a small
+// module with two functions and execute it with the JIT.
+//
+// Goal:
+// The goal of this snippet is to create in the memory
+// the LLVM module consisting of two functions as follow:
+//
+// int add1(int x) {
+// return x+1;
+// }
+//
+// int foo() {
+// return add1(10);
+// }
+//
+// then compile the module via JIT, then execute the `foo'
+// function and return result to a driver, i.e. to a "host program".
+//
+// Some remarks and questions:
+//
+// - could we invoke some code using noname functions too?
+// e.g. evaluate "foo()+foo()" without fears to introduce
+// conflict of temporary function name with some real
+// existing function name?
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Module.h"
+#include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Instructions.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/Interpreter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/raw_ostream.h"
+using namespace llvm;
+
+int main() {
+ // Create some module to put our function into it.
+ Module *M = new Module("test");
+
+ // Create the add1 function entry and insert this entry into module M. The
+ // function will have a return type of "int" and take an argument of "int".
+ // The '0' terminates the list of argument types.
+ Function *Add1F =
+ cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function. As before, it automatically inserts
+ // because of the last argument.
+ BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
+
+ // Get pointers to the constant `1'.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+
+ // Get pointers to the integer argument of the add1 function...
+ assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
+ Argument *ArgX = Add1F->arg_begin(); // Get the arg
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the add instruction, inserting it into the end of BB.
+ Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
+
+ // Create the return instruction and add it to the basic block
+ ReturnInst::Create(Add, BB);
+
+ // Now, function add1 is ready.
+
+
+ // Now we going to create function `foo', which returns an int and takes no
+ // arguments.
+ Function *FooF =
+ cast<Function>(M->getOrInsertFunction("foo", Type::Int32Ty, (Type *)0));
+
+ // Add a basic block to the FooF function.
+ BB = BasicBlock::Create("EntryBlock", FooF);
+
+ // Get pointers to the constant `10'.
+ Value *Ten = ConstantInt::get(Type::Int32Ty, 10);
+
+ // Pass Ten to the call call:
+ CallInst *Add1CallRes = CallInst::Create(Add1F, Ten, "add1", BB);
+ Add1CallRes->setTailCall(true);
+
+ // Create the return instruction and add it to the basic block.
+ ReturnInst::Create(Add1CallRes, BB);
+
+ // Now we create the JIT.
+ ExistingModuleProvider* MP = new ExistingModuleProvider(M);
+ ExecutionEngine* EE = ExecutionEngine::create(MP, false);
+
+ outs() << "We just constructed this LLVM module:\n\n" << *M;
+ outs() << "\n\nRunning foo: ";
+ outs().flush();
+
+ // Call the `foo' function with no arguments:
+ std::vector<GenericValue> noargs;
+ GenericValue gv = EE->runFunction(FooF, noargs);
+
+ // Import result of execution:
+ outs() << "Result: " << gv.IntVal << "\n";
+ EE->freeMachineCodeForFunction(FooF);
+ delete EE;
+ llvm_shutdown();
+ return 0;
+}
diff --git a/examples/HowToUseJIT/Makefile b/examples/HowToUseJIT/Makefile
new file mode 100644
index 0000000..c8919db
--- /dev/null
+++ b/examples/HowToUseJIT/Makefile
@@ -0,0 +1,15 @@
+##===- examples/HowToUseJIT/Makefile -----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL = ../..
+TOOLNAME = HowToUseJIT
+EXAMPLE_TOOL = 1
+
+LINK_COMPONENTS := jit interpreter nativecodegen
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/Kaleidoscope/CMakeLists.txt b/examples/Kaleidoscope/CMakeLists.txt
new file mode 100644
index 0000000..9a18aae
--- /dev/null
+++ b/examples/Kaleidoscope/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(LLVM_LINK_COMPONENTS core jit native)
+
+add_llvm_example(Kaleidoscope
+ toy.cpp
+ )
diff --git a/examples/Kaleidoscope/Makefile b/examples/Kaleidoscope/Makefile
new file mode 100644
index 0000000..c1759a0
--- /dev/null
+++ b/examples/Kaleidoscope/Makefile
@@ -0,0 +1,15 @@
+##===- examples/Kaleidoscope/Makefile ----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL = ../..
+TOOLNAME = Kaleidoscope
+EXAMPLE_TOOL = 1
+
+LINK_COMPONENTS := core jit native
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/Kaleidoscope/toy.cpp b/examples/Kaleidoscope/toy.cpp
new file mode 100644
index 0000000..bec430c
--- /dev/null
+++ b/examples/Kaleidoscope/toy.cpp
@@ -0,0 +1,1135 @@
+#include "llvm/DerivedTypes.h"
+#include "llvm/ExecutionEngine/ExecutionEngine.h"
+#include "llvm/Module.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/PassManager.h"
+#include "llvm/Analysis/Verifier.h"
+#include "llvm/Target/TargetData.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Support/IRBuilder.h"
+#include <cstdio>
+#include <string>
+#include <map>
+#include <vector>
+using namespace llvm;
+
+//===----------------------------------------------------------------------===//
+// Lexer
+//===----------------------------------------------------------------------===//
+
+// The lexer returns tokens [0-255] if it is an unknown character, otherwise one
+// of these for known things.
+enum Token {
+ tok_eof = -1,
+
+ // commands
+ tok_def = -2, tok_extern = -3,
+
+ // primary
+ tok_identifier = -4, tok_number = -5,
+
+ // control
+ tok_if = -6, tok_then = -7, tok_else = -8,
+ tok_for = -9, tok_in = -10,
+
+ // operators
+ tok_binary = -11, tok_unary = -12,
+
+ // var definition
+ tok_var = -13
+};
+
+static std::string IdentifierStr; // Filled in if tok_identifier
+static double NumVal; // Filled in if tok_number
+
+/// gettok - Return the next token from standard input.
+static int gettok() {
+ static int LastChar = ' ';
+
+ // Skip any whitespace.
+ while (isspace(LastChar))
+ LastChar = getchar();
+
+ if (isalpha(LastChar)) { // identifier: [a-zA-Z][a-zA-Z0-9]*
+ IdentifierStr = LastChar;
+ while (isalnum((LastChar = getchar())))
+ IdentifierStr += LastChar;
+
+ if (IdentifierStr == "def") return tok_def;
+ if (IdentifierStr == "extern") return tok_extern;
+ if (IdentifierStr == "if") return tok_if;
+ if (IdentifierStr == "then") return tok_then;
+ if (IdentifierStr == "else") return tok_else;
+ if (IdentifierStr == "for") return tok_for;
+ if (IdentifierStr == "in") return tok_in;
+ if (IdentifierStr == "binary") return tok_binary;
+ if (IdentifierStr == "unary") return tok_unary;
+ if (IdentifierStr == "var") return tok_var;
+ return tok_identifier;
+ }
+
+ if (isdigit(LastChar) || LastChar == '.') { // Number: [0-9.]+
+ std::string NumStr;
+ do {
+ NumStr += LastChar;
+ LastChar = getchar();
+ } while (isdigit(LastChar) || LastChar == '.');
+
+ NumVal = strtod(NumStr.c_str(), 0);
+ return tok_number;
+ }
+
+ if (LastChar == '#') {
+ // Comment until end of line.
+ do LastChar = getchar();
+ while (LastChar != EOF && LastChar != '\n' && LastChar != '\r');
+
+ if (LastChar != EOF)
+ return gettok();
+ }
+
+ // Check for end of file. Don't eat the EOF.
+ if (LastChar == EOF)
+ return tok_eof;
+
+ // Otherwise, just return the character as its ascii value.
+ int ThisChar = LastChar;
+ LastChar = getchar();
+ return ThisChar;
+}
+
+//===----------------------------------------------------------------------===//
+// Abstract Syntax Tree (aka Parse Tree)
+//===----------------------------------------------------------------------===//
+
+/// ExprAST - Base class for all expression nodes.
+class ExprAST {
+public:
+ virtual ~ExprAST() {}
+ virtual Value *Codegen() = 0;
+};
+
+/// NumberExprAST - Expression class for numeric literals like "1.0".
+class NumberExprAST : public ExprAST {
+ double Val;
+public:
+ NumberExprAST(double val) : Val(val) {}
+ virtual Value *Codegen();
+};
+
+/// VariableExprAST - Expression class for referencing a variable, like "a".
+class VariableExprAST : public ExprAST {
+ std::string Name;
+public:
+ VariableExprAST(const std::string &name) : Name(name) {}
+ const std::string &getName() const { return Name; }
+ virtual Value *Codegen();
+};
+
+/// UnaryExprAST - Expression class for a unary operator.
+class UnaryExprAST : public ExprAST {
+ char Opcode;
+ ExprAST *Operand;
+public:
+ UnaryExprAST(char opcode, ExprAST *operand)
+ : Opcode(opcode), Operand(operand) {}
+ virtual Value *Codegen();
+};
+
+/// BinaryExprAST - Expression class for a binary operator.
+class BinaryExprAST : public ExprAST {
+ char Op;
+ ExprAST *LHS, *RHS;
+public:
+ BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs)
+ : Op(op), LHS(lhs), RHS(rhs) {}
+ virtual Value *Codegen();
+};
+
+/// CallExprAST - Expression class for function calls.
+class CallExprAST : public ExprAST {
+ std::string Callee;
+ std::vector<ExprAST*> Args;
+public:
+ CallExprAST(const std::string &callee, std::vector<ExprAST*> &args)
+ : Callee(callee), Args(args) {}
+ virtual Value *Codegen();
+};
+
+/// IfExprAST - Expression class for if/then/else.
+class IfExprAST : public ExprAST {
+ ExprAST *Cond, *Then, *Else;
+public:
+ IfExprAST(ExprAST *cond, ExprAST *then, ExprAST *_else)
+ : Cond(cond), Then(then), Else(_else) {}
+ virtual Value *Codegen();
+};
+
+/// ForExprAST - Expression class for for/in.
+class ForExprAST : public ExprAST {
+ std::string VarName;
+ ExprAST *Start, *End, *Step, *Body;
+public:
+ ForExprAST(const std::string &varname, ExprAST *start, ExprAST *end,
+ ExprAST *step, ExprAST *body)
+ : VarName(varname), Start(start), End(end), Step(step), Body(body) {}
+ virtual Value *Codegen();
+};
+
+/// VarExprAST - Expression class for var/in
+class VarExprAST : public ExprAST {
+ std::vector<std::pair<std::string, ExprAST*> > VarNames;
+ ExprAST *Body;
+public:
+ VarExprAST(const std::vector<std::pair<std::string, ExprAST*> > &varnames,
+ ExprAST *body)
+ : VarNames(varnames), Body(body) {}
+
+ virtual Value *Codegen();
+};
+
+/// PrototypeAST - This class represents the "prototype" for a function,
+/// which captures its argument names as well as if it is an operator.
+class PrototypeAST {
+ std::string Name;
+ std::vector<std::string> Args;
+ bool isOperator;
+ unsigned Precedence; // Precedence if a binary op.
+public:
+ PrototypeAST(const std::string &name, const std::vector<std::string> &args,
+ bool isoperator = false, unsigned prec = 0)
+ : Name(name), Args(args), isOperator(isoperator), Precedence(prec) {}
+
+ bool isUnaryOp() const { return isOperator && Args.size() == 1; }
+ bool isBinaryOp() const { return isOperator && Args.size() == 2; }
+
+ char getOperatorName() const {
+ assert(isUnaryOp() || isBinaryOp());
+ return Name[Name.size()-1];
+ }
+
+ unsigned getBinaryPrecedence() const { return Precedence; }
+
+ Function *Codegen();
+
+ void CreateArgumentAllocas(Function *F);
+};
+
+/// FunctionAST - This class represents a function definition itself.
+class FunctionAST {
+ PrototypeAST *Proto;
+ ExprAST *Body;
+public:
+ FunctionAST(PrototypeAST *proto, ExprAST *body)
+ : Proto(proto), Body(body) {}
+
+ Function *Codegen();
+};
+
+//===----------------------------------------------------------------------===//
+// Parser
+//===----------------------------------------------------------------------===//
+
+/// CurTok/getNextToken - Provide a simple token buffer. CurTok is the current
+/// token the parser it looking at. getNextToken reads another token from the
+/// lexer and updates CurTok with its results.
+static int CurTok;
+static int getNextToken() {
+ return CurTok = gettok();
+}
+
+/// BinopPrecedence - This holds the precedence for each binary operator that is
+/// defined.
+static std::map<char, int> BinopPrecedence;
+
+/// GetTokPrecedence - Get the precedence of the pending binary operator token.
+static int GetTokPrecedence() {
+ if (!isascii(CurTok))
+ return -1;
+
+ // Make sure it's a declared binop.
+ int TokPrec = BinopPrecedence[CurTok];
+ if (TokPrec <= 0) return -1;
+ return TokPrec;
+}
+
+/// Error* - These are little helper functions for error handling.
+ExprAST *Error(const char *Str) { fprintf(stderr, "Error: %s\n", Str);return 0;}
+PrototypeAST *ErrorP(const char *Str) { Error(Str); return 0; }
+FunctionAST *ErrorF(const char *Str) { Error(Str); return 0; }
+
+static ExprAST *ParseExpression();
+
+/// identifierexpr
+/// ::= identifier
+/// ::= identifier '(' expression* ')'
+static ExprAST *ParseIdentifierExpr() {
+ std::string IdName = IdentifierStr;
+
+ getNextToken(); // eat identifier.
+
+ if (CurTok != '(') // Simple variable ref.
+ return new VariableExprAST(IdName);
+
+ // Call.
+ getNextToken(); // eat (
+ std::vector<ExprAST*> Args;
+ if (CurTok != ')') {
+ while (1) {
+ ExprAST *Arg = ParseExpression();
+ if (!Arg) return 0;
+ Args.push_back(Arg);
+
+ if (CurTok == ')') break;
+
+ if (CurTok != ',')
+ return Error("Expected ')' or ',' in argument list");
+ getNextToken();
+ }
+ }
+
+ // Eat the ')'.
+ getNextToken();
+
+ return new CallExprAST(IdName, Args);
+}
+
+/// numberexpr ::= number
+static ExprAST *ParseNumberExpr() {
+ ExprAST *Result = new NumberExprAST(NumVal);
+ getNextToken(); // consume the number
+ return Result;
+}
+
+/// parenexpr ::= '(' expression ')'
+static ExprAST *ParseParenExpr() {
+ getNextToken(); // eat (.
+ ExprAST *V = ParseExpression();
+ if (!V) return 0;
+
+ if (CurTok != ')')
+ return Error("expected ')'");
+ getNextToken(); // eat ).
+ return V;
+}
+
+/// ifexpr ::= 'if' expression 'then' expression 'else' expression
+static ExprAST *ParseIfExpr() {
+ getNextToken(); // eat the if.
+
+ // condition.
+ ExprAST *Cond = ParseExpression();
+ if (!Cond) return 0;
+
+ if (CurTok != tok_then)
+ return Error("expected then");
+ getNextToken(); // eat the then
+
+ ExprAST *Then = ParseExpression();
+ if (Then == 0) return 0;
+
+ if (CurTok != tok_else)
+ return Error("expected else");
+
+ getNextToken();
+
+ ExprAST *Else = ParseExpression();
+ if (!Else) return 0;
+
+ return new IfExprAST(Cond, Then, Else);
+}
+
+/// forexpr ::= 'for' identifier '=' expr ',' expr (',' expr)? 'in' expression
+static ExprAST *ParseForExpr() {
+ getNextToken(); // eat the for.
+
+ if (CurTok != tok_identifier)
+ return Error("expected identifier after for");
+
+ std::string IdName = IdentifierStr;
+ getNextToken(); // eat identifier.
+
+ if (CurTok != '=')
+ return Error("expected '=' after for");
+ getNextToken(); // eat '='.
+
+
+ ExprAST *Start = ParseExpression();
+ if (Start == 0) return 0;
+ if (CurTok != ',')
+ return Error("expected ',' after for start value");
+ getNextToken();
+
+ ExprAST *End = ParseExpression();
+ if (End == 0) return 0;
+
+ // The step value is optional.
+ ExprAST *Step = 0;
+ if (CurTok == ',') {
+ getNextToken();
+ Step = ParseExpression();
+ if (Step == 0) return 0;
+ }
+
+ if (CurTok != tok_in)
+ return Error("expected 'in' after for");
+ getNextToken(); // eat 'in'.
+
+ ExprAST *Body = ParseExpression();
+ if (Body == 0) return 0;
+
+ return new ForExprAST(IdName, Start, End, Step, Body);
+}
+
+/// varexpr ::= 'var' identifier ('=' expression)?
+// (',' identifier ('=' expression)?)* 'in' expression
+static ExprAST *ParseVarExpr() {
+ getNextToken(); // eat the var.
+
+ std::vector<std::pair<std::string, ExprAST*> > VarNames;
+
+ // At least one variable name is required.
+ if (CurTok != tok_identifier)
+ return Error("expected identifier after var");
+
+ while (1) {
+ std::string Name = IdentifierStr;
+ getNextToken(); // eat identifier.
+
+ // Read the optional initializer.
+ ExprAST *Init = 0;
+ if (CurTok == '=') {
+ getNextToken(); // eat the '='.
+
+ Init = ParseExpression();
+ if (Init == 0) return 0;
+ }
+
+ VarNames.push_back(std::make_pair(Name, Init));
+
+ // End of var list, exit loop.
+ if (CurTok != ',') break;
+ getNextToken(); // eat the ','.
+
+ if (CurTok != tok_identifier)
+ return Error("expected identifier list after var");
+ }
+
+ // At this point, we have to have 'in'.
+ if (CurTok != tok_in)
+ return Error("expected 'in' keyword after 'var'");
+ getNextToken(); // eat 'in'.
+
+ ExprAST *Body = ParseExpression();
+ if (Body == 0) return 0;
+
+ return new VarExprAST(VarNames, Body);
+}
+
+
+/// primary
+/// ::= identifierexpr
+/// ::= numberexpr
+/// ::= parenexpr
+/// ::= ifexpr
+/// ::= forexpr
+/// ::= varexpr
+static ExprAST *ParsePrimary() {
+ switch (CurTok) {
+ default: return Error("unknown token when expecting an expression");
+ case tok_identifier: return ParseIdentifierExpr();
+ case tok_number: return ParseNumberExpr();
+ case '(': return ParseParenExpr();
+ case tok_if: return ParseIfExpr();
+ case tok_for: return ParseForExpr();
+ case tok_var: return ParseVarExpr();
+ }
+}
+
+/// unary
+/// ::= primary
+/// ::= '!' unary
+static ExprAST *ParseUnary() {
+ // If the current token is not an operator, it must be a primary expr.
+ if (!isascii(CurTok) || CurTok == '(' || CurTok == ',')
+ return ParsePrimary();
+
+ // If this is a unary operator, read it.
+ int Opc = CurTok;
+ getNextToken();
+ if (ExprAST *Operand = ParseUnary())
+ return new UnaryExprAST(Opc, Operand);
+ return 0;
+}
+
+/// binoprhs
+/// ::= ('+' unary)*
+static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) {
+ // If this is a binop, find its precedence.
+ while (1) {
+ int TokPrec = GetTokPrecedence();
+
+ // If this is a binop that binds at least as tightly as the current binop,
+ // consume it, otherwise we are done.
+ if (TokPrec < ExprPrec)
+ return LHS;
+
+ // Okay, we know this is a binop.
+ int BinOp = CurTok;
+ getNextToken(); // eat binop
+
+ // Parse the unary expression after the binary operator.
+ ExprAST *RHS = ParseUnary();
+ if (!RHS) return 0;
+
+ // If BinOp binds less tightly with RHS than the operator after RHS, let
+ // the pending operator take RHS as its LHS.
+ int NextPrec = GetTokPrecedence();
+ if (TokPrec < NextPrec) {
+ RHS = ParseBinOpRHS(TokPrec+1, RHS);
+ if (RHS == 0) return 0;
+ }
+
+ // Merge LHS/RHS.
+ LHS = new BinaryExprAST(BinOp, LHS, RHS);
+ }
+}
+
+/// expression
+/// ::= unary binoprhs
+///
+static ExprAST *ParseExpression() {
+ ExprAST *LHS = ParseUnary();
+ if (!LHS) return 0;
+
+ return ParseBinOpRHS(0, LHS);
+}
+
+/// prototype
+/// ::= id '(' id* ')'
+/// ::= binary LETTER number? (id, id)
+/// ::= unary LETTER (id)
+static PrototypeAST *ParsePrototype() {
+ std::string FnName;
+
+ unsigned Kind = 0; // 0 = identifier, 1 = unary, 2 = binary.
+ unsigned BinaryPrecedence = 30;
+
+ switch (CurTok) {
+ default:
+ return ErrorP("Expected function name in prototype");
+ case tok_identifier:
+ FnName = IdentifierStr;
+ Kind = 0;
+ getNextToken();
+ break;
+ case tok_unary:
+ getNextToken();
+ if (!isascii(CurTok))
+ return ErrorP("Expected unary operator");
+ FnName = "unary";
+ FnName += (char)CurTok;
+ Kind = 1;
+ getNextToken();
+ break;
+ case tok_binary:
+ getNextToken();
+ if (!isascii(CurTok))
+ return ErrorP("Expected binary operator");
+ FnName = "binary";
+ FnName += (char)CurTok;
+ Kind = 2;
+ getNextToken();
+
+ // Read the precedence if present.
+ if (CurTok == tok_number) {
+ if (NumVal < 1 || NumVal > 100)
+ return ErrorP("Invalid precedecnce: must be 1..100");
+ BinaryPrecedence = (unsigned)NumVal;
+ getNextToken();
+ }
+ break;
+ }
+
+ if (CurTok != '(')
+ return ErrorP("Expected '(' in prototype");
+
+ std::vector<std::string> ArgNames;
+ while (getNextToken() == tok_identifier)
+ ArgNames.push_back(IdentifierStr);
+ if (CurTok != ')')
+ return ErrorP("Expected ')' in prototype");
+
+ // success.
+ getNextToken(); // eat ')'.
+
+ // Verify right number of names for operator.
+ if (Kind && ArgNames.size() != Kind)
+ return ErrorP("Invalid number of operands for operator");
+
+ return new PrototypeAST(FnName, ArgNames, Kind != 0, BinaryPrecedence);
+}
+
+/// definition ::= 'def' prototype expression
+static FunctionAST *ParseDefinition() {
+ getNextToken(); // eat def.
+ PrototypeAST *Proto = ParsePrototype();
+ if (Proto == 0) return 0;
+
+ if (ExprAST *E = ParseExpression())
+ return new FunctionAST(Proto, E);
+ return 0;
+}
+
+/// toplevelexpr ::= expression
+static FunctionAST *ParseTopLevelExpr() {
+ if (ExprAST *E = ParseExpression()) {
+ // Make an anonymous proto.
+ PrototypeAST *Proto = new PrototypeAST("", std::vector<std::string>());
+ return new FunctionAST(Proto, E);
+ }
+ return 0;
+}
+
+/// external ::= 'extern' prototype
+static PrototypeAST *ParseExtern() {
+ getNextToken(); // eat extern.
+ return ParsePrototype();
+}
+
+//===----------------------------------------------------------------------===//
+// Code Generation
+//===----------------------------------------------------------------------===//
+
+static Module *TheModule;
+static IRBuilder<> Builder;
+static std::map<std::string, AllocaInst*> NamedValues;
+static FunctionPassManager *TheFPM;
+
+Value *ErrorV(const char *Str) { Error(Str); return 0; }
+
+/// CreateEntryBlockAlloca - Create an alloca instruction in the entry block of
+/// the function. This is used for mutable variables etc.
+static AllocaInst *CreateEntryBlockAlloca(Function *TheFunction,
+ const std::string &VarName) {
+ IRBuilder<> TmpB(&TheFunction->getEntryBlock(),
+ TheFunction->getEntryBlock().begin());
+ return TmpB.CreateAlloca(Type::DoubleTy, 0, VarName.c_str());
+}
+
+
+Value *NumberExprAST::Codegen() {
+ return ConstantFP::get(APFloat(Val));
+}
+
+Value *VariableExprAST::Codegen() {
+ // Look this variable up in the function.
+ Value *V = NamedValues[Name];
+ if (V == 0) return ErrorV("Unknown variable name");
+
+ // Load the value.
+ return Builder.CreateLoad(V, Name.c_str());
+}
+
+Value *UnaryExprAST::Codegen() {
+ Value *OperandV = Operand->Codegen();
+ if (OperandV == 0) return 0;
+
+ Function *F = TheModule->getFunction(std::string("unary")+Opcode);
+ if (F == 0)
+ return ErrorV("Unknown unary operator");
+
+ return Builder.CreateCall(F, OperandV, "unop");
+}
+
+
+Value *BinaryExprAST::Codegen() {
+ // Special case '=' because we don't want to emit the LHS as an expression.
+ if (Op == '=') {
+ // Assignment requires the LHS to be an identifier.
+ VariableExprAST *LHSE = dynamic_cast<VariableExprAST*>(LHS);
+ if (!LHSE)
+ return ErrorV("destination of '=' must be a variable");
+ // Codegen the RHS.
+ Value *Val = RHS->Codegen();
+ if (Val == 0) return 0;
+
+ // Look up the name.
+ Value *Variable = NamedValues[LHSE->getName()];
+ if (Variable == 0) return ErrorV("Unknown variable name");
+
+ Builder.CreateStore(Val, Variable);
+ return Val;
+ }
+
+
+ Value *L = LHS->Codegen();
+ Value *R = RHS->Codegen();
+ if (L == 0 || R == 0) return 0;
+
+ switch (Op) {
+ case '+': return Builder.CreateAdd(L, R, "addtmp");
+ case '-': return Builder.CreateSub(L, R, "subtmp");
+ case '*': return Builder.CreateMul(L, R, "multmp");
+ case '<':
+ L = Builder.CreateFCmpULT(L, R, "cmptmp");
+ // Convert bool 0/1 to double 0.0 or 1.0
+ return Builder.CreateUIToFP(L, Type::DoubleTy, "booltmp");
+ default: break;
+ }
+
+ // If it wasn't a builtin binary operator, it must be a user defined one. Emit
+ // a call to it.
+ Function *F = TheModule->getFunction(std::string("binary")+Op);
+ assert(F && "binary operator not found!");
+
+ Value *Ops[] = { L, R };
+ return Builder.CreateCall(F, Ops, Ops+2, "binop");
+}
+
+Value *CallExprAST::Codegen() {
+ // Look up the name in the global module table.
+ Function *CalleeF = TheModule->getFunction(Callee);
+ if (CalleeF == 0)
+ return ErrorV("Unknown function referenced");
+
+ // If argument mismatch error.
+ if (CalleeF->arg_size() != Args.size())
+ return ErrorV("Incorrect # arguments passed");
+
+ std::vector<Value*> ArgsV;
+ for (unsigned i = 0, e = Args.size(); i != e; ++i) {
+ ArgsV.push_back(Args[i]->Codegen());
+ if (ArgsV.back() == 0) return 0;
+ }
+
+ return Builder.CreateCall(CalleeF, ArgsV.begin(), ArgsV.end(), "calltmp");
+}
+
+Value *IfExprAST::Codegen() {
+ Value *CondV = Cond->Codegen();
+ if (CondV == 0) return 0;
+
+ // Convert condition to a bool by comparing equal to 0.0.
+ CondV = Builder.CreateFCmpONE(CondV,
+ ConstantFP::get(APFloat(0.0)),
+ "ifcond");
+
+ Function *TheFunction = Builder.GetInsertBlock()->getParent();
+
+ // Create blocks for the then and else cases. Insert the 'then' block at the
+ // end of the function.
+ BasicBlock *ThenBB = BasicBlock::Create("then", TheFunction);
+ BasicBlock *ElseBB = BasicBlock::Create("else");
+ BasicBlock *MergeBB = BasicBlock::Create("ifcont");
+
+ Builder.CreateCondBr(CondV, ThenBB, ElseBB);
+
+ // Emit then value.
+ Builder.SetInsertPoint(ThenBB);
+
+ Value *ThenV = Then->Codegen();
+ if (ThenV == 0) return 0;
+
+ Builder.CreateBr(MergeBB);
+ // Codegen of 'Then' can change the current block, update ThenBB for the PHI.
+ ThenBB = Builder.GetInsertBlock();
+
+ // Emit else block.
+ TheFunction->getBasicBlockList().push_back(ElseBB);
+ Builder.SetInsertPoint(ElseBB);
+
+ Value *ElseV = Else->Codegen();
+ if (ElseV == 0) return 0;
+
+ Builder.CreateBr(MergeBB);
+ // Codegen of 'Else' can change the current block, update ElseBB for the PHI.
+ ElseBB = Builder.GetInsertBlock();
+
+ // Emit merge block.
+ TheFunction->getBasicBlockList().push_back(MergeBB);
+ Builder.SetInsertPoint(MergeBB);
+ PHINode *PN = Builder.CreatePHI(Type::DoubleTy, "iftmp");
+
+ PN->addIncoming(ThenV, ThenBB);
+ PN->addIncoming(ElseV, ElseBB);
+ return PN;
+}
+
+Value *ForExprAST::Codegen() {
+ // Output this as:
+ // var = alloca double
+ // ...
+ // start = startexpr
+ // store start -> var
+ // goto loop
+ // loop:
+ // ...
+ // bodyexpr
+ // ...
+ // loopend:
+ // step = stepexpr
+ // endcond = endexpr
+ //
+ // curvar = load var
+ // nextvar = curvar + step
+ // store nextvar -> var
+ // br endcond, loop, endloop
+ // outloop:
+
+ Function *TheFunction = Builder.GetInsertBlock()->getParent();
+
+ // Create an alloca for the variable in the entry block.
+ AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
+
+ // Emit the start code first, without 'variable' in scope.
+ Value *StartVal = Start->Codegen();
+ if (StartVal == 0) return 0;
+
+ // Store the value into the alloca.
+ Builder.CreateStore(StartVal, Alloca);
+
+ // Make the new basic block for the loop header, inserting after current
+ // block.
+ BasicBlock *LoopBB = BasicBlock::Create("loop", TheFunction);
+
+ // Insert an explicit fall through from the current block to the LoopBB.
+ Builder.CreateBr(LoopBB);
+
+ // Start insertion in LoopBB.
+ Builder.SetInsertPoint(LoopBB);
+
+ // Within the loop, the variable is defined equal to the PHI node. If it
+ // shadows an existing variable, we have to restore it, so save it now.
+ AllocaInst *OldVal = NamedValues[VarName];
+ NamedValues[VarName] = Alloca;
+
+ // Emit the body of the loop. This, like any other expr, can change the
+ // current BB. Note that we ignore the value computed by the body, but don't
+ // allow an error.
+ if (Body->Codegen() == 0)
+ return 0;
+
+ // Emit the step value.
+ Value *StepVal;
+ if (Step) {
+ StepVal = Step->Codegen();
+ if (StepVal == 0) return 0;
+ } else {
+ // If not specified, use 1.0.
+ StepVal = ConstantFP::get(APFloat(1.0));
+ }
+
+ // Compute the end condition.
+ Value *EndCond = End->Codegen();
+ if (EndCond == 0) return EndCond;
+
+ // Reload, increment, and restore the alloca. This handles the case where
+ // the body of the loop mutates the variable.
+ Value *CurVar = Builder.CreateLoad(Alloca, VarName.c_str());
+ Value *NextVar = Builder.CreateAdd(CurVar, StepVal, "nextvar");
+ Builder.CreateStore(NextVar, Alloca);
+
+ // Convert condition to a bool by comparing equal to 0.0.
+ EndCond = Builder.CreateFCmpONE(EndCond,
+ ConstantFP::get(APFloat(0.0)),
+ "loopcond");
+
+ // Create the "after loop" block and insert it.
+ BasicBlock *AfterBB = BasicBlock::Create("afterloop", TheFunction);
+
+ // Insert the conditional branch into the end of LoopEndBB.
+ Builder.CreateCondBr(EndCond, LoopBB, AfterBB);
+
+ // Any new code will be inserted in AfterBB.
+ Builder.SetInsertPoint(AfterBB);
+
+ // Restore the unshadowed variable.
+ if (OldVal)
+ NamedValues[VarName] = OldVal;
+ else
+ NamedValues.erase(VarName);
+
+
+ // for expr always returns 0.0.
+ return Constant::getNullValue(Type::DoubleTy);
+}
+
+Value *VarExprAST::Codegen() {
+ std::vector<AllocaInst *> OldBindings;
+
+ Function *TheFunction = Builder.GetInsertBlock()->getParent();
+
+ // Register all variables and emit their initializer.
+ for (unsigned i = 0, e = VarNames.size(); i != e; ++i) {
+ const std::string &VarName = VarNames[i].first;
+ ExprAST *Init = VarNames[i].second;
+
+ // Emit the initializer before adding the variable to scope, this prevents
+ // the initializer from referencing the variable itself, and permits stuff
+ // like this:
+ // var a = 1 in
+ // var a = a in ... # refers to outer 'a'.
+ Value *InitVal;
+ if (Init) {
+ InitVal = Init->Codegen();
+ if (InitVal == 0) return 0;
+ } else { // If not specified, use 0.0.
+ InitVal = ConstantFP::get(APFloat(0.0));
+ }
+
+ AllocaInst *Alloca = CreateEntryBlockAlloca(TheFunction, VarName);
+ Builder.CreateStore(InitVal, Alloca);
+
+ // Remember the old variable binding so that we can restore the binding when
+ // we unrecurse.
+ OldBindings.push_back(NamedValues[VarName]);
+
+ // Remember this binding.
+ NamedValues[VarName] = Alloca;
+ }
+
+ // Codegen the body, now that all vars are in scope.
+ Value *BodyVal = Body->Codegen();
+ if (BodyVal == 0) return 0;
+
+ // Pop all our variables from scope.
+ for (unsigned i = 0, e = VarNames.size(); i != e; ++i)
+ NamedValues[VarNames[i].first] = OldBindings[i];
+
+ // Return the body computation.
+ return BodyVal;
+}
+
+
+Function *PrototypeAST::Codegen() {
+ // Make the function type: double(double,double) etc.
+ std::vector<const Type*> Doubles(Args.size(), Type::DoubleTy);
+ FunctionType *FT = FunctionType::get(Type::DoubleTy, Doubles, false);
+
+ Function *F = Function::Create(FT, Function::ExternalLinkage, Name, TheModule);
+
+ // If F conflicted, there was already something named 'Name'. If it has a
+ // body, don't allow redefinition or reextern.
+ if (F->getName() != Name) {
+ // Delete the one we just made and get the existing one.
+ F->eraseFromParent();
+ F = TheModule->getFunction(Name);
+
+ // If F already has a body, reject this.
+ if (!F->empty()) {
+ ErrorF("redefinition of function");
+ return 0;
+ }
+
+ // If F took a different number of args, reject.
+ if (F->arg_size() != Args.size()) {
+ ErrorF("redefinition of function with different # args");
+ return 0;
+ }
+ }
+
+ // Set names for all arguments.
+ unsigned Idx = 0;
+ for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
+ ++AI, ++Idx)
+ AI->setName(Args[Idx]);
+
+ return F;
+}
+
+/// CreateArgumentAllocas - Create an alloca for each argument and register the
+/// argument in the symbol table so that references to it will succeed.
+void PrototypeAST::CreateArgumentAllocas(Function *F) {
+ Function::arg_iterator AI = F->arg_begin();
+ for (unsigned Idx = 0, e = Args.size(); Idx != e; ++Idx, ++AI) {
+ // Create an alloca for this variable.
+ AllocaInst *Alloca = CreateEntryBlockAlloca(F, Args[Idx]);
+
+ // Store the initial value into the alloca.
+ Builder.CreateStore(AI, Alloca);
+
+ // Add arguments to variable symbol table.
+ NamedValues[Args[Idx]] = Alloca;
+ }
+}
+
+
+Function *FunctionAST::Codegen() {
+ NamedValues.clear();
+
+ Function *TheFunction = Proto->Codegen();
+ if (TheFunction == 0)
+ return 0;
+
+ // If this is an operator, install it.
+ if (Proto->isBinaryOp())
+ BinopPrecedence[Proto->getOperatorName()] = Proto->getBinaryPrecedence();
+
+ // Create a new basic block to start insertion into.
+ BasicBlock *BB = BasicBlock::Create("entry", TheFunction);
+ Builder.SetInsertPoint(BB);
+
+ // Add all arguments to the symbol table and create their allocas.
+ Proto->CreateArgumentAllocas(TheFunction);
+
+ if (Value *RetVal = Body->Codegen()) {
+ // Finish off the function.
+ Builder.CreateRet(RetVal);
+
+ // Validate the generated code, checking for consistency.
+ verifyFunction(*TheFunction);
+
+ // Optimize the function.
+ TheFPM->run(*TheFunction);
+
+ return TheFunction;
+ }
+
+ // Error reading body, remove function.
+ TheFunction->eraseFromParent();
+
+ if (Proto->isBinaryOp())
+ BinopPrecedence.erase(Proto->getOperatorName());
+ return 0;
+}
+
+//===----------------------------------------------------------------------===//
+// Top-Level parsing and JIT Driver
+//===----------------------------------------------------------------------===//
+
+static ExecutionEngine *TheExecutionEngine;
+
+static void HandleDefinition() {
+ if (FunctionAST *F = ParseDefinition()) {
+ if (Function *LF = F->Codegen()) {
+ fprintf(stderr, "Read function definition:");
+ LF->dump();
+ }
+ } else {
+ // Skip token for error recovery.
+ getNextToken();
+ }
+}
+
+static void HandleExtern() {
+ if (PrototypeAST *P = ParseExtern()) {
+ if (Function *F = P->Codegen()) {
+ fprintf(stderr, "Read extern: ");
+ F->dump();
+ }
+ } else {
+ // Skip token for error recovery.
+ getNextToken();
+ }
+}
+
+static void HandleTopLevelExpression() {
+ // Evaluate a top level expression into an anonymous function.
+ if (FunctionAST *F = ParseTopLevelExpr()) {
+ if (Function *LF = F->Codegen()) {
+ // JIT the function, returning a function pointer.
+ void *FPtr = TheExecutionEngine->getPointerToFunction(LF);
+
+ // Cast it to the right type (takes no arguments, returns a double) so we
+ // can call it as a native function.
+ double (*FP)() = (double (*)())(intptr_t)FPtr;
+ fprintf(stderr, "Evaluated to %f\n", FP());
+ }
+ } else {
+ // Skip token for error recovery.
+ getNextToken();
+ }
+}
+
+/// top ::= definition | external | expression | ';'
+static void MainLoop() {
+ while (1) {
+ fprintf(stderr, "ready> ");
+ switch (CurTok) {
+ case tok_eof: return;
+ case ';': getNextToken(); break; // ignore top level semicolons.
+ case tok_def: HandleDefinition(); break;
+ case tok_extern: HandleExtern(); break;
+ default: HandleTopLevelExpression(); break;
+ }
+ }
+}
+
+
+
+//===----------------------------------------------------------------------===//
+// "Library" functions that can be "extern'd" from user code.
+//===----------------------------------------------------------------------===//
+
+/// putchard - putchar that takes a double and returns 0.
+extern "C"
+double putchard(double X) {
+ putchar((char)X);
+ return 0;
+}
+
+/// printd - printf that takes a double prints it as "%f\n", returning 0.
+extern "C"
+double printd(double X) {
+ printf("%f\n", X);
+ return 0;
+}
+
+//===----------------------------------------------------------------------===//
+// Main driver code.
+//===----------------------------------------------------------------------===//
+
+int main() {
+ // Install standard binary operators.
+ // 1 is lowest precedence.
+ BinopPrecedence['='] = 2;
+ BinopPrecedence['<'] = 10;
+ BinopPrecedence['+'] = 20;
+ BinopPrecedence['-'] = 20;
+ BinopPrecedence['*'] = 40; // highest.
+
+ // Prime the first token.
+ fprintf(stderr, "ready> ");
+ getNextToken();
+
+ // Make the module, which holds all the code.
+ TheModule = new Module("my cool jit");
+
+ // Create the JIT.
+ TheExecutionEngine = ExecutionEngine::create(TheModule);
+
+ {
+ ExistingModuleProvider OurModuleProvider(TheModule);
+ FunctionPassManager OurFPM(&OurModuleProvider);
+
+ // Set up the optimizer pipeline. Start with registering info about how the
+ // target lays out data structures.
+ OurFPM.add(new TargetData(*TheExecutionEngine->getTargetData()));
+ // Promote allocas to registers.
+ OurFPM.add(createPromoteMemoryToRegisterPass());
+ // Do simple "peephole" optimizations and bit-twiddling optzns.
+ OurFPM.add(createInstructionCombiningPass());
+ // Reassociate expressions.
+ OurFPM.add(createReassociatePass());
+ // Eliminate Common SubExpressions.
+ OurFPM.add(createGVNPass());
+ // Simplify the control flow graph (deleting unreachable blocks, etc).
+ OurFPM.add(createCFGSimplificationPass());
+
+ // Set the global so the code gen can use this.
+ TheFPM = &OurFPM;
+
+ // Run the main "interpreter loop" now.
+ MainLoop();
+
+ TheFPM = 0;
+
+ // Print out all of the generated code.
+ TheModule->dump();
+
+ } // Free module provider (and thus the module) and pass manager.
+
+ return 0;
+}
+
diff --git a/examples/Makefile b/examples/Makefile
new file mode 100644
index 0000000..eb5dabd
--- /dev/null
+++ b/examples/Makefile
@@ -0,0 +1,19 @@
+##===- examples/Makefile -----------------------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL=..
+
+include $(LEVEL)/Makefile.config
+
+PARALLEL_DIRS:= BrainF Fibonacci HowToUseJIT Kaleidoscope ModuleMaker
+
+ifeq ($(HAVE_PTHREAD),1)
+PARALLEL_DIRS += ParallelJIT
+endif
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/ModuleMaker/CMakeLists.txt b/examples/ModuleMaker/CMakeLists.txt
new file mode 100644
index 0000000..81e9115
--- /dev/null
+++ b/examples/ModuleMaker/CMakeLists.txt
@@ -0,0 +1,5 @@
+set(LLVM_LINK_COMPONENTS bitwriter)
+
+add_llvm_example(ModuleMaker
+ ModuleMaker.cpp
+ )
diff --git a/examples/ModuleMaker/Makefile b/examples/ModuleMaker/Makefile
new file mode 100644
index 0000000..9454cf5
--- /dev/null
+++ b/examples/ModuleMaker/Makefile
@@ -0,0 +1,14 @@
+##===- examples/ModuleMaker/Makefile -----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL=../..
+TOOLNAME=ModuleMaker
+EXAMPLE_TOOL = 1
+LINK_COMPONENTS := bitwriter
+
+include $(LEVEL)/Makefile.common
diff --git a/examples/ModuleMaker/ModuleMaker.cpp b/examples/ModuleMaker/ModuleMaker.cpp
new file mode 100644
index 0000000..154e2406
--- /dev/null
+++ b/examples/ModuleMaker/ModuleMaker.cpp
@@ -0,0 +1,61 @@
+//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This programs is a simple example that creates an LLVM module "from scratch",
+// emitting it as a bitcode file to standard out. This is just to show how
+// LLVM projects work and to demonstrate some of the LLVM APIs.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Module.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Constants.h"
+#include "llvm/Instructions.h"
+#include "llvm/Bitcode/ReaderWriter.h"
+#include <iostream>
+using namespace llvm;
+
+int main() {
+ // Create the "module" or "program" or "translation unit" to hold the
+ // function
+ Module *M = new Module("test");
+
+ // Create the main function: first create the type 'int ()'
+ FunctionType *FT = FunctionType::get(Type::Int32Ty, std::vector<const Type*>(),
+ /*not vararg*/false);
+
+ // By passing a module as the last parameter to the Function constructor,
+ // it automatically gets appended to the Module.
+ Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
+
+ // Add a basic block to the function... again, it automatically inserts
+ // because of the last argument.
+ BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
+
+ // Get pointers to the constant integers...
+ Value *Two = ConstantInt::get(Type::Int32Ty, 2);
+ Value *Three = ConstantInt::get(Type::Int32Ty, 3);
+
+ // Create the add instruction... does not insert...
+ Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
+ "addresult");
+
+ // explicitly insert it into the basic block...
+ BB->getInstList().push_back(Add);
+
+ // Create the return instruction and add it to the basic block
+ BB->getInstList().push_back(ReturnInst::Create(Add));
+
+ // Output the bitcode file to stdout
+ WriteBitcodeToFile(M, std::cout);
+
+ // Delete the module and all of its contents.
+ delete M;
+ return 0;
+}
diff --git a/examples/ModuleMaker/README.txt b/examples/ModuleMaker/README.txt
new file mode 100644
index 0000000..ecbe30e
--- /dev/null
+++ b/examples/ModuleMaker/README.txt
@@ -0,0 +1,8 @@
+//===----------------------------------------------------------------------===//
+// ModuleMaker Sample project
+//===----------------------------------------------------------------------===//
+
+This project is an extremely simple example of using some simple pieces of the
+LLVM API. The actual executable generated by this project simply emits an
+LLVM bytecode file to standard output. It is designed to show some basic
+usage of LLVM APIs, and how to link to LLVM libraries.
diff --git a/examples/ParallelJIT/CMakeLists.txt b/examples/ParallelJIT/CMakeLists.txt
new file mode 100644
index 0000000..d8dd708
--- /dev/null
+++ b/examples/ParallelJIT/CMakeLists.txt
@@ -0,0 +1,7 @@
+set(LLVM_LINK_COMPONENTS jit interpreter nativecodegen)
+
+add_llvm_example(ParallelJIT
+ ParallelJIT.cpp
+ )
+
+target_link_libraries(ParallelJIT pthread)
diff --git a/examples/ParallelJIT/Makefile b/examples/ParallelJIT/Makefile
new file mode 100644
index 0000000..8a49d42
--- /dev/null
+++ b/examples/ParallelJIT/Makefile
@@ -0,0 +1,17 @@
+##===- examples/ParallelJIT/Makefile -----------------------*- Makefile -*-===##
+#
+# The LLVM Compiler Infrastructure
+#
+# This file is distributed under the University of Illinois Open Source
+# License. See LICENSE.TXT for details.
+#
+##===----------------------------------------------------------------------===##
+LEVEL = ../..
+TOOLNAME = ParallelJIT
+EXAMPLE_TOOL = 1
+
+LINK_COMPONENTS := jit interpreter nativecodegen
+
+include $(LEVEL)/Makefile.common
+
+LIBS += -lpthread
diff --git a/examples/ParallelJIT/ParallelJIT.cpp b/examples/ParallelJIT/ParallelJIT.cpp
new file mode 100644
index 0000000..e812d84
--- /dev/null
+++ b/examples/ParallelJIT/ParallelJIT.cpp
@@ -0,0 +1,298 @@
+//===-- examples/ParallelJIT/ParallelJIT.cpp - Exercise threaded-safe JIT -===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Parallel JIT
+//
+// This test program creates two LLVM functions then calls them from three
+// separate threads. It requires the pthreads library.
+// The three threads are created and then block waiting on a condition variable.
+// Once all threads are blocked on the conditional variable, the main thread
+// wakes them up. This complicated work is performed so that all three threads
+// call into the JIT at the same time (or the best possible approximation of the
+// same time). This test had assertion errors until I got the locking right.
+
+#include <pthread.h>
+#include "llvm/Module.h"
+#include "llvm/Constants.h"
+#include "llvm/DerivedTypes.h"
+#include "llvm/Instructions.h"
+#include "llvm/ModuleProvider.h"
+#include "llvm/ExecutionEngine/JIT.h"
+#include "llvm/ExecutionEngine/Interpreter.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include <iostream>
+using namespace llvm;
+
+static Function* createAdd1(Module *M) {
+ // Create the add1 function entry and insert this entry into module M. The
+ // function will have a return type of "int" and take an argument of "int".
+ // The '0' terminates the list of argument types.
+ Function *Add1F =
+ cast<Function>(M->getOrInsertFunction("add1", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function. As before, it automatically inserts
+ // because of the last argument.
+ BasicBlock *BB = BasicBlock::Create("EntryBlock", Add1F);
+
+ // Get pointers to the constant `1'.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+
+ // Get pointers to the integer argument of the add1 function...
+ assert(Add1F->arg_begin() != Add1F->arg_end()); // Make sure there's an arg
+ Argument *ArgX = Add1F->arg_begin(); // Get the arg
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the add instruction, inserting it into the end of BB.
+ Instruction *Add = BinaryOperator::CreateAdd(One, ArgX, "addresult", BB);
+
+ // Create the return instruction and add it to the basic block
+ ReturnInst::Create(Add, BB);
+
+ // Now, function add1 is ready.
+ return Add1F;
+}
+
+static Function *CreateFibFunction(Module *M) {
+ // Create the fib function and insert it into module M. This function is said
+ // to return an int and take an int parameter.
+ Function *FibF =
+ cast<Function>(M->getOrInsertFunction("fib", Type::Int32Ty, Type::Int32Ty,
+ (Type *)0));
+
+ // Add a basic block to the function.
+ BasicBlock *BB = BasicBlock::Create("EntryBlock", FibF);
+
+ // Get pointers to the constants.
+ Value *One = ConstantInt::get(Type::Int32Ty, 1);
+ Value *Two = ConstantInt::get(Type::Int32Ty, 2);
+
+ // Get pointer to the integer argument of the add1 function...
+ Argument *ArgX = FibF->arg_begin(); // Get the arg.
+ ArgX->setName("AnArg"); // Give it a nice symbolic name for fun.
+
+ // Create the true_block.
+ BasicBlock *RetBB = BasicBlock::Create("return", FibF);
+ // Create an exit block.
+ BasicBlock* RecurseBB = BasicBlock::Create("recurse", FibF);
+
+ // Create the "if (arg < 2) goto exitbb"
+ Value *CondInst = new ICmpInst(ICmpInst::ICMP_SLE, ArgX, Two, "cond", BB);
+ BranchInst::Create(RetBB, RecurseBB, CondInst, BB);
+
+ // Create: ret int 1
+ ReturnInst::Create(One, RetBB);
+
+ // create fib(x-1)
+ Value *Sub = BinaryOperator::CreateSub(ArgX, One, "arg", RecurseBB);
+ Value *CallFibX1 = CallInst::Create(FibF, Sub, "fibx1", RecurseBB);
+
+ // create fib(x-2)
+ Sub = BinaryOperator::CreateSub(ArgX, Two, "arg", RecurseBB);
+ Value *CallFibX2 = CallInst::Create(FibF, Sub, "fibx2", RecurseBB);
+
+ // fib(x-1)+fib(x-2)
+ Value *Sum =
+ BinaryOperator::CreateAdd(CallFibX1, CallFibX2, "addresult", RecurseBB);
+
+ // Create the return instruction and add it to the basic block
+ ReturnInst::Create(Sum, RecurseBB);
+
+ return FibF;
+}
+
+struct threadParams {
+ ExecutionEngine* EE;
+ Function* F;
+ int value;
+};
+
+// We block the subthreads just before they begin to execute:
+// we want all of them to call into the JIT at the same time,
+// to verify that the locking is working correctly.
+class WaitForThreads
+{
+public:
+ WaitForThreads()
+ {
+ n = 0;
+ waitFor = 0;
+
+ int result = pthread_cond_init( &condition, NULL );
+ assert( result == 0 );
+
+ result = pthread_mutex_init( &mutex, NULL );
+ assert( result == 0 );
+ }
+
+ ~WaitForThreads()
+ {
+ int result = pthread_cond_destroy( &condition );
+ assert( result == 0 );
+
+ result = pthread_mutex_destroy( &mutex );
+ assert( result == 0 );
+ }
+
+ // All threads will stop here until another thread calls releaseThreads
+ void block()
+ {
+ int result = pthread_mutex_lock( &mutex );
+ assert( result == 0 );
+ n ++;
+ //~ std::cout << "block() n " << n << " waitFor " << waitFor << std::endl;
+
+ assert( waitFor == 0 || n <= waitFor );
+ if ( waitFor > 0 && n == waitFor )
+ {
+ // There are enough threads blocked that we can release all of them
+ std::cout << "Unblocking threads from block()" << std::endl;
+ unblockThreads();
+ }
+ else
+ {
+ // We just need to wait until someone unblocks us
+ result = pthread_cond_wait( &condition, &mutex );
+ assert( result == 0 );
+ }
+
+ // unlock the mutex before returning
+ result = pthread_mutex_unlock( &mutex );
+ assert( result == 0 );
+ }
+
+ // If there are num or more threads blocked, it will signal them all
+ // Otherwise, this thread blocks until there are enough OTHER threads
+ // blocked
+ void releaseThreads( size_t num )
+ {
+ int result = pthread_mutex_lock( &mutex );
+ assert( result == 0 );
+
+ if ( n >= num ) {
+ std::cout << "Unblocking threads from releaseThreads()" << std::endl;
+ unblockThreads();
+ }
+ else
+ {
+ waitFor = num;
+ pthread_cond_wait( &condition, &mutex );
+ }
+
+ // unlock the mutex before returning
+ result = pthread_mutex_unlock( &mutex );
+ assert( result == 0 );
+ }
+
+private:
+ void unblockThreads()
+ {
+ // Reset the counters to zero: this way, if any new threads
+ // enter while threads are exiting, they will block instead
+ // of triggering a new release of threads
+ n = 0;
+
+ // Reset waitFor to zero: this way, if waitFor threads enter
+ // while threads are exiting, they will block instead of
+ // triggering a new release of threads
+ waitFor = 0;
+
+ int result = pthread_cond_broadcast( &condition );
+ assert(result == 0); result=result;
+ }
+
+ size_t n;
+ size_t waitFor;
+ pthread_cond_t condition;
+ pthread_mutex_t mutex;
+};
+
+static WaitForThreads synchronize;
+
+void* callFunc( void* param )
+{
+ struct threadParams* p = (struct threadParams*) param;
+
+ // Call the `foo' function with no arguments:
+ std::vector<GenericValue> Args(1);
+ Args[0].IntVal = APInt(32, p->value);
+
+ synchronize.block(); // wait until other threads are at this point
+ GenericValue gv = p->EE->runFunction(p->F, Args);
+
+ return (void*)(intptr_t)gv.IntVal.getZExtValue();
+}
+
+int main()
+{
+ // Create some module to put our function into it.
+ Module *M = new Module("test");
+
+ Function* add1F = createAdd1( M );
+ Function* fibF = CreateFibFunction( M );
+
+ // Now we create the JIT.
+ ExistingModuleProvider* MP = new ExistingModuleProvider(M);
+ ExecutionEngine* EE = ExecutionEngine::create(MP, false);
+
+ //~ std::cout << "We just constructed this LLVM module:\n\n" << *M;
+ //~ std::cout << "\n\nRunning foo: " << std::flush;
+
+ // Create one thread for add1 and two threads for fib
+ struct threadParams add1 = { EE, add1F, 1000 };
+ struct threadParams fib1 = { EE, fibF, 39 };
+ struct threadParams fib2 = { EE, fibF, 42 };
+
+ pthread_t add1Thread;
+ int result = pthread_create( &add1Thread, NULL, callFunc, &add1 );
+ if ( result != 0 ) {
+ std::cerr << "Could not create thread" << std::endl;
+ return 1;
+ }
+
+ pthread_t fibThread1;
+ result = pthread_create( &fibThread1, NULL, callFunc, &fib1 );
+ if ( result != 0 ) {
+ std::cerr << "Could not create thread" << std::endl;
+ return 1;
+ }
+
+ pthread_t fibThread2;
+ result = pthread_create( &fibThread2, NULL, callFunc, &fib2 );
+ if ( result != 0 ) {
+ std::cerr << "Could not create thread" << std::endl;
+ return 1;
+ }
+
+ synchronize.releaseThreads(3); // wait until other threads are at this point
+
+ void* returnValue;
+ result = pthread_join( add1Thread, &returnValue );
+ if ( result != 0 ) {
+ std::cerr << "Could not join thread" << std::endl;
+ return 1;
+ }
+ std::cout << "Add1 returned " << intptr_t(returnValue) << std::endl;
+
+ result = pthread_join( fibThread1, &returnValue );
+ if ( result != 0 ) {
+ std::cerr << "Could not join thread" << std::endl;
+ return 1;
+ }
+ std::cout << "Fib1 returned " << intptr_t(returnValue) << std::endl;
+
+ result = pthread_join( fibThread2, &returnValue );
+ if ( result != 0 ) {
+ std::cerr << "Could not join thread" << std::endl;
+ return 1;
+ }
+ std::cout << "Fib2 returned " << intptr_t(returnValue) << std::endl;
+
+ return 0;
+}
OpenPOWER on IntegriCloud