source: dev/BasicCompiler64/Compile_Func.cpp@ 139

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

CP4を刻印

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