source: dev/BasicCompiler64/Compile_Func.cpp@ 13

Last change on this file since 13 was 11, checked in by dai_9181, 18 years ago

Const変数の書き込み規制を有効化(グローバル/ローカル変数のみ)
定数オブジェクトと定数メンバは未実装。

File size: 6.6 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(char *Parameter){
42 int type,TypeSize;
43 LONG_PTR lpIndex;
44 BOOL bArrayHead;
45
46 type=GetVarType(Parameter,&lpIndex,0);
47
48 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(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 GetObjectName(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(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(char *Parameter){
215 int type;
216 RELATIVE_VAR RelativeVar;
217
218 //変数のアドレスを取得
219 if(!GetVarOffsetReadOnly(Parameter,&type,&RelativeVar,0)) return;
220
221 SetVarPtrToReg(REG_RAX,&RelativeVar);
222}
223void Opcode_Func_GetPtrData(char *Parameter,int type){
224 int i2;
225
226 int reg=REG_RAX;
227 i2=NumOpe(&reg,Parameter,0,0,0);
228 if(!IsWholeNumberType(i2)){
229 extern int cp;
230 SetError(11,Parameter,cp);
231 return;
232 }
233
234 if(type==DEF_DOUBLE){
235 //movlpd xmm0,qword ptr[rax]
236 op_movlpd_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
237 }
238 else if(type==DEF_SINGLE){
239 //movss xmm0,dword ptr[rax]
240 op_movss_RM(REG_XMM0,REG_RAX,0,MOD_BASE);
241 }
242 else{
243 //mov rax,ptr[rax]
244 op_mov_RM(GetTypeSize(type,-1),REG_RAX,REG_RAX,0,MOD_BASE);
245 }
246}
247
248int Opcode_CallFunc(char *Parameter,int FuncNum){
249 switch(FuncNum){
250 case FUNC_LEN:
251 Opcode_Func_Len(Parameter);
252 return DEF_LONG;
253 case FUNC_ADDRESSOF:
254 Opcode_Func_AddressOf(Parameter);
255 return DEF_PTR_VOID;
256 case FUNC_SIZEOF:
257 Opcode_Func_SizeOf(Parameter);
258 return DEF_LONG;
259 case FUNC_VARPTR:
260 Opcode_Func_VarPtr(Parameter);
261 return DEF_PTR_VOID;
262
263 case FUNC_GETDOUBLE:
264 Opcode_Func_GetPtrData(Parameter,DEF_DOUBLE);
265 return DEF_DOUBLE;
266 case FUNC_GETSINGLE:
267 Opcode_Func_GetPtrData(Parameter,DEF_SINGLE);
268 return DEF_SINGLE;
269 case FUNC_GETQWORD:
270 Opcode_Func_GetPtrData(Parameter,DEF_QWORD);
271 return DEF_QWORD;
272 case FUNC_GETDWORD:
273 Opcode_Func_GetPtrData(Parameter,DEF_DWORD);
274 return DEF_DWORD;
275 case FUNC_GETWORD:
276 Opcode_Func_GetPtrData(Parameter,DEF_WORD);
277 return DEF_WORD;
278 case FUNC_GETBYTE:
279 Opcode_Func_GetPtrData(Parameter,DEF_BYTE);
280 return DEF_BYTE;
281 }
282 return 0;
283}
Note: See TracBrowser for help on using the repository browser.