blob: 537ee341dcc3457767b8d641f76a60d9bc465618 [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 ()'
Owen Anderson9adc0ab2009-07-14 23:09:55 +000033 FunctionType *FT =
34 Context.getFunctionType(Type::Int32Ty, /*not vararg*/false);
Misha Brukman237cef42005-04-20 16:42:34 +000035
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000036 // By passing a module as the last parameter to the Function constructor,
37 // it automatically gets appended to the Module.
Gabor Greif051a9502008-04-06 20:25:17 +000038 Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
Misha Brukman237cef42005-04-20 16:42:34 +000039
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000040 // Add a basic block to the function... again, it automatically inserts
41 // because of the last argument.
Gabor Greif051a9502008-04-06 20:25:17 +000042 BasicBlock *BB = BasicBlock::Create("EntryBlock", F);
Misha Brukman237cef42005-04-20 16:42:34 +000043
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000044 // Get pointers to the constant integers...
Owen Anderson9adc0ab2009-07-14 23:09:55 +000045 Value *Two = Context.getConstantInt(Type::Int32Ty, 2);
46 Value *Three = Context.getConstantInt(Type::Int32Ty, 3);
Misha Brukman237cef42005-04-20 16:42:34 +000047
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000048 // Create the add instruction... does not insert...
Gabor Greif7cbd8a32008-05-16 19:29:10 +000049 Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000050 "addresult");
Misha Brukman237cef42005-04-20 16:42:34 +000051
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000052 // explicitly insert it into the basic block...
53 BB->getInstList().push_back(Add);
Misha Brukman237cef42005-04-20 16:42:34 +000054
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000055 // Create the return instruction and add it to the basic block
Gabor Greif051a9502008-04-06 20:25:17 +000056 BB->getInstList().push_back(ReturnInst::Create(Add));
Misha Brukman237cef42005-04-20 16:42:34 +000057
Gabor Greifa99be512007-07-05 17:07:56 +000058 // Output the bitcode file to stdout
Chris Lattner4bcca0f2007-05-06 09:29:13 +000059 WriteBitcodeToFile(M, std::cout);
Misha Brukman237cef42005-04-20 16:42:34 +000060
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000061 // Delete the module and all of its contents.
62 delete M;
63 return 0;
64}