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