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

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