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 | // |
| 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 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", |
| 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" |
Reid Spencer | 321f831 | 2004-07-04 12:22:14 +0000 | [diff] [blame] | 21 | #include <iostream> |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 22 | |
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() { |
| 26 | // Create the "module" or "program" or "translation unit" to hold the |
| 27 | // function |
| 28 | Module *M = new Module("test"); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 30 | // Create the main function: first create the type 'int ()' |
| 31 | FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(), |
| 32 | /*not vararg*/false); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 34 | // By passing a module as the last parameter to the Function constructor, |
| 35 | // it automatically gets appended to the Module. |
| 36 | Function *F = new Function(FT, Function::ExternalLinkage, "main", M); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 38 | // Add a basic block to the function... again, it automatically inserts |
| 39 | // because of the last argument. |
| 40 | BasicBlock *BB = new BasicBlock("EntryBlock", F); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 42 | // Get pointers to the constant integers... |
| 43 | Value *Two = ConstantSInt::get(Type::IntTy, 2); |
| 44 | Value *Three = ConstantSInt::get(Type::IntTy, 3); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 46 | // Create the add instruction... does not insert... |
| 47 | Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three, |
| 48 | "addresult"); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 50 | // explicitly insert it into the basic block... |
| 51 | BB->getInstList().push_back(Add); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 53 | // Create the return instruction and add it to the basic block |
| 54 | BB->getInstList().push_back(new ReturnInst(Add)); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 55 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 56 | // Output the bytecode file to stdout |
| 57 | WriteBytecodeToFile(M, std::cout); |
Misha Brukman | 237cef4 | 2005-04-20 16:42:34 +0000 | [diff] [blame] | 58 | |
Chris Lattner | 8ca0eeb | 2003-08-21 22:29:52 +0000 | [diff] [blame] | 59 | // Delete the module and all of its contents. |
| 60 | delete M; |
| 61 | return 0; |
| 62 | } |