1 //************************************************************************** 2 //** 3 //** ## ## ## ## ## #### #### ### ### 4 //** ## ## ## ## ## ## ## ## ## ## #### #### 5 //** ## ## ## ## ## ## ## ## ## ## ## ## ## ## 6 //** ## ## ######## ## ## ## ## ## ## ## ### ## 7 //** ### ## ## ### ## ## ## ## ## ## 8 //** # ## ## # #### #### ## ## 9 //** 10 //** $Id: vc_method.h 4132 2010-03-02 19:59:53Z dj_jl $ 11 //** 12 //** Copyright (C) 1999-2006 Jānis Legzdiņš 13 //** 14 //** This program is free software; you can redistribute it and/or 15 //** modify it under the terms of the GNU General Public License 16 //** as published by the Free Software Foundation; either version 2 17 //** of the License, or (at your option) any later version. 18 //** 19 //** This program is distributed in the hope that it will be useful, 20 //** but WITHOUT ANY WARRANTY; without even the implied warranty of 21 //** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 22 //** GNU General Public License for more details. 23 //** 24 //************************************************************************** 25 26 //========================================================================== 27 // 28 // Method flags 29 // 30 //========================================================================== 31 32 enum 33 { 34 FUNC_Native = 0x0001, // Native method 35 FUNC_Static = 0x0002, // Static method 36 FUNC_VarArgs = 0x0004, // Variable argument count 37 FUNC_Final = 0x0008, // Final version of a method 38 FUNC_Spawner = 0x0010, // Automatic cast of return value 39 FUNC_Net = 0x0020, // Method is network-replicated 40 FUNC_NetReliable = 0x0040, // Sent reliably over the network 41 FUNC_Iterator = 0x0080, // Can be used in foreach statements 42 43 FUNC_NetFlags = FUNC_Net | FUNC_NetReliable, 44 }; 45 46 //========================================================================== 47 // 48 // Parameter flags 49 // 50 //========================================================================== 51 52 enum 53 { 54 FPARM_Optional = 0x01, 55 FPARM_Out = 0x02, 56 }; 57 58 //========================================================================== 59 // 60 // builtin_t 61 // 62 //========================================================================== 63 64 typedef void (*builtin_t)(); 65 66 //========================================================================== 67 // 68 // FBuiltinInfo 69 // 70 //========================================================================== 71 72 class FBuiltinInfo 73 { 74 const char *Name; 75 VClass *OuterClass; 76 builtin_t Func; 77 FBuiltinInfo *Next; 78 79 static FBuiltinInfo *Builtins; 80 81 friend class VMethod; 82 83 public: FBuiltinInfo(const char * InName,VClass * InClass,builtin_t InFunc)84 FBuiltinInfo(const char *InName, VClass *InClass, builtin_t InFunc) 85 : Name(InName), OuterClass(InClass), Func(InFunc) 86 { 87 Next = Builtins; 88 Builtins = this; 89 } 90 }; 91 92 //========================================================================== 93 // 94 // FInstruction 95 // 96 //========================================================================== 97 98 struct FInstruction 99 { 100 vint32 Address; 101 vint32 Opcode; 102 vint32 Arg1; 103 vint32 Arg2; 104 VMemberBase* Member; 105 VName NameArg; 106 VFieldType TypeArg; 107 108 friend VStream& operator << (VStream&, FInstruction&); 109 }; 110 111 //========================================================================== 112 // 113 // VMethodParam 114 // 115 //========================================================================== 116 117 class VMethodParam 118 { 119 public: 120 VExpression* TypeExpr; 121 VName Name; 122 TLocation Loc; 123 124 VMethodParam(); 125 ~VMethodParam(); 126 }; 127 128 //========================================================================== 129 // 130 // VMethod 131 // 132 //========================================================================== 133 134 class VMethod : public VMemberBase 135 { 136 public: 137 enum { MAX_PARAMS = 16 }; 138 139 // Persistent fields 140 vint32 NumLocals; 141 vint32 Flags; 142 VFieldType ReturnType; 143 vint32 NumParams; 144 vint32 ParamsSize; 145 VFieldType ParamTypes[MAX_PARAMS]; 146 vuint8 ParamFlags[MAX_PARAMS]; 147 TArray<FInstruction> Instructions; 148 VMethod* SuperMethod; 149 VMethod* ReplCond; 150 151 // Compiler fields 152 VExpression* ReturnTypeExpr; 153 VMethodParam Params[MAX_PARAMS]; 154 VStatement* Statement; 155 VName SelfTypeName; 156 157 // Run-time fields 158 vuint32 Profile1; 159 vuint32 Profile2; 160 TArray<vuint8> Statements; 161 builtin_t NativeFunc; 162 vint16 VTableIndex; 163 vint32 NetIndex; 164 VMethod* NextNetMethod; 165 166 VMethod(VName, VMemberBase*, TLocation); 167 ~VMethod(); 168 169 void Serialise(VStream&); 170 bool Define(); 171 void Emit(); 172 void DumpAsm(); 173 void PostLoad(); 174 175 friend inline VStream& operator<<(VStream& Strm, VMethod*& Obj) 176 { return Strm << *(VMemberBase**)&Obj; } 177 178 private: 179 void CompileCode(); 180 void OptimiseInstructions(); 181 }; 182