ScummVM API documentation
as_restore.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2021 Andreas Jonsson
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any
7  damages arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any
10  purpose, including commercial applications, and to alter it and
11  redistribute it freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you
14  must not claim that you wrote the original software. If you use
15  this software in a product, an acknowledgment in the product
16  documentation would be appreciated but is not required.
17 
18  2. Altered source versions must be plainly marked as such, and
19  must not be misrepresented as being the original software.
20 
21  3. This notice may not be removed or altered from any source
22  distribution.
23 
24  The original version of this library can be located at:
25  http://www.angelcode.com/angelscript/
26 
27  Andreas Jonsson
28  andreas@angelcode.com
29 */
30 
31 
32 //
33 // as_restore.h
34 //
35 // Functions for saving and restoring module bytecode
36 // asCRestore was originally written by Dennis Bollyn, dennis@gyrbo.be
37 // It was later split in two classes asCReader and asCWriter by me
38 
39 #ifndef AS_RESTORE_H
40 #define AS_RESTORE_H
41 
42 #include "as_scriptengine.h"
43 #include "as_context.h"
44 #include "as_map.h"
45 
46 BEGIN_AS_NAMESPACE
47 
48 class asCReader {
49 public:
50  asCReader(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine);
51 
52  int Read(bool *wasDebugInfoStripped);
53 
54 protected:
55  asCModule *module;
56  asIBinaryStream *stream;
57  asCScriptEngine *engine;
58  bool noDebugInfo;
59  bool error;
60  asUINT bytesRead;
61 
62  int Error(const char *msg);
63 
64  int ReadInner();
65 
66  int ReadData(void *data, asUINT size);
67  void ReadString(asCString *str);
68  asCScriptFunction *ReadFunction(bool &isNew, bool addToModule = true, bool addToEngine = true, bool addToGC = true, bool *isExternal = 0);
69  void ReadFunctionSignature(asCScriptFunction *func, asCObjectType **parentClass = 0);
70  void ReadGlobalProperty();
71  void ReadObjectProperty(asCObjectType *ot);
72  void ReadDataType(asCDataType *dt);
73  asCTypeInfo *ReadTypeInfo();
74  void ReadTypeDeclaration(asCTypeInfo *ot, int phase, bool *isExternal = 0);
75  void ReadByteCode(asCScriptFunction *func);
76  asWORD ReadEncodedUInt16();
77  asUINT ReadEncodedUInt();
78  int ReadEncodedInt();
79  asQWORD ReadEncodedUInt64();
80  asUINT SanityCheck(asUINT val, asUINT max);
81  int SanityCheck(int val, asUINT max);
82 
83  void ReadUsedTypeIds();
84  void ReadUsedFunctions();
85  void ReadUsedGlobalProps();
86  void ReadUsedStringConstants();
87  void ReadUsedObjectProps();
88 
89  asCTypeInfo *FindType(int idx);
90  int FindTypeId(int idx);
91  short FindObjectPropOffset(asWORD index);
92  asCScriptFunction *FindFunction(int idx);
93 
94  // After loading, each function needs to be translated to update pointers, function ids, etc
95  void TranslateFunction(asCScriptFunction *func);
96  void CalculateAdjustmentByPos(asCScriptFunction *func);
97  int AdjustStackPosition(int pos);
98  int AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);
99  void CalculateStackNeeded(asCScriptFunction *func);
100  asCScriptFunction *GetCalledFunction(asCScriptFunction *func, asDWORD programPos);
101 
102  // Temporary storage for persisting variable data
103  asCArray<int> usedTypeIds;
104  asCArray<asCTypeInfo *> usedTypes;
105  asCArray<asCScriptFunction *> usedFunctions;
106  asCArray<void *> usedGlobalProperties;
107  asCArray<void *> usedStringConstants;
108 
109  asCArray<asCScriptFunction *> savedFunctions;
110  asCArray<asCDataType> savedDataTypes;
111  asCArray<asCString> savedStrings;
112 
113  asCArray<int> adjustByPos;
114  asCArray<int> adjustNegativeStackByPos;
115 
116  struct SObjProp {
117  asCObjectType *objType;
118  asCObjectProperty *prop;
119  };
120  asCArray<SObjProp> usedObjectProperties;
121 
122  asCMap<void *, bool> existingShared;
123  asCMap<asCScriptFunction *, bool> dontTranslate;
124 
125  // Helper class for adjusting offsets within initialization list buffers
126  struct SListAdjuster {
127  SListAdjuster(asCReader *rd, asDWORD *bc, asCObjectType *ot);
128  void AdjustAllocMem();
129  int AdjustOffset(int offset);
130  void SetRepeatCount(asUINT rc);
131  void SetNextType(int typeId);
132 
133  struct SInfo {
134  asUINT repeatCount;
135  asSListPatternNode *startNode;
136  };
137  asCArray<SInfo> stack;
138 
139  asCReader *reader;
140  asDWORD *allocMemBC;
141  asUINT maxOffset;
142  asCObjectType *patternType;
143  asUINT repeatCount;
144  int lastOffset;
145  int nextOffset;
146  asUINT lastAdjustedOffset;
147  asSListPatternNode *patternNode;
148  int nextTypeId;
149  };
150  asCArray<SListAdjuster *> listAdjusters;
151 };
152 
153 #ifndef AS_NO_COMPILER
154 
155 class asCWriter {
156 public:
157  asCWriter(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine, bool stripDebugInfo);
158 
159  int Write();
160 
161 protected:
162  asCModule *module;
163  asIBinaryStream *stream;
164  asCScriptEngine *engine;
165  bool stripDebugInfo;
166  bool error;
167  asUINT bytesWritten;
168 
169  int Error(const char *msg);
170 
171  int WriteData(const void *data, asUINT size);
172 
173  void WriteString(asCString *str);
174  void WriteFunction(asCScriptFunction *func);
175  void WriteFunctionSignature(asCScriptFunction *func);
176  void WriteGlobalProperty(asCGlobalProperty *prop);
177  void WriteObjectProperty(asCObjectProperty *prop);
178  void WriteDataType(const asCDataType *dt);
179  void WriteTypeInfo(asCTypeInfo *ot);
180  void WriteTypeDeclaration(asCTypeInfo *ot, int phase);
181  void WriteByteCode(asCScriptFunction *func);
182  void WriteEncodedInt64(asINT64 i);
183 
184  // Helper functions for storing variable data
185  int FindTypeInfoIdx(asCTypeInfo *ti);
186  int FindTypeIdIdx(int typeId);
187  int FindFunctionIndex(asCScriptFunction *func);
188  int FindGlobalPropPtrIndex(void *);
189  int FindStringConstantIndex(void *str);
190  int FindObjectPropIndex(short offset, int typeId, asDWORD *bc);
191 
192  void CalculateAdjustmentByPos(asCScriptFunction *func);
193  int AdjustStackPosition(int pos);
194  int AdjustProgramPosition(int pos);
195  int AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);
196 
197  // Intermediate data used for storing that which isn't constant, function id's, pointers, etc
198  void WriteUsedTypeIds();
199  void WriteUsedFunctions();
200  void WriteUsedGlobalProps();
201  void WriteUsedStringConstants();
202  void WriteUsedObjectProps();
203 
204  // Temporary storage for persisting variable data
205  asCArray<int> usedTypeIds;
206  asCArray<asCTypeInfo *> usedTypes;
207  asCArray<asCScriptFunction *> usedFunctions;
208  asCArray<void *> usedGlobalProperties;
209  asCArray<void *> usedStringConstants;
210  asCMap<void *, int> stringToIndexMap;
211 
212  asCArray<asCScriptFunction *> savedFunctions;
213  asCArray<asCDataType> savedDataTypes;
214  asCArray<asCString> savedStrings;
215  asCMap<asCString, int> stringToIdMap;
216  asCArray<int> adjustStackByPos;
217  asCArray<int> adjustNegativeStackByPos;
218  asCArray<int> bytecodeNbrByPos;
219 
220  struct SObjProp {
221  asCObjectType *objType;
222  asCObjectProperty *prop;
223  };
224  asCArray<SObjProp> usedObjectProperties;
225 
226  // Helper class for adjusting offsets within initialization list buffers
227  struct SListAdjuster {
229  int AdjustOffset(int offset, asCObjectType *listPatternType);
230  void SetRepeatCount(asUINT rc);
231  void SetNextType(int typeId);
232 
233  struct SInfo {
234  asUINT repeatCount;
235  asSListPatternNode *startNode;
236  };
237  asCArray<SInfo> stack;
238 
239  asCObjectType *patternType;
240  asUINT repeatCount;
241  asSListPatternNode *patternNode;
242  asUINT entries;
243  int lastOffset; // Last offset adjusted
244  int nextOffset; // next expected offset to be adjusted
245  int nextTypeId;
246  };
247  asCArray<SListAdjuster *> listAdjusters;
248 };
249 
250 #endif
251 
252 END_AS_NAMESPACE
253 
254 #endif // AS_RESTORE_H
Definition: as_module.h:93
Definition: as_restore.h:220
Definition: as_typeinfo.h:64
Definition: as_restore.h:155
Definition: as_restore.h:48
Definition: as_scriptfunction.h:146
Definition: as_objecttype.h:100
Definition: as_property.h:53
Definition: as_restore.h:116
Definition: as_scriptfunction.h:74
Definition: as_datatype.h:60
void NORETURN_PRE error(MSVC_PRINTF const char *s,...) GCC_PRINTF(1
Definition: as_property.h:68
Definition: angelscript.h:1145
Definition: as_restore.h:227
Definition: as_restore.h:133
Definition: as_scriptengine.h:64
Definition: as_restore.h:233
Definition: as_string.h:41
Definition: as_restore.h:126