Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 45ca7c1 | 2007-12-29 20:37:57 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 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 Anderson | 25209b4 | 2009-07-01 16:58:40 +0000 | [diff] [blame^] | 16 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #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" |
| 22 | #include <iostream> |
| 23 | using namespace llvm; |
| 24 | |
| 25 | int main() { |
Owen Anderson | 25209b4 | 2009-07-01 16:58:40 +0000 | [diff] [blame^] | 26 | LLVMContext Context; |
| 27 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | // Create the "module" or "program" or "translation unit" to hold the |
| 29 | // function |
Owen Anderson | 25209b4 | 2009-07-01 16:58:40 +0000 | [diff] [blame^] | 30 | Module *M = new Module("test", &Context); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 31 | |
| 32 | // Create the main function: first create the type 'int ()' |
Chris Lattner | 3fd51c0 | 2009-07-01 04:13:31 +0000 | [diff] [blame] | 33 | FunctionType *FT = FunctionType::get(Type::Int32Ty, /*not vararg*/false); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 34 | |
| 35 | // By passing a module as the last parameter to the Function constructor, |
| 36 | // it automatically gets appended to the Module. |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 37 | Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | |
| 39 | // Add a basic block to the function... again, it automatically inserts |
| 40 | // because of the last argument. |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 41 | BasicBlock *BB = BasicBlock::Create("EntryBlock", F); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 42 | |
| 43 | // Get pointers to the constant integers... |
| 44 | Value *Two = ConstantInt::get(Type::Int32Ty, 2); |
| 45 | Value *Three = ConstantInt::get(Type::Int32Ty, 3); |
| 46 | |
| 47 | // Create the add instruction... does not insert... |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 48 | Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | "addresult"); |
| 50 | |
| 51 | // explicitly insert it into the basic block... |
| 52 | BB->getInstList().push_back(Add); |
| 53 | |
| 54 | // Create the return instruction and add it to the basic block |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 55 | BB->getInstList().push_back(ReturnInst::Create(Add)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | |
| 57 | // Output the bitcode file to stdout |
| 58 | WriteBitcodeToFile(M, std::cout); |
| 59 | |
| 60 | // Delete the module and all of its contents. |
| 61 | delete M; |
| 62 | return 0; |
| 63 | } |