This repository has been archived by the owner on Apr 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 119
/
compiler.h
342 lines (292 loc) · 10.4 KB
/
compiler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#pragma once
#include "data.h"
#include "message.h"
#include "message_script.h"
#include "s_classtab.h"
#include "s_compress.h"
#include "s_deftab.h"
#include "s_eventmsg.h"
#include "s_eventtab.h"
#include "s_functab.h"
#include "s_postevents.h"
#include "s_stack.h"
#include "s_vartab.h"
#include "script_libriary.h"
#include "string_codec.h"
#include "strings_list.h"
#include "tclass_list.h"
#include "token.h"
#define COMPILER_LOG_FILENAME u8"compile.log"
#define COMPILER_ERRORLOG_FILENAME u8"error.log"
#define BCODE_BUFFER_BLOCKSIZE 4096
#define IOBUFFER_SIZE 65535
struct SEGMENT_DESC
{
const char *name;
uint32_t offset;
uint32_t size;
uint32_t id;
uint32_t lines;
bool bUnload;
//--------------
char *pData;
char *pCode;
uint32_t BCode_Program_size;
uint32_t BCode_Buffer_size;
STRINGS_LIST *Files_list;
};
struct OFFSET_INFO
{
uint32_t offset;
uint32_t segment_id;
};
struct EXTDATA_HEADER
{
char sFileInfo[32];
uint32_t dwExtDataOffset;
uint32_t dwExtDataSize;
};
/*struct DEFINE_INFO
{
uint32_t deftype;
uint32_t data4b;
};*/
struct DOUBLE_DWORD
{
uint32_t dw1;
uint32_t dw2;
};
class SLIBHOLDER
{
public:
HINSTANCE hInst;
SCRIPT_LIBRIARY *pLib;
char *pName;
SLIBHOLDER() : hInst(nullptr)
{
pLib = nullptr;
pName = nullptr;
};
~SLIBHOLDER()
{
if (pLib)
delete pLib;
if (pName)
delete pName;
};
void SetName(const char *pFileName)
{
if (pName)
delete pName;
const auto len = strlen(pFileName) + 1;
pName = new char[len];
memcpy(pName, pFileName, len);
}
};
class S_DEBUG;
enum COMPILER_STAGE
{
CS_SYSTEM,
CS_COMPILATION,
CS_RUNTIME
};
class CORE;
class COMPILER : public VIRTUAL_COMPILER
{
friend CORE;
friend S_DEBUG;
COMPILER_STAGE CompilerStage;
STRINGS_LIST LabelTable;
// STRINGS_LIST EventTable;
STRINGS_LIST LabelUpdateTable;
TOKEN Token;
MESSAGE *pEventMessage;
std::vector<SEGMENT_DESC> SegmentTable;
uint32_t SegmentsNum;
uint32_t RunningSegmentID;
uint32_t InstructionPointer;
char *pBuffer;
uint32_t dwCurPointer, dwMaxSize;
char *ProgramDirectory;
bool bCompleted;
bool bEntityUpdate;
char *pDebExpBuffer;
uint32_t nDebExpBufferSize;
FuncInfo *pRun_fi; // running function info
FuncTable FuncTab;
VarTable VarTab;
S_CLASSTAB ClassTab;
S_DEFTAB DefTab;
S_STACK SStack;
S_EVENTTAB EventTab;
// TCLASS_LIST<S_EVENTMSG> EventMsg;
POSTEVENTS_LIST EventMsg;
TCLASS_LIST<SLIBHOLDER> LibriaryFuncs;
STRING_CODEC SCodec;
bool bRuntimeLog;
uint32_t nRuntimeLogEventsBufferSize;
uint32_t nRuntimeLogEventsNum;
std::vector<uint32_t> pRuntimeLogEvent;
uint32_t nRuntimeTicks;
bool bFirstRun;
bool bScriptTrace;
bool bWriteCodeFile;
bool bDebugInfo;
char DebugSourceFileName[MAX_PATH];
char gs[MAX_PATH];
uint32_t DebugSourceLine;
char *pCompileTokenTempBuffer;
// HANDLE hSaveFileFileHandle;
bool bDebugExpressionRun;
bool bTraceMode;
bool bEventsBreak;
char DebugTraceFileName[MAX_PATH];
uint32_t nDebugTraceLineCode;
uint32_t nIOBufferSize;
uint32_t nIOFullSize;
char *pIOBuffer;
// script registers
DATA rAX;
DATA rBX;
DATA ExpressionResult;
ATTRIBUTES *rAP;
public:
std::shared_ptr<spdlog::logger> tracelog;
std::shared_ptr<spdlog::logger> errorlog;
std::shared_ptr<spdlog::logger> warninglog;
bool bBreakOnError;
COMPRESS Compress;
COMPILER();
~COMPILER();
VSTRING_CODEC *GetVSC()
{
return &SCodec;
}
char *LoadFile(const char *file_name, uint32_t &file_size, bool bFullPath = false);
// char * AppendProgram(char * base_program, long base_program_size, char * append_program, long
// append_program_size, long& new_program_size);
bool AppendProgram(char *&pBase_program, uint32_t &Base_program_size, const char *pAppend_program,
uint32_t &Append_program_size, bool bAddLinefeed);
void Trace(const char *data_PTR, ...);
void DTrace(const char *data_PTR, ...);
void SetError(const char *data_PTR, ...);
void SetWarning(const char *data_PTR, ...);
bool CreateProgram(const char *file_name);
bool AddProgramFile(const char *file_name);
void UnloadSegment(const char *segment_name);
uint32_t GetSegmentIndex(uint32_t segment_id);
void ProcessFrame(uint32_t DeltaTime);
bool Run();
void Release();
void SetProgramDirectory(const char *dir_name);
VDATA *ProcessEvent(const char *event_name, MESSAGE message);
VDATA *ProcessEvent(const char *event_name);
void SetEventHandler(const char *event_name, const char *func_name, long flag, bool bStatic = false);
void DelEventHandler(const char *event_name, const char *func_name);
bool Completed()
{
return bCompleted;
}
char *GetName();
void ExitProgram();
void ClearEvents();
uint32_t GetIntFunctionCode(const char *func_name);
DATA *BC_CallIntFunction(uint32_t func_code, DATA *&pVResult, uint32_t arguments);
uint32_t GetIntFunctionsNum();
bool InitInternalFunctions();
bool ProcessDebugExpression(const char *pExpression, DATA &Result);
bool SetOnDebugExpression(const char *pLValue, const char *pRValue, DATA &Result);
bool ProcessDebugExpression0(const char *pExpression, DATA &Result);
bool Compile(SEGMENT_DESC &Segment, char *pInternalCode = nullptr, uint32_t pInternalCodeSize = 0);
void CompileToken(SEGMENT_DESC &Segment, S_TOKEN_TYPE Token_type, uint32_t data_blocks_num = 0, ...);
void ResizeBCodeBuffer(SEGMENT_DESC &Segment, uint32_t add_size);
bool BC_LoadSegment(const char *file_name);
bool BC_SegmentIsLoaded(const char *file_name);
bool BC_Execute(uint32_t function_code, DATA *&pVReturnResult, const char *pDbgExpSource = nullptr);
bool BC_CallFunction(uint32_t func_code, uint32_t &ip, DATA *&pVResult);
void FindErrorSource();
S_TOKEN_TYPE BC_TokenGet(uint32_t &ip, uint32_t &token_data_size);
S_TOKEN_TYPE BC_TokenGet();
S_TOKEN_TYPE TokenLastReadResult;
uint32_t TLR_DataOffset;
const char *pRunCodeBase;
bool TokenIs(S_TOKEN_TYPE test);
S_TOKEN_TYPE TokenType()
{
return TokenLastReadResult;
}
S_TOKEN_TYPE NextTokenType();
S_TOKEN_TYPE SkipAuxiliaryTokens();
bool BC_ProcessExpression(DATA *value);
void BC_ProcessExpression_L0(DATA *value);
void BC_ProcessExpression_L1(DATA *value, bool bSkip = false);
void BC_ProcessExpression_L2(DATA *value, bool bSkip = false);
void BC_ProcessExpression_L3(DATA *value, bool bSkip = false);
void BC_ProcessExpression_L4(DATA *value, bool bSkip = false);
void BC_ProcessExpression_L5(DATA *value, bool bSkip = false);
void BC_ProcessExpression_L6(DATA *value, bool bSkip = false);
void BC_ProcessExpression_L7(DATA *value, bool bSkip = false);
/* void SkipExpression_L1();
void SkipExpression_L2();
void SkipExpression_L3();
void SkipExpression_L4();
void SkipExpression_L5();
void SkipExpression_L6();
void SkipExpression_L7();
*/
//-----------------------------------------------------
uint32_t CurrentFuncCode;
void CompileNumber(SEGMENT_DESC &Segment);
void CompileFloatNumber(SEGMENT_DESC &Segment);
void CompileString(SEGMENT_DESC &Segment);
bool CompileUnknown(SEGMENT_DESC &Segment);
bool CompileBlock(SEGMENT_DESC &Segment, bool &bFunctionBlock, uint32_t &inout, S_TOKEN_TYPE bound_type,
uint32_t continue_jump, uint32_t break_offset, STRINGS_LIST &BreakTable);
S_TOKEN_TYPE CompileAuxiliaryTokens(SEGMENT_DESC &Segment); //, bool & bFunctionBlock, DWORD & inout);
bool BC_Jump(SEGMENT_DESC &Segment, uint32_t offset);
void UpdateOffsets(SEGMENT_DESC &Segment, STRINGS_LIST &list, uint32_t offset, const char *sname = nullptr);
S_TOKEN_TYPE DetectUnknown(uint32_t &code);
void DumpAttributes(ATTRIBUTES *pA, long level);
bool IsIntFuncVarArgsNum(uint32_t code);
uint32_t GetInternalFunctionArgumentsNum(uint32_t code);
bool CreateMessage(MESSAGE_SCRIPT *pMs, uint32_t stack_offset, uint32_t vindex, bool s2s = false);
void ProcessEvent(const char *event_name, MESSAGE *pMs);
bool SaveState(std::fstream &fileS);
bool LoadState(std::fstream &fileS);
bool OnLoad();
void SaveDataDebug(char *data_PTR, ...);
void SaveData(const void *data_PTR, uint32_t data_size);
bool ReadData(void *data_PTR, uint32_t data_size);
void SaveString(const char *pS);
char *ReadString();
void SaveVariable(DATA *pV, bool bdim = false);
bool ReadVariable(char *name, /*DWORD code,*/ bool bdim = false, uint32_t a_index = 0);
bool FindReferencedVariable(DATA *pRef, uint32_t &var_index, uint32_t &array_index);
bool FindReferencedVariableByRootA(ATTRIBUTES *pA, uint32_t &var_index, uint32_t &array_index);
ATTRIBUTES *TraceARoot(ATTRIBUTES *pA, const char *&pAccess);
void SaveAttributesData(ATTRIBUTES *pRoot);
void ReadAttributesData(ATTRIBUTES *pRoot, ATTRIBUTES *pParent);
void WriteVDword(uint32_t v);
uint32_t ReadVDword();
void AddPostEvent(S_EVENTMSG *pEM);
void LoadPreprocess();
void CopyOffsets(SEGMENT_DESC &Segment, STRINGS_LIST &srclist, STRINGS_LIST &dstlist, const char *sname);
void FormatDialog(char *file_name);
void FormatAllDialog(const char *directory_name);
// bool SetSaveData(const char * file_name, const char * save_data);
// bool GetSaveData(const char * file_name, DATA * pV);
bool SetSaveData(const char *file_name, void *save_data, long data_size);
void *GetSaveData(const char *file_name, long &data_size);
void AddRuntimeEvent();
uint32_t SetScriptFunction(IFUNCINFO *pFuncInfo);
bool CompileExpression(SEGMENT_DESC &Segment);
bool CompileExpression_L0(SEGMENT_DESC &Segment);
bool CompileExpression_L1(SEGMENT_DESC &Segment);
bool CompileExpression_L2(SEGMENT_DESC &Segment);
bool CompileExpression_L3(SEGMENT_DESC &Segment);
bool CompileExpression_L4(SEGMENT_DESC &Segment);
bool CompileExpression_L5(SEGMENT_DESC &Segment);
bool CompileExpression_L6(SEGMENT_DESC &Segment);
bool CompileExpression_L7(SEGMENT_DESC &Segment);
DATA *GetOperand(const char *pCodeBase, uint32_t &ip, S_TOKEN_TYPE *pTokenType = nullptr);
};