[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 | void Call_DebugSys_SaveContext(){
|
---|
| 11 | //call _System_GetEip
|
---|
[206] | 12 | extern const UserProc *pSub_System_GetEip;
|
---|
[225] | 13 | compiler.codeGenerator.op_call(pSub_System_GetEip);
|
---|
[3] | 14 |
|
---|
| 15 | //push eax
|
---|
[225] | 16 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
[3] | 17 |
|
---|
| 18 | //push ebp
|
---|
[225] | 19 | compiler.codeGenerator.op_push(REG_EBP);
|
---|
[3] | 20 |
|
---|
| 21 | //call _DebugSys_SaveContext
|
---|
[206] | 22 | extern const UserProc *pSub_DebugSys_SaveContext;
|
---|
[225] | 23 | compiler.codeGenerator.op_call(pSub_DebugSys_SaveContext);
|
---|
[3] | 24 | }
|
---|
| 25 |
|
---|
[76] | 26 | bool Opcode_CallProcPtr( const char *variable, const char *lpszParms,ProcPointer *pProcPointer){
|
---|
[3] | 27 |
|
---|
| 28 | extern BOOL bDebugCompile;
|
---|
| 29 | extern BOOL bDebugSupportProc;
|
---|
| 30 | if(bDebugCompile&&bDebugSupportProc==0)
|
---|
| 31 | Call_DebugSys_SaveContext();
|
---|
| 32 |
|
---|
| 33 |
|
---|
| 34 | ////////////////////////
|
---|
| 35 | // パラメータのセット
|
---|
| 36 | ////////////////////////
|
---|
| 37 |
|
---|
| 38 | //パラメータオブジェクトを生成
|
---|
[71] | 39 | ParamImpl *pobj_parameter=0;
|
---|
[76] | 40 | pobj_parameter=new ParamImpl(lpszParms);
|
---|
[3] | 41 |
|
---|
[77] | 42 | // デフォルト引数を適用
|
---|
| 43 | pobj_parameter->ApplyDefaultParameters( pProcPointer->Params() );
|
---|
| 44 |
|
---|
[3] | 45 | //エラーチェック
|
---|
[75] | 46 | if( !pobj_parameter->ErrorCheck(variable,pProcPointer->Params() ) ){
|
---|
[31] | 47 | //パラメータにエラーがあるときは処理を終える
|
---|
[76] | 48 | return false;
|
---|
[31] | 49 | }
|
---|
[3] | 50 |
|
---|
[20] | 51 | //一時オブジェクトを生成
|
---|
[75] | 52 | pobj_parameter->NewTempParameters( variable,pProcPointer->Params() );
|
---|
[20] | 53 |
|
---|
[3] | 54 | //レジスタ、スタックフレームにセット
|
---|
[75] | 55 | pobj_parameter->SetParameter(variable,pProcPointer->Params() );
|
---|
[3] | 56 |
|
---|
| 57 |
|
---|
| 58 |
|
---|
[20] | 59 | ////////////////////////
|
---|
| 60 | // call
|
---|
| 61 | ////////////////////////
|
---|
[3] | 62 | RELATIVE_VAR RelativeVar;
|
---|
[76] | 63 | GetVarOffsetReadOnly(variable,&RelativeVar,Type());
|
---|
[3] | 64 | SetVarPtrToEax(&RelativeVar);
|
---|
| 65 |
|
---|
| 66 | //mov eax,dword ptr[eax]
|
---|
[235] | 67 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EAX, REG_EAX, 0, MOD_BASE );
|
---|
[3] | 68 |
|
---|
| 69 | //call eax
|
---|
[235] | 70 | compiler.codeGenerator.op_call_R( REG_EAX );
|
---|
[3] | 71 |
|
---|
[20] | 72 |
|
---|
| 73 |
|
---|
| 74 | //一時オブジェクトを破棄
|
---|
| 75 | pobj_parameter->DeleteTempParameters();
|
---|
| 76 |
|
---|
| 77 | //パラメータオブジェクトを破棄
|
---|
| 78 | delete pobj_parameter;
|
---|
| 79 |
|
---|
[76] | 80 | return true;
|
---|
[3] | 81 | }
|
---|
| 82 |
|
---|
[290] | 83 | bool Opcode_CallProc(const char *Parameter,const UserProc *pUserProc,DWORD dwFlags,const char *ObjectName ){
|
---|
[51] | 84 | int i2;
|
---|
[3] | 85 |
|
---|
[75] | 86 | if( pUserProc->IsMacro() ){
|
---|
| 87 | if( lstrcmpi( pUserProc->GetName().c_str(), "Print" ) == 0 ){
|
---|
[3] | 88 | Opcode_Print(Parameter,0);
|
---|
[76] | 89 | return true;
|
---|
[3] | 90 | }
|
---|
[75] | 91 | if( lstrcmpi( pUserProc->GetName().c_str(), "Input" ) == 0 ){
|
---|
[3] | 92 | Opcode_Input(Parameter);
|
---|
[76] | 93 | return true;
|
---|
[3] | 94 | }
|
---|
[75] | 95 | if( lstrcmpi( pUserProc->GetName().c_str(), "Write" ) == 0 ){
|
---|
[3] | 96 | Opcode_Print(Parameter,1);
|
---|
[76] | 97 | return true;
|
---|
[3] | 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[75] | 101 | pUserProc->Using();
|
---|
[3] | 102 |
|
---|
[47] | 103 | bool isStatic = false;
|
---|
[76] | 104 | const CClass *pobj_c = NULL;
|
---|
[135] | 105 | const CMethod *pMethod = NULL;
|
---|
[292] | 106 | Type leftType;
|
---|
[75] | 107 | if( pUserProc->GetParentClassPtr() ){
|
---|
[3] | 108 | //クラスのメンバ関数を呼び出す場合はアクセスチェックを行う
|
---|
[97] | 109 | if(ObjectName[0] && (dwFlags&PROCFLAG_NEW)==0){
|
---|
[3] | 110 | if(lstrcmpi(ObjectName,"Super")==0){
|
---|
[27] | 111 | //クラスメンバ関数内から基底クラスの呼び出し
|
---|
[206] | 112 | pobj_c=compiler.pCompilingClass;
|
---|
[3] | 113 | }
|
---|
| 114 | else{
|
---|
[47] | 115 | //"->"によってオブジェクトを指定する通常のメンバ関数呼び出し
|
---|
[76] | 116 | Type varType;
|
---|
| 117 | GetVarType( ObjectName, varType, false );
|
---|
[290] | 118 | if( NATURAL_TYPE( varType.GetBasicType() ) == DEF_OBJECT )
|
---|
| 119 | {
|
---|
| 120 | pobj_c = &varType.GetClass();
|
---|
[292] | 121 | leftType = varType;
|
---|
[290] | 122 | }
|
---|
| 123 | else
|
---|
| 124 | {
|
---|
[265] | 125 | pobj_c=compiler.GetObjectModule().meta.GetClasses().Find(ObjectName);
|
---|
[47] | 126 | if( pobj_c ){
|
---|
| 127 | isStatic = true;
|
---|
| 128 | }
|
---|
| 129 | else{
|
---|
| 130 | SetError(300,NULL,cp);
|
---|
| 131 | }
|
---|
[3] | 132 | }
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | else{
|
---|
| 136 | if(dwFlags&PROCFLAG_NEW){
|
---|
| 137 | //New演算子によるコンストラクタ呼び出し
|
---|
[75] | 138 | pobj_c=pUserProc->GetParentClassPtr();
|
---|
[3] | 139 | }
|
---|
| 140 | else{
|
---|
| 141 | //クラスメンバ関数内から同一クラスのメンバ関数の呼び出し
|
---|
[206] | 142 | pobj_c=compiler.pCompilingClass;
|
---|
[3] | 143 | }
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
[18] | 147 | /////////////////////////////////
|
---|
| 148 | // メソッド情報を取得
|
---|
| 149 | /////////////////////////////////
|
---|
[27] | 150 | pMethod = NULL;
|
---|
[135] | 151 | if( ! isStatic ) pMethod = pobj_c->GetMethods().GetMethodPtr( pUserProc );
|
---|
[27] | 152 | if( ! pMethod ){
|
---|
[18] | 153 | //動的メソッドが取得できなかったときは静的メソッドを当たる
|
---|
[135] | 154 | pMethod = pobj_c->GetStaticMethods().GetMethodPtr( pUserProc );
|
---|
[18] | 155 | if( !pMethod ){
|
---|
| 156 | SetError(300,NULL,cp);
|
---|
[76] | 157 | return false;
|
---|
[3] | 158 | }
|
---|
[26] | 159 |
|
---|
| 160 | //静的メンバ
|
---|
[47] | 161 | isStatic = true;
|
---|
[3] | 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | //////////////////////////////
|
---|
| 166 | // アクセスエラーチェック
|
---|
| 167 | //////////////////////////////
|
---|
| 168 |
|
---|
| 169 | if(ObjectName[0]){
|
---|
| 170 | //外部からの呼び出し
|
---|
[206] | 171 | if(pobj_c==compiler.pCompilingClass){
|
---|
[3] | 172 | //同一クラスオブジェクトの場合はプライベートアクセスを容認する
|
---|
[137] | 173 | if( pMethod->IsNoneAccess() ){
|
---|
[75] | 174 | SetError(109,pUserProc->GetName(),cp);
|
---|
[76] | 175 | return false;
|
---|
[3] | 176 | }
|
---|
| 177 | }
|
---|
| 178 | else{
|
---|
[137] | 179 | if( pMethod->IsPrivate()
|
---|
| 180 | || pMethod->IsNoneAccess() ){
|
---|
[75] | 181 | SetError(109,pUserProc->GetName(),cp);
|
---|
[76] | 182 | return false;
|
---|
[3] | 183 | }
|
---|
[137] | 184 | if( pMethod->IsProtected() ){
|
---|
[75] | 185 | SetError(110,pUserProc->GetName(),cp);
|
---|
[76] | 186 | return false;
|
---|
[3] | 187 | }
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | else{
|
---|
| 191 | //クラス内部からの呼び出し(継承によるACCESS_NONのみをエラーとする)
|
---|
[137] | 192 | if( pMethod->IsNoneAccess() ){
|
---|
[75] | 193 | SetError(109,pUserProc->GetName(),cp);
|
---|
[76] | 194 | return false;
|
---|
[3] | 195 | }
|
---|
| 196 | }
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 |
|
---|
| 200 | ///////////////////////////////////////////////////////////////
|
---|
[64] | 201 | // _System_LocalThisのダミーをセット
|
---|
[3] | 202 | ///////////////////////////////////////////////////////////////
|
---|
| 203 |
|
---|
| 204 | char temporary[VN_SIZE]={0};
|
---|
[75] | 205 | if( pUserProc->GetParentClassPtr() && isStatic == false ){
|
---|
[3] | 206 | //_System_LocalThis(第一パラメータ)のダミーを作成
|
---|
| 207 | lstrcpy(temporary,"0,");
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | if(Parameter[0]=='\0'&&temporary[0])
|
---|
| 211 | temporary[lstrlen(temporary)-1]=0;
|
---|
| 212 | else lstrcat(temporary,Parameter);
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 | ////////////////////////
|
---|
| 216 | // パラメータをセット
|
---|
| 217 | ////////////////////////
|
---|
| 218 |
|
---|
| 219 | //パラメータオブジェクトを生成
|
---|
[71] | 220 | ParamImpl *pobj_parameter=0;
|
---|
| 221 | pobj_parameter=new ParamImpl(temporary);
|
---|
[3] | 222 |
|
---|
[77] | 223 | // デフォルト引数を適用
|
---|
| 224 | pobj_parameter->ApplyDefaultParameters( pUserProc->RealParams() );
|
---|
| 225 |
|
---|
[292] | 226 | // 型パラメータを適用
|
---|
| 227 | pobj_parameter->SetLeftType( leftType );
|
---|
| 228 |
|
---|
[3] | 229 | //エラーチェック
|
---|
[75] | 230 | if( !pobj_parameter->ErrorCheck(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetSecondParmNum() ) ){
|
---|
[31] | 231 | //パラメータにエラーがあるときは処理を終える
|
---|
[76] | 232 | return false;
|
---|
[31] | 233 | }
|
---|
[3] | 234 |
|
---|
[75] | 235 | if(pUserProc->IsMacro()){
|
---|
[3] | 236 | //マクロ関数の場合は、パラメータ省略を考慮する
|
---|
[75] | 237 | pobj_parameter->MacroParameterSupport( pUserProc->RealParams() );
|
---|
[3] | 238 | }
|
---|
| 239 |
|
---|
[20] | 240 | //一時オブジェクトを生成
|
---|
[75] | 241 | int tempSize = pobj_parameter->NewTempParameters( pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum() );
|
---|
[20] | 242 |
|
---|
[3] | 243 | //レジスタ、スタックフレームにセット
|
---|
[75] | 244 | int ParmSize = pobj_parameter->SetParameter(pUserProc->GetName(),pUserProc->RealParams(),pUserProc->GetRealSecondParmNum() );
|
---|
[3] | 245 |
|
---|
[75] | 246 | if(pUserProc->ReturnType().IsStruct() ){
|
---|
[3] | 247 | //////////////////////////////////////////////////////
|
---|
[64] | 248 | // 戻り値に構造体インスタンスを持つ場合
|
---|
| 249 | // ※ByRef _System_ReturnValue パラメータをセット
|
---|
[3] | 250 | //////////////////////////////////////////////////////
|
---|
| 251 |
|
---|
[75] | 252 | int object_size = pUserProc->ReturnType().GetClass().GetSize();
|
---|
[3] | 253 |
|
---|
| 254 | //push object_size
|
---|
[225] | 255 | compiler.codeGenerator.op_push_V(object_size);
|
---|
[3] | 256 |
|
---|
| 257 | //call calloc
|
---|
[206] | 258 | extern const UserProc *pSub_calloc;
|
---|
[225] | 259 | compiler.codeGenerator.op_call(pSub_calloc);
|
---|
[3] | 260 |
|
---|
| 261 | //push eax
|
---|
[225] | 262 | compiler.codeGenerator.op_push(REG_EAX);
|
---|
[3] | 263 | }
|
---|
| 264 |
|
---|
| 265 |
|
---|
[75] | 266 | if( pUserProc->GetParentClassPtr() && isStatic == false ){
|
---|
[3] | 267 | //////////////////////////////////////////////////////
|
---|
| 268 | // メンバ関数の場合
|
---|
| 269 | // ※_System_LocalThis パラメータをセット
|
---|
| 270 | //////////////////////////////////////////////////////
|
---|
| 271 |
|
---|
[97] | 272 | if(ObjectName[0] && (dwFlags&PROCFLAG_NEW)==0){
|
---|
[3] | 273 | if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
|
---|
[18] | 274 | else{
|
---|
| 275 | RELATIVE_VAR RelativeVar;
|
---|
[135] | 276 | if( pMethod->IsConst() ){
|
---|
[18] | 277 | //Constアクセスが可能なメソッドの場合
|
---|
[76] | 278 | if( !GetVarOffsetReadOnly( ObjectName, &RelativeVar, Type() ) ){
|
---|
| 279 | return false;
|
---|
| 280 | }
|
---|
[18] | 281 | }
|
---|
| 282 | else{
|
---|
| 283 | //Constアクセスが不可能なメソッドの場合
|
---|
[76] | 284 | if( !GetVarOffsetReadWrite( ObjectName, &RelativeVar, Type() ) ){
|
---|
| 285 | return false;
|
---|
| 286 | }
|
---|
[18] | 287 | }
|
---|
[3] | 288 |
|
---|
[18] | 289 | SetVarPtrToEax(&RelativeVar);
|
---|
[3] | 290 |
|
---|
[64] | 291 | // 参照を実体ポインタにする
|
---|
[225] | 292 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE );
|
---|
[3] | 293 | }
|
---|
| 294 | }
|
---|
| 295 | else{
|
---|
| 296 | InClassMember:
|
---|
| 297 | if(dwFlags&PROCFLAG_NEW){
|
---|
| 298 | //New演算子によるコンストラクタ呼び出しの場合
|
---|
[64] | 299 |
|
---|
[3] | 300 | //mov ecx,dword ptr[esp+ParmSize]
|
---|
[225] | 301 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_ECX, REG_ESP, ParmSize + tempSize, MOD_BASE_DISP32 );
|
---|
[3] | 302 | }
|
---|
| 303 | else{
|
---|
| 304 | //Thisポインタをecxにコピー
|
---|
| 305 | SetThisPtrToReg(REG_ECX);
|
---|
| 306 | }
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | //push ecx
|
---|
[225] | 310 | compiler.codeGenerator.op_push(REG_ECX);
|
---|
[3] | 311 | }
|
---|
| 312 |
|
---|
[75] | 313 | if( pUserProc->IsVirtual() ){
|
---|
[3] | 314 | //仮想関数(オブジェクトメソッド)呼び出し
|
---|
| 315 | //pObj->func_table->func1
|
---|
| 316 | // ->func2
|
---|
| 317 | // ->func3
|
---|
| 318 |
|
---|
| 319 | //mov edx,dword ptr[ecx]
|
---|
[235] | 320 | compiler.codeGenerator.op_mov_RM( sizeof(long), REG_EDX, REG_ECX, 0, MOD_BASE );
|
---|
[3] | 321 |
|
---|
[75] | 322 | i2 = pobj_c->GetFuncNumInVtbl( pUserProc );
|
---|
[3] | 323 |
|
---|
| 324 | //call dword ptr[edx+func_index]
|
---|
| 325 | if(i2*PTR_SIZE<=0x7F){
|
---|
[250] | 326 | compiler.codeGenerator.PutOld(
|
---|
| 327 | (char)0xFF,
|
---|
| 328 | (char)0x52,
|
---|
| 329 | (char)(i2*PTR_SIZE)
|
---|
| 330 | );
|
---|
[3] | 331 | }
|
---|
| 332 | else{
|
---|
[250] | 333 | compiler.codeGenerator.PutOld(
|
---|
| 334 | (char)0xFF,
|
---|
| 335 | (char)0x92
|
---|
| 336 | );
|
---|
| 337 | compiler.codeGenerator.PutOld( (long)(i2*PTR_SIZE), Schedule::None );
|
---|
[3] | 338 | }
|
---|
| 339 | }
|
---|
| 340 | else{
|
---|
| 341 | //通常呼び出し
|
---|
| 342 |
|
---|
| 343 | //call ProcAddr
|
---|
[225] | 344 | compiler.codeGenerator.op_call(pUserProc);
|
---|
[3] | 345 | }
|
---|
| 346 |
|
---|
[75] | 347 | if(pUserProc->IsCdecl()){
|
---|
[3] | 348 | //add esp,ParmSize
|
---|
[225] | 349 | compiler.codeGenerator.op_add_esp(ParmSize);
|
---|
[3] | 350 | }
|
---|
| 351 |
|
---|
[20] | 352 | //一時オブジェクトを破棄
|
---|
| 353 | pobj_parameter->DeleteTempParameters();
|
---|
| 354 |
|
---|
| 355 | //パラメータオブジェクトを破棄
|
---|
| 356 | delete pobj_parameter;
|
---|
[76] | 357 |
|
---|
| 358 | return true;
|
---|
[3] | 359 | }
|
---|
| 360 |
|
---|
[250] | 361 | bool Opcode_CallDllProc( const char *lpszParms, const DllProc *pDllProc ){
|
---|
[3] | 362 |
|
---|
| 363 | extern BOOL bDebugCompile;
|
---|
| 364 | extern BOOL bDebugSupportProc;
|
---|
[113] | 365 | if(bDebugCompile&&bDebugSupportProc==0&& pDllProc->IsEqualSymbol( "DebugBreak" ) ){
|
---|
[3] | 366 | Call_DebugSys_SaveContext();
|
---|
[75] | 367 | }
|
---|
[3] | 368 |
|
---|
| 369 |
|
---|
| 370 | ////////////////////////
|
---|
| 371 | // パラメータのセット
|
---|
| 372 | ////////////////////////
|
---|
| 373 |
|
---|
| 374 | //パラメータオブジェクトを生成
|
---|
[71] | 375 | ParamImpl *pobj_parameter=0;
|
---|
[76] | 376 | pobj_parameter=new ParamImpl(lpszParms);
|
---|
[3] | 377 |
|
---|
[77] | 378 | // デフォルト引数を適用
|
---|
| 379 | pobj_parameter->ApplyDefaultParameters( pDllProc->Params() );
|
---|
| 380 |
|
---|
[3] | 381 | //エラーチェック
|
---|
[75] | 382 | if( !pobj_parameter->ErrorCheck( pDllProc->GetName(), pDllProc->Params() ) ){
|
---|
[31] | 383 | //パラメータにエラーがあるときは処理を終える
|
---|
[76] | 384 | return false;
|
---|
[31] | 385 | }
|
---|
[3] | 386 |
|
---|
[45] | 387 | //一時オブジェクトを生成
|
---|
[75] | 388 | pobj_parameter->NewTempParameters( pDllProc->GetName(), pDllProc->Params() );
|
---|
[45] | 389 |
|
---|
[3] | 390 | //レジスタ、スタックフレームにセット
|
---|
[75] | 391 | int ParmSize = pobj_parameter->SetParameter(pDllProc->GetName(), pDllProc->Params() );
|
---|
[3] | 392 |
|
---|
| 393 |
|
---|
| 394 | //動的リンクされたプロシージャの呼び出し
|
---|
| 395 |
|
---|
| 396 | //call dword ptr[LookupTable]
|
---|
[250] | 397 | compiler.codeGenerator.op_call( pDllProc );
|
---|
[3] | 398 |
|
---|
[75] | 399 | if(pDllProc->IsCdecl()){
|
---|
[3] | 400 | //add esp,ParmSize
|
---|
[225] | 401 | compiler.codeGenerator.op_add_esp(ParmSize);
|
---|
[3] | 402 | }
|
---|
| 403 |
|
---|
[45] | 404 | //一時オブジェクトを破棄
|
---|
| 405 | pobj_parameter->DeleteTempParameters();
|
---|
| 406 |
|
---|
| 407 | //パラメータオブジェクトを破棄
|
---|
| 408 | delete pobj_parameter;
|
---|
| 409 |
|
---|
[76] | 410 | return true;
|
---|
[3] | 411 | }
|
---|