ScummVM API documentation
cc_instance.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef AGS_ENGINE_SCRIPT_CC_INSTANCE_H
23 #define AGS_ENGINE_SCRIPT_CC_INSTANCE_H
24 
25 #include "ags/lib/std/memory.h"
26 #include "ags/lib/std/map.h"
27 #include "ags/engine/ac/timer.h"
28 #include "ags/shared/script/cc_internal.h"
29 #include "ags/shared/script/cc_script.h" // ccScript
30 #include "ags/engine/script/non_blocking_script_function.h"
31 #include "ags/shared/util/string.h"
32 
33 namespace AGS3 {
34 
35 using namespace AGS;
36 
37 #define INSTF_SHAREDATA 1
38 #define INSTF_ABORTED 2
39 #define INSTF_FREE 4
40 #define INSTF_RUNNING 8 // set by main code to confirm script isn't stuck
41 #define CC_STACK_SIZE 250
42 #define CC_STACK_DATA_SIZE (1000 * sizeof(int32_t))
43 #define MAX_CALL_STACK 100
44 
45 // 256 because we use 8 bits to hold instance number
46 #define MAX_LOADED_INSTANCES 256
47 
48 #define INSTANCE_ID_SHIFT 24LL
49 #define INSTANCE_ID_MASK 0x00000000000000ffLL
50 #define INSTANCE_ID_REMOVEMASK 0x0000000000ffffffLL
51 
54  Code = 0;
55  InstanceId = 0;
56  }
57 
58  int32_t Code;
59  int32_t InstanceId;
60 };
61 
63  ScriptOperation() {
64  ArgCount = 0;
65  }
66 
67  ScriptInstruction Instruction;
68  RuntimeScriptValue Args[MAX_SCMD_ARGS];
69  int ArgCount;
70 };
71 
73  ScriptVariable() {
74  ScAddress = -1; // address = 0 is valid one, -1 means undefined
75  }
76 
77  int32_t ScAddress; // original 32-bit relative data address, written in compiled script;
78  // if we are to use Map or HashMap, this could be used as Key
79  RuntimeScriptValue RValue;
80 };
81 
82 struct FunctionCallStack;
83 
86  : Line(0) {
87  }
88 
89  ScriptPosition(const Shared::String &section, int32_t line)
90  : Section(section)
91  , Line(line) {
92  }
93 
94  Shared::String Section;
95  int32_t Line;
96 };
97 
98 // Running instance of the script
99 struct ccInstance {
100 public:
103 public:
104  int32_t flags;
105  PScVarMap globalvars;
106  char *globaldata;
107  int32_t globaldatasize;
108  // Executed byte-code. Unlike ccScript's code array which is int32_t, the one
109  // in ccInstance must be intptr_t to accommodate real pointers placed after
110  // performing fixups.
111  intptr_t *code;
112  ccInstance *runningInst; // might point to another instance if in far call
113  int32_t codesize;
114  char *strings;
115  int32_t stringssize;
116  RuntimeScriptValue *exports;
117  RuntimeScriptValue *stack;
118  int num_stackentries;
119  // An array for keeping stack data; stack entries reference unknown data from here
120  // TODO: probably change to dynamic array later
121  char *stackdata; // for storing stack data of unknown type
122  char *stackdata_ptr;// works similar to original stack pointer, points to the next unused byte in stack data array
123  int32_t stackdatasize; // conventional size of stack data in bytes
124  //
125  RuntimeScriptValue registers[CC_NUM_REGISTERS];
126  int32_t pc; // program counter
127  int32_t line_number; // source code line number
128  PScript instanceof;
129  int loadedInstanceId;
130  int returnValue;
131 
132  int callStackSize;
133  int32_t callStackLineNumber[MAX_CALL_STACK];
134  int32_t callStackAddr[MAX_CALL_STACK];
135  ccInstance *callStackCodeInst[MAX_CALL_STACK];
136 
137  // array of real import indexes used in script
138  uint32_t *resolved_imports;
139  int numimports;
140 
141  char *code_fixups;
142 
143  // returns the currently executing instance, or NULL if none
144  static ccInstance *GetCurrentInstance(void);
145  // clears recorded stack of current instances
146  // FIXME: reimplement this in a safer way, this must be done automatically
147  // when destroying all script instances, e.g. on game quit.
148  static void FreeInstanceStack();
149  // create a runnable instance of the supplied script
150  static ccInstance *CreateFromScript(PScript script);
151  static ccInstance *CreateEx(PScript scri, ccInstance *joined);
152  static void SetExecTimeout(unsigned sys_poll_ms, unsigned abort_ms, unsigned abort_loops);
153 
154  ccInstance();
155  ~ccInstance();
156  // Create a runnable instance of the same script, sharing global memory
157  ccInstance *Fork();
158  // Specifies that when the current function returns to the script, it
159  // will stop and return from CallInstance
160  void Abort();
161  // Aborts instance, then frees the memory later when it is done with
162  void AbortAndDestroy();
163 
164  // Call an exported function in the script
165  int CallScriptFunction(const char *funcname, int32_t num_params, const RuntimeScriptValue *params);
166 
167  // Get the script's execution position and callstack as human-readable text
168  Shared::String GetCallStack(int max_lines = INT_MAX) const;
169  // Get the script's execution position
170  void GetScriptPosition(ScriptPosition &script_pos) const;
171  // Get the address of an exported symbol (function or variable) in the script
172  RuntimeScriptValue GetSymbolAddress(const char *symname) const;
173  void DumpInstruction(const ScriptOperation &op) const;
174  // Tells whether this instance is in the process of executing the byte-code
175  bool IsBeingRun() const;
176  // Notifies that the game was being updated (script not hanging)
177  void NotifyAlive();
178 
179  // For each import, find the instance that corresponds to it and save it
180  // in resolved_imports[]. Return whether the function is successful
181  bool ResolveScriptImports(const ccScript *scri);
182 
183  // Using resolved_imports[], resolve the IMPORT fixups
184  // Also change CALLEXT op-codes to CALLAS when they pertain to a script instance
185  bool ResolveImportFixups(const ccScript *scri);
186 
187 private:
188  bool _Create(PScript scri, ccInstance *joined);
189  // free the memory associated with the instance
190  void Free();
191 
192  bool CreateGlobalVars(const ccScript *scri);
193  bool AddGlobalVar(const ScriptVariable &glvar);
194  ScriptVariable *FindGlobalVar(int32_t var_addr);
195  bool CreateRuntimeCodeFixups(const ccScript *scri);
196  //bool ReadOperation(ScriptOperation &op, int32_t at_pc);
197 
198  // Begin executing script starting from the given bytecode index
199  int Run(int32_t curpc);
200  // Runtime fixups
201  //bool FixupArgument(intptr_t code_value, char fixup_type, RuntimeScriptValue &argument);
202 
203  // Stack processing
204  // Push writes new value and increments stack ptr;
205  // stack ptr now points to the __next empty__ entry
206  void PushValueToStack(const RuntimeScriptValue &rval);
207  void PushDataToStack(int32_t num_bytes);
208  // Pop decrements stack ptr, returns last stored value and invalidates! stack tail;
209  // stack ptr now points to the __next empty__ entry
210  RuntimeScriptValue PopValueFromStack();
211  // helper function to pop & dump several values
212  void PopValuesFromStack(int32_t num_entries);
213  void PopDataFromStack(int32_t num_bytes);
214  // Return stack ptr at given offset from stack head;
215  // Offset is in data bytes; program stack ptr is __not__ changed
216  RuntimeScriptValue GetStackPtrOffsetFw(int32_t fw_offset);
217  // Return stack ptr at given offset from stack tail;
218  // Offset is in data bytes; program stack ptr is __not__ changed
219  RuntimeScriptValue GetStackPtrOffsetRw(int32_t rw_offset);
220 
221  // Function call stack processing
222  void PushToFuncCallStack(FunctionCallStack &func_callstack, const RuntimeScriptValue &rval);
223  void PopFromFuncCallStack(FunctionCallStack &func_callstack, int32_t num_entries);
224 
225  // Last time the script was noted of being "alive"
226  AGS_Clock::time_point _lastAliveTs;
227 };
228 
229 extern void script_commands_init();
230 extern void script_commands_free();
231 
232 } // namespace AGS3
233 
234 #endif
Definition: achievements_tables.h:27
Definition: runtime_script_value.h:65
Definition: cc_instance.h:84
Definition: cc_instance.h:52
Definition: cc_instance.h:62
Definition: geometry.h:114
Definition: string.h:62
Definition: map.h:196
Definition: cc_script.h:37
Definition: ptr.h:159
Definition: cc_instance.h:99
Definition: ags.h:40
Definition: cc_instance.h:72