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