blob: e2584e7ff34dd57198960502f2108aca3e19b7e9 [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
16#include "llvm/Module.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Constants.h"
19#include "llvm/Instructions.h"
Chris Lattner4bcca0f2007-05-06 09:29:13 +000020#include "llvm/Bitcode/ReaderWriter.h"
21#include <iostream>
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 ()'
Chris Lattner0fd38062009-07-01 04:13:31 +000030 FunctionType *FT = FunctionType::get(Type::Int32Ty, /*not vararg*/false);
Misha Brukman237cef42005-04-20 16:42:34 +000031
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000032 // By passing a module as the last parameter to the Function constructor,
33 // it automatically gets appended to the Module.
Gabor Greif051a9502008-04-06 20:25:17 +000034 Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
Misha Brukman237cef42005-04-20 16:42:34 +000035
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000036 // Add a basic block to the function... again, it automatically inserts
37 // because of the last argument.
Gabor Greif051a9502008-04-06 20:25:17 +000038 BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
Misha Brukman237cef42005-04-20 16:42:34 +000039
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000040 // Get pointers to the constant integers...
Reid Spencerdb8d2be2006-12-31 05:50:28 +000041 Value *Two = ConstantInt::get(Type::Int32Ty, 2);
42 Value *Three = ConstantInt::get(Type::Int32Ty, 3);
Misha Brukman237cef42005-04-20 16:42:34 +000043
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000044 // Create the add instruction... does not insert...
Gabor Greif7cbd8a32008-05-16 19:29:10 +000045 Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000046 "addresult");
Misha Brukman237cef42005-04-20 16:42:34 +000047
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000048 // explicitly insert it into the basic block...
49 BB->getInstList().push_back(Add);
Misha Brukman237cef42005-04-20 16:42:34 +000050
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000051 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +000052 BB->getInstList().push_back(ReturnInst::Create(Add));
Misha Brukman237cef42005-04-20 16:42:34 +000053
Gabor Greifa99be512007-07-05 17:07:56 +000054 // Output the bitcode file to stdout
Chris Lattner4bcca0f2007-05-06 09:29:13 +000055 WriteBitcodeToFile(M, std::cout);
Misha Brukman237cef42005-04-20 16:42:34 +000056
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000057 // Delete the module and all of its contents.
58 delete M;
59 return 0;
60}