| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /*
- AngelCode Scripting Library
- Copyright (c) 2003-2022 Andreas Jonsson
- This software is provided 'as-is', without any express or implied
- warranty. In no event will the authors be held liable for any
- damages arising from the use of this software.
- Permission is granted to anyone to use this software for any
- purpose, including commercial applications, and to alter it and
- redistribute it freely, subject to the following restrictions:
- 1. The origin of this software must not be misrepresented; you
- must not claim that you wrote the original software. If you use
- this software in a product, an acknowledgment in the product
- documentation would be appreciated but is not required.
- 2. Altered source versions must be plainly marked as such, and
- must not be misrepresented as being the original software.
- 3. This notice may not be removed or altered from any source
- distribution.
- The original version of this library can be located at:
- http://www.angelcode.com/angelscript/
- Andreas Jonsson
- [email protected]
- */
- //
- // as_restore.h
- //
- // Functions for saving and restoring module bytecode
- // asCRestore was originally written by Dennis Bollyn, [email protected]
- // It was later split in two classes asCReader and asCWriter by me
- #ifndef AS_RESTORE_H
- #define AS_RESTORE_H
- #include "as_scriptengine.h"
- #include "as_context.h"
- #include "as_map.h"
- BEGIN_AS_NAMESPACE
- class asCReader
- {
- public:
- asCReader(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine);
- int Read(bool *wasDebugInfoStripped);
- protected:
- asCModule *module;
- asIBinaryStream *stream;
- asCScriptEngine *engine;
- bool noDebugInfo;
- bool error;
- asUINT bytesRead;
- int Error(const char *msg);
- int ReadInner();
- int ReadData(void *data, asUINT size);
- void ReadString(asCString *str);
- asCScriptFunction *ReadFunction(bool &isNew, bool addToModule = true, bool addToEngine = true, bool addToGC = true, bool *isExternal = 0);
- void ReadFunctionSignature(asCScriptFunction *func, asCObjectType **parentClass = 0);
- void ReadGlobalProperty();
- void ReadObjectProperty(asCObjectType *ot);
- void ReadDataType(asCDataType *dt);
- asCTypeInfo *ReadTypeInfo();
- void ReadTypeDeclaration(asCTypeInfo *ot, int phase, bool *isExternal = 0);
- void ReadByteCode(asCScriptFunction *func);
- asWORD ReadEncodedUInt16();
- asUINT ReadEncodedUInt();
- int ReadEncodedInt();
- asQWORD ReadEncodedUInt64();
- asUINT SanityCheck(asUINT val, asUINT max);
- int SanityCheck(int val, asUINT max);
- void ReadUsedTypeIds();
- void ReadUsedFunctions();
- void ReadUsedGlobalProps();
- void ReadUsedStringConstants();
- void ReadUsedObjectProps();
- asCTypeInfo * FindType(int idx);
- int FindTypeId(int idx);
- short FindObjectPropOffset(asWORD index);
- asCScriptFunction *FindFunction(int idx);
- // After loading, each function needs to be translated to update pointers, function ids, etc
- void TranslateFunction(asCScriptFunction *func);
- void CalculateAdjustmentByPos(asCScriptFunction *func);
- int AdjustStackPosition(int pos);
- int AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);
- void CalculateStackNeeded(asCScriptFunction *func);
- // Temporary storage for persisting variable data
- asCArray<int> usedTypeIds;
- asCArray<asCTypeInfo*> usedTypes;
- asCArray<asCScriptFunction*> usedFunctions;
- asCArray<void*> usedGlobalProperties;
- asCArray<void*> usedStringConstants;
- asCArray<asCScriptFunction*> savedFunctions;
- asCArray<asCDataType> savedDataTypes;
- asCArray<asCString> savedStrings;
- asCArray<int> adjustByPos;
- asCArray<int> adjustNegativeStackByPos;
- struct SObjProp
- {
- asCObjectType *objType;
- asCObjectProperty *prop;
- };
- asCArray<SObjProp> usedObjectProperties;
- asCMap<void*,bool> existingShared;
- asCMap<asCScriptFunction*,bool> dontTranslate;
- // Helper class for adjusting offsets within initialization list buffers
- struct SListAdjuster
- {
- SListAdjuster(asCReader *rd, asDWORD *bc, asCObjectType *ot);
- void AdjustAllocMem();
- int AdjustOffset(int offset);
- void SetRepeatCount(asUINT rc);
- void SetNextType(int typeId);
- struct SInfo
- {
- asUINT repeatCount;
- asSListPatternNode *startNode;
- };
- asCArray<SInfo> stack;
- asCReader *reader;
- asDWORD *allocMemBC;
- asUINT maxOffset;
- asCObjectType *patternType;
- asUINT repeatCount;
- int lastOffset;
- int nextOffset;
- asUINT lastAdjustedOffset;
- asSListPatternNode *patternNode;
- int nextTypeId;
- };
- asCArray<SListAdjuster*> listAdjusters;
- };
- #ifndef AS_NO_COMPILER
- class asCWriter
- {
- public:
- asCWriter(asCModule *module, asIBinaryStream *stream, asCScriptEngine *engine, bool stripDebugInfo);
- int Write();
- protected:
- asCModule *module;
- asIBinaryStream *stream;
- asCScriptEngine *engine;
- bool stripDebugInfo;
- bool error;
- asUINT bytesWritten;
- int Error(const char *msg);
- int WriteData(const void *data, asUINT size);
- void WriteString(asCString *str);
- void WriteFunction(asCScriptFunction *func);
- void WriteFunctionSignature(asCScriptFunction *func);
- void WriteGlobalProperty(asCGlobalProperty *prop);
- void WriteObjectProperty(asCObjectProperty *prop);
- void WriteDataType(const asCDataType *dt);
- void WriteTypeInfo(asCTypeInfo *ot);
- void WriteTypeDeclaration(asCTypeInfo *ot, int phase);
- void WriteByteCode(asCScriptFunction *func);
- void WriteEncodedInt64(asINT64 i);
- // Helper functions for storing variable data
- int FindTypeInfoIdx(asCTypeInfo *ti);
- int FindTypeIdIdx(int typeId);
- int FindFunctionIndex(asCScriptFunction *func);
- int FindGlobalPropPtrIndex(void *);
- int FindStringConstantIndex(void *str);
- int FindObjectPropIndex(short offset, int typeId, asDWORD *bc);
- void CalculateAdjustmentByPos(asCScriptFunction *func);
- int AdjustStackPosition(int pos);
- int AdjustProgramPosition(int pos);
- int AdjustGetOffset(int offset, asCScriptFunction *func, asDWORD programPos);
- // Intermediate data used for storing that which isn't constant, function id's, pointers, etc
- void WriteUsedTypeIds();
- void WriteUsedFunctions();
- void WriteUsedGlobalProps();
- void WriteUsedStringConstants();
- void WriteUsedObjectProps();
- // Temporary storage for persisting variable data
- asCArray<int> usedTypeIds;
- asCArray<asCTypeInfo*> usedTypes;
- asCArray<asCScriptFunction*> usedFunctions;
- asCArray<void*> usedGlobalProperties;
- asCArray<void*> usedStringConstants;
- asCMap<void*, int> stringToIndexMap;
- asCArray<asCScriptFunction*> savedFunctions;
- asCArray<asCDataType> savedDataTypes;
- asCArray<asCString> savedStrings;
- asCMap<asCString, int> stringToIdMap;
- asCArray<int> adjustStackByPos;
- asCArray<int> adjustNegativeStackByPos;
- asCArray<int> bytecodeNbrByPos;
- struct SObjProp
- {
- asCObjectType *objType;
- asCObjectProperty *prop;
- };
- asCArray<SObjProp> usedObjectProperties;
- // Helper class for adjusting offsets within initialization list buffers
- struct SListAdjuster
- {
- SListAdjuster(asCObjectType *ot);
- int AdjustOffset(int offset, asCObjectType *listPatternType);
- void SetRepeatCount(asUINT rc);
- void SetNextType(int typeId);
- struct SInfo
- {
- asUINT repeatCount;
- asSListPatternNode *startNode;
- };
- asCArray<SInfo> stack;
- asCObjectType *patternType;
- asUINT repeatCount;
- asSListPatternNode *patternNode;
- asUINT entries;
- int lastOffset; // Last offset adjusted
- int nextOffset; // next expected offset to be adjusted
- int nextTypeId;
- };
- asCArray<SListAdjuster*> listAdjusters;
- };
- #endif
- END_AS_NAMESPACE
- #endif // AS_RESTORE_H
|