1 //===- ModuleMaker.cpp - Example project which creates modules --*- C++ -*-===//
3 // This programs is a simple example that creates an LLVM module "from scratch",
4 // emitting it as a bytecode file to standard out. This is just to show how
5 // LLVM projects work and to demonstrate some of the LLVM APIs.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Module.h"
10 #include "llvm/DerivedTypes.h"
11 #include "llvm/Constants.h"
12 #include "llvm/Instructions.h"
13 #include "llvm/Bytecode/Writer.h"
18 // Create the "module" or "program" or "translation unit" to hold the
20 Module *M = new Module("test");
22 // Create the main function: first create the type 'int ()'
23 FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(),
26 // By passing a module as the last parameter to the Function constructor,
27 // it automatically gets appended to the Module.
28 Function *F = new Function(FT, Function::ExternalLinkage, "main", M);
30 // Add a basic block to the function... again, it automatically inserts
31 // because of the last argument.
32 BasicBlock *BB = new BasicBlock("EntryBlock", F);
34 // Get pointers to the constant integers...
35 Value *Two = ConstantSInt::get(Type::IntTy, 2);
36 Value *Three = ConstantSInt::get(Type::IntTy, 3);
38 // Create the add instruction... does not insert...
39 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
42 // explicitly insert it into the basic block...
43 BB->getInstList().push_back(Add);
45 // Create the return instruction and add it to the basic block
46 BB->getInstList().push_back(new ReturnInst(Add));
48 // Output the bytecode file to stdout
49 WriteBytecodeToFile(M, std::cout);
51 // Delete the module and all of its contents.