2004-09-11 20:30:11 +00:00
|
|
|
//===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
|
2005-04-20 16:42:34 +00:00
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
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.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
2016-11-11 06:02:04 +00:00
|
|
|
#include "llvm/Bitcode/BitcodeWriter.h"
|
2016-05-25 01:18:36 +00:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2013-01-02 11:56:33 +00:00
|
|
|
#include "llvm/IR/Constants.h"
|
|
|
|
|
#include "llvm/IR/DerivedTypes.h"
|
2016-05-25 01:18:36 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
|
#include "llvm/IR/InstrTypes.h"
|
|
|
|
|
#include "llvm/IR/Instruction.h"
|
2013-01-02 11:56:33 +00:00
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
|
#include "llvm/IR/Module.h"
|
2016-05-25 01:18:36 +00:00
|
|
|
#include "llvm/IR/Type.h"
|
2009-08-23 07:49:08 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2016-05-25 01:18:36 +00:00
|
|
|
|
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
|
2018-02-14 19:23:27 +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;
|
|
|
|
|
}
|