blob: 1d06c2d0dd0f880022a1254614cdfc332f883304 [file] [log] [blame]
Chris Lattner8ca0eeb2003-08-21 22:29:52 +00001//===- ModuleMaker.cpp - Example project which creates modules --*- C++ -*-===//
2//
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.
6//
7//===----------------------------------------------------------------------===//
8
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"
14
15int main() {
16 // Create the "module" or "program" or "translation unit" to hold the
17 // function
18 Module *M = new Module("test");
19
20 // Create the main function: first create the type 'int ()'
21 FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(),
22 /*not vararg*/false);
23
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);
27
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);
31
32 // Get pointers to the constant integers...
33 Value *Two = ConstantSInt::get(Type::IntTy, 2);
34 Value *Three = ConstantSInt::get(Type::IntTy, 3);
35
36 // Create the add instruction... does not insert...
37 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
38 "addresult");
39
40 // explicitly insert it into the basic block...
41 BB->getInstList().push_back(Add);
42
43 // Create the return instruction and add it to the basic block
44 BB->getInstList().push_back(new ReturnInst(Add));
45
46 // Output the bytecode file to stdout
47 WriteBytecodeToFile(M, std::cout);
48
49 // Delete the module and all of its contents.
50 delete M;
51 return 0;
52}