Pol  Revision:4b29d2b
executor.h
Go to the documentation of this file.
1 
8 #ifndef __EXECUTOR_H
9 #define __EXECUTOR_H
10 
11 
12 #include "pol_global_config.h"
13 
14 #ifndef __EXECTYPE_H
15 #include "exectype.h"
16 #endif
17 
18 #ifndef BSCRIPT_BOBJECT_H
19 #include "bobject.h"
20 #endif
21 
22 #include <exception>
23 #include <memory>
24 #include <set>
25 #include <stack>
26 #include <string>
27 #include <vector>
28 
29 #include <format/format.h>
30 #include "../clib/refptr.h"
31 #include "../clib/spinlock.h"
32 #include "bobject.h"
33 #include "eprog.h"
34 #include "executortype.h"
35 
36 
37 namespace Pol
38 {
39 namespace Core
40 {
41 class UOExecutor;
42 
43 void list_script( UOExecutor* uoexec );
44 }
45 namespace Bscript
46 {
47 class Executor;
48 class ExecutorModule;
49 class ModuleFunction;
50 class String;
51 class Token;
52 #ifdef ESCRIPT_PROFILE
53 #include <map>
54 
55 struct profile_instr
56 {
57  unsigned long sum;
58  unsigned long max;
59  unsigned long min;
60  unsigned long count;
61 };
62 typedef std::map<std::string, profile_instr> escript_profile_map;
63 extern escript_profile_map EscriptProfileMap;
64 #endif
65 
66 typedef std::vector<BObjectRef> ValueStackCont;
67 // FIXME: how to make this a nested struct in Executor?
69 {
70  unsigned PC;
71  unsigned ValueStackDepth;
72 };
73 
75 {
76  std::unique_ptr<BObjectRefVec> Locals;
77  ValueStackCont ValueStack;
78  unsigned PC;
79 };
80 
81 class Executor
82 {
83 public:
85  virtual size_t sizeEstimate() const;
86 
87  friend void Core::list_script( Core::UOExecutor* uoexec );
88  int done;
89  void seterror( bool err );
90  bool error() const;
91  bool error_;
92  bool halt_;
93  bool run_ok_;
94 
96  {
99  INSTRUCTIONS
100  };
102  unsigned PC; // program counter
103 
104  bool AttachFunctionalityModules();
105 
106 
107  bool setProgram( EScriptProgram* prog );
108 
110 
111  std::vector<BObjectRefVec*> upperLocals2;
112 
113  std::vector<ReturnContext> ControlStack;
114 
116 
118 
119 
120  ValueStackCont ValueStack;
121 
122  static ExecInstrFunc GetInstrFunc( const Token& token );
123 
124  /*
125  These must both be deleted. instr references _symbols, so it should be deleted first.
126  FIXME: there should be a separate object, called EProgram or something,
127  that owns both the instructions and the symbols. It should be ref_counted,
128  so a code repository can store programs that multiple Executors use.
129  That means debugger stuff has to come out of Instruction.
130  */
131  unsigned nLines;
132 
133  std::vector<BObjectRef> fparams;
134 
135  friend class ExecutorModule;
136  void setFunctionResult( BObjectImp* imp );
137 
138 protected:
139  int getParams( unsigned howMany );
140  void cleanParams();
141 
142 public:
143  int makeString( unsigned param );
144  bool hasParams( unsigned howmany ) const { return ( fparams.size() >= howmany ); }
145  size_t numParams() const { return fparams.size(); }
146  BObjectImp* getParamImp( unsigned param );
147  BObjectImp* getParamImp( unsigned param, BObjectImp::BObjectType type );
148  BObjectImp* getParamImp2( unsigned param, BObjectImp::BObjectType type );
149  BObject* getParamObj( unsigned param );
150 
151  const String* getStringParam( unsigned param );
152  const BLong* getLongParam( unsigned param );
153 
154  bool getStringParam( unsigned param, const String*& pstr );
155  bool getParam( unsigned param, int& value );
156  bool getParam( unsigned param, int& value, int maxval );
157  bool getParam( unsigned param, int& value, int minval, int maxval );
158  bool getRealParam( unsigned param, double& value );
159  bool getObjArrayParam( unsigned param, ObjArray*& pobjarr );
160 
161  bool getParam( unsigned param, unsigned& value );
162 
163  bool getParam( unsigned param, unsigned short& value );
164  bool getParam( unsigned param, unsigned short& value, unsigned short maxval );
165  bool getParam( unsigned param, unsigned short& value, unsigned short minval,
166  unsigned short maxval );
167 
168  bool getParam( unsigned param, short& value );
169  bool getParam( unsigned param, short& value, short maxval );
170  bool getParam( unsigned param, short& value, short minval, short maxval );
171 
172  void* getApplicPtrParam( unsigned param, const BApplicObjType* pointer_type );
173  BApplicObjBase* getApplicObjParam( unsigned param, const BApplicObjType* object_type );
174 
175 
176  const char* paramAsString( unsigned param );
177  double paramAsDouble( unsigned param );
178  int paramAsLong( unsigned param );
179 
180 protected:
181  int makeDouble( unsigned param );
182 
183 
184  BObject* getParam( unsigned param );
185 
186  BObject getValue( void );
187  BObjectRef getObjRef( void );
188 
189 public:
190  int getToken( Token& token, unsigned position );
191  BObjectRef& LocalVar( unsigned int varnum );
192  BObjectRef& GlobalVar( unsigned int varnum );
193  BObject* makeObj( const Token& token );
194  int makeGlobal( const Token& token );
195  void popParam( const Token& token );
196  void popParamByRef( const Token& token );
197  void getArg( const Token& token );
198  void pushArg( BObjectImp* arg );
199  void pushArg( const BObjectRef& ref );
200 
201  static BObjectRef addmember( BObject& left, const BObject& right );
202  static BObjectRef removemember( BObject& left, const BObject& right );
203  static BObjectRef checkmember( BObject& left, const BObject& right );
204  void addmember2( BObject& left, const BObject& right );
205 
206  // execmodules: modules associated with the current program. References modules owned by
207  // availmodules.
208  std::vector<ExecutorModule*> execmodules;
209  std::vector<ExecutorModule*> availmodules; // owns
210 
211 public:
212  Executor();
213  virtual ~Executor();
214 
215  void addModule( ExecutorModule* module ); // NOTE, executor deletes its modules when done
216  ExecutorModule* findModule( const std::string& name );
217 
219  // NOTE: the debugger code expects these to be virtual..
220  void execFunc( const Token& token );
221  void execInstr();
222 
223  void ins_nop( const Instruction& ins );
224  void ins_jmpiftrue( const Instruction& ins );
225  void ins_jmpiffalse( const Instruction& ins );
226  void ins_globalvar( const Instruction& ins );
227  void ins_localvar( const Instruction& ins );
228  void ins_makeLocal( const Instruction& ins );
229  void ins_declareArray( const Instruction& ins );
230  void ins_long( const Instruction& ins );
231  void ins_double( const Instruction& ins );
232  void ins_string( const Instruction& ins );
233  void ins_error( const Instruction& ins );
234  void ins_struct( const Instruction& ins );
235  void ins_array( const Instruction& ins );
236  void ins_dictionary( const Instruction& ins );
237  void ins_uninit( const Instruction& ins );
238  void ins_ident( const Instruction& ins );
239  void ins_unminus( const Instruction& ins );
240 
241  void ins_logical_and( const Instruction& ins );
242  void ins_logical_or( const Instruction& ins );
243  void ins_logical_not( const Instruction& ins );
244 
245  void ins_bitwise_not( const Instruction& ins );
246 
247  void ins_set_member( const Instruction& ins );
248  void ins_set_member_consume( const Instruction& ins );
249  void ins_get_member( const Instruction& ins );
250  void ins_get_member_id( const Instruction& ins ); // test id
251  void ins_set_member_id( const Instruction& ins ); // test id
252  void ins_set_member_id_consume( const Instruction& ins ); // test id
253  void ins_set_member_id_consume_plusequal( const Instruction& ins ); // test id
254  void ins_set_member_id_consume_minusequal( const Instruction& ins ); // test id
255  void ins_set_member_id_consume_timesequal( const Instruction& ins ); // test id
256  void ins_set_member_id_consume_divideequal( const Instruction& ins ); // test id
257  void ins_set_member_id_consume_modulusequal( const Instruction& ins ); // test id
258 
259  void ins_assign_localvar( const Instruction& ins );
260  void ins_assign_globalvar( const Instruction& ins );
261  void ins_assign_consume( const Instruction& ins );
262  void ins_consume( const Instruction& ins );
263  void ins_assign( const Instruction& ins );
264  void ins_array_assign( const Instruction& ins );
265  void ins_array_assign_consume( const Instruction& ins );
266  void ins_multisubscript_assign( const Instruction& ins );
267  void ins_multisubscript_assign_consume( const Instruction& ins );
268  void ins_multisubscript( const Instruction& ins );
269 
270  void ins_add( const Instruction& ins );
271  void ins_subtract( const Instruction& ins );
272  void ins_mult( const Instruction& ins );
273  void ins_div( const Instruction& ins );
274  void ins_modulus( const Instruction& ins );
275 
276  void ins_insert_into( const Instruction& ins );
277 
278  void ins_plusequal( const Instruction& ins );
279  void ins_minusequal( const Instruction& ins );
280  void ins_timesequal( const Instruction& ins );
281  void ins_divideequal( const Instruction& ins );
282  void ins_modulusequal( const Instruction& ins );
283 
284  void ins_bitshift_right( const Instruction& ins );
285  void ins_bitshift_left( const Instruction& ins );
286  void ins_bitwise_and( const Instruction& ins );
287  void ins_bitwise_xor( const Instruction& ins );
288  void ins_bitwise_or( const Instruction& ins );
289 
290  void ins_equal( const Instruction& ins );
291  void ins_notequal( const Instruction& ins );
292  void ins_lessthan( const Instruction& ins );
293  void ins_lessequal( const Instruction& ins );
294  void ins_greaterthan( const Instruction& ins );
295  void ins_greaterequal( const Instruction& ins );
296 
297  void ins_goto( const Instruction& ins );
298  void ins_arraysubscript( const Instruction& ins );
299  void ins_func( const Instruction& ins );
300  void ins_call_method( const Instruction& ins );
301  void ins_call_method_id( const Instruction& ins );
302  void ins_statementbegin( const Instruction& ins );
303  void ins_progend( const Instruction& ins );
304  void ins_makelocal( const Instruction& ins );
305  void ins_jsr_userfunc( const Instruction& ins );
306  void ins_pop_param( const Instruction& ins );
307  void ins_pop_param_byref( const Instruction& ins );
308  void ins_get_arg( const Instruction& ins );
309  void ins_leave_block( const Instruction& ins );
310  void ins_gosub( const Instruction& ins );
311  void ins_return( const Instruction& ins );
312  void ins_exit( const Instruction& ins );
313 
314  void ins_member( const Instruction& ins );
315  void ins_addmember( const Instruction& ins );
316  void ins_removemember( const Instruction& ins );
317  void ins_checkmember( const Instruction& ins );
318  void ins_dictionary_addmember( const Instruction& ins );
319  void ins_addmember2( const Instruction& ins );
320  void ins_addmember_assign( const Instruction& ins );
321  void ins_in( const Instruction& ins );
322 
323  void ins_initforeach( const Instruction& ins );
324  void ins_stepforeach( const Instruction& ins );
325  void ins_initforeach2( const Instruction& ins );
326  void ins_stepforeach2( const Instruction& ins );
327 
328  void ins_casejmp( const Instruction& ins );
329  void ins_initfor( const Instruction& ins );
330  void ins_nextfor( const Instruction& ins );
331 
332  void ins_funcref( const Instruction& ins );
333 
334  static int ins_casejmp_findlong( const Token& token, BLong* blong );
335  static int ins_casejmp_findstring( const Token& token, String* bstringimp );
336  static int ins_casejmp_finddefault( const Token& token );
337 
338  bool running_to_completion() const;
339  void set_running_to_completion( bool to_completion );
340 
341  bool runnable() const;
342  void calcrunnable();
343 
344  bool halt() const;
345  void sethalt( bool halt );
346 
347  bool debugging() const;
348  void setdebugging( bool debugging );
349 
350  void attach_debugger();
351  void detach_debugger();
352  std::string dbg_get_instruction( size_t atPC ) const;
353  void dbg_ins_trace();
354  void dbg_step_into();
355  void dbg_step_over();
356  void dbg_run();
357  void dbg_break();
358  void dbg_setbp( unsigned atPC );
359  void dbg_clrbp( unsigned atPC );
360  void dbg_clrallbp();
361 
362  bool exec();
363  void reinitExec();
364  void initForFnCall( unsigned in_PC );
365  void show_context( unsigned atPC );
366  void show_context( fmt::Writer& os, unsigned atPC );
367 
368  int getDebugLevel() { return debug_level; }
369  void setDebugLevel( DEBUG_LEVEL level ) { debug_level = level; }
370  void setViewMode( bool vm ) { viewmode_ = vm; }
371  const std::string& scriptname() const;
372  bool empty_scriptname();
373  const EScriptProgram* prog() const;
374 
375 private:
377  bool prog_ok_;
378  bool viewmode_;
379 
381 
384  {
395  DEBUG_STATE_STEPPING_OVER
396  };
398  std::set<unsigned> breakpoints_;
399  std::set<unsigned> tmpbreakpoints_;
400  unsigned bp_skip_;
401 
403 
404 private: // not implemented
405  Executor( const Executor& exec );
406  Executor& operator=( const Executor& exec );
407 #ifdef ESCRIPT_PROFILE
408  unsigned long GetTimeUs();
409  void profile_escript( std::string name, unsigned long profile_start );
410 #endif
411 };
412 
413 inline const std::string& Executor::scriptname() const
414 {
415  return prog_->name;
416 }
417 
419 {
420  return prog_->name.get().empty();
421 }
422 
423 inline const EScriptProgram* Executor::prog() const
424 {
425  return prog_.get();
426 }
427 
428 inline bool Executor::runnable( void ) const
429 {
430  return run_ok_;
431 }
433 {
434  run_ok_ = !error_ && !halt_;
435 }
436 
437 inline void Executor::seterror( bool err )
438 {
439  error_ = err;
440  calcrunnable();
441 }
442 inline bool Executor::error() const
443 {
444  return error_;
445 }
446 
447 inline void Executor::sethalt( bool halt )
448 {
449  halt_ = halt;
450  calcrunnable();
451 }
452 inline bool Executor::halt() const
453 {
454  return halt_;
455 }
456 
457 inline bool Executor::debugging() const
458 {
459  return debugging_;
460 }
461 inline void Executor::setdebugging( bool debugging )
462 {
463  debugging_ = debugging;
464 }
465 
466 
468 {
469  return runs_to_completion_;
470 }
471 inline void Executor::set_running_to_completion( bool to_completion )
472 {
473  runs_to_completion_ = to_completion;
474 }
475 }
476 }
477 #endif
DEBUG_STATE debug_state_
Definition: executor.h:397
DEBUG_LEVEL debug_level
Definition: executor.h:101
BObjectImp * func_result_
Definition: executor.h:402
const EScriptProgram * prog() const
Definition: executor.h:423
std::set< unsigned > breakpoints_
Definition: executor.h:398
ValueStackCont ValueStack
Definition: executor.h:77
void setDebugLevel(DEBUG_LEVEL level)
Definition: executor.h:369
ValueStackCont ValueStack
Definition: executor.h:120
void seterror(bool err)
Definition: executor.h:437
void setdebugging(bool debugging)
Definition: executor.h:461
std::vector< BObjectRef > ValueStackCont
Definition: executor.h:51
std::vector< BObjectRef > BObjectRefVec
Definition: exectype.h:21
bool error() const
Definition: executor.h:442
std::vector< ExecutorModule * > execmodules
Definition: executor.h:208
void(Executor::* ExecInstrFunc)(const Instruction &)
Definition: executortype.h:16
bool debugging() const
Definition: executor.h:457
BObjectRefVec Globals2
Definition: executor.h:109
std::set< unsigned > tmpbreakpoints_
Definition: executor.h:399
Definition: refptr.h:65
std::vector< ReturnContext > ControlStack
Definition: executor.h:113
std::vector< BObjectRef > fparams
Definition: executor.h:133
std::vector< BObjectRefVec * > upperLocals2
Definition: executor.h:111
void sethalt(bool halt)
Definition: executor.h:447
size_t numParams() const
Definition: executor.h:145
ref_ptr< EScriptProgram > prog_
Definition: executor.h:376
static UninitObject * m_SharedUninitObject
Definition: executor.h:117
void list_script(UOExecutor *uoexec)
Definition: scrsched.cpp:718
bool runnable() const
Definition: executor.h:428
bool running_to_completion() const
Definition: executor.h:467
const std::string & scriptname() const
Definition: executor.h:413
std::string name
Definition: osmod.cpp:943
bool hasParams(unsigned howmany) const
Definition: executor.h:144
BObjectRefVec * Locals2
Definition: executor.h:115
void setViewMode(bool vm)
Definition: executor.h:370
static Clib::SpinLock _executor_lock
Definition: executor.h:84
void set_running_to_completion(bool to_completion)
Definition: executor.h:471
std::unique_ptr< BObjectRefVec > Locals
Definition: executor.h:76
Definition: berror.cpp:12
bool halt() const
Definition: executor.h:452
BApplicObj< T > * getApplicObjParam(ExecutorModule &ex, unsigned param, const BApplicObjType *object_type)
Definition: execmodl.h:93
ModuleFunction * current_module_function
Definition: executor.h:218
std::vector< ExecutorModule * > availmodules
Definition: executor.h:209