1987319d3SReid Spencer //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===// 25b81eea7SReid Spencer // 33779fd65SChris Lattner // The LLVM Compiler Infrastructure 43779fd65SChris Lattner // 5bcf65db6SChris Lattner // This file is distributed under the University of Illinois Open Source 6bcf65db6SChris Lattner // License. See LICENSE.TXT for details. 73779fd65SChris Lattner // 83779fd65SChris Lattner //===----------------------------------------------------------------------===// 93779fd65SChris Lattner // 105b81eea7SReid Spencer // This programs is a simple example that creates an LLVM module "from scratch", 11e16561cdSGabor Greif // emitting it as a bitcode file to standard out. This is just to show how 125b81eea7SReid Spencer // LLVM projects work and to demonstrate some of the LLVM APIs. 135b81eea7SReid Spencer // 145b81eea7SReid Spencer //===----------------------------------------------------------------------===// 155b81eea7SReid Spencer 16605e30e9SChandler Carruth #include "llvm/Bitcode/ReaderWriter.h" 17*2b8e4170SEugene Zelenko #include "llvm/IR/BasicBlock.h" 18005f27a0SChandler Carruth #include "llvm/IR/Constants.h" 19005f27a0SChandler Carruth #include "llvm/IR/DerivedTypes.h" 20*2b8e4170SEugene Zelenko #include "llvm/IR/Function.h" 21*2b8e4170SEugene Zelenko #include "llvm/IR/InstrTypes.h" 22*2b8e4170SEugene Zelenko #include "llvm/IR/Instruction.h" 23005f27a0SChandler Carruth #include "llvm/IR/Instructions.h" 24005f27a0SChandler Carruth #include "llvm/IR/LLVMContext.h" 25005f27a0SChandler Carruth #include "llvm/IR/Module.h" 26*2b8e4170SEugene Zelenko #include "llvm/IR/Type.h" 276973395cSChris Lattner #include "llvm/Support/raw_ostream.h" 28*2b8e4170SEugene Zelenko 295b81eea7SReid Spencer using namespace llvm; 305b81eea7SReid Spencer 315b81eea7SReid Spencer int main() { 326773d388SOwen Anderson LLVMContext Context; 336773d388SOwen Anderson 345b81eea7SReid Spencer // Create the "module" or "program" or "translation unit" to hold the 355b81eea7SReid Spencer // function 361cf085d5SOwen Anderson Module *M = new Module("test", Context); 375b81eea7SReid Spencer 385b81eea7SReid Spencer // Create the main function: first create the type 'int ()' 39b6b25300SOwen Anderson FunctionType *FT = 4055f1c09eSOwen Anderson FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false); 415b81eea7SReid Spencer 425b81eea7SReid Spencer // By passing a module as the last parameter to the Function constructor, 435b81eea7SReid Spencer // it automatically gets appended to the Module. 44e9ecc68dSGabor Greif Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M); 455b81eea7SReid Spencer 465b81eea7SReid Spencer // Add a basic block to the function... again, it automatically inserts 475b81eea7SReid Spencer // because of the last argument. 4855f1c09eSOwen Anderson BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F); 495b81eea7SReid Spencer 505b81eea7SReid Spencer // Get pointers to the constant integers... 5155f1c09eSOwen Anderson Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2); 5255f1c09eSOwen Anderson Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3); 535b81eea7SReid Spencer 545b81eea7SReid Spencer // Create the add instruction... does not insert... 55e1f6e4b2SGabor Greif Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three, 565b81eea7SReid Spencer "addresult"); 575b81eea7SReid Spencer 585b81eea7SReid Spencer // explicitly insert it into the basic block... 595b81eea7SReid Spencer BB->getInstList().push_back(Add); 605b81eea7SReid Spencer 615b81eea7SReid Spencer // Create the return instruction and add it to the basic block 6255f1c09eSOwen Anderson BB->getInstList().push_back(ReturnInst::Create(Context, Add)); 635b81eea7SReid Spencer 64e16561cdSGabor Greif // Output the bitcode file to stdout 656973395cSChris Lattner WriteBitcodeToFile(M, outs()); 665b81eea7SReid Spencer 675b81eea7SReid Spencer // Delete the module and all of its contents. 685b81eea7SReid Spencer delete M; 695b81eea7SReid Spencer return 0; 705b81eea7SReid Spencer } 71