[3] | 1 | #include "../BasicCompiler_Common/common.h"
|
---|
| 2 | #include "Opcode.h"
|
---|
| 3 |
|
---|
[76] | 4 | void FreeTempObject(int reg,const CClass *pobj_c){
|
---|
[3] | 5 | if(!IsSafeReg(reg)) SetError(300,NULL,cp);
|
---|
| 6 |
|
---|
[51] | 7 | CMethod *method = pobj_c->GetDestructorMethod();
|
---|
| 8 | if( method ){
|
---|
[3] | 9 | //push reg
|
---|
| 10 | op_push(reg);
|
---|
| 11 |
|
---|
| 12 | //call DestructorProcAddr
|
---|
[75] | 13 | op_call( method->pUserProc );
|
---|
[3] | 14 | }
|
---|
| 15 |
|
---|
| 16 | //push reg
|
---|
| 17 | op_push(reg);
|
---|
| 18 |
|
---|
| 19 | //call free
|
---|
[75] | 20 | extern UserProc *pSub_free;
|
---|
[3] | 21 | op_call(pSub_free);
|
---|
| 22 | }
|
---|
| 23 |
|
---|
[76] | 24 | int CallOperatorProc(int idCalc, const Type &baseType, int *type_stack,LONG_PTR *index_stack,BOOL *bUseHeap,int &sp){
|
---|
[3] | 25 | //オーバーロードされたオペレータ関数を呼び出す
|
---|
| 26 | CClass *pobj_c;
|
---|
| 27 | pobj_c=(CClass *)index_stack[sp-2];
|
---|
| 28 |
|
---|
[75] | 29 | std::vector<UserProc *> subs;
|
---|
[50] | 30 | pobj_c->EnumMethod( idCalc, subs );
|
---|
| 31 | if( subs.size() == 0 ){
|
---|
[3] | 32 | return 0;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 |
|
---|
| 36 | //項の数
|
---|
| 37 | BOOL bTwoTerm=1;
|
---|
| 38 | if(idCalc==CALC_AS) bTwoTerm=0;
|
---|
| 39 |
|
---|
| 40 |
|
---|
| 41 | /////////////////////////////////////////////
|
---|
| 42 | // オーバーロード解決用のパラメータを設定
|
---|
| 43 | /////////////////////////////////////////////
|
---|
| 44 |
|
---|
[75] | 45 | Parameters params;
|
---|
[3] | 46 |
|
---|
| 47 | if(bTwoTerm){
|
---|
[76] | 48 | params.push_back( new Parameter( "", Type( type_stack[sp-1], index_stack[sp-1] ) ) );
|
---|
[3] | 49 | }
|
---|
| 50 |
|
---|
| 51 | //オーバーロードを解決
|
---|
| 52 | char temporary[255];
|
---|
| 53 | if(idCalc==CALC_EQUAL) lstrcpy(temporary,"==");
|
---|
| 54 | else GetCalcName(idCalc,temporary);
|
---|
[76] | 55 | UserProc *pUserProc = OverloadSolution( temporary, subs, params, baseType );
|
---|
[3] | 56 |
|
---|
[75] | 57 | if(!pUserProc){
|
---|
| 58 | if(bTwoTerm){
|
---|
| 59 | delete params[0];
|
---|
| 60 | }
|
---|
[3] | 61 | return -1;
|
---|
| 62 | }
|
---|
| 63 | else{
|
---|
| 64 | //オーバーロードされていないが、パラメータ個数が一致しないとき
|
---|
[75] | 65 | if(params.size()!=pUserProc->Params().size()){
|
---|
| 66 | if(bTwoTerm){
|
---|
| 67 | delete params[0];
|
---|
| 68 | }
|
---|
[3] | 69 | return -1;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[76] | 73 | for(int i=0;i<(int)params.size();i++){
|
---|
[3] | 74 | CheckDifferentType(
|
---|
[75] | 75 | pUserProc->Params()[i]->GetBasicType(),
|
---|
| 76 | pUserProc->Params()[i]->GetIndex(),
|
---|
| 77 | params[i]->GetBasicType(),
|
---|
| 78 | params[i]->GetIndex(),
|
---|
[3] | 79 | "",
|
---|
| 80 | i);
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[75] | 83 | if(bTwoTerm){
|
---|
| 84 | delete params[0];
|
---|
| 85 | }
|
---|
[3] | 86 |
|
---|
[76] | 87 | int right_side_size = GetTypeSize(type_stack[sp-1],index_stack[sp-1]);
|
---|
[3] | 88 |
|
---|
| 89 | if(bTwoTerm){
|
---|
[75] | 90 | if( pUserProc->RealParams()[1]->IsStruct() &&pUserProc->RealParams()[1]->IsRef() == false ){
|
---|
[3] | 91 | //一時オブジェクトはメソッド内で破棄される
|
---|
| 92 | bUseHeap[sp-1]=0;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 |
|
---|
[75] | 97 | if( pUserProc->ReturnType().IsStruct() ){
|
---|
[3] | 98 | //////////////////////////////////////////////////////
|
---|
[64] | 99 | // 戻り値に構造体インスタンスを持つ場合
|
---|
| 100 | // ※ByRef _System_ReturnValue パラメータ用領域を取得
|
---|
[3] | 101 | //////////////////////////////////////////////////////
|
---|
| 102 |
|
---|
[75] | 103 | int object_size = pUserProc->ReturnType().GetClass().GetSize();
|
---|
[3] | 104 |
|
---|
| 105 | //push object_size
|
---|
[67] | 106 | op_push_V(object_size);
|
---|
[3] | 107 |
|
---|
| 108 | //call calloc
|
---|
[75] | 109 | extern UserProc *pSub_calloc;
|
---|
[3] | 110 | op_call(pSub_calloc);
|
---|
| 111 |
|
---|
| 112 | //mov ebx,eax
|
---|
| 113 | op_mov_RR(REG_EBX,REG_EAX);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | //2つの項を取り出す
|
---|
| 118 | if(bTwoTerm){
|
---|
| 119 | if(right_side_size==sizeof(_int64)){
|
---|
| 120 | //pop eax
|
---|
| 121 | op_pop(REG_EAX);
|
---|
| 122 |
|
---|
| 123 | //pop edx
|
---|
| 124 | op_pop(REG_EDX);
|
---|
| 125 | }
|
---|
| 126 | else{
|
---|
| 127 | //pop eax
|
---|
| 128 | op_pop(REG_EAX);
|
---|
| 129 | }
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | //pop ecx
|
---|
| 133 | op_pop(REG_ECX);
|
---|
| 134 |
|
---|
| 135 |
|
---|
| 136 | //ヒープ解放用に退避
|
---|
| 137 | if(bUseHeap[sp-1]){
|
---|
| 138 | //mov esi,eax
|
---|
| 139 | op_mov_RR(REG_ESI,REG_EAX);
|
---|
| 140 | }
|
---|
| 141 | if(bUseHeap[sp-2]){
|
---|
| 142 | //mov edi,ecx
|
---|
| 143 | op_mov_RR(REG_EDI,REG_ECX);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | if(bTwoTerm){
|
---|
| 149 | if(right_side_size==sizeof(_int64)){
|
---|
| 150 | //push edx
|
---|
| 151 | op_push(REG_EDX);
|
---|
| 152 |
|
---|
| 153 | //push eax
|
---|
| 154 | op_push(REG_EAX);
|
---|
| 155 | }
|
---|
| 156 | else{
|
---|
| 157 | //push eax
|
---|
| 158 | op_push(REG_EAX);
|
---|
| 159 | }
|
---|
[64] | 160 |
|
---|
[75] | 161 | if( pUserProc->RealParams()[1]->IsRef() ){
|
---|
[64] | 162 | //一時参照を作成
|
---|
| 163 |
|
---|
| 164 | //mov eax,esp
|
---|
| 165 | op_mov_RR( REG_EAX, REG_ESP );
|
---|
| 166 |
|
---|
| 167 | //push eax
|
---|
| 168 | op_push( REG_EAX );
|
---|
| 169 | }
|
---|
[3] | 170 | }
|
---|
| 171 |
|
---|
[75] | 172 | if( pUserProc->ReturnType().IsStruct() ){
|
---|
[3] | 173 | //push ebx
|
---|
| 174 | op_push(REG_EBX);
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | //push ecx
|
---|
| 178 | op_push(REG_ECX);
|
---|
| 179 |
|
---|
| 180 | //call operator_proc
|
---|
[75] | 181 | op_call(pUserProc);
|
---|
[3] | 182 |
|
---|
[64] | 183 | if(bTwoTerm){
|
---|
[75] | 184 | if( pUserProc->RealParams()[1]->IsRef() ){
|
---|
[64] | 185 | //一時参照を破棄
|
---|
| 186 | op_pop( REG_NON );
|
---|
| 187 | }
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[75] | 190 | if( !pUserProc->ReturnType().IsNull() ){
|
---|
[3] | 191 | //スタックへプッシュ
|
---|
[75] | 192 | PushReturnValue(pUserProc->ReturnType().GetBasicType());
|
---|
[3] | 193 | }
|
---|
| 194 |
|
---|
| 195 | if(bUseHeap[sp-1]){
|
---|
| 196 | FreeTempObject(REG_ESI,(CClass *)index_stack[sp-1]);
|
---|
| 197 | }
|
---|
| 198 | if(bUseHeap[sp-2]){
|
---|
| 199 | FreeTempObject(REG_EDI,(CClass *)index_stack[sp-2]);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | sp--;
|
---|
[76] | 203 | type_stack[sp-1]=pUserProc->ReturnType().GetBasicType();
|
---|
[75] | 204 | index_stack[sp-1]=pUserProc->ReturnType().GetIndex();
|
---|
[3] | 205 |
|
---|
[75] | 206 | if( pUserProc->ReturnType().IsStruct() ){
|
---|
[64] | 207 | //構造体が戻ったときはヒープ領域にインスタンスが格納されている
|
---|
[3] | 208 | //※後にfreeする必要あり
|
---|
| 209 | bUseHeap[sp-1]=1;
|
---|
| 210 | }
|
---|
| 211 | else bUseHeap[sp-1]=0;
|
---|
| 212 |
|
---|
| 213 | return 1;
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[76] | 216 | void CallCastOperatorProc(Type &calcType,BOOL bCalcUseHeap,const Type &toType){
|
---|
| 217 | int type_stack[10];
|
---|
[3] | 218 | LONG_PTR index_stack[10];
|
---|
| 219 | BOOL array_bUseHeap[10];
|
---|
| 220 | int sp=2;
|
---|
| 221 |
|
---|
| 222 | if(bCalcUseHeap){
|
---|
| 223 | //未解放のインスタンスが存在する旨を示す警告
|
---|
| 224 | SetError(-105,NULL,cp);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | //左辺
|
---|
[76] | 228 | type_stack[0]=calcType.GetBasicType();
|
---|
| 229 | index_stack[0]=calcType.GetIndex();
|
---|
[3] | 230 | array_bUseHeap[0]=0;
|
---|
[76] | 231 | type_stack[1]=toType.GetBasicType();
|
---|
| 232 | index_stack[1]=toType.GetIndex();
|
---|
[3] | 233 | array_bUseHeap[1]=0;
|
---|
| 234 |
|
---|
[76] | 235 | int iRet = CallOperatorProc(CALC_AS,toType,type_stack,index_stack,array_bUseHeap,sp);
|
---|
[3] | 236 |
|
---|
| 237 | if(iRet==1){
|
---|
| 238 | //成功したとき
|
---|
[76] | 239 | calcType.SetType( type_stack[0], index_stack[0] );
|
---|
[3] | 240 | return;
|
---|
| 241 | }
|
---|
| 242 | else if(iRet==-1){
|
---|
| 243 | //エラーが発行されたとき
|
---|
| 244 | return;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | //エラーを発行
|
---|
| 248 | SetError(-1,"キャスト演算子がオーバーロードされていません。",cp);
|
---|
| 249 | }
|
---|
[76] | 250 | void CallIndexerGetterProc(const CClass *pobj_Class,char *ObjectName,char *Parameter,Type &resultType){
|
---|
[75] | 251 | std::vector<UserProc *> subs;
|
---|
[50] | 252 | pobj_Class->EnumMethod( CALC_ARRAY_GET, subs );
|
---|
| 253 | if( subs.size() == 0 ){
|
---|
[3] | 254 | return;
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[50] | 257 | Opcode_CallProc(Parameter,subs[0],0,ObjectName,DEF_OBJECT);
|
---|
[76] | 258 | resultType = subs[0]->ReturnType();
|
---|
[3] | 259 | }
|
---|