as_bytecode.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2018 Andreas Jonsson
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any
  6. damages arising from the use of this software.
  7. Permission is granted to anyone to use this software for any
  8. purpose, including commercial applications, and to alter it and
  9. redistribute it freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you
  11. must not claim that you wrote the original software. If you use
  12. this software in a product, an acknowledgment in the product
  13. documentation would be appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and
  15. must not be misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source
  17. distribution.
  18. The original version of this library can be located at:
  19. http://www.angelcode.com/angelscript/
  20. Andreas Jonsson
  21. [email protected]
  22. */
  23. //
  24. // as_bytecode.h
  25. //
  26. // A class for constructing the final byte code
  27. //
  28. #ifndef AS_BYTECODE_H
  29. #define AS_BYTECODE_H
  30. #include "as_config.h"
  31. #ifndef AS_NO_COMPILER
  32. #include "as_array.h"
  33. BEGIN_AS_NAMESPACE
  34. #define BYTECODE_SIZE 4
  35. #define MAX_DATA_SIZE 8
  36. #define MAX_INSTR_SIZE (BYTECODE_SIZE+MAX_DATA_SIZE)
  37. class asCScriptEngine;
  38. class asCScriptFunction;
  39. class asCByteInstruction;
  40. class asCByteCode
  41. {
  42. public:
  43. asCByteCode(asCScriptEngine *engine);
  44. ~asCByteCode();
  45. void ClearAll();
  46. int GetSize();
  47. void Finalize(const asCArray<int> &tempVariableOffsets);
  48. void Optimize();
  49. void OptimizeLocally(const asCArray<int> &tempVariableOffsets);
  50. void ExtractLineNumbers();
  51. void ExtractObjectVariableInfo(asCScriptFunction *outFunc);
  52. void ExtractTryCatchInfo(asCScriptFunction *outFunc);
  53. int ResolveJumpAddresses();
  54. int FindLabel(int label, asCByteInstruction *from, asCByteInstruction **dest, int *positionDelta);
  55. void AddPath(asCArray<asCByteInstruction *> &paths, asCByteInstruction *instr, int stackSize);
  56. void Output(asDWORD *array);
  57. void AddCode(asCByteCode *bc);
  58. void PostProcess();
  59. #ifdef AS_DEBUG
  60. void DebugOutput(const char *name, asCScriptFunction *func);
  61. #endif
  62. int GetLastInstr();
  63. int RemoveLastInstr();
  64. asDWORD GetLastInstrValueDW();
  65. void InsertIfNotExists(asCArray<int> &vars, int var);
  66. void GetVarsUsed(asCArray<int> &vars);
  67. bool IsVarUsed(int offset);
  68. void ExchangeVar(int oldOffset, int newOffset);
  69. bool IsSimpleExpression();
  70. void Label(short label);
  71. void Line(int line, int column, int scriptIdx);
  72. void ObjInfo(int offset, int info);
  73. void Block(bool start);
  74. void TryBlock(short catchLabel);
  75. void VarDecl(int varDeclIdx);
  76. void Call(asEBCInstr bc, int funcID, int pop);
  77. void CallPtr(asEBCInstr bc, int funcPtrVar, int pop);
  78. void Alloc(asEBCInstr bc, void *objID, int funcID, int pop);
  79. void Ret(int pop);
  80. void JmpP(int var, asDWORD max);
  81. int InsertFirstInstrDWORD(asEBCInstr bc, asDWORD param);
  82. int InsertFirstInstrQWORD(asEBCInstr bc, asQWORD param);
  83. int Instr(asEBCInstr bc);
  84. int InstrQWORD(asEBCInstr bc, asQWORD param);
  85. int InstrDOUBLE(asEBCInstr bc, double param);
  86. int InstrPTR(asEBCInstr bc, void *param);
  87. int InstrDWORD(asEBCInstr bc, asDWORD param);
  88. int InstrWORD(asEBCInstr bc, asWORD param);
  89. int InstrSHORT(asEBCInstr bc, short param);
  90. int InstrFLOAT(asEBCInstr bc, float param);
  91. int InstrINT(asEBCInstr bc, int param);
  92. int InstrW_W_W(asEBCInstr bc, int a, int b, int c);
  93. int InstrSHORT_B(asEBCInstr bc, short a, asBYTE b);
  94. int InstrSHORT_W(asEBCInstr bc, short a, asWORD b);
  95. int InstrSHORT_DW(asEBCInstr bc, short a, asDWORD b);
  96. int InstrSHORT_QW(asEBCInstr bc, short a, asQWORD b);
  97. int InstrW_DW(asEBCInstr bc, asWORD a, asDWORD b);
  98. int InstrW_QW(asEBCInstr bc, asWORD a, asQWORD b);
  99. int InstrW_PTR(asEBCInstr bc, short a, void *param);
  100. int InstrW_FLOAT(asEBCInstr bc, asWORD a, float b);
  101. int InstrW_W(asEBCInstr bc, int w, int b);
  102. int InstrSHORT_DW_DW(asEBCInstr bc, short a, asDWORD b, asDWORD c);
  103. asCScriptEngine *GetEngine() const { return engine; };
  104. asCArray<int> lineNumbers;
  105. asCArray<int> sectionIdxs;
  106. int largestStackUsed;
  107. protected:
  108. // Assignments are not allowed
  109. void operator=(const asCByteCode &) {}
  110. // Helpers for Optimize
  111. bool CanBeSwapped(asCByteInstruction *curr);
  112. asCByteInstruction *ChangeFirstDeleteNext(asCByteInstruction *curr, asEBCInstr bc);
  113. asCByteInstruction *DeleteFirstChangeNext(asCByteInstruction *curr, asEBCInstr bc);
  114. asCByteInstruction *DeleteInstruction(asCByteInstruction *instr);
  115. void RemoveInstruction(asCByteInstruction *instr);
  116. asCByteInstruction *GoBack(asCByteInstruction *curr);
  117. asCByteInstruction *GoForward(asCByteInstruction *curr);
  118. void InsertBefore(asCByteInstruction *before, asCByteInstruction *instr);
  119. bool RemoveUnusedValue(asCByteInstruction *curr, asCByteInstruction **next);
  120. bool IsTemporary(int offset);
  121. bool IsTempRegUsed(asCByteInstruction *curr);
  122. bool IsTempVarRead(asCByteInstruction *curr, int offset);
  123. bool PostponeInitOfTemp(asCByteInstruction *curr, asCByteInstruction **next);
  124. bool IsTempVarReadByInstr(asCByteInstruction *curr, int var);
  125. bool IsTempVarOverwrittenByInstr(asCByteInstruction *curr, int var);
  126. bool IsInstrJmpOrLabel(asCByteInstruction *curr);
  127. int AddInstruction();
  128. int AddInstructionFirst();
  129. asCByteInstruction *first;
  130. asCByteInstruction *last;
  131. const asCArray<int> *temporaryVariables;
  132. asCScriptEngine *engine;
  133. };
  134. class asCByteInstruction
  135. {
  136. public:
  137. asCByteInstruction();
  138. void AddAfter(asCByteInstruction *nextCode);
  139. void AddBefore(asCByteInstruction *nextCode);
  140. void Remove();
  141. int GetSize();
  142. int GetStackIncrease();
  143. asCByteInstruction *next;
  144. asCByteInstruction *prev;
  145. asEBCInstr op;
  146. asQWORD arg;
  147. short wArg[3];
  148. int size;
  149. int stackInc;
  150. // Testing
  151. bool marked;
  152. int stackSize;
  153. };
  154. END_AS_NAMESPACE
  155. #endif // AS_NO_COMPILER
  156. #endif