blob: 6bc52c12a034497903ca80e622ed4d7d149282ca [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner45ca7c12007-12-29 20:37:57 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This programs is a simple example that creates an LLVM module "from scratch",
11// emitting it as a bitcode file to standard out. This is just to show how
12// LLVM projects work and to demonstrate some of the LLVM APIs.
13//
14//===----------------------------------------------------------------------===//
15
Owen Anderson25209b42009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000017#include "llvm/Module.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
21#include "llvm/Bitcode/ReaderWriter.h"
Chris Lattner371c1ef2009-08-23 07:49:08 +000022#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023using namespace llvm;
24
25int main() {
Owen Anderson25209b42009-07-01 16:58:40 +000026 LLVMContext Context;
27
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028 // Create the "module" or "program" or "translation unit" to hold the
29 // function
Owen Andersona148fdd2009-07-01 21:22:36 +000030 Module *M = new Module("test", Context);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031
32 // Create the main function: first create the type 'int ()'
Owen Anderson9f5b2aa2009-07-14 23:09:55 +000033 FunctionType *FT =
Owen Anderson35b47072009-08-13 21:58:54 +000034 FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035
36 // By passing a module as the last parameter to the Function constructor,
37 // it automatically gets appended to the Module.
Gabor Greifd6da1d02008-04-06 20:25:17 +000038 Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039
40 // Add a basic block to the function... again, it automatically inserts
41 // because of the last argument.
Owen Anderson35b47072009-08-13 21:58:54 +000042 BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000043
44 // Get pointers to the constant integers...
Owen Anderson35b47072009-08-13 21:58:54 +000045 Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
46 Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000047
48 // Create the add instruction... does not insert...
Gabor Greifa645dd32008-05-16 19:29:10 +000049 Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 "addresult");
51
52 // explicitly insert it into the basic block...
53 BB->getInstList().push_back(Add);
54
55 // Create the return instruction and add it to the basic block
Owen Anderson35b47072009-08-13 21:58:54 +000056 BB->getInstList().push_back(ReturnInst::Create(Context, Add));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000057
58 // Output the bitcode file to stdout
Chris Lattner371c1ef2009-08-23 07:49:08 +000059 WriteBitcodeToFile(M, outs());
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61 // Delete the module and all of its contents.
62 delete M;
63 return 0;
64}