source: dev/BasicCompiler64/Compile_Func.cpp@ 106

Last change on this file since 106 was 97, checked in by dai_9181, 18 years ago

関数の戻り値オブジェクトのメンバ・メソッドを一時オブジェクトを介さずに参照できるようにした。

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