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

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