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 
16*6773d388SOwen Anderson #include "llvm/LLVMContext.h"
175b81eea7SReid Spencer #include "llvm/Module.h"
185b81eea7SReid Spencer #include "llvm/DerivedTypes.h"
195b81eea7SReid Spencer #include "llvm/Constants.h"
205b81eea7SReid Spencer #include "llvm/Instructions.h"
2141528e6eSChris Lattner #include "llvm/Bitcode/ReaderWriter.h"
2241528e6eSChris Lattner #include <iostream>
235b81eea7SReid Spencer using namespace llvm;
245b81eea7SReid Spencer 
255b81eea7SReid Spencer int main() {
26*6773d388SOwen Anderson   LLVMContext Context;
27*6773d388SOwen Anderson 
285b81eea7SReid Spencer   // Create the "module" or "program" or "translation unit" to hold the
295b81eea7SReid Spencer   // function
30*6773d388SOwen Anderson   Module *M = new Module("test", &Context);
315b81eea7SReid Spencer 
325b81eea7SReid Spencer   // Create the main function: first create the type 'int ()'
3396122debSChris Lattner   FunctionType *FT = FunctionType::get(Type::Int32Ty, /*not vararg*/false);
345b81eea7SReid Spencer 
355b81eea7SReid Spencer   // By passing a module as the last parameter to the Function constructor,
365b81eea7SReid Spencer   // it automatically gets appended to the Module.
37e9ecc68dSGabor Greif   Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
385b81eea7SReid Spencer 
395b81eea7SReid Spencer   // Add a basic block to the function... again, it automatically inserts
405b81eea7SReid Spencer   // because of the last argument.
41e9ecc68dSGabor Greif   BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
425b81eea7SReid Spencer 
435b81eea7SReid Spencer   // Get pointers to the constant integers...
44bc013ba3SReid Spencer   Value *Two = ConstantInt::get(Type::Int32Ty, 2);
45bc013ba3SReid Spencer   Value *Three = ConstantInt::get(Type::Int32Ty, 3);
465b81eea7SReid Spencer 
475b81eea7SReid Spencer   // Create the add instruction... does not insert...
48e1f6e4b2SGabor Greif   Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
495b81eea7SReid Spencer                                             "addresult");
505b81eea7SReid Spencer 
515b81eea7SReid Spencer   // explicitly insert it into the basic block...
525b81eea7SReid Spencer   BB->getInstList().push_back(Add);
535b81eea7SReid Spencer 
545b81eea7SReid Spencer   // Create the return instruction and add it to the basic block
55e9ecc68dSGabor Greif   BB->getInstList().push_back(ReturnInst::Create(Add));
565b81eea7SReid Spencer 
57e16561cdSGabor Greif   // Output the bitcode file to stdout
5841528e6eSChris Lattner   WriteBitcodeToFile(M, std::cout);
595b81eea7SReid Spencer 
605b81eea7SReid Spencer   // Delete the module and all of its contents.
615b81eea7SReid Spencer   delete M;
625b81eea7SReid Spencer   return 0;
635b81eea7SReid Spencer }
64