//===-- BrainF.cpp - BrainF compiler example ------------------------------===// // // 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 // //===----------------------------------------------------------------------===// // // This class compiles the BrainF language into LLVM assembly. // // The BrainF language has 8 commands: // Command Equivalent C Action // ------- ------------ ------ // , *h=getchar(); Read a character from stdin, 255 on EOF // . putchar(*h); Write a character to stdout // - --*h; Decrement tape // + ++*h; Increment tape // < --h; Move head left // > ++h; Move head right // [ while(*h) { Start loop // ] } End loop // //===----------------------------------------------------------------------===// #include "BrainF.h" #include "llvm/ADT/APInt.h" #include "llvm/IR/BasicBlock.h" #include "llvm/IR/Constant.h" #include "llvm/IR/Constants.h" #include "llvm/IR/DerivedTypes.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalValue.h" #include "llvm/IR/GlobalVariable.h" #include "llvm/IR/InstrTypes.h" #include "llvm/IR/Instruction.h" #include "llvm/IR/Instructions.h" #include "llvm/IR/Intrinsics.h" #include "llvm/IR/Module.h" #include "llvm/IR/Type.h" #include <cstdlib> #include <iostream> usingnamespacellvm; //Set the constants for naming const char *BrainF::tapereg = …; const char *BrainF::headreg = …; const char *BrainF::label = …; const char *BrainF::testreg = …; Module *BrainF::parse(std::istream *in1, int mem, CompileFlags cf, LLVMContext& Context) { … } void BrainF::header(LLVMContext& C) { … } void BrainF::readloop(PHINode *phi, BasicBlock *oldbb, BasicBlock *testbb, LLVMContext &C) { … }