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