blob: 4c09db4df751f01b1c548304bc887fb4ce85576f [file] [log] [blame]
Reid Spencer66e7cd02004-09-11 20:30:11 +00001//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
Misha Brukman237cef42005-04-20 16:42:34 +00002//
Chris Lattner7db7fa02005-03-15 15:46:23 +00003// 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 Brukman237cef42005-04-20 16:42:34 +00007//
Chris Lattner7db7fa02005-03-15 15:46:23 +00008//===----------------------------------------------------------------------===//
Chris Lattner8ca0eeb2003-08-21 22:29:52 +00009//
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 Spencer321f8312004-07-04 12:22:14 +000021#include <iostream>
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000022
Brian Gaeked0fde302003-11-11 22:41:34 +000023using namespace llvm;
24
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000025int main() {
26 // Create the "module" or "program" or "translation unit" to hold the
27 // function
28 Module *M = new Module("test");
Misha Brukman237cef42005-04-20 16:42:34 +000029
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000030 // 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 Brukman237cef42005-04-20 16:42:34 +000033
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000034 // 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 Brukman237cef42005-04-20 16:42:34 +000037
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000038 // 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 Brukman237cef42005-04-20 16:42:34 +000041
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000042 // Get pointers to the constant integers...
43 Value *Two = ConstantSInt::get(Type::IntTy, 2);
44 Value *Three = ConstantSInt::get(Type::IntTy, 3);
Misha Brukman237cef42005-04-20 16:42:34 +000045
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000046 // Create the add instruction... does not insert...
47 Instruction *Add = BinaryOperator::create(Instruction::Add, Two, Three,
48 "addresult");
Misha Brukman237cef42005-04-20 16:42:34 +000049
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000050 // explicitly insert it into the basic block...
51 BB->getInstList().push_back(Add);
Misha Brukman237cef42005-04-20 16:42:34 +000052
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000053 // Create the return instruction and add it to the basic block
54 BB->getInstList().push_back(new ReturnInst(Add));
Misha Brukman237cef42005-04-20 16:42:34 +000055
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000056 // Output the bytecode file to stdout
Chris Lattnerb4611312006-05-14 19:08:39 +000057 try {
58 WriteBytecodeToFile(M, std::cout);
59 } catch (const std::string &Error) {
60 std::cerr << "Error writing file: " << Error << "\n";
61 return 1;
62 }
Misha Brukman237cef42005-04-20 16:42:34 +000063
Chris Lattner8ca0eeb2003-08-21 22:29:52 +000064 // Delete the module and all of its contents.
65 delete M;
66 return 0;
67}