| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <jenga/include/smoothie/Smoothie.h>
|
|---|
| 4 |
|
|---|
| 5 | #include <Compiler.h>
|
|---|
| 6 |
|
|---|
| 7 | #include "../BasicCompiler_Common/common.h"
|
|---|
| 8 | #include "Opcode.h"
|
|---|
| 9 |
|
|---|
| 10 | int GetFunctionFromName(char *FuncName){
|
|---|
| 11 | if( lstrcmpi( FuncName, "CUDbl" ) == 0 ) return FUNC_CUDBL;
|
|---|
| 12 | if( lstrcmpi( FuncName, "Fix" ) == 0 ) return FUNC_FIX;
|
|---|
| 13 | if( lstrcmpi( FuncName, "Len" ) == 0 ) return FUNC_LEN;
|
|---|
| 14 | if( lstrcmpi( FuncName, "AddressOf" ) == 0 ) return FUNC_ADDRESSOF;
|
|---|
| 15 | if( lstrcmpi( FuncName, "SizeOf" ) == 0 ) return FUNC_SIZEOF;
|
|---|
| 16 | if( lstrcmpi( FuncName, "VarPtr" ) == 0 ) return FUNC_VARPTR;
|
|---|
| 17 | if( lstrcmpi( FuncName, "ObjPtr" ) == 0 ) return FUNC_OBJPTR;
|
|---|
| 18 | if( lstrcmpi( FuncName, "__delegate_dynamicmethod_call" ) == 0 ) return FUNC_DELEGATE_DYNAMICMETHOD_CALL;
|
|---|
| 19 | if( lstrcmpi( FuncName, "__delegate_staticmethod_call" ) == 0 ) return FUNC_DELEGATE_STATICMETHOD_CALL;
|
|---|
| 20 | if( lstrcmpi( FuncName, "_System_GetNowScopeCatchAddresses" ) == 0 )return FUNC_SYSTEM_GET_NOW_SCOPE_CATCH_ADDRESS;
|
|---|
| 21 | if( lstrcmpi( FuncName, "_System_GetNowScopeFinallyAddresses" ) == 0 )return FUNC_SYSTEM_GET_NOW_SCOPE_FINALLY_ADDRESS;
|
|---|
| 22 | if( lstrcmpi( FuncName, "_System_GetBp" ) == 0 ) return FUNC_SYSTEM_GET_BP;
|
|---|
| 23 | if( lstrcmpi( FuncName, "_System_GetSp" ) == 0 ) return FUNC_SYSTEM_GET_SP;
|
|---|
| 24 | if( lstrcmpi( FuncName, "GetDouble" ) == 0 ) return FUNC_GETDOUBLE;
|
|---|
| 25 | if( lstrcmpi( FuncName, "GetSingle" ) == 0 ) return FUNC_GETSINGLE;
|
|---|
| 26 | if( lstrcmpi( FuncName, "GetQWord" ) == 0 ) return FUNC_GETQWORD;
|
|---|
| 27 | if( lstrcmpi( FuncName, "GetDWord" ) == 0 ) return FUNC_GETDWORD;
|
|---|
| 28 | if( lstrcmpi( FuncName, "GetWord" ) == 0 ) return FUNC_GETWORD;
|
|---|
| 29 | if( lstrcmpi( FuncName, "GetByte" ) == 0 ) return FUNC_GETBYTE;
|
|---|
| 30 | return 0;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | void Opcode_Func_Fix(const char *lpszParms){
|
|---|
| 34 | Type resultType;
|
|---|
| 35 | if( !NumOpe( lpszParms, Type(), resultType ) ){
|
|---|
| 36 | return;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | if( resultType.IsDouble() ){
|
|---|
| 40 | //fld qword ptr[esp]
|
|---|
| 41 | compiler.codeGenerator.op_fld_ptr_esp(DEF_DOUBLE);
|
|---|
| 42 |
|
|---|
| 43 | //fnstcw word ptr[esp]
|
|---|
| 44 | compiler.codeGenerator.PutOld(
|
|---|
| 45 | (char)0xD9,
|
|---|
| 46 | (char)0x3C,
|
|---|
| 47 | (char)0x24
|
|---|
| 48 | );
|
|---|
| 49 |
|
|---|
| 50 | //mov ax,word ptr[esp]
|
|---|
| 51 | compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_ESP, 0, MOD_BASE );
|
|---|
| 52 |
|
|---|
| 53 | //or ah,0Ch
|
|---|
| 54 | compiler.codeGenerator.PutOld(
|
|---|
| 55 | (char)0x80,
|
|---|
| 56 | (char)0xCC,
|
|---|
| 57 | (char)0x0C
|
|---|
| 58 | );
|
|---|
| 59 |
|
|---|
| 60 | //mov word ptr[esp-2],ax
|
|---|
| 61 | compiler.codeGenerator.op_mov_MR( sizeof(short), REG_EAX, REG_ESP, -2, MOD_BASE_DISP8 );
|
|---|
| 62 |
|
|---|
| 63 | //fldcw word ptr[esp-2]
|
|---|
| 64 | compiler.codeGenerator.PutOld(
|
|---|
| 65 | (char)0xD9,
|
|---|
| 66 | (char)0x6C,
|
|---|
| 67 | (char)0x24,
|
|---|
| 68 | (char)0xFE
|
|---|
| 69 | );
|
|---|
| 70 |
|
|---|
| 71 | //fistp dword ptr[esp+4]
|
|---|
| 72 | compiler.codeGenerator.PutOld(
|
|---|
| 73 | (char)0xDB,
|
|---|
| 74 | (char)0x5C,
|
|---|
| 75 | (char)0x24,
|
|---|
| 76 | (char)0x04
|
|---|
| 77 | );
|
|---|
| 78 |
|
|---|
| 79 | //fldcw word ptr[esp]
|
|---|
| 80 | compiler.codeGenerator.PutOld(
|
|---|
| 81 | (char)0xD9,
|
|---|
| 82 | (char)0x2C,
|
|---|
| 83 | (char)0x24
|
|---|
| 84 | );
|
|---|
| 85 |
|
|---|
| 86 | //add esp,4
|
|---|
| 87 | compiler.codeGenerator.op_add_esp(4);
|
|---|
| 88 | }
|
|---|
| 89 | else if( resultType.IsSingle() ){
|
|---|
| 90 | //fld dword ptr[esp]
|
|---|
| 91 | compiler.codeGenerator.op_fld_ptr_esp(DEF_SINGLE);
|
|---|
| 92 |
|
|---|
| 93 | //sub esp,4
|
|---|
| 94 | compiler.codeGenerator.op_sub_esp(4);
|
|---|
| 95 |
|
|---|
| 96 | //fnstcw word ptr[esp]
|
|---|
| 97 | compiler.codeGenerator.PutOld(
|
|---|
| 98 | (char)0xD9,
|
|---|
| 99 | (char)0x3C,
|
|---|
| 100 | (char)0x24
|
|---|
| 101 | );
|
|---|
| 102 |
|
|---|
| 103 | //mov ax,word ptr[esp]
|
|---|
| 104 | compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_ESP, 0, MOD_BASE );
|
|---|
| 105 |
|
|---|
| 106 | //or ah,0Ch
|
|---|
| 107 | compiler.codeGenerator.PutOld(
|
|---|
| 108 | (char)0x80,
|
|---|
| 109 | (char)0xCC,
|
|---|
| 110 | (char)0x0C
|
|---|
| 111 | );
|
|---|
| 112 |
|
|---|
| 113 | //mov word ptr[esp-2],ax
|
|---|
| 114 | compiler.codeGenerator.op_mov_MR( sizeof(short), REG_EAX, REG_ESP, -2, MOD_BASE_DISP8 );
|
|---|
| 115 |
|
|---|
| 116 | //fldcw word ptr[esp-2]
|
|---|
| 117 | compiler.codeGenerator.PutOld(
|
|---|
| 118 | (char)0xD9,
|
|---|
| 119 | (char)0x6C,
|
|---|
| 120 | (char)0x24,
|
|---|
| 121 | (char)0xFE
|
|---|
| 122 | );
|
|---|
| 123 |
|
|---|
| 124 | //fistp dword ptr[esp+4]
|
|---|
| 125 | compiler.codeGenerator.PutOld(
|
|---|
| 126 | (char)0xDB,
|
|---|
| 127 | (char)0x5C,
|
|---|
| 128 | (char)0x24,
|
|---|
| 129 | (char)0x04
|
|---|
| 130 | );
|
|---|
| 131 |
|
|---|
| 132 | //fldcw word ptr[esp]
|
|---|
| 133 | compiler.codeGenerator.PutOld(
|
|---|
| 134 | (char)0xD9,
|
|---|
| 135 | (char)0x2C,
|
|---|
| 136 | (char)0x24
|
|---|
| 137 | );
|
|---|
| 138 |
|
|---|
| 139 | //add esp,4
|
|---|
| 140 | compiler.codeGenerator.op_add_esp(4);
|
|---|
| 141 | }
|
|---|
| 142 | else if( resultType.Is64() ){
|
|---|
| 143 | //pop eax
|
|---|
| 144 | compiler.codeGenerator.op_pop(REG_EAX);
|
|---|
| 145 |
|
|---|
| 146 | //add esp,4
|
|---|
| 147 | compiler.codeGenerator.op_add_esp(4);
|
|---|
| 148 |
|
|---|
| 149 | //push eax
|
|---|
| 150 | compiler.codeGenerator.op_push(REG_EAX);
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | //pop eax
|
|---|
| 154 | compiler.codeGenerator.op_pop(REG_EAX);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | void Opcode_Func_CUDbl(const char *Parameter){
|
|---|
| 158 | Type resultType;
|
|---|
| 159 | if( !NumOpe(Parameter,Type(),resultType) ){
|
|---|
| 160 | return;
|
|---|
| 161 | }
|
|---|
| 162 | ChangeTypeToLong(resultType.GetBasicType());
|
|---|
| 163 |
|
|---|
| 164 | //pop eax
|
|---|
| 165 | compiler.codeGenerator.op_pop(REG_EAX);
|
|---|
| 166 |
|
|---|
| 167 | //push 0
|
|---|
| 168 | compiler.codeGenerator.op_push_V( 0 );
|
|---|
| 169 |
|
|---|
| 170 | //push eax
|
|---|
| 171 | compiler.codeGenerator.op_push(REG_EAX);
|
|---|
| 172 |
|
|---|
| 173 | //fild qword ptr[esp]
|
|---|
| 174 | compiler.codeGenerator.op_fld_ptr_esp(DEF_INT64);
|
|---|
| 175 |
|
|---|
| 176 | //add esp,8
|
|---|
| 177 | compiler.codeGenerator.op_add_esp(8);
|
|---|
| 178 | }
|
|---|
| 179 | void Opcode_Func_Len(const char *Parameter){
|
|---|
| 180 | BOOL bArrayHead;
|
|---|
| 181 |
|
|---|
| 182 | const char *tempParm=Parameter;
|
|---|
| 183 | char temporary[VN_SIZE];
|
|---|
| 184 | char temp2[32];
|
|---|
| 185 | Type type;
|
|---|
| 186 | if( !GetVarType(Parameter,type,0) ){
|
|---|
| 187 | sprintf(temporary,"_System_DummyStr2=%s",Parameter);
|
|---|
| 188 | OpcodeCalc(temporary);
|
|---|
| 189 |
|
|---|
| 190 | lstrcpy(temp2,"_System_DummyStr2");
|
|---|
| 191 | tempParm=temp2;
|
|---|
| 192 |
|
|---|
| 193 | type.SetType( DEF_OBJECT, compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr() );
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | if( type.IsStringClass() ){
|
|---|
| 197 | //Stringオブジェクトの場合
|
|---|
| 198 | sprintf(temporary,"%s.Length",tempParm);
|
|---|
| 199 |
|
|---|
| 200 | int reg=REG_RAX;
|
|---|
| 201 | NumOpe(temporary,Type(),Type());
|
|---|
| 202 |
|
|---|
| 203 | //pop eax
|
|---|
| 204 | compiler.codeGenerator.op_pop(REG_EAX);
|
|---|
| 205 |
|
|---|
| 206 | return;
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | Subscripts subscripts;
|
|---|
| 210 | RELATIVE_VAR RelativeVar;
|
|---|
| 211 | if(!GetVarOffsetReadOnly(tempParm,&RelativeVar,type,&subscripts)) return;
|
|---|
| 212 |
|
|---|
| 213 | if(type.GetBasicType()&FLAG_PTR){
|
|---|
| 214 | type.SetBasicType( type.GetBasicType() & ( ~FLAG_PTR ) );
|
|---|
| 215 |
|
|---|
| 216 | bArrayHead=1;
|
|---|
| 217 | }
|
|---|
| 218 | else bArrayHead=0;
|
|---|
| 219 |
|
|---|
| 220 | int typeSize = type.GetSize();
|
|---|
| 221 |
|
|---|
| 222 | if(bArrayHead) typeSize*=JumpSubScripts(subscripts);
|
|---|
| 223 |
|
|---|
| 224 | //mov eax,typeSize
|
|---|
| 225 | compiler.codeGenerator.op_mov_RV( REG_EAX, typeSize );
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | void _Opcode_Func_AddressOf( const char *methodInstanceName, const UserProc &userProc )
|
|---|
| 229 | {
|
|---|
| 230 | if( userProc.IsVirtual() ){
|
|---|
| 231 | ///////////////////////////////
|
|---|
| 232 | // 仮想関数の場合
|
|---|
| 233 | // thisポインタをrcxにコピー
|
|---|
| 234 | ///////////////////////////////
|
|---|
| 235 |
|
|---|
| 236 | const CClass *pobj_c;
|
|---|
| 237 |
|
|---|
| 238 | char ObjectName[VN_SIZE];
|
|---|
| 239 | ReferenceKind referenceKind;
|
|---|
| 240 | SplitObjectName( methodInstanceName, ObjectName, referenceKind );
|
|---|
| 241 |
|
|---|
| 242 | if(ObjectName[0]){
|
|---|
| 243 | if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
|
|---|
| 244 | else{
|
|---|
| 245 | RELATIVE_VAR RelativeVar;
|
|---|
| 246 | Type type;
|
|---|
| 247 | if(!GetVarOffsetReadOnly(ObjectName,&RelativeVar,type)) return;
|
|---|
| 248 | SetVarPtrToEax(&RelativeVar);
|
|---|
| 249 |
|
|---|
| 250 | //mov ecx,eax
|
|---|
| 251 | compiler.codeGenerator.op_mov_RR(REG_ECX,REG_EAX);
|
|---|
| 252 |
|
|---|
| 253 | //参照タイプが整合しているかをチェック
|
|---|
| 254 | if( !( type.IsObject() && referenceKind == RefDot
|
|---|
| 255 | || type.IsObjectPtr() && referenceKind == RefPointer ) )
|
|---|
| 256 | {
|
|---|
| 257 | SetError(104,ObjectName,cp);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | if(type.IsObjectPtr()){
|
|---|
| 261 | //mov ecx,dword ptr[ecx]
|
|---|
| 262 | compiler.codeGenerator.op_mov_RM(sizeof(long),REG_ECX,REG_ECX,0,MOD_BASE);
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|
| 266 | else{
|
|---|
| 267 | InClassMember:
|
|---|
| 268 | //自身のオブジェクトのThisポインタをrcxにコピー
|
|---|
| 269 | SetThisPtrToReg(REG_RCX);
|
|---|
| 270 |
|
|---|
| 271 | pobj_c=compiler.pCompilingClass;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 | int vtblIndex;
|
|---|
| 276 | if( pobj_c->IsInterface() )
|
|---|
| 277 | {
|
|---|
| 278 | // インターフェイスメソッド呼び出し
|
|---|
| 279 |
|
|---|
| 280 | int offset_vtbl = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__vtbl" );
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 | // vtblのポインタを取得
|
|---|
| 284 | //mov edx,dword ptr[ecx+offset_vtbl]
|
|---|
| 285 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, offset_vtbl, MOD_BASE_DISP8 );
|
|---|
| 286 |
|
|---|
| 287 | int offset_this = compiler.GetObjectModule().meta.GetClasses().GetInterfaceInfoClassPtr()->GetMemberOffset( "__this" );
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 | // インターフェイスの場合は更に__thisを取得する
|
|---|
| 292 | //mov rcx,qword ptr[rcx+offset_this]
|
|---|
| 293 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ECX, offset_this, MOD_BASE_DISP8 );
|
|---|
| 294 |
|
|---|
| 295 | int vtblMasterListIndex;
|
|---|
| 296 | pobj_c->GetVtblMasterListIndexAndVtblIndex( &userProc, vtblMasterListIndex, vtblIndex );
|
|---|
| 297 | if( vtblMasterListIndex != 0 )
|
|---|
| 298 | {
|
|---|
| 299 | SetError();
|
|---|
| 300 | }
|
|---|
| 301 | }
|
|---|
| 302 | else if( pobj_c->IsComInterface() )
|
|---|
| 303 | {
|
|---|
| 304 | // COMインターフェイス メソッド呼び出し
|
|---|
| 305 |
|
|---|
| 306 | //仮想関数(オブジェクトメソッド)呼び出し
|
|---|
| 307 | // pObj -> vtbl1 -> func1
|
|---|
| 308 | // -> func2
|
|---|
| 309 | // -> func3
|
|---|
| 310 |
|
|---|
| 311 | int vtblMasterListIndex;
|
|---|
| 312 | pobj_c->GetVtblMasterListIndexAndVtblIndex( &userProc, vtblMasterListIndex, vtblIndex );
|
|---|
| 313 |
|
|---|
| 314 | // vtblのポインタを取得
|
|---|
| 315 | //mov edx,dword ptr[ecx]
|
|---|
| 316 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
|
|---|
| 317 | }
|
|---|
| 318 | else
|
|---|
| 319 | {
|
|---|
| 320 | //仮想関数(オブジェクトメソッド)呼び出し
|
|---|
| 321 | // pObj -> vtbl_master_list -> vtbl1 -> func1
|
|---|
| 322 | // -> func2
|
|---|
| 323 | // -> func3
|
|---|
| 324 | // -> vtbl2 -> func1
|
|---|
| 325 | // -> func2
|
|---|
| 326 | // -> func3
|
|---|
| 327 |
|
|---|
| 328 | int vtblMasterListIndex;
|
|---|
| 329 | pobj_c->GetVtblMasterListIndexAndVtblIndex( &userProc, vtblMasterListIndex, vtblIndex );
|
|---|
| 330 |
|
|---|
| 331 | // vtblマスターリストのポインタを取得
|
|---|
| 332 | //mov edx,dword ptr[ecx+sizeof(com_vtbl)]
|
|---|
| 333 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, PTR_SIZE, MOD_BASE_DISP8 );
|
|---|
| 334 |
|
|---|
| 335 | // vtblのポインタを取得
|
|---|
| 336 | //mov edx,dword ptr[edx+vtblMasterListIndex]
|
|---|
| 337 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_EDX, vtblMasterListIndex*PTR_SIZE, MOD_BASE_DISP32 );
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | //mov eax,dword ptr[edx+func_index]
|
|---|
| 341 | if( vtblIndex * PTR_SIZE <= 0x7F )
|
|---|
| 342 | {
|
|---|
| 343 | compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EAX,REG_EDX,vtblIndex*PTR_SIZE,MOD_BASE_DISP8);
|
|---|
| 344 | }
|
|---|
| 345 | else{
|
|---|
| 346 | compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EAX,REG_EDX,vtblIndex*PTR_SIZE,MOD_BASE_DISP32);
|
|---|
| 347 | }
|
|---|
| 348 | }
|
|---|
| 349 | else{
|
|---|
| 350 | //一般の関数
|
|---|
| 351 |
|
|---|
| 352 | //mov eax,ProcAddr
|
|---|
| 353 | compiler.codeGenerator.op_addressof( REG_EAX, &userProc );
|
|---|
| 354 | }
|
|---|
| 355 |
|
|---|
| 356 | userProc.Using();
|
|---|
| 357 | }
|
|---|
| 358 | void Opcode_CreateDelegate( const CClass &dgClass, const char *methodInstanceName, const UserProc &userProc )
|
|---|
| 359 | {
|
|---|
| 360 | /////////////////////////////////////////////////////////////////
|
|---|
| 361 | // 関数ポインタをpush
|
|---|
| 362 | /////////////////////////////////////////////////////////////////
|
|---|
| 363 |
|
|---|
| 364 | //push AddressOf(userProc)
|
|---|
| 365 | _Opcode_Func_AddressOf( methodInstanceName, userProc );
|
|---|
| 366 | compiler.codeGenerator.op_push( REG_EAX );
|
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 | if( userProc.GetMethod().IsDynamic() )
|
|---|
| 370 | {
|
|---|
| 371 | /////////////////////////////////////////////////////////////////
|
|---|
| 372 | // オブジェクト ポインタをpush
|
|---|
| 373 | /////////////////////////////////////////////////////////////////
|
|---|
| 374 |
|
|---|
| 375 | // オブジェクト名を取得
|
|---|
| 376 | char objectName[VN_SIZE];
|
|---|
| 377 | char memberName[VN_SIZE];
|
|---|
| 378 | char *thisPtrName = "This";
|
|---|
| 379 | Type type;
|
|---|
| 380 | if( SplitMemberName( methodInstanceName, objectName, memberName ) )
|
|---|
| 381 | {
|
|---|
| 382 | if( GetVarType( objectName, type, false ) )
|
|---|
| 383 | {
|
|---|
| 384 | thisPtrName = objectName;
|
|---|
| 385 | }
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | // オブジェクト ポインタを取得
|
|---|
| 389 | RELATIVE_VAR relativeVar;
|
|---|
| 390 | GetVarOffsetReadOnly( thisPtrName, &relativeVar, type );
|
|---|
| 391 | if( !type.IsObject() )
|
|---|
| 392 | {
|
|---|
| 393 | extern int cp;
|
|---|
| 394 | SetError(1,NULL,cp);
|
|---|
| 395 | return;
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | SetVarPtrToEax( &relativeVar );
|
|---|
| 399 |
|
|---|
| 400 | //mov eax,dword ptr[eax]
|
|---|
| 401 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
|
|---|
| 402 |
|
|---|
| 403 | //push eax
|
|---|
| 404 | compiler.codeGenerator.op_push( REG_EAX );
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 |
|
|---|
| 408 | /////////////////////////////////////////////////////////////////
|
|---|
| 409 | // call _CreateDynamicDelegate/_CreateStaticDelegate
|
|---|
| 410 | /////////////////////////////////////////////////////////////////
|
|---|
| 411 |
|
|---|
| 412 | std::vector<const UserProc *> subs;
|
|---|
| 413 | if( userProc.GetMethod().IsDynamic() )
|
|---|
| 414 | {
|
|---|
| 415 | dgClass.GetStaticMethods().Enum( "_CreateDynamicDelegate", subs );
|
|---|
| 416 | }
|
|---|
| 417 | else
|
|---|
| 418 | {
|
|---|
| 419 | dgClass.GetStaticMethods().Enum( "_CreateStaticDelegate", subs );
|
|---|
| 420 | }
|
|---|
| 421 |
|
|---|
| 422 | // call _CreateDynamicDelegate
|
|---|
| 423 | compiler.codeGenerator.op_call( subs[0] );
|
|---|
| 424 | }
|
|---|
| 425 | void Opcode_Func_AddressOf( const char *name, const Type &baseType, bool isCallOn, Type &resultType )
|
|---|
| 426 | {
|
|---|
| 427 | extern int cp;
|
|---|
| 428 | const UserProc *pUserProc;
|
|---|
| 429 |
|
|---|
| 430 | const Parameters *pBaseParams = NULL;
|
|---|
| 431 | if( baseType.IsProcPtr() )
|
|---|
| 432 | {
|
|---|
| 433 | // 左辺で関数ポインタを要求されているとき
|
|---|
| 434 | pBaseParams = &compiler.GetObjectModule().meta.GetProcPointers()[baseType.GetIndex()]->Params();
|
|---|
| 435 | }
|
|---|
| 436 | else if( baseType.IsDelegate() )
|
|---|
| 437 | {
|
|---|
| 438 | // 左辺でデリゲートを要求されているとき
|
|---|
| 439 | pBaseParams = &baseType.GetClass().GetDelegate().Params();
|
|---|
| 440 | }
|
|---|
| 441 |
|
|---|
| 442 | if( pBaseParams )
|
|---|
| 443 | {
|
|---|
| 444 | //左辺の型にのっとり、オーバーロードを解決
|
|---|
| 445 |
|
|---|
| 446 | std::vector<const UserProc *> subs;
|
|---|
| 447 | GetOverloadSubHash( name, subs );
|
|---|
| 448 | if( subs.size() == 0 ){
|
|---|
| 449 | SetError(27,name,cp);
|
|---|
| 450 | return;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | //オーバーロードを解決
|
|---|
| 454 | pUserProc=OverloadSolution( name, subs, *pBaseParams, Type() );
|
|---|
| 455 |
|
|---|
| 456 | if( isCallOn && baseType.IsDelegate() )
|
|---|
| 457 | {
|
|---|
| 458 | // コード生成を伴う場合はエラーチェックを行う
|
|---|
| 459 | if( !pUserProc->Params().Equals( *pBaseParams )
|
|---|
| 460 | || !pUserProc->ReturnType().Equals( baseType.GetClass().GetDelegate().ReturnType() ) )
|
|---|
| 461 | {
|
|---|
| 462 | if( baseType.IsDelegate() )
|
|---|
| 463 | {
|
|---|
| 464 | SetError(67, name, cp );
|
|---|
| 465 | }
|
|---|
| 466 | else
|
|---|
| 467 | {
|
|---|
| 468 | SetError(66, name, cp );
|
|---|
| 469 | }
|
|---|
| 470 | }
|
|---|
| 471 | }
|
|---|
| 472 |
|
|---|
| 473 | if(!pUserProc){
|
|---|
| 474 | SetError(27,name,cp);
|
|---|
| 475 | return;
|
|---|
| 476 | }
|
|---|
| 477 | }
|
|---|
| 478 | else{
|
|---|
| 479 | pUserProc=GetSubHash(name);
|
|---|
| 480 | if(!pUserProc){
|
|---|
| 481 | SetError(27,name,cp);
|
|---|
| 482 | return;
|
|---|
| 483 | }
|
|---|
| 484 | }
|
|---|
| 485 |
|
|---|
| 486 | if( baseType.IsDelegate() )
|
|---|
| 487 | {
|
|---|
| 488 | if( isCallOn )
|
|---|
| 489 | {
|
|---|
| 490 | // デリゲートのとき
|
|---|
| 491 | Opcode_CreateDelegate( baseType.GetClass(), name, *pUserProc );
|
|---|
| 492 | }
|
|---|
| 493 | resultType = baseType;
|
|---|
| 494 | }
|
|---|
| 495 | else
|
|---|
| 496 | {
|
|---|
| 497 | if( isCallOn )
|
|---|
| 498 | {
|
|---|
| 499 | // 関数ポインタのとき
|
|---|
| 500 | _Opcode_Func_AddressOf( name, *pUserProc );
|
|---|
| 501 | }
|
|---|
| 502 | resultType.SetBasicType( DEF_PTR_VOID );
|
|---|
| 503 | }
|
|---|
| 504 | }
|
|---|
| 505 | void Opcode_Func_SizeOf( const string &typeName ){
|
|---|
| 506 | Type tempType;
|
|---|
| 507 | if( !compiler.StringToType( typeName, tempType ) ){
|
|---|
| 508 | SetError(3,typeName,cp);
|
|---|
| 509 | return;
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | int typeSize = ( tempType.IsObject() ) ?
|
|---|
| 513 | tempType.GetClass().GetSize() : tempType.GetSize();
|
|---|
| 514 |
|
|---|
| 515 | //mov eax,size
|
|---|
| 516 | compiler.codeGenerator.op_mov_RV( REG_EAX, typeSize );
|
|---|
| 517 | }
|
|---|
| 518 | void Opcode_Func_VarPtr( const char *Parameter, Type &resultType, bool isCallOn ){
|
|---|
| 519 | if( isCallOn == false ){
|
|---|
| 520 | // 戻り値の型を取得するだけ
|
|---|
| 521 |
|
|---|
| 522 | //変数のアドレスを取得
|
|---|
| 523 | if(!GetVarType( Parameter, resultType, true )) return;
|
|---|
| 524 |
|
|---|
| 525 | resultType.PtrLevelUp();
|
|---|
| 526 |
|
|---|
| 527 | return;
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | RELATIVE_VAR RelativeVar;
|
|---|
| 531 |
|
|---|
| 532 | //変数のアドレスを取得
|
|---|
| 533 | if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
|
|---|
| 534 |
|
|---|
| 535 | int beforeType = resultType.GetBasicType();
|
|---|
| 536 |
|
|---|
| 537 | resultType.PtrLevelUp();
|
|---|
| 538 |
|
|---|
| 539 | SetVarPtrToEax(&RelativeVar);
|
|---|
| 540 | }
|
|---|
| 541 | void Opcode_Func_ObjPtr( const char *Parameter, Type &resultType, bool isCallOn ){
|
|---|
| 542 | if( isCallOn == false ){
|
|---|
| 543 | // 戻り値の型を取得するだけ
|
|---|
| 544 |
|
|---|
| 545 | //変数のアドレスを取得
|
|---|
| 546 | if(!GetVarType( Parameter, resultType, true )) return;
|
|---|
| 547 |
|
|---|
| 548 | resultType.PtrLevelUp();
|
|---|
| 549 |
|
|---|
| 550 | return;
|
|---|
| 551 | }
|
|---|
| 552 |
|
|---|
| 553 | RELATIVE_VAR RelativeVar;
|
|---|
| 554 |
|
|---|
| 555 | //変数のアドレスを取得
|
|---|
| 556 | if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
|
|---|
| 557 |
|
|---|
| 558 | int beforeType = resultType.GetBasicType();
|
|---|
| 559 |
|
|---|
| 560 | resultType.PtrLevelUp();
|
|---|
| 561 |
|
|---|
| 562 | SetVarPtrToEax(&RelativeVar);
|
|---|
| 563 |
|
|---|
| 564 | if( lstrcmpi( Parameter, "This" )==0 ){
|
|---|
| 565 | // Thisの場合は特別にオブジェクトポインタが返ってくるので、何もせずに抜ける
|
|---|
| 566 | }
|
|---|
| 567 | else if( beforeType == DEF_OBJECT ){
|
|---|
| 568 | //参照をオブジェクトポインタに変更
|
|---|
| 569 |
|
|---|
| 570 | //mov eax,dword ptr[eax]
|
|---|
| 571 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
|
|---|
| 572 | }
|
|---|
| 573 | else{
|
|---|
| 574 | SetError(134,NULL,cp );
|
|---|
| 575 | }
|
|---|
| 576 | }
|
|---|
| 577 |
|
|---|
| 578 | void Opcode_Func_delegate_call( const char *paramsStr, Type &resultType, bool isDynamicCall, bool isCallOn )
|
|---|
| 579 | {
|
|---|
| 580 | if( isCallOn )
|
|---|
| 581 | {
|
|---|
| 582 | int i = 0;
|
|---|
| 583 | char methodPtrParamStr[VN_SIZE];
|
|---|
| 584 | i = GetOneParameter( paramsStr, i, methodPtrParamStr );
|
|---|
| 585 |
|
|---|
| 586 | char objPtrValueStr[VN_SIZE]="";
|
|---|
| 587 | if( isDynamicCall )
|
|---|
| 588 | {
|
|---|
| 589 | i = GetOneParameter( paramsStr, i, objPtrValueStr );
|
|---|
| 590 | }
|
|---|
| 591 |
|
|---|
| 592 | Opcode_CallDelegate( compiler.pCompilingClass->GetDelegate(), methodPtrParamStr, objPtrValueStr, paramsStr + i );
|
|---|
| 593 | }
|
|---|
| 594 |
|
|---|
| 595 | resultType = UserProc::CompilingUserProc().ReturnType();
|
|---|
| 596 | }
|
|---|
| 597 | void Opcode_Func_System_Get_Bp()
|
|---|
| 598 | {
|
|---|
| 599 | //mov eax,ebp
|
|---|
| 600 | compiler.codeGenerator.op_mov_RR(REG_EAX,REG_EBP);
|
|---|
| 601 | }
|
|---|
| 602 | void Opcode_Func_System_Get_Sp()
|
|---|
| 603 | {
|
|---|
| 604 | //mov eax,esp
|
|---|
| 605 | compiler.codeGenerator.op_mov_RR(REG_EAX,REG_ESP);
|
|---|
| 606 | }
|
|---|
| 607 |
|
|---|
| 608 | void Opcode_Func_GetPtrData(const char *Parameter,const int type){
|
|---|
| 609 | Type tempType;
|
|---|
| 610 | if( !NumOpe(Parameter,Type(),tempType) ){
|
|---|
| 611 | return;
|
|---|
| 612 | }
|
|---|
| 613 | if(!tempType.IsWhole()){
|
|---|
| 614 | SetError(11,Parameter,cp);
|
|---|
| 615 | return;
|
|---|
| 616 | }
|
|---|
| 617 | ChangeTypeToLong(tempType.GetBasicType());
|
|---|
| 618 |
|
|---|
| 619 | if(type==DEF_DOUBLE){
|
|---|
| 620 | //pop eax
|
|---|
| 621 | compiler.codeGenerator.op_pop(REG_EAX);
|
|---|
| 622 |
|
|---|
| 623 | //fld qword ptr[eax]
|
|---|
| 624 | compiler.codeGenerator.PutOld(
|
|---|
| 625 | (char)0xDD,
|
|---|
| 626 | (char)0x00
|
|---|
| 627 | );
|
|---|
| 628 | }
|
|---|
| 629 | else if(type==DEF_SINGLE||type==DEF_DWORD){
|
|---|
| 630 | //pop eax
|
|---|
| 631 | compiler.codeGenerator.op_pop(REG_EAX);
|
|---|
| 632 |
|
|---|
| 633 | //mov eax,dword ptr[eax]
|
|---|
| 634 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
|
|---|
| 635 | }
|
|---|
| 636 | else if(type==DEF_QWORD){
|
|---|
| 637 | //pop ecx
|
|---|
| 638 | compiler.codeGenerator.op_pop(REG_ECX);
|
|---|
| 639 |
|
|---|
| 640 | //mov eax,dword ptr[ecx]
|
|---|
| 641 | compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EAX,REG_ECX,0,MOD_BASE);
|
|---|
| 642 |
|
|---|
| 643 | //mov edx,dword ptr[ecx+sizeof(long)]
|
|---|
| 644 | compiler.codeGenerator.op_mov_RM(sizeof(long),REG_EDX,REG_ECX,sizeof(long),MOD_BASE_DISP8);
|
|---|
| 645 | }
|
|---|
| 646 | else if(type==DEF_WORD){
|
|---|
| 647 | //pop ebx
|
|---|
| 648 | compiler.codeGenerator.op_pop(REG_EBX);
|
|---|
| 649 |
|
|---|
| 650 | //xor eax,eax
|
|---|
| 651 | compiler.codeGenerator.op_xor_RR(REG_EAX);
|
|---|
| 652 |
|
|---|
| 653 | //mov ax,word ptr[ebx]
|
|---|
| 654 | compiler.codeGenerator.op_mov_RM( sizeof(short), REG_EAX, REG_EBX, 0, MOD_BASE );
|
|---|
| 655 | }
|
|---|
| 656 | else if(type==DEF_BYTE){
|
|---|
| 657 | //pop ebx
|
|---|
| 658 | compiler.codeGenerator.op_pop(REG_EBX);
|
|---|
| 659 |
|
|---|
| 660 | //xor eax,eax
|
|---|
| 661 | compiler.codeGenerator.op_xor_RR(REG_EAX);
|
|---|
| 662 |
|
|---|
| 663 | //mov al,byte ptr[ebx]
|
|---|
| 664 | compiler.codeGenerator.op_mov_RM( sizeof(char), REG_EAX, REG_EBX, 0, MOD_BASE );
|
|---|
| 665 | }
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | bool Opcode_CallFunc( const char *Parameter, const int FuncNum, const Type &baseType, Type &resultType, bool isCallOn )
|
|---|
| 669 | {
|
|---|
| 670 | switch(FuncNum){
|
|---|
| 671 | case FUNC_FIX:
|
|---|
| 672 | if( isCallOn ) Opcode_Func_Fix(Parameter);
|
|---|
| 673 | resultType.SetBasicType( DEF_LONG );
|
|---|
| 674 | break;
|
|---|
| 675 | case FUNC_CUDBL:
|
|---|
| 676 | if( isCallOn ) Opcode_Func_CUDbl(Parameter);
|
|---|
| 677 | resultType.SetBasicType( DEF_DOUBLE );
|
|---|
| 678 | break;
|
|---|
| 679 | case FUNC_LEN:
|
|---|
| 680 | if( isCallOn ) Opcode_Func_Len(Parameter);
|
|---|
| 681 | resultType.SetBasicType( DEF_LONG );
|
|---|
| 682 | break;
|
|---|
| 683 | case FUNC_ADDRESSOF:
|
|---|
| 684 | Opcode_Func_AddressOf( Parameter, baseType, isCallOn, resultType );
|
|---|
| 685 | break;
|
|---|
| 686 | case FUNC_SIZEOF:
|
|---|
| 687 | if( isCallOn ) Opcode_Func_SizeOf(Parameter);
|
|---|
| 688 | resultType.SetBasicType( DEF_LONG );
|
|---|
| 689 | break;
|
|---|
| 690 | case FUNC_VARPTR:
|
|---|
| 691 | Opcode_Func_VarPtr( Parameter, resultType, isCallOn );
|
|---|
| 692 | break;
|
|---|
| 693 | case FUNC_OBJPTR:
|
|---|
| 694 | Opcode_Func_ObjPtr( Parameter, resultType, isCallOn );
|
|---|
| 695 | break;
|
|---|
| 696 | case FUNC_DELEGATE_DYNAMICMETHOD_CALL:
|
|---|
| 697 | Opcode_Func_delegate_call( Parameter, resultType, true, isCallOn );
|
|---|
| 698 | break;
|
|---|
| 699 | case FUNC_DELEGATE_STATICMETHOD_CALL:
|
|---|
| 700 | Opcode_Func_delegate_call( Parameter, resultType, false, isCallOn );
|
|---|
| 701 | break;
|
|---|
| 702 | case FUNC_SYSTEM_GET_NOW_SCOPE_CATCH_ADDRESS:
|
|---|
| 703 | if( isCallOn ) Exception::Opcode_Func_System_GetNowScopeCatchAddress();
|
|---|
| 704 | resultType.SetBasicType( DEF_PTR_VOID );
|
|---|
| 705 | break;
|
|---|
| 706 | case FUNC_SYSTEM_GET_NOW_SCOPE_FINALLY_ADDRESS:
|
|---|
| 707 | if( isCallOn ) Exception::Opcode_Func_System_GetNowScopeFinallyAddress();
|
|---|
| 708 | resultType.SetBasicType( DEF_PTR_VOID );
|
|---|
| 709 | break;
|
|---|
| 710 | case FUNC_SYSTEM_GET_BP:
|
|---|
| 711 | if( isCallOn ) Opcode_Func_System_Get_Bp();
|
|---|
| 712 | resultType.SetBasicType( DEF_LONG );
|
|---|
| 713 | break;
|
|---|
| 714 | case FUNC_SYSTEM_GET_SP:
|
|---|
| 715 | if( isCallOn ) Opcode_Func_System_Get_Sp();
|
|---|
| 716 | resultType.SetBasicType( DEF_LONG );
|
|---|
| 717 | break;
|
|---|
| 718 |
|
|---|
| 719 | case FUNC_GETDOUBLE:
|
|---|
| 720 | if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
|
|---|
| 721 | resultType.SetBasicType( DEF_DOUBLE );
|
|---|
| 722 | break;
|
|---|
| 723 | case FUNC_GETSINGLE:
|
|---|
| 724 | if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
|
|---|
| 725 | resultType.SetBasicType( DEF_SINGLE );
|
|---|
| 726 | break;
|
|---|
| 727 | case FUNC_GETQWORD:
|
|---|
| 728 | if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
|
|---|
| 729 | resultType.SetBasicType( DEF_QWORD );
|
|---|
| 730 | break;
|
|---|
| 731 | case FUNC_GETDWORD:
|
|---|
| 732 | if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
|
|---|
| 733 | resultType.SetBasicType( DEF_DWORD );
|
|---|
| 734 | break;
|
|---|
| 735 | case FUNC_GETWORD:
|
|---|
| 736 | if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_WORD);
|
|---|
| 737 | resultType.SetBasicType( DEF_WORD );
|
|---|
| 738 | break;
|
|---|
| 739 | case FUNC_GETBYTE:
|
|---|
| 740 | if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
|
|---|
| 741 | resultType.SetBasicType( DEF_BYTE );
|
|---|
| 742 | break;
|
|---|
| 743 | default:
|
|---|
| 744 | return false;
|
|---|
| 745 | }
|
|---|
| 746 | return true;
|
|---|
| 747 | }
|
|---|