Reid Spencer | 66e7cd0 | 2004-09-11 20:30:11 +0000 | [diff] [blame] | 1 | //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===// |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 7db7fa0 | 2005-03-15 15:46:23 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | fc001bb | 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. |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 7db7fa0 | 2005-03-15 15:46:23 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 9 | // |
| 10 | // This programs is a simple example that creates an LLVM module "from scratch", |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 11 | // emitting it as a bitcode file to standard out. This is just to show how |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 12 | // LLVM projects work and to demonstrate some of the LLVM APIs. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 16 | #include "llvm/LLVMContext.h" |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/DerivedTypes.h" |
| 19 | #include "llvm/Constants.h" |
| 20 | #include "llvm/Instructions.h" |
Chris Lattner | 4bcca0f | 2007-05-06 09:29:13 +0000 | [diff] [blame] | 21 | #include "llvm/Bitcode/ReaderWriter.h" |
| 22 | #include <iostream> |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | using namespace llvm; |
| 24 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 25 | int main() { |
Owen Anderson | 8b477ed | 2009-07-01 16:58:40 +0000 | [diff] [blame] | 26 | LLVMContext Context; |
| 27 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 28 | // Create the "module" or "program" or "translation unit" to hold the |
| 29 | // function |
Owen Anderson | 31895e7 | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 30 | Module *M = new Module("test", Context); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 32 | // Create the main function: first create the type 'int ()' |
Owen Anderson | 9adc0ab | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 33 | FunctionType *FT = |
| 34 | Context.getFunctionType(Type::Int32Ty, /*not vararg*/false); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 36 | // By passing a module as the last parameter to the Function constructor, |
| 37 | // it automatically gets appended to the Module. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 38 | Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 39 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 40 | // Add a basic block to the function... again, it automatically inserts |
| 41 | // because of the last argument. |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 42 | BasicBlock *BB = BasicBlock::Create("EntryBlock", F); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 43 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 44 | // Get pointers to the constant integers... |
Owen Anderson | eed707b | 2009-07-24 23:12:02 +0000 | [diff] [blame^] | 45 | Value *Two = ConstantInt::get(Type::Int32Ty, 2); |
| 46 | Value *Three = ConstantInt::get(Type::Int32Ty, 3); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 48 | // Create the add instruction... does not insert... |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 49 | Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three, |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 50 | "addresult"); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 52 | // explicitly insert it into the basic block... |
| 53 | BB->getInstList().push_back(Add); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 55 | // Create the return instruction and add it to the basic block |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 56 | BB->getInstList().push_back(ReturnInst::Create(Add)); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 57 | |
Gabor Greif | a99be51 | 2007-07-05 17:07:56 +0000 | [diff] [blame] | 58 | // Output the bitcode file to stdout |
Chris Lattner | 4bcca0f | 2007-05-06 09:29:13 +0000 | [diff] [blame] | 59 | WriteBitcodeToFile(M, std::cout); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 61 | // Delete the module and all of its contents. |
| 62 | delete M; |
| 63 | return 0; |
| 64 | } |