1987319d3SReid Spencer //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
25b81eea7SReid Spencer //
3*3779fd65SChris Lattner //                     The LLVM Compiler Infrastructure
4*3779fd65SChris Lattner //
5*3779fd65SChris Lattner // This file was developed by the LLVM research group and is distributed under
6*3779fd65SChris Lattner // the University of Illinois Open Source License. See LICENSE.TXT for details.
7*3779fd65SChris Lattner //
8*3779fd65SChris Lattner //===----------------------------------------------------------------------===//
9*3779fd65SChris Lattner //
105b81eea7SReid Spencer // This programs is a simple example that creates an LLVM module "from scratch",
115b81eea7SReid Spencer // emitting it as a bytecode 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"
205b81eea7SReid Spencer #include "llvm/Bytecode/Writer.h"
215b81eea7SReid Spencer #include <iostream>
225b81eea7SReid Spencer 
235b81eea7SReid Spencer using namespace llvm;
245b81eea7SReid Spencer 
255b81eea7SReid Spencer int main() {
265b81eea7SReid Spencer   // Create the "module" or "program" or "translation unit" to hold the
275b81eea7SReid Spencer   // function
285b81eea7SReid Spencer   Module *M = new Module("test");
295b81eea7SReid Spencer 
305b81eea7SReid Spencer   // Create the main function: first create the type 'int ()'
315b81eea7SReid Spencer   FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(),
325b81eea7SReid Spencer                                        /*not vararg*/false);
335b81eea7SReid Spencer 
345b81eea7SReid Spencer   // By passing a module as the last parameter to the Function constructor,
355b81eea7SReid Spencer   // it automatically gets appended to the Module.
365b81eea7SReid Spencer   Function *F = new Function(FT, Function::ExternalLinkage, "main", M);
375b81eea7SReid Spencer 
385b81eea7SReid Spencer   // Add a basic block to the function... again, it automatically inserts
395b81eea7SReid Spencer   // because of the last argument.
405b81eea7SReid Spencer   BasicBlock *BB = new BasicBlock("EntryBlock", F);
415b81eea7SReid Spencer 
425b81eea7SReid Spencer   // Get pointers to the constant integers...
435b81eea7SReid Spencer   Value *Two = ConstantSInt::get(Type::IntTy, 2);
445b81eea7SReid Spencer   Value *Three = ConstantSInt::get(Type::IntTy, 3);
455b81eea7SReid Spencer 
465b81eea7SReid Spencer   // Create the add instruction... does not insert...
475b81eea7SReid Spencer   Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
485b81eea7SReid Spencer                                             "addresult");
495b81eea7SReid Spencer 
505b81eea7SReid Spencer   // explicitly insert it into the basic block...
515b81eea7SReid Spencer   BB->getInstList().push_back(Add);
525b81eea7SReid Spencer 
535b81eea7SReid Spencer   // Create the return instruction and add it to the basic block
545b81eea7SReid Spencer   BB->getInstList().push_back(new ReturnInst(Add));
555b81eea7SReid Spencer 
565b81eea7SReid Spencer   // Output the bytecode file to stdout
575b81eea7SReid Spencer   WriteBytecodeToFile(M, std::cout);
585b81eea7SReid Spencer 
595b81eea7SReid Spencer   // Delete the module and all of its contents.
605b81eea7SReid Spencer   delete M;
615b81eea7SReid Spencer   return 0;
625b81eea7SReid Spencer }
63