source: dev/BasicCompiler64/Compile_Set_Var.cpp@ 19

Last change on this file since 19 was 3, checked in by dai_9181, 18 years ago
File size: 9.8 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4BOOL IsUse_r11(RELATIVE_VAR *pRelativeVar){
5 if(pRelativeVar->bOffsetOffset||pRelativeVar->dwKind==VAR_DIRECTMEM) return 1;
6 return 0;
7}
8
9void SetObjectVariableFromRax(LONG_PTR lpVarIndex,int CalcType,LONG_PTR lpCalcIndex,RELATIVE_VAR *pRelativeVar,BOOL bUseHeap){
10 int RightTermReg;
11 pobj_reg=new CRegister(REG_RCX);
12
13 //VarRegにオブジェクトポインタをコピー
14 int VarReg;
15 VarReg=pobj_reg->LockReg();
16 SetVarPtrToReg(VarReg,pRelativeVar);
17
18 //右辺
19 if(IsRealNumberType(CalcType)){
20 RightTermReg=pobj_reg->LockXmmReg();
21
22 if(CalcType==DEF_DOUBLE){
23 //movlsd RightTermReg,xmm0
24 op_movsd_RR(RightTermReg,REG_XMM0);
25 }
26 else if(CalcType==DEF_SINGLE){
27 //movlss RightTermReg,xmm0
28 op_movss_RR(RightTermReg,REG_XMM0);
29 }
30 }
31 else{
32 RightTermReg=pobj_reg->LockReg();
33
34 //mov RightTermReg,rax
35 op_mov_RR(RightTermReg,REG_RAX);
36 }
37
38
39 ///////////////////////////////////////////////////////////////////
40 // オペレータ '=' のオーバーロード関数を呼ぶ
41 ///////////////////////////////////////////////////////////////////
42
43 int type[10];
44 LONG_PTR index_stack[10];
45 BOOL array_bUseHeap[10];
46 int sp=2;
47
48 //左辺
49 type[0]=DEF_OBJECT;
50 index_stack[0]=lpVarIndex;
51 array_bUseHeap[0]=0;
52
53 //右辺
54 type[1]=CalcType;
55 index_stack[1]=lpCalcIndex;
56 array_bUseHeap[1]=bUseHeap;
57
58
59 int iRet;
60 iRet=CallOperatorProc(CALC_SUBSITUATION,NULL,type,index_stack,array_bUseHeap,sp);
61
62 //右辺用レジスタを解除
63 if(IsRealNumberType(CalcType)) pobj_reg->UnlockXmmReg();
64 else pobj_reg->UnlockReg();
65
66 //左辺用レジスタを解除
67 pobj_reg->UnlockReg();
68
69 //レジスタ管理オブジェクトを破棄
70 delete pobj_reg;
71 pobj_reg=0;
72
73 if(iRet==-1||iRet==1){
74 //成功したとき、またはエラーが発行されたとき
75 return;
76 }
77
78
79
80 if(CalcType==DEF_OBJECT&&lpVarIndex==lpCalcIndex){
81 //双方のオブジェクト型が完全に一致したとき
82 //※コピーを行う
83
84 //mov rsi,RightTermReg
85 op_mov_RR(REG_RSI,RightTermReg);
86
87 //mov rdi,VarReg
88 op_mov_RR(REG_RDI,VarReg);
89
90 int object_size;
91 object_size=GetSizeOfClass((CClass *)lpVarIndex);
92
93 //mov rcx,object_size
94 op_mov_RV(sizeof(_int64),REG_RCX,object_size);
95
96 if(bUseHeap){
97 //mov rax,rsi
98 op_mov_RR(REG_RAX,REG_RSI);
99 }
100
101 //rep movs byte ptr[rdi],byte ptr[rsi]
102 op_rep_movs(sizeof(BYTE));
103
104 if(bUseHeap){
105 //mov rcx,rax
106 op_mov_RR(REG_RCX,REG_RAX);
107
108 //call free
109 extern SUBINFO *pSub_free;
110 op_call(pSub_free);
111 }
112
113 return;
114 }
115 else{
116 SetError(1,NULL,cp);
117 }
118}
119
120void SetDoubleVariable(int type,RELATIVE_VAR *pRelative){
121 //////////////////////////
122 // Double型変数に書き込む
123 //////////////////////////
124
125 //xmm0に型変換
126 ChangeTypeToXmm_Double(type,REG_XMM0,REG_RAX);
127
128 if(pRelative->dwKind==VAR_GLOBAL){
129 if(pRelative->bOffsetOffset){
130 //movsd qword ptr[r11+offset],xmm0
131 OpBuffer[obp++]=(char)0xF2;
132 OpBuffer[obp++]=(char)0x41;
133 OpBuffer[obp++]=(char)0x0F;
134 OpBuffer[obp++]=(char)0x11;
135 OpBuffer[obp++]=(char)0x83;
136 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
137 pobj_GlobalVarSchedule->add();
138 obp+=sizeof(long);
139 }
140 else{
141 //movsd qword ptr[offset],xmm0
142 OpBuffer[obp++]=(char)0xF2;
143 OpBuffer[obp++]=(char)0x0F;
144 OpBuffer[obp++]=(char)0x11;
145 OpBuffer[obp++]=(char)0x04;
146 OpBuffer[obp++]=(char)0x25;
147 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
148 pobj_GlobalVarSchedule->add();
149 obp+=sizeof(long);
150 }
151 }
152 else if(pRelative->dwKind==VAR_LOCAL){
153 if(pRelative->bOffsetOffset){
154 //movsd qword ptr[rsp+r11+offset],xmm0
155 OpBuffer[obp++]=(char)0xF2;
156 OpBuffer[obp++]=(char)0x42;
157 OpBuffer[obp++]=(char)0x0F;
158 OpBuffer[obp++]=(char)0x11;
159 OpBuffer[obp++]=(char)0x84;
160 OpBuffer[obp++]=(char)0x1C;
161 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
162 AddLocalVarAddrSchedule();
163 obp+=sizeof(long);
164 }
165 else{
166 //movsd qword ptr[rsp+offset],xmm0
167 OpBuffer[obp++]=(char)0xF2;
168 OpBuffer[obp++]=(char)0x0F;
169 OpBuffer[obp++]=(char)0x11;
170 OpBuffer[obp++]=(char)0x84;
171 OpBuffer[obp++]=(char)0x24;
172 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
173 AddLocalVarAddrSchedule();
174 obp+=sizeof(long);
175 }
176 }
177 else if(pRelative->dwKind==VAR_REFLOCAL){
178 if(pRelative->bOffsetOffset){
179 //add r11,qword ptr[rsp+offset]
180 OpBuffer[obp++]=(char)0x4C;
181 OpBuffer[obp++]=(char)0x03;
182 OpBuffer[obp++]=(char)0x9C;
183 OpBuffer[obp++]=(char)0x24;
184 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
185 AddLocalVarAddrSchedule();
186 obp+=sizeof(long);
187 }
188 else{
189 //mov r11,qword ptr[rsp+offset]
190 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
191 obp-=sizeof(long);
192 AddLocalVarAddrSchedule();
193 obp+=sizeof(long);
194 }
195
196 goto directmem;
197 }
198 else if(pRelative->dwKind==VAR_DIRECTMEM){
199directmem:
200 //movsd qword ptr[r11],xmm0
201 OpBuffer[obp++]=(char)0xF2;
202 OpBuffer[obp++]=(char)0x41;
203 OpBuffer[obp++]=(char)0x0F;
204 OpBuffer[obp++]=(char)0x11;
205 OpBuffer[obp++]=(char)0x03;
206 }
207}
208void SetSingleVariable(int type,RELATIVE_VAR *pRelative){
209 //////////////////////////
210 // Single型変数に書き込む
211 //////////////////////////
212
213 //xmm0に型変換
214 ChangeTypeToXmm_Single(type,REG_XMM0,REG_RAX);
215
216 if(pRelative->dwKind==VAR_GLOBAL){
217 if(pRelative->bOffsetOffset){
218 //movss dword ptr[r11+offset],xmm0
219 OpBuffer[obp++]=(char)0xF3;
220 OpBuffer[obp++]=(char)0x41;
221 OpBuffer[obp++]=(char)0x0F;
222 OpBuffer[obp++]=(char)0x11;
223 OpBuffer[obp++]=(char)0x83;
224 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
225 pobj_GlobalVarSchedule->add();
226 obp+=sizeof(long);
227 }
228 else{
229 //movss dword ptr[offset],xmm0
230 OpBuffer[obp++]=(char)0xF3;
231 OpBuffer[obp++]=(char)0x0F;
232 OpBuffer[obp++]=(char)0x11;
233 OpBuffer[obp++]=(char)0x04;
234 OpBuffer[obp++]=(char)0x25;
235 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
236 pobj_GlobalVarSchedule->add();
237 obp+=sizeof(long);
238 }
239 }
240 else if(pRelative->dwKind==VAR_LOCAL){
241 if(pRelative->bOffsetOffset){
242 //movss dword ptr[rsp+r11+offset],xmm0
243 OpBuffer[obp++]=(char)0xF3;
244 OpBuffer[obp++]=(char)0x42;
245 OpBuffer[obp++]=(char)0x0F;
246 OpBuffer[obp++]=(char)0x11;
247 OpBuffer[obp++]=(char)0x84;
248 OpBuffer[obp++]=(char)0x1C;
249 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
250 AddLocalVarAddrSchedule();
251 obp+=sizeof(long);
252 }
253 else{
254 //movss dword ptr[rsp+offset],xmm0
255 OpBuffer[obp++]=(char)0xF3;
256 OpBuffer[obp++]=(char)0x0F;
257 OpBuffer[obp++]=(char)0x11;
258 OpBuffer[obp++]=(char)0x84;
259 OpBuffer[obp++]=(char)0x24;
260 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
261 AddLocalVarAddrSchedule();
262 obp+=sizeof(long);
263 }
264 }
265 else if(pRelative->dwKind==VAR_REFLOCAL){
266 if(pRelative->bOffsetOffset){
267 //add r11,qword ptr[rsp+offset]
268 OpBuffer[obp++]=(char)0x4C;
269 OpBuffer[obp++]=(char)0x03;
270 OpBuffer[obp++]=(char)0x9C;
271 OpBuffer[obp++]=(char)0x24;
272 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
273 AddLocalVarAddrSchedule();
274 obp+=sizeof(long);
275 }
276 else{
277 //mov r11,qword ptr[rsp+offset]
278 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
279 obp-=sizeof(long);
280 AddLocalVarAddrSchedule();
281 obp+=sizeof(long);
282 }
283
284 goto directmem;
285 }
286 else if(pRelative->dwKind==VAR_DIRECTMEM){
287directmem:
288 //movss dword ptr[r11],xmm0
289 OpBuffer[obp++]=(char)0xF3;
290 OpBuffer[obp++]=(char)0x41;
291 OpBuffer[obp++]=(char)0x0F;
292 OpBuffer[obp++]=(char)0x11;
293 OpBuffer[obp++]=(char)0x03;
294 }
295}
296void SetWholeVariable(int var_size,int type,RELATIVE_VAR *pRelative){
297 if(type==DEF_DOUBLE){
298 //Double型
299
300 //cvttsd2si rax,xmm0
301 op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
302 }
303 else if(type==DEF_SINGLE){
304 //Single型
305
306 //cvttss2si rax,xmm0
307 op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
308 }
309 else{
310 //その他の整数
311
312 if(var_size==sizeof(_int64)){
313 //レジスタの値を64ビット(rax)に拡張する
314 ExtendTypeTo64(type,REG_RAX);
315 }
316 else if(var_size==sizeof(long)){
317 //レジスタの値を32ビット(eax)に拡張する
318 ExtendTypeTo32(type,REG_RAX);
319 }
320 else if(var_size==sizeof(short)){
321 //レジスタの値を16ビット(ax)に拡張する
322 ExtendTypeTo16(type,REG_RAX);
323 }
324 //8ビットは拡張なし
325 }
326
327 if(pRelative->dwKind==VAR_GLOBAL){
328 if(pRelative->bOffsetOffset){
329 //mov ptr[r11+offset],rax/eax/ax/al
330 op_mov_MR(var_size,REG_RAX,REG_R11,(int)pRelative->offset,MOD_BASE_DISP32);
331 obp-=sizeof(long);
332 pobj_GlobalVarSchedule->add();
333 obp+=sizeof(long);
334 }
335 else{
336 //mov ptr[offset],rax/eax/ax/al
337 op_mov_MR(var_size,REG_RAX,0,(int)pRelative->offset,MOD_DISP32);
338 obp-=sizeof(long);
339 pobj_GlobalVarSchedule->add();
340 obp+=sizeof(long);
341 }
342 }
343 else if(pRelative->dwKind==VAR_LOCAL){
344 if(pRelative->bOffsetOffset){
345 //mov ptr[rsp+r11+offset],rax/eax/ax/al
346 op_mov_MR_ex(var_size,REG_RAX,REG_RSP,REG_R11,(int)pRelative->offset,USE_OFFSET);
347 obp-=sizeof(long);
348 AddLocalVarAddrSchedule();
349 obp+=sizeof(long);
350 }
351 else{
352 //mov ptr[rsp+offset],rax/eax/ax/al
353 op_mov_MR(var_size,REG_RAX,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
354 obp-=sizeof(long);
355 AddLocalVarAddrSchedule();
356 obp+=sizeof(long);
357 }
358 }
359 else if(pRelative->dwKind==VAR_REFLOCAL){
360 if(pRelative->bOffsetOffset){
361 //add r11,qword ptr[rsp+offset]
362 OpBuffer[obp++]=(char)0x4C;
363 OpBuffer[obp++]=(char)0x03;
364 OpBuffer[obp++]=(char)0x9C;
365 OpBuffer[obp++]=(char)0x24;
366 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
367 AddLocalVarAddrSchedule();
368 obp+=sizeof(long);
369 }
370 else{
371 //mov r11,qword ptr[rsp+offset]
372 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
373 obp-=sizeof(long);
374 AddLocalVarAddrSchedule();
375 obp+=sizeof(long);
376 }
377
378 goto directmem;
379 }
380 else if(pRelative->dwKind==VAR_DIRECTMEM){
381directmem:
382
383 //mov ptr[r11],rax/eax/ax/al
384 op_mov_MR(var_size,REG_RAX,REG_R11,0,MOD_BASE);
385 }
386}
Note: See TracBrowser for help on using the repository browser.