blob: a1b68449f3bad7d743ac5a21221845cc6896b7d2 [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
Brian Gaeked0fde302003-11-11 22:41:34 +000015using namespace llvm;
16
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000017int main() {
18 // Create the "module" or "program" or "translation unit" to hold the
19 // function
20 Module *M = new Module("test");
21
22 // Create the main function: first create the type 'int ()'
23 FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(),
24 /*not vararg*/false);
25
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);
29
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);
33
34 // Get pointers to the constant integers...
35 Value *Two = ConstantSInt::get(Type::IntTy, 2);
36 Value *Three = ConstantSInt::get(Type::IntTy, 3);
37
38 // Create the add instruction... does not insert...
39 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
40 "addresult");
41
42 // explicitly insert it into the basic block...
43 BB->getInstList().push_back(Add);
44
45 // Create the return instruction and add it to the basic block
46 BB->getInstList().push_back(new ReturnInst(Add));
47
48 // Output the bytecode file to stdout
49 WriteBytecodeToFile(M, std::cout);
50
51 // Delete the module and all of its contents.
52 delete M;
53 return 0;
54}