ScummVM API documentation
as_bytecode.h
1 /*
2  AngelCode Scripting Library
3  Copyright (c) 2003-2018 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_bytecode.h
34 //
35 // A class for constructing the final byte code
36 //
37 
38 
39 
40 #ifndef AS_BYTECODE_H
41 #define AS_BYTECODE_H
42 
43 #include "as_config.h"
44 
45 #ifndef AS_NO_COMPILER
46 
47 #include "as_memory.h"
48 
49 BEGIN_AS_NAMESPACE
50 
51 #define BYTECODE_SIZE 4
52 #define MAX_DATA_SIZE 8
53 #define MAX_INSTR_SIZE (BYTECODE_SIZE+MAX_DATA_SIZE)
54 
55 class asCScriptEngine;
56 class asCScriptFunction;
57 class asCByteInstruction;
58 
59 class asCByteCode {
60 public:
62  ~asCByteCode();
63 
64  void ClearAll();
65 
66  int GetSize();
67 
68  void Finalize(const asCArray<int> &tempVariableOffsets);
69 
70  void Optimize();
71  void OptimizeLocally(const asCArray<int> &tempVariableOffsets);
72  void ExtractLineNumbers();
73  void ExtractObjectVariableInfo(asCScriptFunction *outFunc);
74  void ExtractTryCatchInfo(asCScriptFunction *outFunc);
75  int ResolveJumpAddresses();
76  int FindLabel(int label, asCByteInstruction *from, asCByteInstruction **dest, int *positionDelta);
77 
78  void AddPath(asCArray<asCByteInstruction *> &paths, asCByteInstruction *instr, int stackSize);
79 
80  void Output(asDWORD *array);
81  void AddCode(asCByteCode *bc);
82 
83  void PostProcess();
84 
85 #ifdef AS_DEBUG
86  void DebugOutput(const char *name, asCScriptFunction *func);
87 #endif
88 
89  int GetLastInstr();
90  int RemoveLastInstr();
91  asDWORD GetLastInstrValueDW();
92 
93  void InsertIfNotExists(asCArray<int> &vars, int var);
94  void GetVarsUsed(asCArray<int> &vars);
95  bool IsVarUsed(int offset);
96  void ExchangeVar(int oldOffset, int newOffset);
97  bool IsSimpleExpression();
98 
99  void Label(short label);
100  void Line(int line, int column, int scriptIdx);
101  void ObjInfo(int offset, int info);
102  void Block(bool start);
103  void TryBlock(short catchLabel);
104 
105  void VarDecl(int varDeclIdx);
106  void Call(asEBCInstr bc, int funcID, int pop);
107  void CallPtr(asEBCInstr bc, int funcPtrVar, int pop);
108  void Alloc(asEBCInstr bc, void *objID, int funcID, int pop);
109  void Ret(int pop);
110  void JmpP(int var, asDWORD max);
111 
112  int InsertFirstInstrDWORD(asEBCInstr bc, asDWORD param);
113  int InsertFirstInstrQWORD(asEBCInstr bc, asQWORD param);
114  int Instr(asEBCInstr bc);
115  int InstrQWORD(asEBCInstr bc, asQWORD param);
116  int InstrDOUBLE(asEBCInstr bc, double param);
117  int InstrPTR(asEBCInstr bc, void *param);
118  int InstrDWORD(asEBCInstr bc, asDWORD param);
119  int InstrWORD(asEBCInstr bc, asWORD param);
120  int InstrSHORT(asEBCInstr bc, short param);
121  int InstrFLOAT(asEBCInstr bc, float param);
122  int InstrINT(asEBCInstr bc, int param);
123  int InstrW_W_W(asEBCInstr bc, int a, int b, int c);
124  int InstrSHORT_B(asEBCInstr bc, short a, asBYTE b);
125  int InstrSHORT_W(asEBCInstr bc, short a, asWORD b);
126  int InstrSHORT_DW(asEBCInstr bc, short a, asDWORD b);
127  int InstrSHORT_QW(asEBCInstr bc, short a, asQWORD b);
128  int InstrW_DW(asEBCInstr bc, asWORD a, asDWORD b);
129  int InstrW_QW(asEBCInstr bc, asWORD a, asQWORD b);
130  int InstrW_PTR(asEBCInstr bc, short a, void *param);
131  int InstrW_FLOAT(asEBCInstr bc, asWORD a, float b);
132  int InstrW_W(asEBCInstr bc, int w, int b);
133  int InstrSHORT_DW_DW(asEBCInstr bc, short a, asDWORD b, asDWORD c);
134 
135  asCScriptEngine *GetEngine() const {
136  return engine;
137  };
138 
139  asCArray<int> lineNumbers;
140  asCArray<int> sectionIdxs;
141  int largestStackUsed;
142 
143 protected:
144  // Assignments are not allowed
145  void operator=(const asCByteCode &) {}
146 
147  // Helpers for Optimize
148  bool CanBeSwapped(asCByteInstruction *curr);
149  asCByteInstruction *ChangeFirstDeleteNext(asCByteInstruction *curr, asEBCInstr bc);
150  asCByteInstruction *DeleteFirstChangeNext(asCByteInstruction *curr, asEBCInstr bc);
151  asCByteInstruction *DeleteInstruction(asCByteInstruction *instr);
152  void RemoveInstruction(asCByteInstruction *instr);
153  asCByteInstruction *GoBack(asCByteInstruction *curr);
154  asCByteInstruction *GoForward(asCByteInstruction *curr);
155  void InsertBefore(asCByteInstruction *before, asCByteInstruction *instr);
156  bool RemoveUnusedValue(asCByteInstruction *curr, asCByteInstruction **next);
157  bool IsTemporary(int offset);
158  bool IsTempRegUsed(asCByteInstruction *curr);
159  bool IsTempVarRead(asCByteInstruction *curr, int offset);
160  bool PostponeInitOfTemp(asCByteInstruction *curr, asCByteInstruction **next);
161  bool IsTempVarReadByInstr(asCByteInstruction *curr, int var);
162  bool IsTempVarOverwrittenByInstr(asCByteInstruction *curr, int var);
163  bool IsInstrJmpOrLabel(asCByteInstruction *curr);
164 
165  int AddInstruction();
166  int AddInstructionFirst();
167 
168  asCByteInstruction *first;
169  asCByteInstruction *last;
170 
171  const asCArray<int> *temporaryVariables;
172 
173  asCScriptEngine *engine;
174 };
175 
177 public:
179 
180  void AddAfter(asCByteInstruction *nextCode);
181  void AddBefore(asCByteInstruction *nextCode);
182  void Remove();
183 
184  int GetSize();
185  int GetStackIncrease();
186 
187  asCByteInstruction *next;
188  asCByteInstruction *prev;
189 
190  asEBCInstr op;
191  asQWORD arg;
192  short wArg[3];
193  int size;
194  int stackInc;
195 
196  // Testing
197  bool marked;
198  int stackSize;
199 };
200 
201 END_AS_NAMESPACE
202 
203 #endif // AS_NO_COMPILER
204 
205 #endif
Definition: as_bytecode.h:59
Definition: as_scriptfunction.h:146
Definition: as_bytecode.h:176
Definition: as_scriptengine.h:64
Definition: label.h:25