source: dev/BasicCompiler64/Compile_Set_Var.cpp@ 127

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

不要なソースコード(TODO: 消す)を削除

File size: 10.6 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( const Type &varType, const Type &calcType, 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( calcType.IsReal() ){
20 RightTermReg=pobj_reg->LockXmmReg();
21
22 if( calcType.IsDouble() ){
23 //movlsd RightTermReg,xmm0
24 op_movsd_RR(RightTermReg,REG_XMM0);
25 }
26 else if( calcType.IsSingle() ){
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 if( calcType.IsReal() ) pobj_reg->UnlockXmmReg();
40 else pobj_reg->UnlockReg();
41
42 //左辺用レジスタを解除
43 pobj_reg->UnlockReg();
44
45 //レジスタ管理オブジェクトを破棄
46 delete pobj_reg;
47 pobj_reg=0;
48
49
50 if( calcType.IsStruct() ){
51 if( varType.GetClass().IsEquals( &calcType.GetClass() ) ){ //等しい
52
53 //双方のオブジェクト型が一致、または派生・継承関係にあるとき
54 //※コピーを行う
55
56 //mov rsi,RightTermReg
57 op_mov_RR(REG_RSI,RightTermReg);
58
59 //mov rdi,VarReg
60 op_mov_RR(REG_RDI,VarReg);
61
62 int object_size = varType.GetClass().GetSize();
63
64 //mov rcx,object_size
65 op_mov_RV(sizeof(_int64),REG_RCX,object_size);
66
67 if(bUseHeap){
68 //mov rax,rsi
69 op_mov_RR(REG_RAX,REG_RSI);
70 }
71
72 //rep movs byte ptr[rdi],byte ptr[rsi]
73 op_rep_movs(sizeof(BYTE));
74
75 if(bUseHeap){
76 //mov rcx,rax
77 op_mov_RR(REG_RCX,REG_RAX);
78
79 //call free
80 extern UserProc *pSub_free;
81 op_call(pSub_free);
82 }
83
84 return;
85 }
86 }
87
88 SetError(1,NULL,cp);
89}
90
91
92void SetDoubleVariable(int type,RELATIVE_VAR *pRelative){
93 //////////////////////////
94 // Double型変数に書き込む
95 //////////////////////////
96
97 //xmm0に型変換
98 ChangeTypeToXmm_Double(type,REG_XMM0,REG_RAX);
99
100 if(pRelative->dwKind==VAR_GLOBAL){
101 if(pRelative->bOffsetOffset){
102 //movsd qword ptr[r11+offset],xmm0
103 OpBuffer[obp++]=(char)0xF2;
104 OpBuffer[obp++]=(char)0x41;
105 OpBuffer[obp++]=(char)0x0F;
106 OpBuffer[obp++]=(char)0x11;
107 OpBuffer[obp++]=(char)0x83;
108 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
109 pobj_GlobalVarSchedule->add();
110 obp+=sizeof(long);
111 }
112 else{
113 //movsd qword ptr[offset],xmm0
114 OpBuffer[obp++]=(char)0xF2;
115 OpBuffer[obp++]=(char)0x0F;
116 OpBuffer[obp++]=(char)0x11;
117 OpBuffer[obp++]=(char)0x04;
118 OpBuffer[obp++]=(char)0x25;
119 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
120 pobj_GlobalVarSchedule->add();
121 obp+=sizeof(long);
122 }
123 }
124 else if(pRelative->dwKind==VAR_REFGLOBAL){
125 SetError(300,NULL,cp);
126 }
127 else if(pRelative->dwKind==VAR_LOCAL){
128 if(pRelative->bOffsetOffset){
129 //movsd qword ptr[rsp+r11+offset],xmm0
130 OpBuffer[obp++]=(char)0xF2;
131 OpBuffer[obp++]=(char)0x42;
132 OpBuffer[obp++]=(char)0x0F;
133 OpBuffer[obp++]=(char)0x11;
134 OpBuffer[obp++]=(char)0x84;
135 OpBuffer[obp++]=(char)0x1C;
136 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
137 AddLocalVarAddrSchedule();
138 obp+=sizeof(long);
139 }
140 else{
141 //movsd qword ptr[rsp+offset],xmm0
142 OpBuffer[obp++]=(char)0xF2;
143 OpBuffer[obp++]=(char)0x0F;
144 OpBuffer[obp++]=(char)0x11;
145 OpBuffer[obp++]=(char)0x84;
146 OpBuffer[obp++]=(char)0x24;
147 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
148 AddLocalVarAddrSchedule();
149 obp+=sizeof(long);
150 }
151 }
152 else if( pRelative->dwKind == VAR_REFLOCAL ){
153 if(pRelative->bOffsetOffset){
154 //add r11,qword ptr[rsp+offset]
155 OpBuffer[obp++]=(char)0x4C;
156 OpBuffer[obp++]=(char)0x03;
157 OpBuffer[obp++]=(char)0x9C;
158 OpBuffer[obp++]=(char)0x24;
159 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
160 AddLocalVarAddrSchedule();
161 obp+=sizeof(long);
162 }
163 else{
164 //mov r11,qword ptr[rsp+offset]
165 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
166 obp-=sizeof(long);
167 AddLocalVarAddrSchedule();
168 obp+=sizeof(long);
169 }
170
171 goto directmem;
172 }
173 else if(pRelative->dwKind==VAR_DIRECTMEM){
174directmem:
175 //movsd qword ptr[r11],xmm0
176 OpBuffer[obp++]=(char)0xF2;
177 OpBuffer[obp++]=(char)0x41;
178 OpBuffer[obp++]=(char)0x0F;
179 OpBuffer[obp++]=(char)0x11;
180 OpBuffer[obp++]=(char)0x03;
181 }
182}
183void SetSingleVariable(int type,RELATIVE_VAR *pRelative){
184 //////////////////////////
185 // Single型変数に書き込む
186 //////////////////////////
187
188 //xmm0に型変換
189 ChangeTypeToXmm_Single(type,REG_XMM0,REG_RAX);
190
191 if(pRelative->dwKind==VAR_GLOBAL){
192 if(pRelative->bOffsetOffset){
193 //movss dword ptr[r11+offset],xmm0
194 OpBuffer[obp++]=(char)0xF3;
195 OpBuffer[obp++]=(char)0x41;
196 OpBuffer[obp++]=(char)0x0F;
197 OpBuffer[obp++]=(char)0x11;
198 OpBuffer[obp++]=(char)0x83;
199 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
200 pobj_GlobalVarSchedule->add();
201 obp+=sizeof(long);
202 }
203 else{
204 //movss dword ptr[offset],xmm0
205 OpBuffer[obp++]=(char)0xF3;
206 OpBuffer[obp++]=(char)0x0F;
207 OpBuffer[obp++]=(char)0x11;
208 OpBuffer[obp++]=(char)0x04;
209 OpBuffer[obp++]=(char)0x25;
210 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
211 pobj_GlobalVarSchedule->add();
212 obp+=sizeof(long);
213 }
214 }
215 else if(pRelative->dwKind==VAR_REFGLOBAL){
216 SetError(300,NULL,cp);
217 }
218 else if(pRelative->dwKind==VAR_LOCAL){
219 if(pRelative->bOffsetOffset){
220 //movss dword ptr[rsp+r11+offset],xmm0
221 OpBuffer[obp++]=(char)0xF3;
222 OpBuffer[obp++]=(char)0x42;
223 OpBuffer[obp++]=(char)0x0F;
224 OpBuffer[obp++]=(char)0x11;
225 OpBuffer[obp++]=(char)0x84;
226 OpBuffer[obp++]=(char)0x1C;
227 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
228 AddLocalVarAddrSchedule();
229 obp+=sizeof(long);
230 }
231 else{
232 //movss dword ptr[rsp+offset],xmm0
233 OpBuffer[obp++]=(char)0xF3;
234 OpBuffer[obp++]=(char)0x0F;
235 OpBuffer[obp++]=(char)0x11;
236 OpBuffer[obp++]=(char)0x84;
237 OpBuffer[obp++]=(char)0x24;
238 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
239 AddLocalVarAddrSchedule();
240 obp+=sizeof(long);
241 }
242 }
243 else if( pRelative->dwKind == VAR_REFLOCAL ){
244 if(pRelative->bOffsetOffset){
245 //add r11,qword ptr[rsp+offset]
246 OpBuffer[obp++]=(char)0x4C;
247 OpBuffer[obp++]=(char)0x03;
248 OpBuffer[obp++]=(char)0x9C;
249 OpBuffer[obp++]=(char)0x24;
250 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
251 AddLocalVarAddrSchedule();
252 obp+=sizeof(long);
253 }
254 else{
255 //mov r11,qword ptr[rsp+offset]
256 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
257 obp-=sizeof(long);
258 AddLocalVarAddrSchedule();
259 obp+=sizeof(long);
260 }
261
262 goto directmem;
263 }
264 else if(pRelative->dwKind==VAR_DIRECTMEM){
265directmem:
266 //movss dword ptr[r11],xmm0
267 OpBuffer[obp++]=(char)0xF3;
268 OpBuffer[obp++]=(char)0x41;
269 OpBuffer[obp++]=(char)0x0F;
270 OpBuffer[obp++]=(char)0x11;
271 OpBuffer[obp++]=(char)0x03;
272 }
273}
274void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar){
275 if(VarType==DEF_DOUBLE){
276 //Double型変数へスタックの内容を格納する
277 SetDoubleVariable(CalcType,pRelativeVar);
278 }
279 else if(VarType==DEF_SINGLE){
280 //Single型変数へスタックの内容を格納する
281 SetSingleVariable(CalcType,pRelativeVar);
282 }
283}
284void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
285 if(type==DEF_DOUBLE){
286 //Double型
287
288 //cvttsd2si rax,xmm0
289 op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
290 }
291 else if(type==DEF_SINGLE){
292 //Single型
293
294 //cvttss2si rax,xmm0
295 op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
296 }
297
298 //cmp rax,0
299 op_cmp_value(GetTypeSize(type,-1),REG_RAX,0);
300
301 //setne al
302 op_setne( REG_RAX );
303
304 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative);
305}
306void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative){
307 if(type==DEF_DOUBLE){
308 //Double型
309
310 //cvttsd2si rax,xmm0
311 op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
312 }
313 else if(type==DEF_SINGLE){
314 //Single型
315
316 //cvttss2si rax,xmm0
317 op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
318 }
319 else{
320 //その他の整数
321
322 if(varSize==sizeof(_int64)){
323 //レジスタの値を64ビット(rax)に拡張する
324 ExtendTypeTo64(type,REG_RAX);
325 }
326 else if(varSize==sizeof(long)){
327 //レジスタの値を32ビット(eax)に拡張する
328 ExtendTypeTo32(type,REG_RAX);
329 }
330 else if(varSize==sizeof(short)){
331 //レジスタの値を16ビット(ax)に拡張する
332 ExtendTypeTo16(type,REG_RAX);
333 }
334 //8ビットは拡張なし
335 }
336
337 if(pRelative->dwKind==VAR_GLOBAL){
338 if(pRelative->bOffsetOffset){
339 //mov ptr[r11+offset],rax/eax/ax/al
340 op_mov_MR(varSize,REG_RAX,REG_R11,(int)pRelative->offset,MOD_BASE_DISP32);
341 obp-=sizeof(long);
342 pobj_GlobalVarSchedule->add();
343 obp+=sizeof(long);
344 }
345 else{
346 //mov ptr[offset],rax/eax/ax/al
347 op_mov_MR(varSize,REG_RAX,0,(int)pRelative->offset,MOD_DISP32);
348 obp-=sizeof(long);
349 pobj_GlobalVarSchedule->add();
350 obp+=sizeof(long);
351 }
352 }
353 else if( pRelative->dwKind == VAR_REFGLOBAL ){
354 if(pRelative->bOffsetOffset){
355 //add r11,qword ptr[offset]
356 op_add_RM( sizeof(_int64), REG_R11, REG_NON, (int)pRelative->offset, MOD_DISP32 );
357 obp-=sizeof(long);
358 pobj_GlobalVarSchedule->add();
359 obp+=sizeof(long);
360 }
361 else{
362 //mov r11,qword ptr[offset]
363 op_mov_RM(sizeof(_int64),REG_R11,REG_NON,(int)pRelative->offset,MOD_DISP32);
364 obp-=sizeof(long);
365 pobj_GlobalVarSchedule->add();
366 obp+=sizeof(long);
367 }
368
369 goto directmem;
370 }
371 else if(pRelative->dwKind==VAR_LOCAL){
372 if(pRelative->bOffsetOffset){
373 //mov ptr[rsp+r11+offset],rax/eax/ax/al
374 op_mov_MR_ex(varSize,REG_RAX,REG_RSP,REG_R11,(int)pRelative->offset,USE_OFFSET);
375 obp-=sizeof(long);
376 AddLocalVarAddrSchedule();
377 obp+=sizeof(long);
378 }
379 else{
380 //mov ptr[rsp+offset],rax/eax/ax/al
381 op_mov_MR(varSize,REG_RAX,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
382 obp-=sizeof(long);
383 AddLocalVarAddrSchedule();
384 obp+=sizeof(long);
385 }
386 }
387 else if( pRelative->dwKind == VAR_REFLOCAL ){
388 if(pRelative->bOffsetOffset){
389 //add r11,qword ptr[rsp+offset]
390 op_add_RM( sizeof(_int64), REG_R11, REG_RSP, (int)pRelative->offset, MOD_BASE_DISP32 );
391 obp-=sizeof(long);
392 AddLocalVarAddrSchedule();
393 obp+=sizeof(long);
394 }
395 else{
396 //mov r11,qword ptr[rsp+offset]
397 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32);
398 obp-=sizeof(long);
399 AddLocalVarAddrSchedule();
400 obp+=sizeof(long);
401 }
402
403 goto directmem;
404 }
405 else if(pRelative->dwKind==VAR_DIRECTMEM){
406directmem:
407
408 //mov ptr[r11],rax/eax/ax/al
409 op_mov_MR(varSize,REG_RAX,REG_R11,0,MOD_BASE);
410 }
411}
Note: See TracBrowser for help on using the repository browser.