source: dev/BasicCompiler64/Compile_Func.cpp@ 91

Last change on this file since 91 was 88, checked in by dai_9181, 18 years ago

House→Smoothie
Sourceクラスを用意した。

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