source: dev/trunk/abdev/BasicCompiler64/Compile_Set_Var.cpp@ 235

Last change on this file since 235 was 232, checked in by dai_9181, 17 years ago
File size: 9.5 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5#include "../BasicCompiler_Common/common.h"
6#include "Opcode.h"
7
8BOOL IsUse_r11(RELATIVE_VAR *pRelativeVar){
9 if(pRelativeVar->bOffsetOffset||pRelativeVar->dwKind==VAR_DIRECTMEM) return 1;
10 return 0;
11}
12
13void SetStructVariableFromRax( const Type &varType, const Type &calcType, RELATIVE_VAR *pRelativeVar,BOOL bUseHeap){
14 int RightTermReg;
15 pobj_reg=new CRegister(REG_RCX);
16
17 //VarRegにオブジェクトポインタをコピー
18 int VarReg;
19 VarReg=pobj_reg->LockReg();
20 SetVarPtrToReg(VarReg,pRelativeVar);
21
22 //右辺
23 if( calcType.IsReal() ){
24 RightTermReg=pobj_reg->LockXmmReg();
25
26 if( calcType.IsDouble() ){
27 //movlsd RightTermReg,xmm0
28 compiler.codeGenerator.op_movsd_RR(RightTermReg,REG_XMM0);
29 }
30 else if( calcType.IsSingle() ){
31 //movlss RightTermReg,xmm0
32 compiler.codeGenerator.op_movss_RR(RightTermReg,REG_XMM0);
33 }
34 }
35 else{
36 RightTermReg=pobj_reg->LockReg();
37
38 //mov RightTermReg,rax
39 compiler.codeGenerator.op_mov_RR(RightTermReg,REG_RAX);
40 }
41
42 //右辺用レジスタを解除
43 if( calcType.IsReal() ) pobj_reg->UnlockXmmReg();
44 else pobj_reg->UnlockReg();
45
46 //左辺用レジスタを解除
47 pobj_reg->UnlockReg();
48
49 //レジスタ管理オブジェクトを破棄
50 delete pobj_reg;
51 pobj_reg=0;
52
53
54 if( calcType.IsStruct() ){
55 if( varType.GetClass().IsEquals( &calcType.GetClass() ) ){ //等しい
56
57 //双方のオブジェクト型が一致、または派生・継承関係にあるとき
58 //※コピーを行う
59
60 //mov rsi,RightTermReg
61 compiler.codeGenerator.op_mov_RR(REG_RSI,RightTermReg);
62
63 //mov rdi,VarReg
64 compiler.codeGenerator.op_mov_RR(REG_RDI,VarReg);
65
66 int object_size = varType.GetClass().GetSize();
67
68 //mov rcx,object_size
69 compiler.codeGenerator.op_mov_RV(sizeof(_int64),REG_RCX,object_size);
70
71 if(bUseHeap){
72 //mov rax,rsi
73 compiler.codeGenerator.op_mov_RR(REG_RAX,REG_RSI);
74 }
75
76 //rep movs byte ptr[rdi],byte ptr[rsi]
77 compiler.codeGenerator.op_rep_movs(sizeof(BYTE));
78
79 if(bUseHeap){
80 //mov rcx,rax
81 compiler.codeGenerator.op_mov_RR(REG_RCX,REG_RAX);
82
83 //call free
84 extern const UserProc *pSub_free;
85 compiler.codeGenerator.op_call(pSub_free);
86 }
87
88 return;
89 }
90 }
91
92 SetError(1,NULL,cp);
93}
94
95
96void SetDoubleVariable(int type,RELATIVE_VAR *pRelative){
97 //////////////////////////
98 // Double型変数に書き込む
99 //////////////////////////
100
101 //xmm0に型変換
102 ChangeTypeToXmm_Double(type,REG_XMM0,REG_RAX);
103
104 if(pRelative->dwKind==VAR_GLOBAL){
105 if(pRelative->bOffsetOffset){
106 //movsd qword ptr[r11+offset],xmm0
107 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_R11, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::GlobalVar );
108 }
109 else{
110 //movsd qword ptr[offset],xmm0
111 compiler.codeGenerator.op_movsd_MR( REG_XMM0, 0, (long)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
112 }
113 }
114 else if(pRelative->dwKind==VAR_REFGLOBAL){
115 SetError(300,NULL,cp);
116 }
117 else if(pRelative->dwKind==VAR_LOCAL){
118 if(pRelative->bOffsetOffset){
119 //movsd qword ptr[rsp+r11+offset],xmm0
120 OpBuffer[obp++]=(char)0xF2;
121 OpBuffer[obp++]=(char)0x42;
122 OpBuffer[obp++]=(char)0x0F;
123 OpBuffer[obp++]=(char)0x11;
124 OpBuffer[obp++]=(char)0x84;
125 OpBuffer[obp++]=(char)0x1C;
126 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
127 AddLocalVarAddrSchedule();
128 obp+=sizeof(long);
129 }
130 else{
131 //movsd qword ptr[rsp+offset],xmm0
132 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
133 }
134 }
135 else if( pRelative->dwKind == VAR_REFLOCAL ){
136 if(pRelative->bOffsetOffset){
137 //add r11,qword ptr[rsp+offset]
138 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
139 }
140 else{
141 //mov r11,qword ptr[rsp+offset]
142 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
143 }
144
145 goto directmem;
146 }
147 else if(pRelative->dwKind==VAR_DIRECTMEM){
148directmem:
149 //movsd qword ptr[r11],xmm0
150 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
151 }
152}
153void SetSingleVariable(int type,RELATIVE_VAR *pRelative){
154 //////////////////////////
155 // Single型変数に書き込む
156 //////////////////////////
157
158 //xmm0に型変換
159 ChangeTypeToXmm_Single(type,REG_XMM0,REG_RAX);
160
161 if(pRelative->dwKind==VAR_GLOBAL){
162 if(pRelative->bOffsetOffset){
163 //movss dword ptr[r11+offset],xmm0
164 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::GlobalVar );
165 }
166 else{
167 //movss dword ptr[offset],xmm0
168 compiler.codeGenerator.op_movss_MR( REG_XMM0, 0, (long)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
169 }
170 }
171 else if(pRelative->dwKind==VAR_REFGLOBAL){
172 SetError(300,NULL,cp);
173 }
174 else if(pRelative->dwKind==VAR_LOCAL){
175 if(pRelative->bOffsetOffset){
176 //movss dword ptr[rsp+r11+offset],xmm0
177 OpBuffer[obp++]=(char)0xF3;
178 OpBuffer[obp++]=(char)0x42;
179 OpBuffer[obp++]=(char)0x0F;
180 OpBuffer[obp++]=(char)0x11;
181 OpBuffer[obp++]=(char)0x84;
182 OpBuffer[obp++]=(char)0x1C;
183 *((long *)(OpBuffer+obp))=(int)pRelative->offset;
184 AddLocalVarAddrSchedule();
185 obp+=sizeof(long);
186 }
187 else{
188 //movss dword ptr[rsp+offset],xmm0
189 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
190 }
191 }
192 else if( pRelative->dwKind == VAR_REFLOCAL ){
193 if(pRelative->bOffsetOffset){
194 //add r11,qword ptr[rsp+offset]
195 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
196 }
197 else{
198 //mov r11,qword ptr[rsp+offset]
199 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
200 }
201
202 goto directmem;
203 }
204 else if(pRelative->dwKind==VAR_DIRECTMEM){
205directmem:
206 //movss dword ptr[r11],xmm0
207 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
208 }
209}
210void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar){
211 if(VarType==DEF_DOUBLE){
212 //Double型変数へスタックの内容を格納する
213 SetDoubleVariable(CalcType,pRelativeVar);
214 }
215 else if(VarType==DEF_SINGLE){
216 //Single型変数へスタックの内容を格納する
217 SetSingleVariable(CalcType,pRelativeVar);
218 }
219}
220void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
221 if(type==DEF_DOUBLE){
222 //Double型
223
224 //cvttsd2si rax,xmm0
225 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
226 }
227 else if(type==DEF_SINGLE){
228 //Single型
229
230 //cvttss2si rax,xmm0
231 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
232 }
233
234 //cmp rax,0
235 compiler.codeGenerator.op_cmp_value(GetTypeSize(type,-1),REG_RAX,0);
236
237 //setne al
238 compiler.codeGenerator.op_setne( REG_RAX );
239
240 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative);
241}
242void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative){
243 if(type==DEF_DOUBLE){
244 //Double型
245
246 //cvttsd2si rax,xmm0
247 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
248 }
249 else if(type==DEF_SINGLE){
250 //Single型
251
252 //cvttss2si rax,xmm0
253 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
254 }
255 else{
256 //その他の整数
257
258 if(varSize==sizeof(_int64)){
259 //レジスタの値を64ビット(rax)に拡張する
260 ExtendTypeTo64(type,REG_RAX);
261 }
262 else if(varSize==sizeof(long)){
263 //レジスタの値を32ビット(eax)に拡張する
264 ExtendTypeTo32(type,REG_RAX);
265 }
266 else if(varSize==sizeof(short)){
267 //レジスタの値を16ビット(ax)に拡張する
268 ExtendTypeTo16(type,REG_RAX);
269 }
270 //8ビットは拡張なし
271 }
272
273 if(pRelative->dwKind==VAR_GLOBAL){
274 if(pRelative->bOffsetOffset){
275 //mov ptr[r11+offset],rax/eax/ax/al
276 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::GlobalVar );
277 }
278 else{
279 //mov ptr[offset],rax/eax/ax/al
280 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,0,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
281 }
282 }
283 else if( pRelative->dwKind == VAR_REFGLOBAL ){
284 if(pRelative->bOffsetOffset){
285 //add r11,qword ptr[offset]
286 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_NON, (int)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
287 }
288 else{
289 //mov r11,qword ptr[offset]
290 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_NON,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
291 }
292
293 goto directmem;
294 }
295 else if(pRelative->dwKind==VAR_LOCAL){
296 if(pRelative->bOffsetOffset){
297 //mov ptr[rsp+r11+offset],rax/eax/ax/al
298 compiler.codeGenerator.op_mov_MR_ex(varSize,REG_RAX,REG_RSP,REG_R11,(int)pRelative->offset,USE_OFFSET, Schedule::LocalVar );
299 }
300 else{
301 //mov ptr[rsp+offset],rax/eax/ax/al
302 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
303 }
304 }
305 else if( pRelative->dwKind == VAR_REFLOCAL ){
306 if(pRelative->bOffsetOffset){
307 //add r11,qword ptr[rsp+offset]
308 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_RSP, (int)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
309 }
310 else{
311 //mov r11,qword ptr[rsp+offset]
312 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
313 }
314
315 goto directmem;
316 }
317 else if(pRelative->dwKind==VAR_DIRECTMEM){
318directmem:
319
320 //mov ptr[r11],rax/eax/ax/al
321 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,0,MOD_BASE);
322 }
323}
Note: See TracBrowser for help on using the repository browser.