source: dev/trunk/abdev/BasicCompiler64/Compile_Func.cpp@ 332

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

AddressOfの左辺値を加味した処理を簡潔にした

File size: 9.7 KB
RevLine 
[206]1#include "stdafx.h"
2
[183]3#include <jenga/include/smoothie/Smoothie.h>
4
[198]5#include <Compiler.h>
6
[3]7#include "../BasicCompiler_Common/common.h"
8#include "Opcode.h"
9
10int GetFunctionFromName(char *FuncName){
[325]11 if( lstrcmpi( FuncName, "Len" ) == 0 ) return FUNC_LEN;
12 if( lstrcmpi( FuncName, "AddressOf" ) == 0 ) return FUNC_ADDRESSOF;
13 if( lstrcmpi( FuncName, "SizeOf" ) == 0 ) return FUNC_SIZEOF;
14 if( lstrcmpi( FuncName, "VarPtr" ) == 0 ) return FUNC_VARPTR;
15 if( lstrcmpi( FuncName, "ObjPtr" ) == 0 ) return FUNC_OBJPTR;
[330]16 if( lstrcmpi( FuncName, "__delegate_dynamicmethod_call" ) == 0 ) return FUNC_DELEGATE_DYNAMICMETHOD_CALL;
17 if( lstrcmpi( FuncName, "__delegate_staticmethod_call" ) == 0 ) return FUNC_DELEGATE_STATICMETHOD_CALL;
[325]18 if( lstrcmpi( FuncName, "GetDouble" ) == 0 ) return FUNC_GETDOUBLE;
19 if( lstrcmpi( FuncName, "GetSingle" ) == 0 ) return FUNC_GETSINGLE;
20 if( lstrcmpi( FuncName, "GetQWord" ) == 0 ) return FUNC_GETQWORD;
21 if( lstrcmpi( FuncName, "GetDWord" ) == 0 ) return FUNC_GETDWORD;
22 if( lstrcmpi( FuncName, "GetWord" ) == 0 ) return FUNC_GETWORD;
23 if( lstrcmpi( FuncName, "GetByte" ) == 0 ) return FUNC_GETBYTE;
[3]24 return 0;
25}
[46]26void Opcode_Func_Len( const char *Parameter ){
[3]27 BOOL bArrayHead;
28
[46]29 const char *tempParm=Parameter;
[3]30 char temporary[VN_SIZE];
31 char temp2[32];
[75]32 Type type;
33 if( !GetVarType(Parameter,type,0) ){
[3]34 sprintf(temporary,"_System_DummyStr2=%s",Parameter);
35 OpcodeCalc(temporary);
36
37 lstrcpy(temp2,"_System_DummyStr2");
38 tempParm=temp2;
39
[266]40 type.SetType( DEF_OBJECT, compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr() );
[3]41 }
42
[97]43 if( type.IsStringClass() ){
[3]44 //Stringオブジェクトの場合
45 sprintf(temporary,"%s.Length",tempParm);
46
47 int reg=REG_RAX;
[76]48 NumOpe(&reg,temporary,Type(),Type());
[3]49 return;
50 }
51
[206]52 Subscripts subscripts;
[3]53 RELATIVE_VAR RelativeVar;
[206]54 if(!GetVarOffsetReadOnly(tempParm,&RelativeVar,type,&subscripts)) return;
[3]55
[75]56 if(type.GetBasicType()&FLAG_PTR){
57 type.SetBasicType( type.GetBasicType() & ( ~FLAG_PTR ) );
[3]58
59 bArrayHead=1;
60 }
61 else bArrayHead=0;
62
[75]63 int typeSize = type.GetSize();
[3]64
[206]65 if(bArrayHead) typeSize*=JumpSubScripts(subscripts);
[3]66
67 //mov rax,TypeSize
[226]68 compiler.codeGenerator.op_mov_RV(sizeof(_int64),REG_RAX,typeSize);
[3]69
70 return;
71}
[331]72void Opcode_Func_AddressOf( const char *name, const Type &baseType ){
[3]73 extern int cp;
[206]74 const UserProc *pUserProc;
[3]75
[331]76 if( baseType.IsProcPtr() )
77 {
[3]78 //左辺の型にのっとり、オーバーロードを解決
79
[206]80 std::vector<const UserProc *> subs;
[50]81 GetOverloadSubHash( name, subs );
82 if( subs.size() == 0 ){
[3]83 SetError(27,name,cp);
84 return;
85 }
86
87 //オーバーロードを解決
[331]88 pUserProc=OverloadSolution(name,subs,compiler.GetObjectModule().meta.GetProcPointers()[baseType.GetIndex()]->Params(), Type() );
[3]89
[75]90 if(!pUserProc){
[3]91 SetError(27,name,cp);
92 return;
93 }
94 }
95 else{
[75]96 pUserProc=GetSubHash(name);
97 if(!pUserProc){
[3]98 SetError(27,name,cp);
99 return;
100 }
101 }
102
[75]103 if( pUserProc->IsVirtual() ){
[3]104 ///////////////////////////////
105 // 仮想関数の場合
106 // thisポインタをrcxにコピー
107 ///////////////////////////////
108
[115]109 const CClass *pobj_c;
[3]110
111 char ObjectName[VN_SIZE];
[308]112 ReferenceKind referenceKind;
113 SplitObjectName(name,ObjectName, referenceKind );
[3]114
115 if(ObjectName[0]){
116 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
117 else{
118 RELATIVE_VAR RelativeVar;
[75]119 Type type;
120 if(!GetVarOffsetReadOnly(ObjectName,&RelativeVar,type)) return;
[3]121 SetVarPtrToReg(REG_RCX,&RelativeVar);
122
123 //参照タイプが整合しているかをチェック
[308]124 if( !( type.IsObject() && referenceKind == RefDot
125 || type.IsObjectPtr() && referenceKind == RefPointer ) )
126 {
127 SetError(104,ObjectName,cp);
128 }
[3]129
[75]130 if(type.IsObjectPtr()){
[3]131 //mov rcx,qword ptr[rcx]
[226]132 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RCX,REG_RCX,0,MOD_BASE);
[3]133 }
134 }
135 }
136 else{
137InClassMember:
138 //自身のオブジェクトのThisポインタをrcxにコピー
139 SetThisPtrToReg(REG_RCX);
140
[206]141 pobj_c=compiler.pCompilingClass;
[3]142 }
143
144
145 //仮想関数(オブジェクトメソッド)
146 //pObj->func_table->func1
147 // ->func2
148 // ->func3
149
150 //mov r11,qword ptr[rcx]
[226]151 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RCX,0,MOD_BASE);
[3]152
[75]153 int i2 = pobj_c->GetFuncNumInVtbl( pUserProc );
[3]154
155 //mov rax,qword ptr[r11+func_index]
156 if(i2*PTR_SIZE<=0x7F){
[226]157 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RAX,REG_R11,i2*PTR_SIZE,MOD_BASE_DISP8);
[3]158 }
159 else{
[226]160 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_RAX,REG_R11,i2*PTR_SIZE,MOD_BASE_DISP32);
[3]161 }
162 }
163 else{
164 //一般の関数
165
166 //mov rax,ProcAddr
[262]167 compiler.codeGenerator.op_addressof( REG_RAX, pUserProc );
[3]168 }
169
[75]170 pUserProc->Using();
[3]171}
[79]172void Opcode_Func_SizeOf( const string &typeName ){
173 Type tempType;
[308]174 if( !compiler.StringToType( typeName, tempType ) ){
[79]175 SetError(3,typeName,cp);
176 return;
[64]177 }
[3]178
[79]179 int typeSize = ( tempType.IsObject() ) ?
180 tempType.GetClass().GetSize() : tempType.GetSize();
181
[3]182 //mov rax,size
[226]183 compiler.codeGenerator.op_mov_RV(sizeof(_int64),REG_RAX,typeSize);
[3]184}
[75]185void Opcode_Func_VarPtr( const char *Parameter, Type &resultType, bool isCallOn ){
186 if( isCallOn == false ){
187 // 戻り値の型を取得するだけ
188
189 //変数のアドレスを取得
190 if(!GetVarType( Parameter, resultType, true )) return;
191
192 resultType.PtrLevelUp();
193
194 return;
195 }
196
[3]197 RELATIVE_VAR RelativeVar;
198
199 //変数のアドレスを取得
[75]200 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
[3]201
[75]202 int beforeType = resultType.GetBasicType();
[64]203
[75]204 resultType.PtrLevelUp();
[46]205
[3]206 SetVarPtrToReg(REG_RAX,&RelativeVar);
[64]207
[109]208 // TODO: 取り除く(この動きはObjPtrに託す)
[130]209 /*
[64]210 if( beforeType == DEF_OBJECT && lstrcmpi( Parameter, "This" ) != 0 ){
211 //参照をオブジェクトポインタに変更
212
213 //mov rax,qword ptr[rax]
[226]214 compiler.codeGenerator.op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
[111]215
216 SetError(-120,NULL,cp);
[130]217 }*/
[3]218}
[109]219void Opcode_Func_ObjPtr( const char *Parameter, Type &resultType, bool isCallOn ){
220 if( isCallOn == false ){
221 // 戻り値の型を取得するだけ
222
223 //変数のアドレスを取得
224 if(!GetVarType( Parameter, resultType, true )) return;
225
226 resultType.PtrLevelUp();
227
228 return;
229 }
230
231 RELATIVE_VAR RelativeVar;
232
233 //変数のアドレスを取得
234 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
235
236 int beforeType = resultType.GetBasicType();
237
238 resultType.PtrLevelUp();
239
240 SetVarPtrToReg(REG_RAX,&RelativeVar);
241
[111]242 if( lstrcmpi( Parameter, "This" )==0 ){
243 // Thisの場合は特別にオブジェクトポインタが返ってくるので、何もせずに抜ける
244 }
245 else if( beforeType == DEF_OBJECT ){
[109]246 //参照をオブジェクトポインタに変更
247
248 //mov rax,qword ptr[rax]
[226]249 compiler.codeGenerator.op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
[109]250 }
251 else{
252 SetError(134,NULL,cp );
253 }
254}
[325]255
[330]256void Opcode_Func_delegate_call( const char *paramsStr, Type &resultType, bool isDynamicCall, bool isCallOn )
[325]257{
258 if( isCallOn )
259 {
260 int i = 0;
[330]261 char methodPtrParamStr[VN_SIZE];
262 i = GetOneParameter( paramsStr, i, methodPtrParamStr );
263
264 char objPtrValueStr[VN_SIZE];
265 if( isDynamicCall )
266 {
267 i = GetOneParameter( paramsStr, i, objPtrValueStr );
268 }
269
270 Opcode_CallDelegate( compiler.pCompilingClass->GetDelegate(), methodPtrParamStr, objPtrValueStr, paramsStr + i );
[325]271 }
272
273 resultType = UserProc::CompilingUserProc().ReturnType();
274}
275
[46]276void Opcode_Func_GetPtrData( const char *Parameter, const int type ){
[3]277 int reg=REG_RAX;
[75]278 Type tempType;
279 if( !NumOpe(&reg,Parameter,Type(),tempType) ){
280 return;
281 }
282 if(!tempType.IsWhole()){
[3]283 SetError(11,Parameter,cp);
284 return;
285 }
286
287 if(type==DEF_DOUBLE){
288 //movlpd xmm0,qword ptr[rax]
[226]289 compiler.codeGenerator.op_movlpd_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
[3]290 }
291 else if(type==DEF_SINGLE){
292 //movss xmm0,dword ptr[rax]
[226]293 compiler.codeGenerator.op_movss_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
[3]294 }
295 else{
296 //mov rax,ptr[rax]
[308]297 compiler.codeGenerator.op_mov_RM(Type(type).GetSize(),REG_RAX,REG_RAX,0,MOD_BASE);
[3]298 }
299}
300
[331]301bool Opcode_CallFunc( const char *Parameter, const int FuncNum, const Type &baseType, Type &resultType, bool isCallOn )
302{
[3]303 switch(FuncNum){
304 case FUNC_LEN:
[75]305 if( isCallOn ) Opcode_Func_Len(Parameter);
306 resultType.SetBasicType( DEF_LONG );
[46]307 break;
[3]308 case FUNC_ADDRESSOF:
[331]309 if( isCallOn ) Opcode_Func_AddressOf( Parameter, baseType );
[75]310 resultType.SetBasicType( DEF_PTR_VOID );
[46]311 break;
[3]312 case FUNC_SIZEOF:
[75]313 if( isCallOn ) Opcode_Func_SizeOf(Parameter);
314 resultType.SetBasicType( DEF_LONG );
[46]315 break;
[3]316 case FUNC_VARPTR:
[75]317 Opcode_Func_VarPtr( Parameter, resultType, isCallOn );
[46]318 break;
[109]319 case FUNC_OBJPTR:
320 Opcode_Func_ObjPtr( Parameter, resultType, isCallOn );
321 break;
[330]322 case FUNC_DELEGATE_DYNAMICMETHOD_CALL:
323 Opcode_Func_delegate_call( Parameter, resultType, true, isCallOn );
[325]324 break;
[330]325 case FUNC_DELEGATE_STATICMETHOD_CALL:
326 Opcode_Func_delegate_call( Parameter, resultType, false, isCallOn );
327 break;
[3]328
329 case FUNC_GETDOUBLE:
[75]330 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
331 resultType.SetBasicType( DEF_DOUBLE );
[46]332 break;
[3]333 case FUNC_GETSINGLE:
[75]334 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
335 resultType.SetBasicType( DEF_SINGLE );
[46]336 break;
[3]337 case FUNC_GETQWORD:
[75]338 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
339 resultType.SetBasicType( DEF_QWORD );
[46]340 break;
[3]341 case FUNC_GETDWORD:
[75]342 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
343 resultType.SetBasicType( DEF_DWORD );
[46]344 break;
[3]345 case FUNC_GETWORD:
[75]346 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_WORD);
347 resultType.SetBasicType( DEF_WORD );
[46]348 break;
[3]349 case FUNC_GETBYTE:
[75]350 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
351 resultType.SetBasicType( DEF_BYTE );
[46]352 break;
[75]353 default:
354 return false;
[3]355 }
[75]356 return true;
[3]357}
Note: See TracBrowser for help on using the repository browser.