109467b48Spatrick //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This programs is a simple example that creates an LLVM module "from scratch",
1009467b48Spatrick // emitting it as a bitcode file to standard out. This is just to show how
1109467b48Spatrick // LLVM projects work and to demonstrate some of the LLVM APIs.
1209467b48Spatrick //
1309467b48Spatrick //===----------------------------------------------------------------------===//
1409467b48Spatrick
1509467b48Spatrick #include "llvm/Bitcode/BitcodeWriter.h"
1609467b48Spatrick #include "llvm/IR/BasicBlock.h"
1709467b48Spatrick #include "llvm/IR/Constants.h"
1809467b48Spatrick #include "llvm/IR/DerivedTypes.h"
1909467b48Spatrick #include "llvm/IR/Function.h"
2009467b48Spatrick #include "llvm/IR/InstrTypes.h"
2109467b48Spatrick #include "llvm/IR/Instruction.h"
2209467b48Spatrick #include "llvm/IR/Instructions.h"
2309467b48Spatrick #include "llvm/IR/LLVMContext.h"
2409467b48Spatrick #include "llvm/IR/Module.h"
2509467b48Spatrick #include "llvm/IR/Type.h"
2609467b48Spatrick #include "llvm/Support/raw_ostream.h"
2709467b48Spatrick
2809467b48Spatrick using namespace llvm;
2909467b48Spatrick
main()3009467b48Spatrick int main() {
3109467b48Spatrick LLVMContext Context;
3209467b48Spatrick
3309467b48Spatrick // Create the "module" or "program" or "translation unit" to hold the
3409467b48Spatrick // function
3509467b48Spatrick Module *M = new Module("test", Context);
3609467b48Spatrick
3709467b48Spatrick // Create the main function: first create the type 'int ()'
3809467b48Spatrick FunctionType *FT =
3909467b48Spatrick FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);
4009467b48Spatrick
4109467b48Spatrick // By passing a module as the last parameter to the Function constructor,
4209467b48Spatrick // it automatically gets appended to the Module.
4309467b48Spatrick Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
4409467b48Spatrick
4509467b48Spatrick // Add a basic block to the function... again, it automatically inserts
4609467b48Spatrick // because of the last argument.
4709467b48Spatrick BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);
4809467b48Spatrick
4909467b48Spatrick // Get pointers to the constant integers...
5009467b48Spatrick Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
5109467b48Spatrick Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);
5209467b48Spatrick
5309467b48Spatrick // Create the add instruction... does not insert...
5409467b48Spatrick Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
5509467b48Spatrick "addresult");
5609467b48Spatrick
5709467b48Spatrick // explicitly insert it into the basic block...
58*d415bd75Srobert Add->insertInto(BB, BB->end());
5909467b48Spatrick
6009467b48Spatrick // Create the return instruction and add it to the basic block
61*d415bd75Srobert ReturnInst::Create(Context, Add)->insertInto(BB, BB->end());
6209467b48Spatrick
6309467b48Spatrick // Output the bitcode file to stdout
6409467b48Spatrick WriteBitcodeToFile(*M, outs());
6509467b48Spatrick
6609467b48Spatrick // Delete the module and all of its contents.
6709467b48Spatrick delete M;
6809467b48Spatrick return 0;
6909467b48Spatrick }
70