1987319d3SReid Spencer //===- examples/ModuleMaker/ModuleMaker.cpp - Example project ---*- C++ -*-===//
25b81eea7SReid Spencer //
3*2946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*2946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
5*2946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
63779fd65SChris Lattner //
73779fd65SChris Lattner //===----------------------------------------------------------------------===//
83779fd65SChris Lattner //
95b81eea7SReid Spencer // This programs is a simple example that creates an LLVM module "from scratch",
10e16561cdSGabor Greif // emitting it as a bitcode file to standard out.  This is just to show how
115b81eea7SReid Spencer // LLVM projects work and to demonstrate some of the LLVM APIs.
125b81eea7SReid Spencer //
135b81eea7SReid Spencer //===----------------------------------------------------------------------===//
145b81eea7SReid Spencer 
1559238645STeresa Johnson #include "llvm/Bitcode/BitcodeWriter.h"
162b8e4170SEugene Zelenko #include "llvm/IR/BasicBlock.h"
17005f27a0SChandler Carruth #include "llvm/IR/Constants.h"
18005f27a0SChandler Carruth #include "llvm/IR/DerivedTypes.h"
192b8e4170SEugene Zelenko #include "llvm/IR/Function.h"
202b8e4170SEugene Zelenko #include "llvm/IR/InstrTypes.h"
212b8e4170SEugene Zelenko #include "llvm/IR/Instruction.h"
22005f27a0SChandler Carruth #include "llvm/IR/Instructions.h"
23005f27a0SChandler Carruth #include "llvm/IR/LLVMContext.h"
24005f27a0SChandler Carruth #include "llvm/IR/Module.h"
252b8e4170SEugene Zelenko #include "llvm/IR/Type.h"
266973395cSChris Lattner #include "llvm/Support/raw_ostream.h"
272b8e4170SEugene Zelenko 
285b81eea7SReid Spencer using namespace llvm;
295b81eea7SReid Spencer 
main()305b81eea7SReid Spencer int main() {
316773d388SOwen Anderson   LLVMContext Context;
326773d388SOwen Anderson 
335b81eea7SReid Spencer   // Create the "module" or "program" or "translation unit" to hold the
345b81eea7SReid Spencer   // function
351cf085d5SOwen Anderson   Module *M = new Module("test", Context);
365b81eea7SReid Spencer 
375b81eea7SReid Spencer   // Create the main function: first create the type 'int ()'
38b6b25300SOwen Anderson   FunctionType *FT =
3955f1c09eSOwen Anderson     FunctionType::get(Type::getInt32Ty(Context), /*not vararg*/false);
405b81eea7SReid Spencer 
415b81eea7SReid Spencer   // By passing a module as the last parameter to the Function constructor,
425b81eea7SReid Spencer   // it automatically gets appended to the Module.
43e9ecc68dSGabor Greif   Function *F = Function::Create(FT, Function::ExternalLinkage, "main", M);
445b81eea7SReid Spencer 
455b81eea7SReid Spencer   // Add a basic block to the function... again, it automatically inserts
465b81eea7SReid Spencer   // because of the last argument.
4755f1c09eSOwen Anderson   BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", F);
485b81eea7SReid Spencer 
495b81eea7SReid Spencer   // Get pointers to the constant integers...
5055f1c09eSOwen Anderson   Value *Two = ConstantInt::get(Type::getInt32Ty(Context), 2);
5155f1c09eSOwen Anderson   Value *Three = ConstantInt::get(Type::getInt32Ty(Context), 3);
525b81eea7SReid Spencer 
535b81eea7SReid Spencer   // Create the add instruction... does not insert...
54e1f6e4b2SGabor Greif   Instruction *Add = BinaryOperator::Create(Instruction::Add, Two, Three,
555b81eea7SReid Spencer                                             "addresult");
565b81eea7SReid Spencer 
575b81eea7SReid Spencer   // explicitly insert it into the basic block...
585b81eea7SReid Spencer   BB->getInstList().push_back(Add);
595b81eea7SReid Spencer 
605b81eea7SReid Spencer   // Create the return instruction and add it to the basic block
6155f1c09eSOwen Anderson   BB->getInstList().push_back(ReturnInst::Create(Context, Add));
625b81eea7SReid Spencer 
63e16561cdSGabor Greif   // Output the bitcode file to stdout
6424be43d2SRafael Espindola   WriteBitcodeToFile(*M, outs());
655b81eea7SReid Spencer 
665b81eea7SReid Spencer   // Delete the module and all of its contents.
675b81eea7SReid Spencer   delete M;
685b81eea7SReid Spencer   return 0;
695b81eea7SReid Spencer }
70