source: dev/BasicCompiler64/Compile_Set_Var.cpp@ 73

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

Parameterクラスを適用。32bit側は動くようになったので、64bitのほうを調整する。

File size: 11.3 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 SetStructVariableFromRax(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 TODO: 消す
40 ///////////////////////////////////////////////////////////////////
41 // オペレータ '=' のオーバーロード関数を呼ぶ
42 ///////////////////////////////////////////////////////////////////
43
44 int type[10];
45 LONG_PTR index_stack[10];
46 BOOL array_bUseHeap[10];
47 int sp=2;
48
49 //左辺
50 type[0]=DEF_OBJECT;
51 index_stack[0]=lpVarIndex;
52 array_bUseHeap[0]=0;
53
54 //右辺
55 type[1]=CalcType;
56 index_stack[1]=lpCalcIndex;
57 array_bUseHeap[1]=bUseHeap;
58
59
60 int iRet;
61 iRet=CallOperatorProc(CALC_SUBSITUATION,NULL,type,index_stack,array_bUseHeap,sp);
62*/
63 //右辺用レジスタを解除
64 if(IsRealNumberType(CalcType)) pobj_reg->UnlockXmmReg();
65 else pobj_reg->UnlockReg();
66
67 //左辺用レジスタを解除
68 pobj_reg->UnlockReg();
69
70 //レジスタ管理オブジェクトを破棄
71 delete pobj_reg;
72 pobj_reg=0;
73
74
75 if( CalcType == DEF_STRUCT ){
76 CClass *pVarClass = (CClass *)lpVarIndex;
77 CClass *pCalcClass = (CClass *)lpCalcIndex;
78
79 if( pVarClass->IsEquals( pCalcClass ) ){ //等しい
80
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 CClass *pClass = (CClass *)lpVarIndex;
91 int object_size = pClass->GetSize();
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 }
116
117 SetError(1,NULL,cp);
118}
119
120
121void SetDoubleVariable(int type,RELATIVE_VAR *pRelative){
122 //////////////////////////
123 // Double型変数に書き込む
124 //////////////////////////
125
126 //xmm0に型変換
127 ChangeTypeToXmm_Double(type,REG_XMM0,REG_RAX);
128
129 if(pRelative->dwKind==VAR_GLOBAL){
130 if(pRelative->bOffsetOffset){
131 //movsd qword ptr[r11+offset],xmm0
132 OpBuffer[obp++]=(char)0xF2;
133 OpBuffer[obp++]=(char)0x41;
134 OpBuffer[obp++]=(char)0x0F;
135 OpBuffer[obp++]=(char)0x11;
136 OpBuffer[obp++]=(char)0x83;
137 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
138 pobj_GlobalVarSchedule->add();
139 obp+=sizeof(long);
140 }
141 else{
142 //movsd qword ptr[offset],xmm0
143 OpBuffer[obp++]=(char)0xF2;
144 OpBuffer[obp++]=(char)0x0F;
145 OpBuffer[obp++]=(char)0x11;
146 OpBuffer[obp++]=(char)0x04;
147 OpBuffer[obp++]=(char)0x25;
148 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
149 pobj_GlobalVarSchedule->add();
150 obp+=sizeof(long);
151 }
152 }
153 else if(pRelative->dwKind==VAR_REFGLOBAL){
154 SetError(300,NULL,cp);
155 }
156 else if(pRelative->dwKind==VAR_LOCAL){
157 if(pRelative->bOffsetOffset){
158 //movsd qword ptr[rsp+r11+offset],xmm0
159 OpBuffer[obp++]=(char)0xF2;
160 OpBuffer[obp++]=(char)0x42;
161 OpBuffer[obp++]=(char)0x0F;
162 OpBuffer[obp++]=(char)0x11;
163 OpBuffer[obp++]=(char)0x84;
164 OpBuffer[obp++]=(char)0x1C;
165 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
166 AddLocalVarAddrSchedule();
167 obp+=sizeof(long);
168 }
169 else{
170 //movsd qword ptr[rsp+offset],xmm0
171 OpBuffer[obp++]=(char)0xF2;
172 OpBuffer[obp++]=(char)0x0F;
173 OpBuffer[obp++]=(char)0x11;
174 OpBuffer[obp++]=(char)0x84;
175 OpBuffer[obp++]=(char)0x24;
176 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
177 AddLocalVarAddrSchedule();
178 obp+=sizeof(long);
179 }
180 }
181 else if( pRelative->dwKind == VAR_REFLOCAL ){
182 if(pRelative->bOffsetOffset){
183 //add r11,qword ptr[rsp+offset]
184 OpBuffer[obp++]=(char)0x4C;
185 OpBuffer[obp++]=(char)0x03;
186 OpBuffer[obp++]=(char)0x9C;
187 OpBuffer[obp++]=(char)0x24;
188 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
189 AddLocalVarAddrSchedule();
190 obp+=sizeof(long);
191 }
192 else{
193 //mov r11,qword ptr[rsp+offset]
194 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
195 obp-=sizeof(long);
196 AddLocalVarAddrSchedule();
197 obp+=sizeof(long);
198 }
199
200 goto directmem;
201 }
202 else if(pRelative->dwKind==VAR_DIRECTMEM){
203directmem:
204 //movsd qword ptr[r11],xmm0
205 OpBuffer[obp++]=(char)0xF2;
206 OpBuffer[obp++]=(char)0x41;
207 OpBuffer[obp++]=(char)0x0F;
208 OpBuffer[obp++]=(char)0x11;
209 OpBuffer[obp++]=(char)0x03;
210 }
211}
212void SetSingleVariable(int type,RELATIVE_VAR *pRelative){
213 //////////////////////////
214 // Single型変数に書き込む
215 //////////////////////////
216
217 //xmm0に型変換
218 ChangeTypeToXmm_Single(type,REG_XMM0,REG_RAX);
219
220 if(pRelative->dwKind==VAR_GLOBAL){
221 if(pRelative->bOffsetOffset){
222 //movss dword ptr[r11+offset],xmm0
223 OpBuffer[obp++]=(char)0xF3;
224 OpBuffer[obp++]=(char)0x41;
225 OpBuffer[obp++]=(char)0x0F;
226 OpBuffer[obp++]=(char)0x11;
227 OpBuffer[obp++]=(char)0x83;
228 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
229 pobj_GlobalVarSchedule->add();
230 obp+=sizeof(long);
231 }
232 else{
233 //movss dword ptr[offset],xmm0
234 OpBuffer[obp++]=(char)0xF3;
235 OpBuffer[obp++]=(char)0x0F;
236 OpBuffer[obp++]=(char)0x11;
237 OpBuffer[obp++]=(char)0x04;
238 OpBuffer[obp++]=(char)0x25;
239 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
240 pobj_GlobalVarSchedule->add();
241 obp+=sizeof(long);
242 }
243 }
244 else if(pRelative->dwKind==VAR_REFGLOBAL){
245 SetError(300,NULL,cp);
246 }
247 else if(pRelative->dwKind==VAR_LOCAL){
248 if(pRelative->bOffsetOffset){
249 //movss dword ptr[rsp+r11+offset],xmm0
250 OpBuffer[obp++]=(char)0xF3;
251 OpBuffer[obp++]=(char)0x42;
252 OpBuffer[obp++]=(char)0x0F;
253 OpBuffer[obp++]=(char)0x11;
254 OpBuffer[obp++]=(char)0x84;
255 OpBuffer[obp++]=(char)0x1C;
256 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
257 AddLocalVarAddrSchedule();
258 obp+=sizeof(long);
259 }
260 else{
261 //movss dword ptr[rsp+offset],xmm0
262 OpBuffer[obp++]=(char)0xF3;
263 OpBuffer[obp++]=(char)0x0F;
264 OpBuffer[obp++]=(char)0x11;
265 OpBuffer[obp++]=(char)0x84;
266 OpBuffer[obp++]=(char)0x24;
267 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
268 AddLocalVarAddrSchedule();
269 obp+=sizeof(long);
270 }
271 }
272 else if( pRelative->dwKind == VAR_REFLOCAL ){
273 if(pRelative->bOffsetOffset){
274 //add r11,qword ptr[rsp+offset]
275 OpBuffer[obp++]=(char)0x4C;
276 OpBuffer[obp++]=(char)0x03;
277 OpBuffer[obp++]=(char)0x9C;
278 OpBuffer[obp++]=(char)0x24;
279 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
280 AddLocalVarAddrSchedule();
281 obp+=sizeof(long);
282 }
283 else{
284 //mov r11,qword ptr[rsp+offset]
285 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
286 obp-=sizeof(long);
287 AddLocalVarAddrSchedule();
288 obp+=sizeof(long);
289 }
290
291 goto directmem;
292 }
293 else if(pRelative->dwKind==VAR_DIRECTMEM){
294directmem:
295 //movss dword ptr[r11],xmm0
296 OpBuffer[obp++]=(char)0xF3;
297 OpBuffer[obp++]=(char)0x41;
298 OpBuffer[obp++]=(char)0x0F;
299 OpBuffer[obp++]=(char)0x11;
300 OpBuffer[obp++]=(char)0x03;
301 }
302}
303void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar){
304 if(VarType==DEF_DOUBLE){
305 //Double型変数へスタックの内容を格納する
306 SetDoubleVariable(CalcType,pRelativeVar);
307 }
308 else if(VarType==DEF_SINGLE){
309 //Single型変数へスタックの内容を格納する
310 SetSingleVariable(CalcType,pRelativeVar);
311 }
312}
313void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
314 if(type==DEF_DOUBLE){
315 //Double型
316
317 //cvttsd2si rax,xmm0
318 op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
319 }
320 else if(type==DEF_SINGLE){
321 //Single型
322
323 //cvttss2si rax,xmm0
324 op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
325 }
326
327 //cmp rax,0
328 op_cmp_value(GetTypeSize(type,-1),REG_RAX,0);
329
330 //setne al
331 op_setne( REG_RAX );
332
333 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative);
334}
335void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative){
336 if(type==DEF_DOUBLE){
337 //Double型
338
339 //cvttsd2si rax,xmm0
340 op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
341 }
342 else if(type==DEF_SINGLE){
343 //Single型
344
345 //cvttss2si rax,xmm0
346 op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
347 }
348 else{
349 //その他の整数
350
351 if(varSize==sizeof(_int64)){
352 //レジスタの値を64ビット(rax)に拡張する
353 ExtendTypeTo64(type,REG_RAX);
354 }
355 else if(varSize==sizeof(long)){
356 //レジスタの値を32ビット(eax)に拡張する
357 ExtendTypeTo32(type,REG_RAX);
358 }
359 else if(varSize==sizeof(short)){
360 //レジスタの値を16ビット(ax)に拡張する
361 ExtendTypeTo16(type,REG_RAX);
362 }
363 //8ビットは拡張なし
364 }
365
366 if(pRelative->dwKind==VAR_GLOBAL){
367 if(pRelative->bOffsetOffset){
368 //mov ptr[r11+offset],rax/eax/ax/al
369 op_mov_MR(varSize,REG_RAX,REG_R11,(int)pRelative->offset,MOD_BASE_DISP32);
370 obp-=sizeof(long);
371 pobj_GlobalVarSchedule->add();
372 obp+=sizeof(long);
373 }
374 else{
375 //mov ptr[offset],rax/eax/ax/al
376 op_mov_MR(varSize,REG_RAX,0,(int)pRelative->offset,MOD_DISP32);
377 obp-=sizeof(long);
378 pobj_GlobalVarSchedule->add();
379 obp+=sizeof(long);
380 }
381 }
382 else if( pRelative->dwKind == VAR_REFGLOBAL ){
383 if(pRelative->bOffsetOffset){
384 //add r11,qword ptr[offset]
385 op_add_RM( sizeof(_int64), REG_R11, REG_NON, (int)pRelative->offset, MOD_DISP32 );
386 obp-=sizeof(long);
387 pobj_GlobalVarSchedule->add();
388 obp+=sizeof(long);
389 }
390 else{
391 //mov r11,qword ptr[offset]
392 op_mov_RM(sizeof(_int64),REG_R11,REG_NON,(int)pRelative->offset,MOD_DISP32);
393 obp-=sizeof(long);
394 pobj_GlobalVarSchedule->add();
395 obp+=sizeof(long);
396 }
397
398 goto directmem;
399 }
400 else if(pRelative->dwKind==VAR_LOCAL){
401 if(pRelative->bOffsetOffset){
402 //mov ptr[rsp+r11+offset],rax/eax/ax/al
403 op_mov_MR_ex(varSize,REG_RAX,REG_RSP,REG_R11,(int)pRelative->offset,USE_OFFSET);
404 obp-=sizeof(long);
405 AddLocalVarAddrSchedule();
406 obp+=sizeof(long);
407 }
408 else{
409 //mov ptr[rsp+offset],rax/eax/ax/al
410 op_mov_MR(varSize,REG_RAX,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
411 obp-=sizeof(long);
412 AddLocalVarAddrSchedule();
413 obp+=sizeof(long);
414 }
415 }
416 else if( pRelative->dwKind == VAR_REFLOCAL ){
417 if(pRelative->bOffsetOffset){
418 //add r11,qword ptr[rsp+offset]
419 op_add_RM( sizeof(_int64), REG_R11, REG_RSP, (int)pRelative->offset, MOD_BASE_DISP32 );
420 obp-=sizeof(long);
421 AddLocalVarAddrSchedule();
422 obp+=sizeof(long);
423 }
424 else{
425 //mov r11,qword ptr[rsp+offset]
426 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
427 obp-=sizeof(long);
428 AddLocalVarAddrSchedule();
429 obp+=sizeof(long);
430 }
431
432 goto directmem;
433 }
434 else if(pRelative->dwKind==VAR_DIRECTMEM){
435directmem:
436
437 //mov ptr[r11],rax/eax/ax/al
438 op_mov_MR(varSize,REG_RAX,REG_R11,0,MOD_BASE);
439 }
440}
Note: See TracBrowser for help on using the repository browser.