as_restore.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. AngelCode Scripting Library
  3. Copyright (c) 2003-2022 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_restore.h
  25. //
  26. // Functions for saving and restoring module bytecode
  27. // asCRestore was originally written by Dennis Bollyn, [email protected]
  28. // It was later split in two classes asCReader and asCWriter by me
  29. #ifndef AS_RESTORE_H
  30. #define AS_RESTORE_H
  31. #include "as_scriptengine.h"
  32. #include "as_context.h"
  33. #include "as_map.h"
  34. BEGIN_AS_NAMESPACE
  35. class asCReader
  36. {
  37. public:
  38. asCReader(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine);
  39. int Read(bool *wasDebugInfoStripped);
  40. protected:
  41. asCModule *module;
  42. asIBinaryStream *stream;
  43. asCScriptEngine *engine;
  44. bool noDebugInfo;
  45. bool error;
  46. asUINT bytesRead;
  47. int Error(const char *msg);
  48. int ReadInner();
  49. int ReadData(void *data, asUINT size);
  50. void ReadString(asCString *str);
  51. asCScriptFunction *ReadFunction(bool &isNew, bool addToModule = true, bool addToEngine = true, bool addToGC = true, bool *isExternal = 0);
  52. void ReadFunctionSignature(asCScriptFunction *func, asCObjectType **parentClass = 0);
  53. void ReadGlobalProperty();
  54. void ReadObjectProperty(asCObjectType *ot);
  55. void ReadDataType(asCDataType *dt);
  56. asCTypeInfo *ReadTypeInfo();
  57. void ReadTypeDeclaration(asCTypeInfo *ot, int phase, bool *isExternal = 0);
  58. void ReadByteCode(asCScriptFunction *func);
  59. asWORD ReadEncodedUInt16();
  60. asUINT ReadEncodedUInt();
  61. int ReadEncodedInt();
  62. asQWORD ReadEncodedUInt64();
  63. asUINT SanityCheck(asUINT val, asUINT max);
  64. int SanityCheck(int val, asUINT max);
  65. void ReadUsedTypeIds();
  66. void ReadUsedFunctions();
  67. void ReadUsedGlobalProps();
  68. void ReadUsedStringConstants();
  69. void ReadUsedObjectProps();
  70. asCTypeInfo * FindType(int idx);
  71. int FindTypeId(int idx);
  72. short FindObjectPropOffset(asWORD index);
  73. asCScriptFunction *FindFunction(int idx);
  74. // After loading, each function needs to be translated to update pointers, function ids, etc
  75. void TranslateFunction(asCScriptFunction *func);
  76. void CalculateAdjustmentByPos(asCScriptFunction *func);
  77. int AdjustStackPosition(int pos);
  78. int AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);
  79. void CalculateStackNeeded(asCScriptFunction *func);
  80. // Temporary storage for persisting variable data
  81. asCArray<int> usedTypeIds;
  82. asCArray<asCTypeInfo*> usedTypes;
  83. asCArray<asCScriptFunction*> usedFunctions;
  84. asCArray<void*> usedGlobalProperties;
  85. asCArray<void*> usedStringConstants;
  86. asCArray<asCScriptFunction*> savedFunctions;
  87. asCArray<asCDataType> savedDataTypes;
  88. asCArray<asCString> savedStrings;
  89. asCArray<int> adjustByPos;
  90. asCArray<int> adjustNegativeStackByPos;
  91. struct SObjProp
  92. {
  93. asCObjectType *objType;
  94. asCObjectProperty *prop;
  95. };
  96. asCArray<SObjProp> usedObjectProperties;
  97. asCMap<void*,bool> existingShared;
  98. asCMap<asCScriptFunction*,bool> dontTranslate;
  99. // Helper class for adjusting offsets within initialization list buffers
  100. struct SListAdjuster
  101. {
  102. SListAdjuster(asCReader *rd, asDWORD *bc, asCObjectType *ot);
  103. void AdjustAllocMem();
  104. int AdjustOffset(int offset);
  105. void SetRepeatCount(asUINT rc);
  106. void SetNextType(int typeId);
  107. struct SInfo
  108. {
  109. asUINT repeatCount;
  110. asSListPatternNode *startNode;
  111. };
  112. asCArray<SInfo> stack;
  113. asCReader *reader;
  114. asDWORD *allocMemBC;
  115. asUINT maxOffset;
  116. asCObjectType *patternType;
  117. asUINT repeatCount;
  118. int lastOffset;
  119. int nextOffset;
  120. asUINT lastAdjustedOffset;
  121. asSListPatternNode *patternNode;
  122. int nextTypeId;
  123. };
  124. asCArray<SListAdjuster*> listAdjusters;
  125. };
  126. #ifndef AS_NO_COMPILER
  127. class asCWriter
  128. {
  129. public:
  130. asCWriter(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine, bool stripDebugInfo);
  131. int Write();
  132. protected:
  133. asCModule *module;
  134. asIBinaryStream *stream;
  135. asCScriptEngine *engine;
  136. bool stripDebugInfo;
  137. bool error;
  138. asUINT bytesWritten;
  139. int Error(const char *msg);
  140. int WriteData(const void *data, asUINT size);
  141. void WriteString(asCString *str);
  142. void WriteFunction(asCScriptFunction *func);
  143. void WriteFunctionSignature(asCScriptFunction *func);
  144. void WriteGlobalProperty(asCGlobalProperty *prop);
  145. void WriteObjectProperty(asCObjectProperty *prop);
  146. void WriteDataType(const asCDataType *dt);
  147. void WriteTypeInfo(asCTypeInfo *ot);
  148. void WriteTypeDeclaration(asCTypeInfo *ot, int phase);
  149. void WriteByteCode(asCScriptFunction *func);
  150. void WriteEncodedInt64(asINT64 i);
  151. // Helper functions for storing variable data
  152. int FindTypeInfoIdx(asCTypeInfo *ti);
  153. int FindTypeIdIdx(int typeId);
  154. int FindFunctionIndex(asCScriptFunction *func);
  155. int FindGlobalPropPtrIndex(void *);
  156. int FindStringConstantIndex(void *str);
  157. int FindObjectPropIndex(short offset, int typeId, asDWORD *bc);
  158. void CalculateAdjustmentByPos(asCScriptFunction *func);
  159. int AdjustStackPosition(int pos);
  160. int AdjustProgramPosition(int pos);
  161. int AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);
  162. // Intermediate data used for storing that which isn't constant, function id's, pointers, etc
  163. void WriteUsedTypeIds();
  164. void WriteUsedFunctions();
  165. void WriteUsedGlobalProps();
  166. void WriteUsedStringConstants();
  167. void WriteUsedObjectProps();
  168. // Temporary storage for persisting variable data
  169. asCArray<int> usedTypeIds;
  170. asCArray<asCTypeInfo*> usedTypes;
  171. asCArray<asCScriptFunction*> usedFunctions;
  172. asCArray<void*> usedGlobalProperties;
  173. asCArray<void*> usedStringConstants;
  174. asCMap<void*, int> stringToIndexMap;
  175. asCArray<asCScriptFunction*> savedFunctions;
  176. asCArray<asCDataType> savedDataTypes;
  177. asCArray<asCString> savedStrings;
  178. asCMap<asCString, int> stringToIdMap;
  179. asCArray<int> adjustStackByPos;
  180. asCArray<int> adjustNegativeStackByPos;
  181. asCArray<int> bytecodeNbrByPos;
  182. struct SObjProp
  183. {
  184. asCObjectType *objType;
  185. asCObjectProperty *prop;
  186. };
  187. asCArray<SObjProp> usedObjectProperties;
  188. // Helper class for adjusting offsets within initialization list buffers
  189. struct SListAdjuster
  190. {
  191. SListAdjuster(asCObjectType *ot);
  192. int AdjustOffset(int offset, asCObjectType *listPatternType);
  193. void SetRepeatCount(asUINT rc);
  194. void SetNextType(int typeId);
  195. struct SInfo
  196. {
  197. asUINT repeatCount;
  198. asSListPatternNode *startNode;
  199. };
  200. asCArray<SInfo> stack;
  201. asCObjectType *patternType;
  202. asUINT repeatCount;
  203. asSListPatternNode *patternNode;
  204. asUINT entries;
  205. int lastOffset; // Last offset adjusted
  206. int nextOffset; // next expected offset to be adjusted
  207. int nextTypeId;
  208. };
  209. asCArray<SListAdjuster*> listAdjusters;
  210. };
  211. #endif
  212. END_AS_NAMESPACE
  213. #endif // AS_RESTORE_H