1 //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
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"
26 // Create the "module" or "program" or "translation unit" to hold the
28 Module *M = new Module("test");
30 // Create the main function: first create the type 'int ()'
31 FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(),
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);
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);
42 // Get pointers to the constant integers...
43 Value *Two = ConstantSInt::get(Type::IntTy, 2);
44 Value *Three = ConstantSInt::get(Type::IntTy, 3);
46 // Create the add instruction... does not insert...
47 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
50 // explicitly insert it into the basic block...
51 BB->getInstList().push_back(Add);
53 // Create the return instruction and add it to the basic block
54 BB->getInstList().push_back(new ReturnInst(Add));
56 // Output the bytecode file to stdout
57 WriteBytecodeToFile(M, std::cout);
59 // Delete the module and all of its contents.