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