2016-05-25 09:18:36 +08:00
|
|
|
//===-- BrainF.h - BrainF compiler class ------------------------*- C++ -*-===//
|
2007-09-13 02:24:00 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08: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
|
2007-09-13 02:24:00 +08:00
|
|
|
//
|
2016-05-25 09:18:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-13 02:24:00 +08:00
|
|
|
//
|
|
|
|
// This class stores the data for the BrainF compiler so it doesn't have
|
|
|
|
// to pass all of it around. The main method is parse.
|
|
|
|
//
|
2016-05-25 09:18:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2007-09-13 02:24:00 +08:00
|
|
|
|
|
|
|
#ifndef BRAINF_H
|
|
|
|
#define BRAINF_H
|
|
|
|
|
2013-01-02 19:56:33 +08:00
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
2016-05-25 09:18:36 +08:00
|
|
|
#include <istream>
|
2007-09-13 02:24:00 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// This class provides a parser for the BrainF language.
|
|
|
|
/// The class itself is made to store values during
|
|
|
|
/// parsing so they don't have to be passed around
|
|
|
|
/// as much.
|
|
|
|
class BrainF {
|
|
|
|
public:
|
|
|
|
/// Options for how BrainF should compile
|
|
|
|
enum CompileFlags {
|
|
|
|
flag_off = 0,
|
|
|
|
flag_arraybounds = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
/// This is the main method. It parses BrainF from in1
|
|
|
|
/// and returns the module with a function
|
|
|
|
/// void brainf()
|
|
|
|
/// containing the resulting code.
|
|
|
|
/// On error, it calls abort.
|
|
|
|
/// The caller must delete the returned module.
|
2009-07-02 05:22:36 +08:00
|
|
|
Module *parse(std::istream *in1, int mem, CompileFlags cf,
|
2009-07-02 07:13:44 +08:00
|
|
|
LLVMContext& C);
|
2007-09-13 02:24:00 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
/// The different symbols in the BrainF language
|
|
|
|
enum Symbol {
|
|
|
|
SYM_NONE,
|
|
|
|
SYM_READ,
|
|
|
|
SYM_WRITE,
|
|
|
|
SYM_MOVE,
|
|
|
|
SYM_CHANGE,
|
|
|
|
SYM_LOOP,
|
|
|
|
SYM_ENDLOOP,
|
|
|
|
SYM_EOF
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Names of the different parts of the language.
|
|
|
|
/// Tape is used for reading and writing the tape.
|
|
|
|
/// headreg is used for the position of the head.
|
|
|
|
/// label is used for the labels for the BasicBlocks.
|
|
|
|
/// testreg is used for testing the loop exit condition.
|
|
|
|
static const char *tapereg;
|
|
|
|
static const char *headreg;
|
|
|
|
static const char *label;
|
|
|
|
static const char *testreg;
|
|
|
|
|
|
|
|
/// Put the brainf function preamble and other fixed pieces of code
|
2009-07-02 07:13:44 +08:00
|
|
|
void header(LLVMContext& C);
|
2007-09-13 02:24:00 +08:00
|
|
|
|
|
|
|
/// The main loop for parsing. It calls itself recursively
|
|
|
|
/// to handle the depth of nesting of "[]".
|
2009-07-15 07:09:55 +08:00
|
|
|
void readloop(PHINode *phi, BasicBlock *oldbb,
|
|
|
|
BasicBlock *testbb, LLVMContext &Context);
|
2007-09-13 02:24:00 +08:00
|
|
|
|
|
|
|
/// Constants during parsing
|
|
|
|
int memtotal;
|
|
|
|
CompileFlags comflag;
|
|
|
|
std::istream *in;
|
|
|
|
Module *module;
|
2019-02-01 11:23:42 +08:00
|
|
|
Function *brainf_func;
|
[opaque pointer types] Add a FunctionCallee wrapper type, and use it.
Recommit r352791 after tweaking DerivedTypes.h slightly, so that gcc
doesn't choke on it, hopefully.
Original Message:
The FunctionCallee type is effectively a {FunctionType*,Value*} pair,
and is a useful convenience to enable code to continue passing the
result of getOrInsertFunction() through to EmitCall, even once pointer
types lose their pointee-type.
Then:
- update the CallInst/InvokeInst instruction creation functions to
take a Callee,
- modify getOrInsertFunction to return FunctionCallee, and
- update all callers appropriately.
One area of particular note is the change to the sanitizer
code. Previously, they had been casting the result of
`getOrInsertFunction` to a `Function*` via
`checkSanitizerInterfaceFunction`, and storing that. That would report
an error if someone had already inserted a function declaraction with
a mismatching signature.
However, in general, LLVM allows for such mismatches, as
`getOrInsertFunction` will automatically insert a bitcast if
needed. As part of this cleanup, cause the sanitizer code to do the
same. (It will call its functions using the expected signature,
however they may have been declared.)
Finally, in a small number of locations, callers of
`getOrInsertFunction` actually were expecting/requiring that a brand
new function was being created. In such cases, I've switched them to
Function::Create instead.
Differential Revision: https://reviews.llvm.org/D57315
llvm-svn: 352827
2019-02-01 10:28:03 +08:00
|
|
|
FunctionCallee getchar_func;
|
|
|
|
FunctionCallee putchar_func;
|
2007-09-13 02:24:00 +08:00
|
|
|
Value *ptr_arr;
|
|
|
|
Value *ptr_arrmax;
|
|
|
|
BasicBlock *endbb;
|
|
|
|
BasicBlock *aberrorbb;
|
|
|
|
|
|
|
|
/// Variables
|
2008-08-09 03:39:37 +08:00
|
|
|
IRBuilder<> *builder;
|
2007-09-13 02:24:00 +08:00
|
|
|
Value *curhead;
|
|
|
|
};
|
|
|
|
|
2016-05-25 09:18:36 +08:00
|
|
|
#endif // BRAINF_H
|