Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "llvm/ADT/SmallVector.h"
- #include "llvm/IR/Verifier.h"
- #include "llvm/IR/BasicBlock.h"
- #include "llvm/IR/CallingConv.h"
- #include "llvm/IR/Function.h"
- #include "llvm/IR/Instructions.h"
- #include "llvm/IR/LLVMContext.h"
- #include "llvm/IR/Module.h"
- #include "llvm/Bitcode/ReaderWriter.h"
- #include "llvm/Support/FileSystem.h"
- #include "llvm/Support/ToolOutputFile.h"
- #include "llvm/Support/raw_ostream.h"
- using namespace llvm;
- Module *makeLLVMModule()
- {
- Module *mod = new Module("sum.ll", getGlobalContext());
- // next values allow to do target specific optimization
- // we can leave in blank for doing only target independent
- // optimization
- // set the data layout (information about data types, and endianess)
- mod->setDataLayout("e-m:e-i64:64-f80:128-n8:16:32:64-S128");
- // set target for code, in this case my machine
- mod->setTargetTriple("x86_64-unknown-linux-gnu");
- // dechlaration of function signature
- // our function had 2 int32 arguments
- SmallVector<Type*, 2> FuncTyArgs;
- FuncTyArgs.push_back(IntegerType::get(mod->getContext(), 32));
- FuncTyArgs.push_back(IntegerType::get(mod->getContext(), 32));
- // now define the function
- FunctionType *FuncTy = FunctionType::get(
- /*Result=*/ IntegerType::get(mod->getContext(), 32),
- /*Params=*/ FuncTyArgs, /*isVarArg=*/ false
- );
- // Now creates the Function
- Function* funcSum = Function::Create(
- /*Type=*/ FuncTy,
- /*Linkage=*/ GlobalValue::ExternalLinkage, // Function can be referred from other modules
- /*Name=*/ "sum",
- mod
- );
- // now we declare the arguments
- // we give them a name, this is
- // optional as LLVM can give a
- // temporary one
- Function::arg_iterator args = funcSum->arg_begin();
- Value *int32_a = args++;
- int32_a->setName("a");
- Value *int32_b = args++;
- int32_b->setName("b");
- // now let's going to start creating
- // the code, to do it, we need
- // to declare a basic block, we will
- // assign a label to the BB
- BasicBlock *labelEntry = BasicBlock::Create(
- mod->getContext(),
- "entry", // label or name
- funcSum, // parent function
- 0
- );
- // Now that entry is ready, we can fill
- // the basic block with instructions
- // first 2 alloca instructions for vars
- // with 4-byte alignment
- AllocaInst *ptrA = new AllocaInst(
- IntegerType::get(mod->getContext(), 32),
- "a.addr",
- labelEntry
- );
- AllocaInst *ptrB = new AllocaInst(
- IntegerType::get(mod->getContext(), 32),
- "b.addr",
- labelEntry
- );
- // now that memory is allocated in IR
- // we can store the parameters in the allocated
- // variables
- StoreInst* st0 = new StoreInst(
- int32_a, // source
- ptrA, // destination
- false,
- labelEntry
- );
- st0->setAlignment(4);
- StoreInst* st1 = new StoreInst(
- int32_b, // source
- ptrB, // destination
- false,
- labelEntry
- );
- st1->setAlignment(4);
- // now we will load the values from
- // allocated memory into variables
- // that will be the parameters to the
- // add function
- LoadInst *ld0 = new LoadInst(
- ptrA, // source
- "", // name for variable, we don't care
- false, // not volatile
- labelEntry
- );
- ld0->setAlignment(4);
- LoadInst *ld1 = new LoadInst(
- ptrB, // source
- "", // name for variable, we don't care
- false, // not volatile
- labelEntry
- );
- ld1->setAlignment(4);
- // Add operation, giving operands
- BinaryOperator *addRes = BinaryOperator::Create(
- Instruction::Add,
- ld0,
- ld1,
- "add",
- labelEntry
- );
- // Return with return from previous instruction
- ReturnInst::Create(
- mod->getContext(),
- addRes,
- labelEntry
- );
- return mod;
- }
- int
- main(int argc, char **argv)
- {
- Module *Mod = makeLLVMModule();
- std::string ErrorInfo;
- std::unique_ptr<tool_output_file> Out(
- new tool_output_file(
- "./my_sum.bc",
- ErrorInfo,
- sys::fs::F_None
- )
- );
- if (!ErrorInfo.empty())
- {
- errs() << ErrorInfo << "\n";
- return -1;
- }
- WriteBitcodeToFile(Mod, Out->os());
- Out->keep();
- outs() << "Generated my_sum.bc correctly\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement