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"
16 // Create the "module" or "program" or "translation unit" to hold the
18 Module *M = new Module("test");
20 // Create the main function: first create the type 'int ()'
21 FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(),
24 // By passing a module as the last parameter to the Function constructor,
25 // it automatically gets appended to the Module.
26 Function *F = new Function(FT, Function::ExternalLinkage, "main", M);
28 // Add a basic block to the function... again, it automatically inserts
29 // because of the last argument.
30 BasicBlock *BB = new BasicBlock("EntryBlock", F);
32 // Get pointers to the constant integers...
33 Value *Two = ConstantSInt::get(Type::IntTy, 2);
34 Value *Three = ConstantSInt::get(Type::IntTy, 3);
36 // Create the add instruction... does not insert...
37 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
40 // explicitly insert it into the basic block...
41 BB->getInstList().push_back(Add);
43 // Create the return instruction and add it to the basic block
44 BB->getInstList().push_back(new ReturnInst(Add));
46 // Output the bytecode file to stdout
47 WriteBytecodeToFile(M, std::cout);
49 // Delete the module and all of its contents.