BrainF.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //===-- BrainF.h - BrainF compiler class ------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This class stores the data for the BrainF compiler so it doesn't have
  10. // to pass all of it around. The main method is parse.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef BRAINF_H
  14. #define BRAINF_H
  15. #include "llvm/IR/IRBuilder.h"
  16. #include "llvm/IR/LLVMContext.h"
  17. #include "llvm/IR/Module.h"
  18. #include <istream>
  19. using namespace llvm;
  20. /// This class provides a parser for the BrainF language.
  21. /// The class itself is made to store values during
  22. /// parsing so they don't have to be passed around
  23. /// as much.
  24. class BrainF {
  25. public:
  26. /// Options for how BrainF should compile
  27. enum CompileFlags {
  28. flag_off = 0,
  29. flag_arraybounds = 1
  30. };
  31. /// This is the main method. It parses BrainF from in1
  32. /// and returns the module with a function
  33. /// void brainf()
  34. /// containing the resulting code.
  35. /// On error, it calls abort.
  36. /// The caller must delete the returned module.
  37. Module *parse(std::istream *in1, int mem, CompileFlags cf,
  38. LLVMContext& C);
  39. protected:
  40. /// The different symbols in the BrainF language
  41. enum Symbol {
  42. SYM_NONE,
  43. SYM_READ,
  44. SYM_WRITE,
  45. SYM_MOVE,
  46. SYM_CHANGE,
  47. SYM_LOOP,
  48. SYM_ENDLOOP,
  49. SYM_EOF
  50. };
  51. /// Names of the different parts of the language.
  52. /// Tape is used for reading and writing the tape.
  53. /// headreg is used for the position of the head.
  54. /// label is used for the labels for the BasicBlocks.
  55. /// testreg is used for testing the loop exit condition.
  56. static const char *tapereg;
  57. static const char *headreg;
  58. static const char *label;
  59. static const char *testreg;
  60. /// Put the brainf function preamble and other fixed pieces of code
  61. void header(LLVMContext& C);
  62. /// The main loop for parsing. It calls itself recursively
  63. /// to handle the depth of nesting of "[]".
  64. void readloop(PHINode *phi, BasicBlock *oldbb,
  65. BasicBlock *testbb, LLVMContext &Context);
  66. /// Constants during parsing
  67. int memtotal;
  68. CompileFlags comflag;
  69. std::istream *in;
  70. Module *module;
  71. Function *brainf_func;
  72. FunctionCallee getchar_func;
  73. FunctionCallee putchar_func;
  74. Value *ptr_arr;
  75. Value *ptr_arrmax;
  76. BasicBlock *endbb;
  77. BasicBlock *aberrorbb;
  78. /// Variables
  79. IRBuilder<> *builder;
  80. Value *curhead;
  81. };
  82. #endif // BRAINF_H