source: dev/BasicCompiler64/Compile_Func.cpp@ 127

Last change on this file since 127 was 117, checked in by dai_9181, 18 years ago

String/ObjectをSystem名前空間に依存しない特殊型として扱うようにした

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 if( beforeType == DEF_OBJECT && lstrcmpi( Parameter, "This" ) != 0 ){
201 //参照をオブジェクトポインタに変更
202
203 //mov rax,qword ptr[rax]
204 op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
205
206 SetError(-120,NULL,cp);
207 }
208}
209void Opcode_Func_ObjPtr( const char *Parameter, Type &resultType, bool isCallOn ){
210 if( isCallOn == false ){
211 // 戻り値の型を取得するだけ
212
213 //変数のアドレスを取得
214 if(!GetVarType( Parameter, resultType, true )) return;
215
216 resultType.PtrLevelUp();
217
218 return;
219 }
220
221 RELATIVE_VAR RelativeVar;
222
223 //変数のアドレスを取得
224 if(!GetVarOffsetReadOnly( Parameter, &RelativeVar, resultType )) return;
225
226 int beforeType = resultType.GetBasicType();
227
228 resultType.PtrLevelUp();
229
230 SetVarPtrToReg(REG_RAX,&RelativeVar);
231
232 if( lstrcmpi( Parameter, "This" )==0 ){
233 // Thisの場合は特別にオブジェクトポインタが返ってくるので、何もせずに抜ける
234 }
235 else if( beforeType == DEF_OBJECT ){
236 //参照をオブジェクトポインタに変更
237
238 //mov rax,qword ptr[rax]
239 op_mov_RM( sizeof(_int64), REG_RAX, REG_RAX, 0, MOD_BASE );
240 }
241 else{
242 SetError(134,NULL,cp );
243 }
244}
245void Opcode_Func_GetPtrData( const char *Parameter, const int type ){
246 int reg=REG_RAX;
247 Type tempType;
248 if( !NumOpe(&reg,Parameter,Type(),tempType) ){
249 return;
250 }
251 if(!tempType.IsWhole()){
252 SetError(11,Parameter,cp);
253 return;
254 }
255
256 if(type==DEF_DOUBLE){
257 //movlpd xmm0,qword ptr[rax]
258 op_movlpd_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
259 }
260 else if(type==DEF_SINGLE){
261 //movss xmm0,dword ptr[rax]
262 op_movss_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
263 }
264 else{
265 //mov rax,ptr[rax]
266 op_mov_RM(GetTypeSize(type,-1),REG_RAX,REG_RAX,0,MOD_BASE);
267 }
268}
269
270bool Opcode_CallFunc( const char *Parameter, const int FuncNum, Type &resultType, bool isCallOn ){
271 switch(FuncNum){
272 case FUNC_LEN:
273 if( isCallOn ) Opcode_Func_Len(Parameter);
274 resultType.SetBasicType( DEF_LONG );
275 break;
276 case FUNC_ADDRESSOF:
277 if( isCallOn ) Opcode_Func_AddressOf(Parameter);
278 resultType.SetBasicType( DEF_PTR_VOID );
279 break;
280 case FUNC_SIZEOF:
281 if( isCallOn ) Opcode_Func_SizeOf(Parameter);
282 resultType.SetBasicType( DEF_LONG );
283 break;
284 case FUNC_VARPTR:
285 Opcode_Func_VarPtr( Parameter, resultType, isCallOn );
286 break;
287 case FUNC_OBJPTR:
288 Opcode_Func_ObjPtr( Parameter, resultType, isCallOn );
289 break;
290
291 case FUNC_GETDOUBLE:
292 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
293 resultType.SetBasicType( DEF_DOUBLE );
294 break;
295 case FUNC_GETSINGLE:
296 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
297 resultType.SetBasicType( DEF_SINGLE );
298 break;
299 case FUNC_GETQWORD:
300 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
301 resultType.SetBasicType( DEF_QWORD );
302 break;
303 case FUNC_GETDWORD:
304 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
305 resultType.SetBasicType( DEF_DWORD );
306 break;
307 case FUNC_GETWORD:
308 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_WORD);
309 resultType.SetBasicType( DEF_WORD );
310 break;
311 case FUNC_GETBYTE:
312 if( isCallOn ) Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
313 resultType.SetBasicType( DEF_BYTE );
314 break;
315 default:
316 return false;
317 }
318 return true;
319}
Note: See TracBrowser for help on using the repository browser.