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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-02 00:58:40 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2003-08-22 06:29:52 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2007-05-06 17:29:13 +08:00
|
|
|
#include "llvm/Bitcode/ReaderWriter.h"
|
|
|
|
#include <iostream>
|
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
|
2007-05-06 17:29:13 +08:00
|
|
|
WriteBitcodeToFile(M, std::cout);
|
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;
|
|
|
|
}
|