# sqfuncproto.h

    00001 /*  see copyright notice in squirrel.h */
    00002 #ifndef _SQFUNCTION_H_
    00003 #define _SQFUNCTION_H_
    00004
    00005 #include "sqopcodes.h"
    00006
    00007 enum SQOuterType {
    00008   otLOCAL = 0,
    00009   otSYMBOL = 1,
    00010   otOUTER = 2
    00011 };
    00012
    00013 struct SQOuterVar
    00014 {
    00015
    00016   SQOuterVar(){}
    00017   SQOuterVar(const SQObjectPtr &name,const SQObjectPtr &src,SQOuterType t)
    00018   {
    00019     _name = name;
    00020     _src=src;
    00021     _type=t;
    00022   }
    00023   SQOuterVar(const SQOuterVar &ov)
    00024   {
    00025     _type=ov._type;
    00026     _src=ov._src;
    00027     _name=ov._name;
    00028   }
    00029   SQOuterType _type;
    00030   SQObjectPtr _name;
    00031   SQObjectPtr _src;
    00032 };
    00033
    00034 struct SQLocalVarInfo
    00035 {
    00036   SQLocalVarInfo():_start_op(0),_end_op(0){}
    00037   SQLocalVarInfo(const SQLocalVarInfo &lvi)
    00038   {
    00039     _name=lvi._name;
    00040     _start_op=lvi._start_op;
    00041     _end_op=lvi._end_op;
    00042     _pos=lvi._pos;
    00043   }
    00044   SQObjectPtr _name;
    00045   SQUnsignedInteger _start_op;
    00046   SQUnsignedInteger _end_op;
    00047   SQUnsignedInteger _pos;
    00048 };
    00049
    00050 struct SQLineInfo { SQInteger _line;SQInteger _op; };
    00051
    00052 typedef sqvector<SQOuterVar> SQOuterVarVec;
    00053 typedef sqvector<SQLocalVarInfo> SQLocalVarInfoVec;
    00054 typedef sqvector<SQLineInfo> SQLineInfoVec;
    00055
    00056 #define _FUNC_SIZE(ni,nl,nparams,nfuncs,nouters,nlineinf,localinf,defparams) (sizeof(SQFunctionProto) \
    00057     +((ni-1)*sizeof(SQInstruction))+(nl*sizeof(SQObjectPtr)) \
    00058     +(nparams*sizeof(SQObjectPtr))+(nfuncs*sizeof(SQObjectPtr)) \
    00059     +(nouters*sizeof(SQOuterVar))+(nlineinf*sizeof(SQLineInfo)) \
    00060     +(localinf*sizeof(SQLocalVarInfo))+(defparams*sizeof(SQInteger)))
    00061
    00062 #define _CONSTRUCT_VECTOR(type,size,ptr) { \
    00063   for(SQInteger n = 0; n < size; n++) { \
    00064       new (&ptr[n]) type(); \
    00065     } \
    00066 }
    00067
    00068 #define _DESTRUCT_VECTOR(type,size,ptr) { \
    00069   for(SQInteger nl = 0; nl < size; nl++) { \
    00070       ptr[nl].~type(); \
    00071   } \
    00072 }
    00073 struct SQFunctionProto : public SQRefCounted
    00074 {
    00075 private:
    00076   SQFunctionProto(){
    00077   _stacksize=0;
    00078   _bgenerator=false;}
    00079 public:
    00080   static SQFunctionProto *Create(SQInteger ninstructions,
    00081     SQInteger nliterals,SQInteger nparameters,
    00082     SQInteger nfunctions,SQInteger noutervalues,
    00083     SQInteger nlineinfos,SQInteger nlocalvarinfos,SQInteger ndefaultparams)
    00084   {
    00085     SQFunctionProto *f;
    00086     //I compact the whole class and members in a single memory allocation
    00087     f = (SQFunctionProto *)sq_vm_malloc(_FUNC_SIZE(ninstructions,nliterals,nparameters,nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams));
    00088     new (f) SQFunctionProto;
    00089     f->_ninstructions = ninstructions;
    00090     f->_literals = (SQObjectPtr*)&f->_instructions[ninstructions];
    00091     f->_nliterals = nliterals;
    00092     f->_parameters = (SQObjectPtr*)&f->_literals[nliterals];
    00093     f->_nparameters = nparameters;
    00094     f->_functions = (SQObjectPtr*)&f->_parameters[nparameters];
    00095     f->_nfunctions = nfunctions;
    00096     f->_outervalues = (SQOuterVar*)&f->_functions[nfunctions];
    00097     f->_noutervalues = noutervalues;
    00098     f->_lineinfos = (SQLineInfo *)&f->_outervalues[noutervalues];
    00099     f->_nlineinfos = nlineinfos;
    00100     f->_localvarinfos = (SQLocalVarInfo *)&f->_lineinfos[nlineinfos];
    00101     f->_nlocalvarinfos = nlocalvarinfos;
    00102     f->_defaultparams = (SQInteger *)&f->_localvarinfos[nlocalvarinfos];
    00103     f->_ndefaultparams = ndefaultparams;
    00104
    00105     _CONSTRUCT_VECTOR(SQObjectPtr,f->_nliterals,f->_literals);
    00106     _CONSTRUCT_VECTOR(SQObjectPtr,f->_nparameters,f->_parameters);
    00107     _CONSTRUCT_VECTOR(SQObjectPtr,f->_nfunctions,f->_functions);
    00108     _CONSTRUCT_VECTOR(SQOuterVar,f->_noutervalues,f->_outervalues);
    00109     //_CONSTRUCT_VECTOR(SQLineInfo,f->_nlineinfos,f->_lineinfos); //not required are 2 integers
    00110     _CONSTRUCT_VECTOR(SQLocalVarInfo,f->_nlocalvarinfos,f->_localvarinfos);
    00111     return f;
    00112   }
    00113   void Release(){
    00114     _DESTRUCT_VECTOR(SQObjectPtr,_nliterals,_literals);
    00115     _DESTRUCT_VECTOR(SQObjectPtr,_nparameters,_parameters);
    00116     _DESTRUCT_VECTOR(SQObjectPtr,_nfunctions,_functions);
    00117     _DESTRUCT_VECTOR(SQOuterVar,_noutervalues,_outervalues);
    00118     //_DESTRUCT_VECTOR(SQLineInfo,_nlineinfos,_lineinfos); //not required are 2 integers
    00119     _DESTRUCT_VECTOR(SQLocalVarInfo,_nlocalvarinfos,_localvarinfos);
    00120     SQInteger size = _FUNC_SIZE(_ninstructions,_nliterals,_nparameters,_nfunctions,_noutervalues,_nlineinfos,_nlocalvarinfos,_ndefaultparams);
    00121     this->~SQFunctionProto();
    00122     sq_vm_free(this,size);
    00123   }
    00124   const SQChar* GetLocal(SQVM *v,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop);
    00125   SQInteger GetLine(SQInstruction *curr);
    00126   bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
    00127   static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
    00128
    00129   SQObjectPtr _sourcename;
    00130   SQObjectPtr _name;
    00131     SQInteger _stacksize;
    00132   bool _bgenerator;
    00133   bool _varparams;
    00134
    00135   SQInteger _nlocalvarinfos;
    00136   SQLocalVarInfo *_localvarinfos;
    00137
    00138   SQInteger _nlineinfos;
    00139   SQLineInfo *_lineinfos;
    00140
    00141   SQInteger _nliterals;
    00142   SQObjectPtr *_literals;
    00143
    00144   SQInteger _nparameters;
    00145   SQObjectPtr *_parameters;
    00146
    00147   SQInteger _nfunctions;
    00148   SQObjectPtr *_functions;
    00149
    00150   SQInteger _noutervalues;
    00151   SQOuterVar *_outervalues;
    00152
    00153   SQInteger _ndefaultparams;
    00154   SQInteger *_defaultparams;
    00155
    00156   SQInteger _ninstructions;
    00157   SQInstruction _instructions[1];
    00158 };
    00159
    00160 #endif //_SQFUNCTION_H_