blob: 4983597952b2b55df84e2062c32410770a7b8c3e [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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",
11// emitting it as a bytecode 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
16#include "llvm/Module.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
20#include "llvm/Bytecode/Writer.h"
Bill Wendling68fe61d2006-11-29 00:19:40 +000021#include "llvm/Support/Streams.h"
Brian Gaeked0fde302003-11-11 22:41:34 +000022using namespace llvm;
23
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000024int main() {
25 // Create the "module" or "program" or "translation unit" to hold the
26 // function
27 Module *M = new Module("test");
Misha Brukman237cef42005-04-20 16:42:34 +000028
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000029 // Create the main function: first create the type 'int ()'
Reid Spencerdb8d2be2006-12-31 05:50:28 +000030 FunctionType *FT = FunctionType::get(Type::Int32Ty, std::vector<const Type*>(),
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000031 /*not vararg*/false);
Misha Brukman237cef42005-04-20 16:42:34 +000032
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000033 // By passing a module as the last parameter to the Function constructor,
34 // it automatically gets appended to the Module.
35 Function *F = new Function(FT, Function::ExternalLinkage, "main", M);
Misha Brukman237cef42005-04-20 16:42:34 +000036
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000037 // Add a basic block to the function... again, it automatically inserts
38 // because of the last argument.
39 BasicBlock *BB = new BasicBlock("EntryBlock", F);
Misha Brukman237cef42005-04-20 16:42:34 +000040
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000041 // Get pointers to the constant integers...
Reid Spencerdb8d2be2006-12-31 05:50:28 +000042 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
43 Value *Three = ConstantInt::get(Type::Int32Ty, 3);
Misha Brukman237cef42005-04-20 16:42:34 +000044
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000045 // Create the add instruction... does not insert...
46 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
47 "addresult");
Misha Brukman237cef42005-04-20 16:42:34 +000048
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000049 // explicitly insert it into the basic block...
50 BB->getInstList().push_back(Add);
Misha Brukman237cef42005-04-20 16:42:34 +000051
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000052 // Create the return instruction and add it to the basic block
53 BB->getInstList().push_back(new ReturnInst(Add));
Misha Brukman237cef42005-04-20 16:42:34 +000054
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000055 // Output the bytecode file to stdout
Bill Wendlinge8156192006-12-07 01:30:32 +000056 WriteBytecodeToFile(M, cout);
Misha Brukman237cef42005-04-20 16:42:34 +000057
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000058 // Delete the module and all of its contents.
59 delete M;
60 return 0;
61}