1*9880d681SAndroid Build Coastguard Worker //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This programs is a simple example that creates an LLVM module "from scratch",
11*9880d681SAndroid Build Coastguard Worker // emitting it as a bitcode file to standard out. This is just to show how
12*9880d681SAndroid Build Coastguard Worker // LLVM projects work and to demonstrate some of the LLVM APIs.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "llvm/Bitcode/ReaderWriter.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/InstrTypes.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instruction.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
28*9880d681SAndroid Build Coastguard Worker
29*9880d681SAndroid Build Coastguard Worker using namespace llvm;
30*9880d681SAndroid Build Coastguard Worker
main()31*9880d681SAndroid Build Coastguard Worker int main() {
32*9880d681SAndroid Build Coastguard Worker LLVMContext Context;
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker // Create the "module" or "program" or "translation unit" to hold the
35*9880d681SAndroid Build Coastguard Worker // function
36*9880d681SAndroid Build Coastguard Worker Module *M = new Module("test", Context);
37*9880d681SAndroid Build Coastguard Worker
38*9880d681SAndroid Build Coastguard Worker // Create the main function: first create the type 'int ()'
39*9880d681SAndroid Build Coastguard Worker FunctionType *FT =
40*9880d681SAndroid Build Coastguard Worker FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);
41*9880d681SAndroid Build Coastguard Worker
42*9880d681SAndroid Build Coastguard Worker // By passing a module as the last parameter to the Function constructor,
43*9880d681SAndroid Build Coastguard Worker // it automatically gets appended to the Module.
44*9880d681SAndroid Build Coastguard Worker Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
45*9880d681SAndroid Build Coastguard Worker
46*9880d681SAndroid Build Coastguard Worker // Add a basic block to the function... again, it automatically inserts
47*9880d681SAndroid Build Coastguard Worker // because of the last argument.
48*9880d681SAndroid Build Coastguard Worker BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);
49*9880d681SAndroid Build Coastguard Worker
50*9880d681SAndroid Build Coastguard Worker // Get pointers to the constant integers...
51*9880d681SAndroid Build Coastguard Worker Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
52*9880d681SAndroid Build Coastguard Worker Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker // Create the add instruction... does not insert...
55*9880d681SAndroid Build Coastguard Worker Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
56*9880d681SAndroid Build Coastguard Worker "addresult");
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker // explicitly insert it into the basic block...
59*9880d681SAndroid Build Coastguard Worker BB->getInstList().push_back(Add);
60*9880d681SAndroid Build Coastguard Worker
61*9880d681SAndroid Build Coastguard Worker // Create the return instruction and add it to the basic block
62*9880d681SAndroid Build Coastguard Worker BB->getInstList().push_back(ReturnInst::Create(Context, Add));
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker // Output the bitcode file to stdout
65*9880d681SAndroid Build Coastguard Worker WriteBitcodeToFile(M, outs());
66*9880d681SAndroid Build Coastguard Worker
67*9880d681SAndroid Build Coastguard Worker // Delete the module and all of its contents.
68*9880d681SAndroid Build Coastguard Worker delete M;
69*9880d681SAndroid Build Coastguard Worker return 0;
70*9880d681SAndroid Build Coastguard Worker }
71