2004-09-11 20:30:11 +00:00
|
|
|
//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2005-03-15 15:46:23 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:37:57 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2005-03-15 15:46:23 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2003-08-21 22:29:52 +00:00
|
|
|
//
|
|
|
|
// This programs is a simple example that creates an LLVM module "from scratch",
|
2007-07-05 17:07:56 +00:00
|
|
|
// emitting it as a bitcode file to standard out. This is just to show how
|
2003-08-21 22:29:52 +00:00
|
|
|
// LLVM projects work and to demonstrate some of the LLVM APIs.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 10:16:57 +00:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
2013-01-02 11:56:33 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2009-08-23 07:49:08 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2003-11-11 22:41:34 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
int main() {
|
2009-07-01 16:58:40 +00:00
|
|
|
LLVMContext Context;
|
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// Create the "module" or "program" or "translation unit" to hold the
|
|
|
|
// function
|
2009-07-01 21:22:36 +00:00
|
|
|
Module *M = new Module("test", Context);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// Create the main function: first create the type 'int ()'
|
2009-07-14 23:09:55 +00:00
|
|
|
FunctionType *FT =
|
2009-08-13 21:58:54 +00:00
|
|
|
FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// By passing a module as the last parameter to the Function constructor,
|
|
|
|
// it automatically gets appended to the Module.
|
2008-04-06 20:25:17 +00:00
|
|
|
Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// Add a basic block to the function... again, it automatically inserts
|
|
|
|
// because of the last argument.
|
2009-08-13 21:58:54 +00:00
|
|
|
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// Get pointers to the constant integers...
|
2009-08-13 21:58:54 +00:00
|
|
|
Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
|
|
|
|
Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// Create the add instruction... does not insert...
|
2008-05-16 19:29:10 +00:00
|
|
|
Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
|
2003-08-21 22:29:52 +00:00
|
|
|
"addresult");
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// explicitly insert it into the basic block...
|
|
|
|
BB->getInstList().push_back(Add);
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// Create the return instruction and add it to the basic block
|
2009-08-13 21:58:54 +00:00
|
|
|
BB->getInstList().push_back(ReturnInst::Create(Context, Add));
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2007-07-05 17:07:56 +00:00
|
|
|
// Output the bitcode file to stdout
|
2009-08-23 07:49:08 +00:00
|
|
|
WriteBitcodeToFile(M, outs());
|
2005-04-20 16:42:34 +00:00
|
|
|
|
2003-08-21 22:29:52 +00:00
|
|
|
// Delete the module and all of its contents.
|
|
|
|
delete M;
|
|
|
|
return 0;
|
|
|
|
}
|