| 1 | #include "stdafx.h"
|
|---|
| 2 |
|
|---|
| 3 | #include <jenga/include/smoothie/LexicalAnalysis.h>
|
|---|
| 4 |
|
|---|
| 5 | #include <Compiler.h>
|
|---|
| 6 |
|
|---|
| 7 | #include "../BasicCompiler_Common/common.h"
|
|---|
| 8 | #include "Opcode.h"
|
|---|
| 9 |
|
|---|
| 10 | void FreeTempObject(int reg,const CClass *pobj_c){
|
|---|
| 11 | if(!IsSafeReg(reg)) SetError(300,NULL,cp);
|
|---|
| 12 |
|
|---|
| 13 | ////////////////////////////////////////////////
|
|---|
| 14 | // 演算過程で利用した一時オブジェクトを破棄
|
|---|
| 15 | // Thisポインタをr14レジスタを介して渡す
|
|---|
| 16 | ////////////////////////////////////////////////
|
|---|
| 17 |
|
|---|
| 18 | const CMethod *method = pobj_c->GetDestructorMethod();
|
|---|
| 19 | if( method ){
|
|---|
| 20 | //mov rcx,reg
|
|---|
| 21 | compiler.codeGenerator.op_mov_RR(REG_RCX,reg);
|
|---|
| 22 |
|
|---|
| 23 | //call DestructorProcAddr
|
|---|
| 24 | compiler.codeGenerator.op_call( &method->GetUserProc() );
|
|---|
| 25 | }
|
|---|
| 26 |
|
|---|
| 27 | //mov rcx,reg
|
|---|
| 28 | compiler.codeGenerator.op_mov_RR(REG_RCX,reg);
|
|---|
| 29 |
|
|---|
| 30 | //call free
|
|---|
| 31 | extern const UserProc *pSub_free;
|
|---|
| 32 | compiler.codeGenerator.op_call(pSub_free);
|
|---|
| 33 | }
|
|---|
| 34 |
|
|---|
| 35 | int CallOperatorProc(BYTE idCalc, const Type &baseType, int *type_stack,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp){
|
|---|
| 36 | //オーバーロードされたオペレータ関数を呼び出す
|
|---|
| 37 | CClass *pobj_c;
|
|---|
| 38 | pobj_c=(CClass *)index_stack[sp-2];
|
|---|
| 39 |
|
|---|
| 40 | std::vector<const UserProc *> subs;
|
|---|
| 41 | pobj_c->GetMethods().Enum( idCalc, subs );
|
|---|
| 42 | if( subs.size() == 0 ){
|
|---|
| 43 | return 0;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | //項の数
|
|---|
| 48 | BOOL bTwoTerm=1;
|
|---|
| 49 | if(idCalc==CALC_AS) bTwoTerm=0;
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | /////////////////////////////////////////////
|
|---|
| 53 | // オーバーロード解決用のパラメータを設定
|
|---|
| 54 | /////////////////////////////////////////////
|
|---|
| 55 |
|
|---|
| 56 | Parameters params;
|
|---|
| 57 |
|
|---|
| 58 | if(bTwoTerm){
|
|---|
| 59 | params.push_back( new Parameter( "", Type( type_stack[sp-1], index_stack[sp-1] ) ) );
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | //オーバーロードを解決
|
|---|
| 63 | char temporary[255];
|
|---|
| 64 | if(idCalc==CALC_EQUAL) lstrcpy(temporary,"==");
|
|---|
| 65 | else GetCalcName(idCalc,temporary);
|
|---|
| 66 | const UserProc *pUserProc = OverloadSolution( temporary, subs, params, baseType );
|
|---|
| 67 |
|
|---|
| 68 | if(!pUserProc){
|
|---|
| 69 | if(bTwoTerm){
|
|---|
| 70 | delete params[0];
|
|---|
| 71 | }
|
|---|
| 72 | return -1;
|
|---|
| 73 | }
|
|---|
| 74 | else{
|
|---|
| 75 | //オーバーロードされていないが、パラメータ個数が一致しないとき
|
|---|
| 76 | if(params.size()!=pUserProc->Params().size()){
|
|---|
| 77 | if(bTwoTerm){
|
|---|
| 78 | delete params[0];
|
|---|
| 79 | }
|
|---|
| 80 | return -1;
|
|---|
| 81 | }
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | for(int i=0;i<(int)params.size();i++){
|
|---|
| 85 | CheckDifferentType(
|
|---|
| 86 | *pUserProc->Params()[i],
|
|---|
| 87 | *params[i],
|
|---|
| 88 | "",
|
|---|
| 89 | i);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | if(bTwoTerm){
|
|---|
| 93 | delete params[0];
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | int right_side_size = Type(type_stack[sp-1],index_stack[sp-1]).GetSize();
|
|---|
| 97 |
|
|---|
| 98 | if(bTwoTerm){
|
|---|
| 99 | if( pUserProc->RealParams()[1]->IsStruct() &&pUserProc->RealParams()[1]->IsRef() == false ){
|
|---|
| 100 | //一時オブジェクトはメソッド内で破棄される
|
|---|
| 101 | bUseHeap[sp-1]=0;
|
|---|
| 102 | }
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | if( pUserProc->ReturnType().IsStruct() ){
|
|---|
| 107 | //////////////////////////////////////////////////////
|
|---|
| 108 | // 戻り値に構造体インスタンスを持つ場合
|
|---|
| 109 | // ※ByRef _System_ReturnValue パラメータ用領域を取得
|
|---|
| 110 | //////////////////////////////////////////////////////
|
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 | //////////////////////////////////////////////////////
|
|---|
| 114 | ///// レジスタ資源のバックアップ
|
|---|
| 115 | { BACKUP_REGISTER_RESOURCE
|
|---|
| 116 | //////////////////////////////////////////////////////
|
|---|
| 117 |
|
|---|
| 118 | int object_size = pUserProc->ReturnType().GetClass().GetSize();
|
|---|
| 119 |
|
|---|
| 120 | //mov rcx,object_size
|
|---|
| 121 | compiler.codeGenerator.op_mov_RV(sizeof(_int64),REG_RCX,object_size);
|
|---|
| 122 |
|
|---|
| 123 | //call calloc
|
|---|
| 124 | extern const UserProc *pSub_calloc;
|
|---|
| 125 | compiler.codeGenerator.op_call(pSub_calloc);
|
|---|
| 126 |
|
|---|
| 127 | //mov r13,rax
|
|---|
| 128 | compiler.codeGenerator.op_mov_RR(REG_R13,REG_RAX);
|
|---|
| 129 |
|
|---|
| 130 | /////////////////////////////////////////////
|
|---|
| 131 | ////// レジスタ資源を復元
|
|---|
| 132 | RESTORE_REGISTER_RESOURCE
|
|---|
| 133 | }////////////////////////////////////////////
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | int reg1,reg2;
|
|---|
| 137 | if(bTwoTerm){
|
|---|
| 138 | //右の項(実数の場合が未完成)
|
|---|
| 139 | SetOneTermToReg_Whole64Calc(type_stack[sp-1],®2);
|
|---|
| 140 | pobj_reg->UnlockReg();
|
|---|
| 141 | if( !pUserProc->RealParams()[1]->IsRef() == false ){
|
|---|
| 142 | //一時参照を作成
|
|---|
| 143 | pobj_sf->push( reg2 );
|
|---|
| 144 | pobj_sf->mov_sp( reg2 );
|
|---|
| 145 | }
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | //左の項
|
|---|
| 149 | SetOneTermToReg_Whole64Calc(DEF_INT64,®1);
|
|---|
| 150 | pobj_reg->UnlockReg();
|
|---|
| 151 |
|
|---|
| 152 | //ヒープ解放用に退避
|
|---|
| 153 | if(bUseHeap[sp-1]){
|
|---|
| 154 | //mov qword ptr[rsp+offset],reg2 ※スタックフレームを利用
|
|---|
| 155 | pobj_sf->push(reg2);
|
|---|
| 156 | }
|
|---|
| 157 | if(bUseHeap[sp-2]){
|
|---|
| 158 | //mov qword ptr[rsp+offset],reg1 ※スタックフレームを利用
|
|---|
| 159 | pobj_sf->push(reg1);
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | //////////////////////////////////////////////////////
|
|---|
| 165 | ///// レジスタ資源のバックアップ
|
|---|
| 166 | { BACKUP_REGISTER_RESOURCE
|
|---|
| 167 | //////////////////////////////////////////////////////
|
|---|
| 168 |
|
|---|
| 169 | if(reg1==REG_RDX||reg1==REG_R8){
|
|---|
| 170 | //mov r14,reg1
|
|---|
| 171 | compiler.codeGenerator.op_mov_RR(REG_R14,reg1);
|
|---|
| 172 | reg1=REG_R14;
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 |
|
|---|
| 176 | if(bTwoTerm){
|
|---|
| 177 | if( pUserProc->ReturnType().IsStruct() ){
|
|---|
| 178 | //mov r8,reg2
|
|---|
| 179 | compiler.codeGenerator.op_mov_RR(REG_R8,reg2);
|
|---|
| 180 | }
|
|---|
| 181 | else{
|
|---|
| 182 | //mov rdx,reg2
|
|---|
| 183 | compiler.codeGenerator.op_mov_RR(REG_RDX,reg2);
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | if( pUserProc->ReturnType().IsStruct() ){
|
|---|
| 188 | //mov rdx,r13
|
|---|
| 189 | compiler.codeGenerator.op_mov_RR(REG_RDX,REG_R13);
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | //mov rcx,reg1
|
|---|
| 193 | compiler.codeGenerator.op_mov_RR(REG_RCX,reg1);
|
|---|
| 194 |
|
|---|
| 195 | //call operator_proc
|
|---|
| 196 | compiler.codeGenerator.op_call(pUserProc);
|
|---|
| 197 |
|
|---|
| 198 | if( !pUserProc->ReturnType().IsNull() ){
|
|---|
| 199 | //戻り値を一時的に退避
|
|---|
| 200 |
|
|---|
| 201 | //mov r13,rax
|
|---|
| 202 | compiler.codeGenerator.op_mov_RR(REG_R13,REG_RAX);
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 | /////////////////////////////////////////////
|
|---|
| 207 | ////// レジスタ資源を復元
|
|---|
| 208 | RESTORE_REGISTER_RESOURCE
|
|---|
| 209 | }////////////////////////////////////////////
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 |
|
|---|
| 213 | if(bUseHeap[sp-2]||bUseHeap[sp-1]){
|
|---|
| 214 |
|
|---|
| 215 | //////////////////////////////////////////////////////
|
|---|
| 216 | ///// レジスタ資源のバックアップ
|
|---|
| 217 | { BACKUP_REGISTER_RESOURCE
|
|---|
| 218 | //////////////////////////////////////////////////////
|
|---|
| 219 |
|
|---|
| 220 | if(bUseHeap[sp-2]){
|
|---|
| 221 | //mov r14,qword ptr[rsp+offset] ※スタックフレームを利用
|
|---|
| 222 | pobj_sf->pop(REG_R14);
|
|---|
| 223 |
|
|---|
| 224 | FreeTempObject(REG_R14,(CClass *)index_stack[sp-2]);
|
|---|
| 225 | }
|
|---|
| 226 | if(bUseHeap[sp-1]){
|
|---|
| 227 | //mov r14,qword ptr[rsp+offset] ※スタックフレームを利用
|
|---|
| 228 | pobj_sf->pop(REG_R14);
|
|---|
| 229 |
|
|---|
| 230 | FreeTempObject(REG_R14,(CClass *)index_stack[sp-1]);
|
|---|
| 231 | }
|
|---|
| 232 |
|
|---|
| 233 | /////////////////////////////////////////////
|
|---|
| 234 | ////// レジスタ資源を復元
|
|---|
| 235 | RESTORE_REGISTER_RESOURCE
|
|---|
| 236 | }////////////////////////////////////////////
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | if(bTwoTerm){
|
|---|
| 240 | if( !pUserProc->RealParams()[1]->IsRef() == false ){
|
|---|
| 241 | //一時参照を破棄
|
|---|
| 242 | pobj_sf->pop();
|
|---|
| 243 | }
|
|---|
| 244 | }
|
|---|
| 245 |
|
|---|
| 246 | if( !pUserProc->ReturnType().IsNull() ){
|
|---|
| 247 | //戻り値をreg1にセット
|
|---|
| 248 | reg1=pobj_reg->LockReg();
|
|---|
| 249 |
|
|---|
| 250 | //mov reg1,r13
|
|---|
| 251 | compiler.codeGenerator.op_mov_RR(reg1,REG_R13);
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 | sp--;
|
|---|
| 255 | type_stack[sp-1]=pUserProc->ReturnType().GetBasicType();
|
|---|
| 256 | index_stack[sp-1]=pUserProc->ReturnType().GetIndex();
|
|---|
| 257 |
|
|---|
| 258 | if( pUserProc->ReturnType().IsStruct() ){
|
|---|
| 259 | //構造体が戻ったときはヒープ領域にインスタンスが格納されている
|
|---|
| 260 | //※後にfreeする必要あり
|
|---|
| 261 | bUseHeap[sp-1]=1;
|
|---|
| 262 | }
|
|---|
| 263 | else bUseHeap[sp-1]=0;
|
|---|
| 264 |
|
|---|
| 265 | return 1;
|
|---|
| 266 | }
|
|---|
| 267 |
|
|---|
| 268 | void CallCastOperatorProc(int reg,Type &calcType,BOOL bCalcUseHeap,const Type &toType){
|
|---|
| 269 | int type_stack[10];
|
|---|
| 270 | LONG_PTR index_stack[10];
|
|---|
| 271 | BOOL array_bUseHeap[10];
|
|---|
| 272 | int sp=2;
|
|---|
| 273 | int iRet;
|
|---|
| 274 |
|
|---|
| 275 |
|
|---|
| 276 | //////////////////////////////////////////////////////
|
|---|
| 277 | ///// レジスタ資源のバックアップ
|
|---|
| 278 | { BACKUP_REGISTER_RESOURCE
|
|---|
| 279 | //////////////////////////////////////////////////////
|
|---|
| 280 |
|
|---|
| 281 | //regを第一項目としてロック
|
|---|
| 282 | pobj_reg=new CRegister(reg);
|
|---|
| 283 | pobj_reg->LockReg();
|
|---|
| 284 |
|
|---|
| 285 | if(bCalcUseHeap){
|
|---|
| 286 | //未解放のインスタンスが存在する旨を示す警告
|
|---|
| 287 | SetError(-105,NULL,cp);
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | //左辺
|
|---|
| 291 | type_stack[0]=calcType.GetBasicType();
|
|---|
| 292 | index_stack[0]=calcType.GetIndex();
|
|---|
| 293 | array_bUseHeap[0]=0;
|
|---|
| 294 | type_stack[1]=toType.GetBasicType();
|
|---|
| 295 | index_stack[1]=toType.GetIndex();
|
|---|
| 296 | array_bUseHeap[1]=0;
|
|---|
| 297 |
|
|---|
| 298 | iRet=CallOperatorProc(CALC_AS,toType,type_stack,index_stack,array_bUseHeap,sp);
|
|---|
| 299 |
|
|---|
| 300 | pobj_reg->UnlockReg();
|
|---|
| 301 |
|
|---|
| 302 | /////////////////////////////////////////////
|
|---|
| 303 | ////// レジスタ資源を復元
|
|---|
| 304 | RESTORE_REGISTER_RESOURCE
|
|---|
| 305 | }////////////////////////////////////////////
|
|---|
| 306 |
|
|---|
| 307 |
|
|---|
| 308 | if(iRet==1){
|
|---|
| 309 | //成功したとき
|
|---|
| 310 | calcType.SetType( type_stack[0], index_stack[0] );
|
|---|
| 311 | return;
|
|---|
| 312 | }
|
|---|
| 313 | else if(iRet==-1){
|
|---|
| 314 | //エラーが発行されたとき
|
|---|
| 315 | return;
|
|---|
| 316 | }
|
|---|
| 317 |
|
|---|
| 318 | //エラーを発行
|
|---|
| 319 | SetError(-1,"キャスト演算子がオーバーロードされていません。",cp);
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | //インデクサ(getter)を呼び出す
|
|---|
| 323 | void CallIndexerGetterProc(int reg, const Type &classType, char *ObjectName,char *Parameter,Type &resultType ){
|
|---|
| 324 |
|
|---|
| 325 | std::vector<const UserProc *> subs;
|
|---|
| 326 | classType.GetClass().GetMethods().Enum( CALC_ARRAY_GET, subs );
|
|---|
| 327 | if( subs.size() == 0 ){
|
|---|
| 328 | return;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | const UserProc *pUserProc = subs[0];
|
|---|
| 332 |
|
|---|
| 333 | //////////////////////////////////////////////////////
|
|---|
| 334 | ///// レジスタ資源のバックアップ
|
|---|
| 335 | { BACKUP_REGISTER_RESOURCE
|
|---|
| 336 | //////////////////////////////////////////////////////
|
|---|
| 337 |
|
|---|
| 338 | Opcode_CallProc(Parameter,pUserProc,0,ObjectName);
|
|---|
| 339 | resultType = pUserProc->ReturnType();
|
|---|
| 340 |
|
|---|
| 341 | //mov reg,rax
|
|---|
| 342 | compiler.codeGenerator.op_mov_RR(reg,REG_RAX);
|
|---|
| 343 |
|
|---|
| 344 | /////////////////////////////////////////////
|
|---|
| 345 | ////// レジスタ資源を復元
|
|---|
| 346 | RESTORE_REGISTER_RESOURCE
|
|---|
| 347 | }////////////////////////////////////////////
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 350 | // 型パラメータを解決
|
|---|
| 351 | ResolveFormalGenericTypeParameter( resultType, classType, pUserProc );
|
|---|
| 352 | }
|
|---|