| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <Compiler.h>
|
|---|
| 4 |
|
|---|
| 5 | #include "../BasicCompiler_Common/common.h"
|
|---|
| 6 | #include "opcode.h"
|
|---|
| 7 |
|
|---|
| 8 | int ParamImpl::NewTempParameters( const string &procName, const Parameters ¶ms, int SecondParmNum ){
|
|---|
| 9 | if( SecondParmNum == -1 ) SecondParmNum = (int)params.size();
|
|---|
| 10 |
|
|---|
| 11 | ///////////////////////////////////////////////////////
|
|---|
| 12 | // 一時オブジェクトをあらかじめスタックに積んでおく
|
|---|
| 13 | ///////////////////////////////////////////////////////
|
|---|
| 14 |
|
|---|
| 15 | useTempObject = false;
|
|---|
| 16 |
|
|---|
| 17 | //一時参照の数
|
|---|
| 18 | nCountOfTempObjects = 0;
|
|---|
| 19 |
|
|---|
| 20 | BOOL bEllipse;
|
|---|
| 21 | if(params.size()){
|
|---|
| 22 | if(params[params.size()-1]->GetBasicType()==DEF_ELLIPSE) bEllipse=1;
|
|---|
| 23 | else bEllipse=0;
|
|---|
| 24 | }
|
|---|
| 25 | else bEllipse=0;
|
|---|
| 26 |
|
|---|
| 27 | for(int i2=ParmsNum-1;i2>=0;i2--){
|
|---|
| 28 | useTempParameters[i2] = false;
|
|---|
| 29 |
|
|---|
| 30 | if(bEllipse&&i2<=(int)params.size()-2) bEllipse=0;
|
|---|
| 31 |
|
|---|
| 32 | if(i2==0){
|
|---|
| 33 | if( params[i2]->GetVarName() == "_System_LocalThis" ){
|
|---|
| 34 | //オブジェクトメンバの第一パラメータのThisポインタ
|
|---|
| 35 | continue;
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 | if( i2==0||i2==1 ){
|
|---|
| 39 | if( params[i2]->GetVarName() == procName ){
|
|---|
| 40 | //オブジェクトメンバの第一または第二パラメータの戻り値用オブジェクト
|
|---|
| 41 | continue;
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | Type dummyType;
|
|---|
| 46 | BOOL bByVal;
|
|---|
| 47 | if(bEllipse){
|
|---|
| 48 | NumOpe_GetType( Parms[i2], Type(), dummyType );
|
|---|
| 49 | bByVal=1;
|
|---|
| 50 | }
|
|---|
| 51 | else{
|
|---|
| 52 | dummyType = *params[i2];
|
|---|
| 53 | bByVal = ( params[i2]->IsRef() == false ) ? TRUE:FALSE;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | if( !bByVal ){
|
|---|
| 58 | //ポインタ参照
|
|---|
| 59 | if(Parms[i2][0]==1&&Parms[i2][1]==ESC_BYVAL){
|
|---|
| 60 | //ポインタ指定
|
|---|
| 61 | continue;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | if( !GetVarType( Parms[i2], Type(), FALSE ) ){
|
|---|
| 65 | //変数ではないとき
|
|---|
| 66 | Type calcType;
|
|---|
| 67 | bool isNeedHeapFreeStructure;
|
|---|
| 68 | NumOpe( Parms[i2], dummyType, calcType, &isNeedHeapFreeStructure );
|
|---|
| 69 | //↑ここでスタックに積む
|
|---|
| 70 |
|
|---|
| 71 | nCountOfTempObjects++;
|
|---|
| 72 |
|
|---|
| 73 | if( !calcType.IsStruct() ){
|
|---|
| 74 | //一時参照を作成
|
|---|
| 75 |
|
|---|
| 76 | //push esp
|
|---|
| 77 | compiler.codeGenerator.op_push( REG_ESP );
|
|---|
| 78 |
|
|---|
| 79 | nCountOfTempObjects++;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | bool result = CheckDifferentType(
|
|---|
| 83 | dummyType,
|
|---|
| 84 | calcType,
|
|---|
| 85 | procName.c_str(),
|
|---|
| 86 | i2);
|
|---|
| 87 |
|
|---|
| 88 | if( result )
|
|---|
| 89 | {
|
|---|
| 90 | useTempParameters[i2] = true;
|
|---|
| 91 | isNeedFreeStructures[i2] = isNeedHeapFreeStructure;
|
|---|
| 92 | useTempObject = true;
|
|---|
| 93 |
|
|---|
| 94 | types[i2] = calcType;
|
|---|
| 95 | }
|
|---|
| 96 | }
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | return nCountOfTempObjects * PTR_SIZE;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | void ParamImpl::DeleteTempParameters(){
|
|---|
| 104 | ///////////////////////////////////////////////////////
|
|---|
| 105 | // 一時オブジェクトを破棄
|
|---|
| 106 | ///////////////////////////////////////////////////////
|
|---|
| 107 | if( !useTempObject ) return;
|
|---|
| 108 |
|
|---|
| 109 | for(int i2=ParmsNum-1;i2>=0;i2--){
|
|---|
| 110 | if( useTempParameters[i2] ){
|
|---|
| 111 | if( types[i2].IsStruct() )
|
|---|
| 112 | {
|
|---|
| 113 | // 構造体の一時メモリ
|
|---|
| 114 |
|
|---|
| 115 | if( isNeedFreeStructures[i2] )
|
|---|
| 116 | {
|
|---|
| 117 | //メモリを解放する
|
|---|
| 118 |
|
|---|
| 119 | //call free
|
|---|
| 120 | extern const UserProc *pSub_free;
|
|---|
| 121 | compiler.codeGenerator.op_call(pSub_free);
|
|---|
| 122 | }
|
|---|
| 123 | else
|
|---|
| 124 | {
|
|---|
| 125 | //pop ... 参照を消す
|
|---|
| 126 | compiler.codeGenerator.op_add_esp( PTR_SIZE );
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | else
|
|---|
| 130 | {
|
|---|
| 131 | if( types[i2].Is64() )
|
|---|
| 132 | {
|
|---|
| 133 | //pop ... 参照を消す
|
|---|
| 134 | //pop ... 上位32ビット
|
|---|
| 135 | //pop ... 下位32ビット
|
|---|
| 136 | compiler.codeGenerator.op_add_esp( PTR_SIZE * 3 );
|
|---|
| 137 | }
|
|---|
| 138 | else
|
|---|
| 139 | {
|
|---|
| 140 | //pop ... 参照を消す
|
|---|
| 141 | //pop ... 値を消す
|
|---|
| 142 | compiler.codeGenerator.op_add_esp( PTR_SIZE * 2 );
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | void ParamImpl::SetStructParameter( const Type &baseType, const char *expression ){
|
|---|
| 150 | int object_size = baseType.GetClass().GetSize();
|
|---|
| 151 |
|
|---|
| 152 | //push object_size
|
|---|
| 153 | compiler.codeGenerator.op_push_V(object_size);
|
|---|
| 154 |
|
|---|
| 155 | //call calloc
|
|---|
| 156 | extern const UserProc *pSub_calloc;
|
|---|
| 157 | compiler.codeGenerator.op_call(pSub_calloc);
|
|---|
| 158 |
|
|---|
| 159 | //push eax(ここでプッシュされた値が実際にパラメータとして引き渡される)
|
|---|
| 160 | compiler.codeGenerator.op_push(REG_EAX);
|
|---|
| 161 |
|
|---|
| 162 | //push eax
|
|---|
| 163 | compiler.codeGenerator.op_push(REG_EAX);
|
|---|
| 164 |
|
|---|
| 165 | Type calcType;
|
|---|
| 166 | bool isNeedHeapFreeStructure;
|
|---|
| 167 | NumOpe( expression,
|
|---|
| 168 | baseType,
|
|---|
| 169 | calcType,
|
|---|
| 170 | &isNeedHeapFreeStructure );
|
|---|
| 171 |
|
|---|
| 172 | // ※スタックにある二つのデータ(コピー先、コピー元)の値を必要とする
|
|---|
| 173 | SetStructVariable( baseType, calcType, isNeedHeapFreeStructure );
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | int ParamImpl::SetParameter( const string &procName, const Parameters ¶ms, int SecondParmNum, const UserProc *pUserProc ){
|
|---|
| 177 | if( SecondParmNum == -1 ) SecondParmNum = (int)params.size();
|
|---|
| 178 |
|
|---|
| 179 | ///////////////////////////////////////////////////////////
|
|---|
| 180 | // パラメータをレジスタ及びスタックフレームにセット
|
|---|
| 181 | ///////////////////////////////////////////////////////////
|
|---|
| 182 | int i2,i3;
|
|---|
| 183 |
|
|---|
| 184 | BOOL bEllipse;
|
|---|
| 185 | if( params.size() ){
|
|---|
| 186 | if(params[params.size()-1]->GetBasicType()==DEF_ELLIPSE) bEllipse=1;
|
|---|
| 187 | else bEllipse=0;
|
|---|
| 188 | }
|
|---|
| 189 | else bEllipse=0;
|
|---|
| 190 |
|
|---|
| 191 | BOOL bHas_System_LocalThis=0;
|
|---|
| 192 | if(ParmsNum>=1){
|
|---|
| 193 | if( params[0]->GetVarName() == "_System_LocalThis" ){
|
|---|
| 194 | bHas_System_LocalThis=1;
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | //戻り値用の変数名を取得
|
|---|
| 199 | const char *lpszVarNameToReturn = (procName[0]==1&&procName[1]==ESC_OPERATOR)?"_System_ReturnValue":procName.c_str();
|
|---|
| 200 |
|
|---|
| 201 | //パラメータをレジスタとスタックに格納
|
|---|
| 202 | int ParmSize=0;
|
|---|
| 203 | RELATIVE_VAR RelativeVar;
|
|---|
| 204 | int nCountOfNowTempObjects = 0;
|
|---|
| 205 | for(i2=ParmsNum-1;i2>=0;i2--){
|
|---|
| 206 | if(bEllipse&&i2<=(int)params.size()-2) bEllipse=0;
|
|---|
| 207 |
|
|---|
| 208 | if(i2==0){
|
|---|
| 209 | if( params[i2]->GetVarName() == "_System_LocalThis" ){
|
|---|
| 210 | //オブジェクトメンバの第一パラメータのThisポインタ
|
|---|
| 211 | continue;
|
|---|
| 212 | }
|
|---|
| 213 | }
|
|---|
| 214 | if(i2==0||i2==1){
|
|---|
| 215 | if( params[i2]->GetVarName() == lpszVarNameToReturn ){
|
|---|
| 216 | //オブジェクトメンバの第一または第二パラメータの戻り値用オブジェクト
|
|---|
| 217 | continue;
|
|---|
| 218 | }
|
|---|
| 219 | }
|
|---|
| 220 |
|
|---|
| 221 | Type dummyType;
|
|---|
| 222 | BOOL bByVal;
|
|---|
| 223 | if(bEllipse){
|
|---|
| 224 | NumOpe_GetType( Parms[i2], Type(), dummyType );
|
|---|
| 225 | bByVal=1;
|
|---|
| 226 | }
|
|---|
| 227 | else{
|
|---|
| 228 | dummyType = *params[i2];
|
|---|
| 229 | bByVal = ( params[i2]->IsRef() == false ) ? TRUE:FALSE;
|
|---|
| 230 |
|
|---|
| 231 | // 型パラメータを解決
|
|---|
| 232 | ResolveFormalGenericTypeParameter( dummyType, leftType, pUserProc );
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | if(bByVal==1){
|
|---|
| 236 | //値参照
|
|---|
| 237 | /*
|
|---|
| 238 | if(Parms[i2][0]==1&&Parms[i2][1]==ESC_BYVAL){
|
|---|
| 239 | char temp2[255];
|
|---|
| 240 | sprintf(temp2,"%s関数の第%dパラメータ",procName.c_str(),i2+1);
|
|---|
| 241 | compiler.errorMessenger.Output(19,temp2,cp);
|
|---|
| 242 | continue;
|
|---|
| 243 | }
|
|---|
| 244 | */
|
|---|
| 245 | if( dummyType.IsStruct() ){
|
|---|
| 246 | SetStructParameter( dummyType, Parms[i2] );
|
|---|
| 247 | goto next;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | Type calcType;
|
|---|
| 251 | bool isNeedHeapFreeStructure;
|
|---|
| 252 | if( !NumOpe( Parms[i2], dummyType, calcType, &isNeedHeapFreeStructure ) )
|
|---|
| 253 | {
|
|---|
| 254 | break;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 | if( calcType.IsObject() )
|
|---|
| 258 | {
|
|---|
| 259 | if( !dummyType.IsObject()
|
|---|
| 260 | ||
|
|---|
| 261 | dummyType.IsObject() &&
|
|---|
| 262 | !dummyType.GetClass().IsEqualsOrSubClass( &calcType.GetClass() ) )
|
|---|
| 263 | {
|
|---|
| 264 | //キャスト演算子のオーバーロードに対応する
|
|---|
| 265 | CallCastOperatorProc( calcType, isNeedHeapFreeStructure,dummyType );
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | if(!bEllipse){
|
|---|
| 270 | //型チェック
|
|---|
| 271 | // TODO: _System_ReturnValueが考慮されていない?
|
|---|
| 272 | if(bHas_System_LocalThis) i3=i2-1;
|
|---|
| 273 | else i3=i2;
|
|---|
| 274 | CheckDifferentType(
|
|---|
| 275 | dummyType,
|
|---|
| 276 | calcType,
|
|---|
| 277 | procName.c_str(),
|
|---|
| 278 | i3);
|
|---|
| 279 | }
|
|---|
| 280 |
|
|---|
| 281 | if( dummyType.IsDouble() ){
|
|---|
| 282 | ChangeTypeToDouble( calcType.GetBasicType() );
|
|---|
| 283 | ParmSize+=sizeof(long)*2;
|
|---|
| 284 | }
|
|---|
| 285 | else if( dummyType.IsSingle() ){
|
|---|
| 286 | ChangeTypeToSingle( calcType.GetBasicType() );
|
|---|
| 287 | ParmSize+=sizeof(long);
|
|---|
| 288 | }
|
|---|
| 289 | else if( dummyType.Is64() ){
|
|---|
| 290 | ChangeTypeToInt64( calcType.GetBasicType() );
|
|---|
| 291 | ParmSize+=sizeof(long)*2;
|
|---|
| 292 | }
|
|---|
| 293 | else if( dummyType.IsLong() || dummyType.IsDWord()
|
|---|
| 294 | || dummyType.IsPointer()
|
|---|
| 295 | || dummyType.IsObject() || dummyType.IsStruct() ){
|
|---|
| 296 | ChangeTypeToLong( calcType.GetBasicType() );
|
|---|
| 297 | ParmSize+=sizeof(long);
|
|---|
| 298 | }
|
|---|
| 299 | else if( dummyType.IsInteger() || dummyType.IsWord() ){
|
|---|
| 300 | ChangeTypeToInteger( calcType.GetBasicType() );
|
|---|
| 301 | ParmSize+=sizeof(long);
|
|---|
| 302 | }
|
|---|
| 303 | else if( dummyType.IsSByte() || dummyType.IsByte() || dummyType.IsBoolean() ){
|
|---|
| 304 | ChangeTypeToByte( calcType.GetBasicType() );
|
|---|
| 305 | ParmSize+=sizeof(long);
|
|---|
| 306 | }
|
|---|
| 307 | else{
|
|---|
| 308 | compiler.errorMessenger.Output(300,NULL,cp);
|
|---|
| 309 | }
|
|---|
| 310 | }
|
|---|
| 311 | else{
|
|---|
| 312 | //ポインタ参照
|
|---|
| 313 | if(Parms[i2][0]==1&&Parms[i2][1]==ESC_BYVAL){
|
|---|
| 314 | //ポインタ指定
|
|---|
| 315 |
|
|---|
| 316 | Type calcType;
|
|---|
| 317 | if( !NumOpe( Parms[i2]+2, dummyType, calcType) ){
|
|---|
| 318 | break;
|
|---|
| 319 | }
|
|---|
| 320 |
|
|---|
| 321 | ChangeTypeToLong( calcType.GetBasicType() );
|
|---|
| 322 |
|
|---|
| 323 | dummyType.PtrLevelUp();
|
|---|
| 324 |
|
|---|
| 325 | //型チェック
|
|---|
| 326 | if(bHas_System_LocalThis) i3=i2-1;
|
|---|
| 327 | else i3=i2;
|
|---|
| 328 | CheckDifferentType(
|
|---|
| 329 | dummyType,
|
|---|
| 330 | calcType,
|
|---|
| 331 | procName.c_str(),
|
|---|
| 332 | i3);
|
|---|
| 333 | }
|
|---|
| 334 | else{
|
|---|
| 335 | if( useTempParameters[i2] ){
|
|---|
| 336 | //一時オブジェクトをコピー
|
|---|
| 337 |
|
|---|
| 338 | if( !types[i2].IsStruct() ){
|
|---|
| 339 | // 一時参照のための領域を考慮する
|
|---|
| 340 | nCountOfNowTempObjects++;
|
|---|
| 341 | }
|
|---|
| 342 |
|
|---|
| 343 | nCountOfNowTempObjects++;
|
|---|
| 344 |
|
|---|
| 345 | //mov eax, dword ptr[esp+offset]
|
|---|
| 346 | compiler.codeGenerator.op_mov_RM(
|
|---|
| 347 | sizeof(long),
|
|---|
| 348 | REG_EAX,
|
|---|
| 349 | REG_ESP,
|
|---|
| 350 | ( ( ParmsNum - i2 - 1 ) + ( nCountOfTempObjects - nCountOfNowTempObjects ) ) * PTR_SIZE,
|
|---|
| 351 | MOD_BASE_DISP32 );
|
|---|
| 352 |
|
|---|
| 353 | //push eax
|
|---|
| 354 | compiler.codeGenerator.op_push(REG_EAX);
|
|---|
| 355 | }
|
|---|
| 356 | else{
|
|---|
| 357 | //変数のアドレスを取得
|
|---|
| 358 | Type varType;
|
|---|
| 359 | if(GetVarOffset(
|
|---|
| 360 | false,
|
|---|
| 361 | false,
|
|---|
| 362 | Parms[i2],
|
|---|
| 363 | &RelativeVar,
|
|---|
| 364 | varType)){
|
|---|
| 365 | if( !dummyType.IsAny() ){
|
|---|
| 366 | //型チェックを行う
|
|---|
| 367 | if( dummyType.GetBasicType() == varType.GetBasicType() ){
|
|---|
| 368 | if( dummyType.IsObject() ){
|
|---|
| 369 | if( !dummyType.GetClass().IsEqualsOrSubClass( &varType.GetClass() ) ){
|
|---|
| 370 | compiler.errorMessenger.Output(11,Parms[i2],cp);
|
|---|
| 371 | }
|
|---|
| 372 | }
|
|---|
| 373 | else if( dummyType.IsStruct() ){
|
|---|
| 374 | if( !dummyType.GetClass().IsEquals( &varType.GetClass() ) ){
|
|---|
| 375 | compiler.errorMessenger.Output(11,Parms[i2],cp);
|
|---|
| 376 | }
|
|---|
| 377 | }
|
|---|
| 378 | }
|
|---|
| 379 | else if( (varType.GetBasicType()&FLAG_PTR)
|
|---|
| 380 | &&((varType.GetBasicType()^FLAG_PTR)==dummyType.GetBasicType())){
|
|---|
| 381 | //仮引数がポインタ参照で、実引数が配列の先頭ポインタのとき
|
|---|
| 382 | }
|
|---|
| 383 | else{
|
|---|
| 384 | compiler.errorMessenger.Output(11,Parms[i2],cp);
|
|---|
| 385 | }
|
|---|
| 386 | }
|
|---|
| 387 |
|
|---|
| 388 | //変数アドレスをレジスタにセット
|
|---|
| 389 | SetVarPtrToEax(&RelativeVar);
|
|---|
| 390 |
|
|---|
| 391 | //push eax
|
|---|
| 392 | compiler.codeGenerator.op_push(REG_EAX);
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|
| 395 | }
|
|---|
| 396 |
|
|---|
| 397 | ParmSize+=PTR_SIZE;
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | next:;
|
|---|
| 401 | }
|
|---|
| 402 |
|
|---|
| 403 | return ParmSize;
|
|---|
| 404 | }
|
|---|