166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//                     The LLVM Compiler Infrastructure
466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This file is distributed under the University of Illinois Open Source
666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// License. See LICENSE.TXT for details.
766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===//
966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
1066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// This programs is a simple example that creates an LLVM module "from scratch",
1166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// emitting it as a bitcode file to standard out.  This is just to show how
1266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman// LLVM projects work and to demonstrate some of the LLVM APIs.
1366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//
1466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman//===----------------------------------------------------------------------===//
1566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
1666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/LLVMContext.h"
1766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Module.h"
1866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/DerivedTypes.h"
1966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Constants.h"
2066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Instructions.h"
2166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Bitcode/ReaderWriter.h"
2266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman#include "llvm/Support/raw_ostream.h"
2366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanusing namespace llvm;
2466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Baumanint main() {
2666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  LLVMContext Context;
2766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
2866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Create the "module" or "program" or "translation unit" to hold the
2966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // function
3066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Module *M = new Module("test", Context);
3166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Create the main function: first create the type 'int ()'
3366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  FunctionType *FT =
3466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman    FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);
3566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
3666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // By passing a module as the last parameter to the Function constructor,
3766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // it automatically gets appended to the Module.
3866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
3966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Add a basic block to the function... again, it automatically inserts
4166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // because of the last argument.
4266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);
4366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Get pointers to the constant integers...
4566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
4666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);
4766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
4866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Create the add instruction... does not insert...
4966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
5066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman                                            "addresult");
5166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // explicitly insert it into the basic block...
5366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  BB->getInstList().push_back(Add);
5466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5566b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Create the return instruction and add it to the basic block
5666b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  BB->getInstList().push_back(ReturnInst::Create(Context, Add));
5766b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
5866b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Output the bitcode file to stdout
5966b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  WriteBitcodeToFile(M, outs());
6066b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman
6166b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  // Delete the module and all of its contents.
6266b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  delete M;
6366b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman  return 0;
6466b8ab22586debccb1f787d4d52b7f042d4ddeb8John Bauman}
65