blob: 59a86d031d2f3823a73d439e68d0cc79923abdc4 [file] [log] [blame]
Reid Spencer66e7cd02004-09-11 20:30:11 +00001//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
Misha Brukman237cef42005-04-20 16:42:34 +00002//
Chris Lattner7db7fa02005-03-15 15:46:23 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerfc001bb2007-12-29 20:37:57 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman237cef42005-04-20 16:42:34 +00007//
Chris Lattner7db7fa02005-03-15 15:46:23 +00008//===----------------------------------------------------------------------===//
Chris Lattner8ca0eeb2003-08-21 22:29:52 +00009//
10// This programs is a simple example that creates an LLVM module "from scratch",
Gabor Greifa99be512007-07-05 17:07:56 +000011// emitting it as a bitcode file to standard out. This is just to show how
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000012// LLVM projects work and to demonstrate some of the LLVM APIs.
13//
14//===----------------------------------------------------------------------===//
15
Owen Anderson8b477ed2009-07-01 16:58:40 +000016#include "llvm/LLVMContext.h"
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000017#include "llvm/Module.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Constants.h"
20#include "llvm/Instructions.h"
Chris Lattner4bcca0f2007-05-06 09:29:13 +000021#include "llvm/Bitcode/ReaderWriter.h"
22#include <iostream>
Brian Gaeked0fde302003-11-11 22:41:34 +000023using namespace llvm;
24
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000025int main() {
Owen Anderson8b477ed2009-07-01 16:58:40 +000026 LLVMContext Context;
27
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000028 // Create the "module" or "program" or "translation unit" to hold the
29 // function
Owen Anderson31895e72009-07-01 21:22:36 +000030 Module *M = new Module("test", Context);
Misha Brukman237cef42005-04-20 16:42:34 +000031
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000032 // Create the main function: first create the type 'int ()'
Chris Lattner0fd38062009-07-01 04:13:31 +000033 FunctionType *FT = FunctionType::get(Type::Int32Ty, /*not vararg*/false);
Misha Brukman237cef42005-04-20 16:42:34 +000034
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000035 // By passing a module as the last parameter to the Function constructor,
36 // it automatically gets appended to the Module.
Gabor Greif051a9502008-04-06 20:25:17 +000037 Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
Misha Brukman237cef42005-04-20 16:42:34 +000038
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000039 // Add a basic block to the function... again, it automatically inserts
40 // because of the last argument.
Gabor Greif051a9502008-04-06 20:25:17 +000041 BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
Misha Brukman237cef42005-04-20 16:42:34 +000042
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000043 // Get pointers to the constant integers...
Reid Spencerdb8d2be2006-12-31 05:50:28 +000044 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
45 Value *Three = ConstantInt::get(Type::Int32Ty, 3);
Misha Brukman237cef42005-04-20 16:42:34 +000046
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000047 // Create the add instruction... does not insert...
Gabor Greif7cbd8a32008-05-16 19:29:10 +000048 Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000049 "addresult");
Misha Brukman237cef42005-04-20 16:42:34 +000050
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000051 // explicitly insert it into the basic block...
52 BB->getInstList().push_back(Add);
Misha Brukman237cef42005-04-20 16:42:34 +000053
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000054 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +000055 BB->getInstList().push_back(ReturnInst::Create(Add));
Misha Brukman237cef42005-04-20 16:42:34 +000056
Gabor Greifa99be512007-07-05 17:07:56 +000057 // Output the bitcode file to stdout
Chris Lattner4bcca0f2007-05-06 09:29:13 +000058 WriteBitcodeToFile(M, std::cout);
Misha Brukman237cef42005-04-20 16:42:34 +000059
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000060 // Delete the module and all of its contents.
61 delete M;
62 return 0;
63}