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

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

コード全体のリファクタリングを実施

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