codeBlock.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _CODEBLOCK_H_
  23. #define _CODEBLOCK_H_
  24. #include <unordered_map>
  25. struct CompilerLocalVariableToRegisterMappingTable
  26. {
  27. // First key: function name
  28. struct RemappingTable
  29. {
  30. std::unordered_map<StringTableEntry, S32> table;
  31. };
  32. std::unordered_map<StringTableEntry, RemappingTable> localVarToRegister;
  33. void add(StringTableEntry functionName, StringTableEntry namespaceName, StringTableEntry varName, S32 reg);
  34. S32 lookup(StringTableEntry namespaceName, StringTableEntry functionName, StringTableEntry varName);
  35. CompilerLocalVariableToRegisterMappingTable copy();
  36. void reset();
  37. };
  38. #include "console/compiler.h"
  39. #include "console/consoleParser.h"
  40. class Stream;
  41. class ConsoleValue;
  42. /// Core TorqueScript code management class.
  43. ///
  44. /// This class represents a block of code, usually mapped directly to a file.
  45. class CodeBlock
  46. {
  47. private:
  48. static CodeBlock* smCodeBlockList;
  49. static CodeBlock* smCurrentCodeBlock;
  50. public:
  51. static bool smInFunction;
  52. static Compiler::ConsoleParser * smCurrentParser;
  53. static CodeBlock* getCurrentBlock()
  54. {
  55. return smCurrentCodeBlock;
  56. }
  57. static CodeBlock *getCodeBlockList()
  58. {
  59. return smCodeBlockList;
  60. }
  61. static StringTableEntry getCurrentCodeBlockName();
  62. static StringTableEntry getCurrentCodeBlockFullPath();
  63. static StringTableEntry getCurrentCodeBlockModName();
  64. static CodeBlock *find(StringTableEntry);
  65. CodeBlock();
  66. ~CodeBlock();
  67. StringTableEntry name;
  68. StringTableEntry fullPath;
  69. StringTableEntry modPath;
  70. char *globalStrings;
  71. char *functionStrings;
  72. U32 functionStringsMaxLen;
  73. U32 globalStringsMaxLen;
  74. F64 *globalFloats;
  75. F64 *functionFloats;
  76. U32 codeSize;
  77. U32 *code;
  78. CompilerLocalVariableToRegisterMappingTable variableRegisterTable;
  79. U32 refCount;
  80. U32 lineBreakPairCount;
  81. U32 *lineBreakPairs;
  82. U32 breakListSize;
  83. U32 *breakList;
  84. CodeBlock *nextFile;
  85. void addToCodeList();
  86. void removeFromCodeList();
  87. void calcBreakList();
  88. void clearAllBreaks();
  89. void setAllBreaks();
  90. void dumpInstructions(U32 startIp = 0, bool upToReturn = false);
  91. /// Returns the first breakable line or 0 if none was found.
  92. /// @param lineNumber The one based line number.
  93. U32 findFirstBreakLine(U32 lineNumber);
  94. void clearBreakpoint(U32 lineNumber);
  95. /// Set a OP_BREAK instruction on a line. If a break
  96. /// is not possible on that line it returns false.
  97. /// @param lineNumber The one based line number.
  98. bool setBreakpoint(U32 lineNumber);
  99. void findBreakLine(U32 ip, U32 &line, U32 &instruction);
  100. const char *getFileLine(U32 ip);
  101. ///
  102. String getFunctionArgs(U32 offset);
  103. bool read(StringTableEntry fileName, Stream &st);
  104. bool compile(const char *dsoName, StringTableEntry fileName, const char *script, bool overrideNoDso = false);
  105. void incRefCount();
  106. void decRefCount();
  107. /// Compiles and executes a block of script storing the compiled code in this
  108. /// CodeBlock. If there is no filename breakpoints will not be generated and
  109. /// the CodeBlock will not be added to the linked list of loaded CodeBlocks.
  110. /// Note that if the script contains no executable statements the CodeBlock
  111. /// will delete itself on return an empty string. The return string is any
  112. /// result of the code executed, if any, or an empty string.
  113. ///
  114. /// @param fileName The file name, including path and extension, for the
  115. /// block of code or an empty string.
  116. /// @param script The script code to compile and execute.
  117. /// @param noCalls Skips calling functions from the script.
  118. /// @param setFrame A zero based index of the stack frame to execute the code
  119. /// with, zero being the top of the stack. If the the index is
  120. /// -1 a new frame is created. If the index is out of range the
  121. /// top stack frame is used.
  122. ConsoleValue compileExec(StringTableEntry fileName, const char *script,
  123. bool noCalls, S32 setFrame = -1);
  124. /// Executes the existing code in the CodeBlock. The return string is any
  125. /// result of the code executed, if any, or an empty string.
  126. ///
  127. /// @param offset The instruction offset to start executing from.
  128. /// @param fnName The name of the function to execute or null.
  129. /// @param ns The namespace of the function to execute or null.
  130. /// @param argc The number of parameters passed to the function or
  131. /// zero to execute code outside of a function.
  132. /// @param argv The function parameter list.
  133. /// @param noCalls Skips calling functions from the script.
  134. /// @param setFrame A zero based index of the stack frame to execute the code
  135. /// with, zero being the top of the stack. If the the index is
  136. /// -1 a new frame is created. If the index is out of range the
  137. /// top stack frame is used.
  138. /// @param packageName The code package name or null.
  139. ConsoleValue exec(U32 offset, const char *fnName, Namespace *ns, U32 argc,
  140. ConsoleValue *argv, bool noCalls, StringTableEntry packageName,
  141. S32 setFrame = -1);
  142. };
  143. #endif