source: dev/BasicCompiler64/Compile_Func.cpp@ 47

Last change on this file since 47 was 46, checked in by dai_9181, 18 years ago

VarPtrの戻り値の型をVoidPtrではなく、ポインタレベルを増加した型にした。
関連関数のパラメータにconstを適用。

File size: 7.0 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 int type,TypeSize;
43 LONG_PTR lpIndex;
44 BOOL bArrayHead;
45
46 type=GetVarType(Parameter,&lpIndex,0);
47
48 const char *tempParm=Parameter;
49 char temporary[VN_SIZE];
50 char temp2[32];
51 if(type==-1){
52 sprintf(temporary,"_System_DummyStr2=%s",Parameter);
53 OpcodeCalc(temporary);
54
55 lstrcpy(temp2,"_System_DummyStr2");
56 tempParm=temp2;
57
58 extern CClass *pobj_StringClass;
59 type=DEF_OBJECT;
60 lpIndex=(LONG_PTR)pobj_StringClass;
61 }
62
63 TYPEINFO TypeInfo={type,lpIndex};
64 if(IsStringObjectType(&TypeInfo)){
65 //Stringオブジェクトの場合
66 sprintf(temporary,"%s.Length",tempParm);
67
68 int reg=REG_RAX;
69 NumOpe(&reg,temporary,0,0,NULL,NULL);
70 return;
71 }
72
73
74 int SubScripts[MAX_ARRAYDIM];
75 RELATIVE_VAR RelativeVar;
76 if(!GetVarOffsetReadOnly(tempParm,&type,&RelativeVar,&lpIndex,SubScripts)) return;
77
78
79 if(type&FLAG_PTR){
80 type&=~FLAG_PTR;
81
82 bArrayHead=1;
83 }
84 else bArrayHead=0;
85
86 TypeSize=GetTypeSize(type,lpIndex);
87
88 if(bArrayHead) TypeSize*=JumpSubScripts(SubScripts);
89
90 //mov rax,TypeSize
91 op_mov_RV(sizeof(_int64),REG_RAX,TypeSize);
92
93 return;
94}
95void Opcode_Func_AddressOf( const char *name ){
96 extern int cp;
97 SUBINFO *psi;
98
99 extern LONG_PTR ProcPtr_BaseIndex;
100 if(ProcPtr_BaseIndex!=-1){
101 //左辺の型にのっとり、オーバーロードを解決
102
103 SUBINFO **ppsi;
104 int num;
105 ppsi=GetOverloadSubHash( name, &num );
106 if(!num){
107 HeapDefaultFree(ppsi);
108
109 SetError(27,name,cp);
110 return;
111 }
112
113 //オーバーロードを解決
114 extern PROCPTRINFO *pProcPtrInfo;
115 psi=OverloadSolution(name,ppsi,num,pProcPtrInfo[ProcPtr_BaseIndex].pParmInfo,pProcPtrInfo[ProcPtr_BaseIndex].ParmNum,NULL);
116 HeapDefaultFree(ppsi);
117
118 if(!psi){
119 SetError(27,name,cp);
120 return;
121 }
122 }
123 else{
124 psi=GetSubHash(name);
125 if(!psi){
126 SetError(27,name,cp);
127 return;
128 }
129 }
130
131
132 if(psi->bVirtual){
133 ///////////////////////////////
134 // 仮想関数の場合
135 // thisポインタをrcxにコピー
136 ///////////////////////////////
137
138 CClass *pobj_c;
139
140 char ObjectName[VN_SIZE];
141 int RefType;
142 SplitObjectName(name,ObjectName,&RefType);
143
144 if(ObjectName[0]){
145 if(lstrcmpi(ObjectName,"Super")==0) goto InClassMember;
146 else{
147 RELATIVE_VAR RelativeVar;
148 int type;
149 if(!GetVarOffsetReadOnly(ObjectName,&type,&RelativeVar,(LONG_PTR *)&pobj_c)) return;
150 SetVarPtrToReg(REG_RCX,&RelativeVar);
151
152 //参照タイプが整合しているかをチェック
153 if(type!=RefType) SetError(104,ObjectName,cp);
154
155 if(type==DEF_PTR_OBJECT){
156 //mov rcx,qword ptr[rcx]
157 op_mov_RM(sizeof(_int64),REG_RCX,REG_RCX,0,MOD_BASE);
158 }
159 }
160 }
161 else{
162InClassMember:
163 //自身のオブジェクトのThisポインタをrcxにコピー
164 SetThisPtrToReg(REG_RCX);
165
166 pobj_c=pobj_CompilingClass;
167 }
168
169
170 //仮想関数(オブジェクトメソッド)
171 //pObj->func_table->func1
172 // ->func2
173 // ->func3
174
175 //mov r11,qword ptr[rcx]
176 op_mov_RM(sizeof(_int64),REG_R11,REG_RCX,0,MOD_BASE);
177
178 int i,i2;
179 for(i=0,i2=0;i<pobj_c->iMethodNum;i++){
180 if(pobj_c->ppobj_Method[i]->psi==psi) break;
181 if(pobj_c->ppobj_Method[i]->psi->bVirtual) i2++;
182 }
183
184 //mov rax,qword ptr[r11+func_index]
185 if(i2*PTR_SIZE<=0x7F){
186 op_mov_RM(sizeof(_int64),REG_RAX,REG_R11,i2*PTR_SIZE,MOD_BASE_DISP8);
187 }
188 else{
189 op_mov_RM(sizeof(_int64),REG_RAX,REG_R11,i2*PTR_SIZE,MOD_BASE_DISP32);
190 }
191 }
192 else{
193 //一般の関数
194
195 //mov rax,ProcAddr
196 op_mov_RV(sizeof(_int64),REG_RAX,0);
197 obp-=sizeof(long);
198 pobj_SubAddrSchedule->add(psi,0);
199 obp+=sizeof(long);
200 }
201
202 psi->bUse=1;
203}
204void Opcode_Func_SizeOf( const char *Parameter ){
205 int type,size;
206 LONG_PTR lpIndex;
207
208 type=GetTypeFixed(Parameter,&lpIndex);
209 size=GetTypeSize(type,lpIndex);
210
211 //mov rax,size
212 op_mov_RV(sizeof(_int64),REG_RAX,size);
213}
214void Opcode_Func_VarPtr( const char *Parameter, TYPEINFO &ReturnTypeInfo ){
215 RELATIVE_VAR RelativeVar;
216
217 //変数のアドレスを取得
218 if(!GetVarOffsetReadOnly( Parameter, &ReturnTypeInfo.type, &RelativeVar, &ReturnTypeInfo.u.lpIndex )) return;
219
220 PTR_LEVEL_UP( ReturnTypeInfo.type );
221
222 SetVarPtrToReg(REG_RAX,&RelativeVar);
223}
224void Opcode_Func_GetPtrData( const char *Parameter, const int type ){
225 int i2;
226
227 int reg=REG_RAX;
228 i2=NumOpe(&reg,Parameter,0,0,0);
229 if(!IsWholeNumberType(i2)){
230 extern int cp;
231 SetError(11,Parameter,cp);
232 return;
233 }
234
235 if(type==DEF_DOUBLE){
236 //movlpd xmm0,qword ptr[rax]
237 op_movlpd_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
238 }
239 else if(type==DEF_SINGLE){
240 //movss xmm0,dword ptr[rax]
241 op_movss_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
242 }
243 else{
244 //mov rax,ptr[rax]
245 op_mov_RM(GetTypeSize(type,-1),REG_RAX,REG_RAX,0,MOD_BASE);
246 }
247}
248
249void Opcode_CallFunc( const char *Parameter, const int FuncNum, TYPEINFO &ReturnTypeInfo ){
250 switch(FuncNum){
251 case FUNC_LEN:
252 Opcode_Func_Len(Parameter);
253 ReturnTypeInfo.type = DEF_LONG;
254 break;
255 case FUNC_ADDRESSOF:
256 Opcode_Func_AddressOf(Parameter);
257 ReturnTypeInfo.type = DEF_PTR_VOID;
258 break;
259 case FUNC_SIZEOF:
260 Opcode_Func_SizeOf(Parameter);
261 ReturnTypeInfo.type = DEF_LONG;
262 break;
263 case FUNC_VARPTR:
264 Opcode_Func_VarPtr( Parameter, ReturnTypeInfo );
265 break;
266
267 case FUNC_GETDOUBLE:
268 Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
269 ReturnTypeInfo.type = DEF_DOUBLE;
270 break;
271 case FUNC_GETSINGLE:
272 Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
273 ReturnTypeInfo.type = DEF_SINGLE;
274 break;
275 case FUNC_GETQWORD:
276 Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
277 ReturnTypeInfo.type = DEF_QWORD;
278 break;
279 case FUNC_GETDWORD:
280 Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
281 ReturnTypeInfo.type = DEF_DWORD;
282 break;
283 case FUNC_GETWORD:
284 Opcode_Func_GetPtrData(Parameter,DEF_WORD);
285 ReturnTypeInfo.type = DEF_WORD;
286 break;
287 case FUNC_GETBYTE:
288 Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
289 ReturnTypeInfo.type = DEF_BYTE;
290 break;
291 }
292}
Note: See TracBrowser for help on using the repository browser.