source: dev/trunk/abdev/BasicCompiler32/OperatorProc.cpp@ 225

Last change on this file since 225 was 225, checked in by dai_9181, 17 years ago

CodeGeneratorクラスのベースを実装

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