ModuleMaker.cpp revision 237cef4b0b94b17ca065efad484f386f42579b61
166e7cd0eea6f116f3ed79acb8948c6d8db50833cReid Spencer//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
2237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman//
37db7fa0828bc4ac375032a847b3fd530cef131e2Chris Lattner//                     The LLVM Compiler Infrastructure
47db7fa0828bc4ac375032a847b3fd530cef131e2Chris Lattner//
57db7fa0828bc4ac375032a847b3fd530cef131e2Chris Lattner// This file was developed by the LLVM research group and is distributed under
67db7fa0828bc4ac375032a847b3fd530cef131e2Chris Lattner// the University of Illinois Open Source License. See LICENSE.TXT for details.
7237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman//
87db7fa0828bc4ac375032a847b3fd530cef131e2Chris Lattner//===----------------------------------------------------------------------===//
98ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner//
108ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner// This programs is a simple example that creates an LLVM module "from scratch",
118ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner// emitting it as a bytecode file to standard out.  This is just to show how
128ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner// LLVM projects work and to demonstrate some of the LLVM APIs.
138ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner//
148ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner//===----------------------------------------------------------------------===//
158ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner
168ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner#include "llvm/Module.h"
178ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner#include "llvm/DerivedTypes.h"
188ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner#include "llvm/Constants.h"
198ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner#include "llvm/Instructions.h"
208ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner#include "llvm/Bytecode/Writer.h"
21321f8319dafc1374cd9512d6555b7a1a07887d94Reid Spencer#include <iostream>
228ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner
23d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekeusing namespace llvm;
24d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
258ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattnerint main() {
268ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Create the "module" or "program" or "translation unit" to hold the
278ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // function
288ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  Module *M = new Module("test");
29237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
308ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Create the main function: first create the type 'int ()'
318ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  FunctionType *FT = FunctionType::get(Type::IntTy, std::vector<const Type*>(),
328ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner                                       /*not vararg*/false);
33237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
348ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // By passing a module as the last parameter to the Function constructor,
358ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // it automatically gets appended to the Module.
368ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  Function *F = new Function(FT, Function::ExternalLinkage, "main", M);
37237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
388ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Add a basic block to the function... again, it automatically inserts
398ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // because of the last argument.
408ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  BasicBlock *BB = new BasicBlock("EntryBlock", F);
41237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
428ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Get pointers to the constant integers...
438ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  Value *Two = ConstantSInt::get(Type::IntTy, 2);
448ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  Value *Three = ConstantSInt::get(Type::IntTy, 3);
45237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
468ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Create the add instruction... does not insert...
478ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
488ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner                                            "addresult");
49237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
508ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // explicitly insert it into the basic block...
518ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  BB->getInstList().push_back(Add);
52237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
538ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Create the return instruction and add it to the basic block
548ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  BB->getInstList().push_back(new ReturnInst(Add));
55237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
568ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Output the bytecode file to stdout
578ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  WriteBytecodeToFile(M, std::cout);
58237cef4b0b94b17ca065efad484f386f42579b61Misha Brukman
598ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  // Delete the module and all of its contents.
608ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  delete M;
618ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner  return 0;
628ca0eebe4ef4fe810dba5ce91306031272f139cfChris Lattner}
63