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
Line 
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;
9 if(lstrcmpi(FuncName,"ObjPtr")==0) return FUNC_OBJPTR;
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}
18void Opcode_Func_Len( const char *Parameter ){
19 BOOL bArrayHead;
20
21 const char *tempParm=Parameter;
22 char temporary[VN_SIZE];
23 char temp2[32];
24 Type type;
25 if( !GetVarType(Parameter,type,0) ){
26 sprintf(temporary,"_System_DummyStr2=%s",Parameter);
27 OpcodeCalc(temporary);
28
29 lstrcpy(temp2,"_System_DummyStr2");
30 tempParm=temp2;
31
32 type.SetType( DEF_OBJECT, pobj_DBClass->GetStringClassPtr() );
33 }
34
35 if( type.IsStringClass() ){
36 //Stringオブジェクトの場合
37 sprintf(temporary,"%s.Length",tempParm);
38
39 int reg=REG_RAX;
40 NumOpe(&reg,temporary,Type(),Type());
41 return;
42 }
43
44 int SubScripts[MAX_ARRAYDIM];
45 RELATIVE_VAR RelativeVar;
46 if(!GetVarOffsetReadOnly(tempParm,&RelativeVar,type,SubScripts)) return;
47
48 if(type.GetBasicType()&FLAG_PTR){
49 type.SetBasicType( type.GetBasicType() & ( ~FLAG_PTR ) );
50
51 bArrayHead=1;
52 }
53 else bArrayHead=0;
54
55 int typeSize = type.GetSize();
56
57 if(bArrayHead) typeSize*=JumpSubScripts(SubScripts);
58
59 //mov rax,TypeSize
60 op_mov_RV(sizeof(_int64),REG_RAX,typeSize);
61
62 return;
63}
64void Opcode_Func_AddressOf( const char *name ){
65 extern int cp;
66 UserProc *pUserProc;
67
68 extern LONG_PTR ProcPtr_BaseIndex;
69 if(ProcPtr_BaseIndex!=-1){
70 //左辺の型にのっとり、オーバーロードを解決
71
72 std::vector<UserProc *> subs;
73 GetOverloadSubHash( name, subs );
74 if( subs.size() == 0 ){
75 SetError(27,name,cp);
76 return;
77 }
78
79 //オーバーロードを解決
80 pUserProc=OverloadSolution(name,subs,Smoothie::Meta::procPointers[ProcPtr_BaseIndex]->Params(), Type() );
81
82 if(!pUserProc){
83 SetError(27,name,cp);
84 return;
85 }
86 }
87 else{
88 pUserProc=GetSubHash(name);
89 if(!pUserProc){
90 SetError(27,name,cp);
91 return;
92 }
93 }
94
95 if( pUserProc->IsVirtual() ){
96 ///////////////////////////////
97 // 仮想関数の場合
98 // thisポインタをrcxにコピー
99 ///////////////////////////////
100
101 const CClass *pobj_c;
102
103 char ObjectName[VN_SIZE];
104 int RefType;
105 SplitObjectName(name,ObjectName,&RefType);
106
107 if(ObjectName[0]){
108 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
109 else{
110 RELATIVE_VAR RelativeVar;
111 Type type;
112 if(!GetVarOffsetReadOnly(ObjectName,&RelativeVar,type)) return;
113 SetVarPtrToReg(REG_RCX,&RelativeVar);
114
115 //参照タイプが整合しているかをチェック
116 if(type.GetBasicType()!=RefType) SetError(104,ObjectName,cp);
117
118 if(type.IsObjectPtr()){
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
141 int i2 = pobj_c->GetFuncNumInVtbl( pUserProc );
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);
157 pobj_SubAddrSchedule->add(pUserProc,0);
158 obp+=sizeof(long);
159 }
160
161 pUserProc->Using();
162}
163void Opcode_Func_SizeOf( const string &typeName ){
164 Type tempType;
165 if( !Type::StringToType( typeName, tempType ) ){
166 SetError(3,typeName,cp);
167 return;
168 }
169
170 int typeSize = ( tempType.IsObject() ) ?
171 tempType.GetClass().GetSize() : tempType.GetSize();
172
173 //mov rax,size
174 op_mov_RV(sizeof(_int64),REG_RAX,typeSize);
175}
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
188 RELATIVE_VAR RelativeVar;
189
190 //変数のアドレスを取得
191 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
192
193 int beforeType = resultType.GetBasicType();
194
195 resultType.PtrLevelUp();
196
197 SetVarPtrToReg(REG_RAX,&RelativeVar);
198
199 // TODO: 取り除く(この動きはObjPtrに託す)
200 /*
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 );
206
207 SetError(-120,NULL,cp);
208 }*/
209}
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
233 if( lstrcmpi( Parameter, "This" )==0 ){
234 // Thisの場合は特別にオブジェクトポインタが返ってくるので、何もせずに抜ける
235 }
236 else if( beforeType == DEF_OBJECT ){
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}
246void Opcode_Func_GetPtrData( const char *Parameter, const int type ){
247 int reg=REG_RAX;
248 Type tempType;
249 if( !NumOpe(&reg,Parameter,Type(),tempType) ){
250 return;
251 }
252 if(!tempType.IsWhole()){
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
271bool Opcode_CallFunc( const char *Parameter, const int FuncNum, Type &resultType, bool isCallOn ){
272 switch(FuncNum){
273 case FUNC_LEN:
274 if( isCallOn ) Opcode_Func_Len(Parameter);
275 resultType.SetBasicType( DEF_LONG );
276 break;
277 case FUNC_ADDRESSOF:
278 if( isCallOn ) Opcode_Func_AddressOf(Parameter);
279 resultType.SetBasicType( DEF_PTR_VOID );
280 break;
281 case FUNC_SIZEOF:
282 if( isCallOn ) Opcode_Func_SizeOf(Parameter);
283 resultType.SetBasicType( DEF_LONG );
284 break;
285 case FUNC_VARPTR:
286 Opcode_Func_VarPtr( Parameter, resultType, isCallOn );
287 break;
288 case FUNC_OBJPTR:
289 Opcode_Func_ObjPtr( Parameter, resultType, isCallOn );
290 break;
291
292 case FUNC_GETDOUBLE:
293 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
294 resultType.SetBasicType( DEF_DOUBLE );
295 break;
296 case FUNC_GETSINGLE:
297 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
298 resultType.SetBasicType( DEF_SINGLE );
299 break;
300 case FUNC_GETQWORD:
301 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
302 resultType.SetBasicType( DEF_QWORD );
303 break;
304 case FUNC_GETDWORD:
305 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
306 resultType.SetBasicType( DEF_DWORD );
307 break;
308 case FUNC_GETWORD:
309 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_WORD);
310 resultType.SetBasicType( DEF_WORD );
311 break;
312 case FUNC_GETBYTE:
313 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
314 resultType.SetBasicType( DEF_BYTE );
315 break;
316 default:
317 return false;
318 }
319 return true;
320}
Note: See TracBrowser for help on using the repository browser.