ScummVM API documentation
sqfuncproto.h
1 /* see copyright notice in squirrel.h */
2 #ifndef _SQFUNCTION_H_
3 #define _SQFUNCTION_H_
4 
5 #include "sqopcodes.h"
6 
7 enum SQOuterType {
8  otLOCAL = 0,
9  otOUTER = 1
10 };
11 
12 struct SQOuterVar
13 {
14 
15  SQOuterVar() : _type(otLOCAL) {}
16  SQOuterVar(const SQObjectPtr &name,const SQObjectPtr &src,SQOuterType t)
17  {
18  _name = name;
19  _src=src;
20  _type=t;
21  }
22  SQOuterType _type;
23  SQObjectPtr _name;
24  SQObjectPtr _src;
25 };
26 
28 {
29  SQLocalVarInfo():_start_op(0),_end_op(0),_pos(0){}
30  SQObjectPtr _name;
31  SQUnsignedInteger _start_op;
32  SQUnsignedInteger _end_op;
33  SQUnsignedInteger _pos;
34 };
35 
36 struct SQLineInfo { SQInteger _line;SQInteger _op; };
37 
41 
42 #define _FUNC_SIZE(ni,nl,nparams,nfuncs,nouters,nlineinf,localinf,defparams) (sizeof(SQFunctionProto) \
43  +((ni-1)*sizeof(SQInstruction))+(nl*sizeof(SQObjectPtr)) \
44  +(nparams*sizeof(SQObjectPtr))+(nfuncs*sizeof(SQObjectPtr)) \
45  +(nouters*sizeof(SQOuterVar))+(nlineinf*sizeof(SQLineInfo)) \
46  +(localinf*sizeof(SQLocalVarInfo))+(defparams*sizeof(SQInteger)))
47 
48 
49 struct SQFunctionProto : public CHAINABLE_OBJ
50 {
51 private:
53  ~SQFunctionProto();
54 
55 public:
56  static SQFunctionProto *Create(SQSharedState *ss,SQInteger ninstructions,
57  SQInteger nliterals,SQInteger nparameters,
58  SQInteger nfunctions,SQInteger noutervalues,
59  SQInteger nlineinfos,SQInteger nlocalvarinfos,SQInteger ndefaultparams)
60  {
61  SQFunctionProto *f;
62  //I compact the whole class and members in a single memory allocation
63  f = (SQFunctionProto *)sq_vm_malloc(_FUNC_SIZE(ninstructions,nliterals,nparameters,nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams));
64  new (f) SQFunctionProto(ss);
65  f->_ninstructions = ninstructions;
66  f->_literals = (SQObjectPtr*)&f->_instructions[ninstructions];
67  f->_nliterals = nliterals;
68  f->_parameters = (SQObjectPtr*)&f->_literals[nliterals];
69  f->_nparameters = nparameters;
70  f->_functions = (SQObjectPtr*)&f->_parameters[nparameters];
71  f->_nfunctions = nfunctions;
72  f->_outervalues = (SQOuterVar*)&f->_functions[nfunctions];
73  f->_noutervalues = noutervalues;
74  f->_lineinfos = (SQLineInfo *)&f->_outervalues[noutervalues];
75  f->_nlineinfos = nlineinfos;
76  f->_localvarinfos = (SQLocalVarInfo *)&f->_lineinfos[nlineinfos];
77  f->_nlocalvarinfos = nlocalvarinfos;
78  f->_defaultparams = (SQInteger *)&f->_localvarinfos[nlocalvarinfos];
79  f->_ndefaultparams = ndefaultparams;
80 
81  _CONSTRUCT_VECTOR(SQObjectPtr,f->_nliterals,f->_literals);
82  _CONSTRUCT_VECTOR(SQObjectPtr,f->_nparameters,f->_parameters);
83  _CONSTRUCT_VECTOR(SQObjectPtr,f->_nfunctions,f->_functions);
84  _CONSTRUCT_VECTOR(SQOuterVar,f->_noutervalues,f->_outervalues);
85  //_CONSTRUCT_VECTOR(SQLineInfo,f->_nlineinfos,f->_lineinfos); //not required are 2 integers
86  _CONSTRUCT_VECTOR(SQLocalVarInfo,f->_nlocalvarinfos,f->_localvarinfos);
87  return f;
88  }
89  void Release(){
90  _DESTRUCT_VECTOR(SQObjectPtr,_nliterals,_literals);
91  _DESTRUCT_VECTOR(SQObjectPtr,_nparameters,_parameters);
92  _DESTRUCT_VECTOR(SQObjectPtr,_nfunctions,_functions);
93  _DESTRUCT_VECTOR(SQOuterVar,_noutervalues,_outervalues);
94  //_DESTRUCT_VECTOR(SQLineInfo,_nlineinfos,_lineinfos); //not required are 2 integers
95  _DESTRUCT_VECTOR(SQLocalVarInfo,_nlocalvarinfos,_localvarinfos);
96  SQInteger size = _FUNC_SIZE(_ninstructions,_nliterals,_nparameters,_nfunctions,_noutervalues,_nlineinfos,_nlocalvarinfos,_ndefaultparams);
97  this->~SQFunctionProto();
98  sq_vm_free(this,size);
99  }
100 
101  const SQChar* GetLocal(SQVM *v,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop);
102  SQInteger GetLine(SQInstruction *curr);
103  bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
104  static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
105 #ifndef NO_GARBAGE_COLLECTOR
106  void Mark(SQCollectable **chain);
107  void Finalize(){ _NULL_SQOBJECT_VECTOR(_literals,_nliterals); }
108  SQObjectType GetType() {return OT_FUNCPROTO;}
109 #endif
110  SQObjectPtr _sourcename;
111  SQObjectPtr _name;
112  SQInteger _stacksize;
113  bool _bgenerator;
114  SQInteger _varparams = 0;
115 
116  SQInteger _nlocalvarinfos = 0;
117  SQLocalVarInfo *_localvarinfos = nullptr;
118 
119  SQInteger _nlineinfos = 0;
120  SQLineInfo *_lineinfos = nullptr;
121 
122  SQInteger _nliterals = 0;
123  SQObjectPtr *_literals = nullptr;
124 
125  SQInteger _nparameters = 0;
126  SQObjectPtr *_parameters = nullptr;
127 
128  SQInteger _nfunctions = 0;
129  SQObjectPtr *_functions = nullptr;
130 
131  SQInteger _noutervalues = 0;
132  SQOuterVar *_outervalues = nullptr;
133 
134  SQInteger _ndefaultparams = 0;
135  SQInteger *_defaultparams = nullptr;
136 
137  SQInteger _ninstructions = 0;
138  SQInstruction _instructions[1];
139 };
140 
141 #endif //_SQFUNCTION_H_
Definition: sqvm.h:33
Definition: sqfuncproto.h:27
Definition: sqfuncproto.h:12
Definition: sqstate.h:59
Definition: sqfuncproto.h:36
Definition: sqopcodes.h:109
Definition: sqobject.h:205
Definition: sqfuncproto.h:49
Definition: sqobject.h:313