1987319d3SReid Spencer //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
25b81eea7SReid Spencer //
33779fd65SChris Lattner //                     The LLVM Compiler Infrastructure
43779fd65SChris Lattner //
53779fd65SChris Lattner // This file was developed by the LLVM research group and is distributed under
63779fd65SChris Lattner // the University of Illinois Open Source 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",
11*e16561cdSGabor 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 
165b81eea7SReid Spencer #include "llvm/Module.h"
175b81eea7SReid Spencer #include "llvm/DerivedTypes.h"
185b81eea7SReid Spencer #include "llvm/Constants.h"
195b81eea7SReid Spencer #include "llvm/Instructions.h"
2041528e6eSChris Lattner #include "llvm/Bitcode/ReaderWriter.h"
2141528e6eSChris Lattner #include <iostream>
225b81eea7SReid Spencer using namespace llvm;
235b81eea7SReid Spencer 
245b81eea7SReid Spencer int main() {
255b81eea7SReid Spencer   // Create the "module" or "program" or "translation unit" to hold the
265b81eea7SReid Spencer   // function
275b81eea7SReid Spencer   Module *M = new Module("test");
285b81eea7SReid Spencer 
295b81eea7SReid Spencer   // Create the main function: first create the type 'int ()'
30bc013ba3SReid Spencer   FunctionType *FT = FunctionType::get(Type::Int32Ty, std::vector<const Type*>(),
315b81eea7SReid Spencer                                        /*not vararg*/false);
325b81eea7SReid Spencer 
335b81eea7SReid Spencer   // By passing a module as the last parameter to the Function constructor,
345b81eea7SReid Spencer   // it automatically gets appended to the Module.
355b81eea7SReid Spencer   Function *F = new Function(FT, Function::ExternalLinkage, "main", M);
365b81eea7SReid Spencer 
375b81eea7SReid Spencer   // Add a basic block to the function... again, it automatically inserts
385b81eea7SReid Spencer   // because of the last argument.
395b81eea7SReid Spencer   BasicBlock *BB = new BasicBlock("EntryBlock", F);
405b81eea7SReid Spencer 
415b81eea7SReid Spencer   // Get pointers to the constant integers...
42bc013ba3SReid Spencer   Value *Two = ConstantInt::get(Type::Int32Ty, 2);
43bc013ba3SReid Spencer   Value *Three = ConstantInt::get(Type::Int32Ty, 3);
445b81eea7SReid Spencer 
455b81eea7SReid Spencer   // Create the add instruction... does not insert...
465b81eea7SReid Spencer   Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
475b81eea7SReid Spencer                                             "addresult");
485b81eea7SReid Spencer 
495b81eea7SReid Spencer   // explicitly insert it into the basic block...
505b81eea7SReid Spencer   BB->getInstList().push_back(Add);
515b81eea7SReid Spencer 
525b81eea7SReid Spencer   // Create the return instruction and add it to the basic block
535b81eea7SReid Spencer   BB->getInstList().push_back(new ReturnInst(Add));
545b81eea7SReid Spencer 
55*e16561cdSGabor Greif   // Output the bitcode file to stdout
5641528e6eSChris Lattner   WriteBitcodeToFile(M, std::cout);
575b81eea7SReid Spencer 
585b81eea7SReid Spencer   // Delete the module and all of its contents.
595b81eea7SReid Spencer   delete M;
605b81eea7SReid Spencer   return 0;
615b81eea7SReid Spencer }
62