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

Last change on this file since 251 was 242, checked in by dai_9181, 17 years ago
File size: 9.4 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 compiler.codeGenerator.PutOld(
121 (char)0xF2,
122 (char)0x42,
123 (char)0x0F,
124 (char)0x11,
125 (char)0x84,
126 (char)0x1C
127 );
128 compiler.codeGenerator.PutOld(
129 (long)pRelative->offset,
130 Schedule::LocalVar
131 );
132 }
133 else{
134 //movsd qword ptr[rsp+offset],xmm0
135 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
136 }
137 }
138 else if( pRelative->dwKind == VAR_REFLOCAL ){
139 if(pRelative->bOffsetOffset){
140 //add r11,qword ptr[rsp+offset]
141 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
142 }
143 else{
144 //mov r11,qword ptr[rsp+offset]
145 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
146 }
147
148 goto directmem;
149 }
150 else if(pRelative->dwKind==VAR_DIRECTMEM){
151directmem:
152 //movsd qword ptr[r11],xmm0
153 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
154 }
155}
156void SetSingleVariable(int type,RELATIVE_VAR *pRelative){
157 //////////////////////////
158 // Single型変数に書き込む
159 //////////////////////////
160
161 //xmm0に型変換
162 ChangeTypeToXmm_Single(type,REG_XMM0,REG_RAX);
163
164 if(pRelative->dwKind==VAR_GLOBAL){
165 if(pRelative->bOffsetOffset){
166 //movss dword ptr[r11+offset],xmm0
167 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::GlobalVar );
168 }
169 else{
170 //movss dword ptr[offset],xmm0
171 compiler.codeGenerator.op_movss_MR( REG_XMM0, 0, (long)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
172 }
173 }
174 else if(pRelative->dwKind==VAR_REFGLOBAL){
175 SetError(300,NULL,cp);
176 }
177 else if(pRelative->dwKind==VAR_LOCAL){
178 if(pRelative->bOffsetOffset){
179 //movss dword ptr[rsp+r11+offset],xmm0
180 compiler.codeGenerator.PutOld(
181 (char)0xF3,
182 (char)0x42,
183 (char)0x0F,
184 (char)0x11,
185 (char)0x84,
186 (char)0x1C
187 );
188 compiler.codeGenerator.PutOld(
189 (long)pRelative->offset,
190 Schedule::LocalVar
191 );
192 }
193 else{
194 //movss dword ptr[rsp+offset],xmm0
195 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
196 }
197 }
198 else if( pRelative->dwKind == VAR_REFLOCAL ){
199 if(pRelative->bOffsetOffset){
200 //add r11,qword ptr[rsp+offset]
201 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
202 }
203 else{
204 //mov r11,qword ptr[rsp+offset]
205 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
206 }
207
208 goto directmem;
209 }
210 else if(pRelative->dwKind==VAR_DIRECTMEM){
211directmem:
212 //movss dword ptr[r11],xmm0
213 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
214 }
215}
216void SetRealVariable(int VarType, int CalcType, RELATIVE_VAR *pRelativeVar){
217 if(VarType==DEF_DOUBLE){
218 //Double型変数へスタックの内容を格納する
219 SetDoubleVariable(CalcType,pRelativeVar);
220 }
221 else if(VarType==DEF_SINGLE){
222 //Single型変数へスタックの内容を格納する
223 SetSingleVariable(CalcType,pRelativeVar);
224 }
225}
226void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
227 if(type==DEF_DOUBLE){
228 //Double型
229
230 //cvttsd2si rax,xmm0
231 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
232 }
233 else if(type==DEF_SINGLE){
234 //Single型
235
236 //cvttss2si rax,xmm0
237 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
238 }
239
240 //cmp rax,0
241 compiler.codeGenerator.op_cmp_value(GetTypeSize(type,-1),REG_RAX,0);
242
243 //setne al
244 compiler.codeGenerator.op_setne( REG_RAX );
245
246 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative);
247}
248void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative){
249 if(type==DEF_DOUBLE){
250 //Double型
251
252 //cvttsd2si rax,xmm0
253 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
254 }
255 else if(type==DEF_SINGLE){
256 //Single型
257
258 //cvttss2si rax,xmm0
259 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
260 }
261 else{
262 //その他の整数
263
264 if(varSize==sizeof(_int64)){
265 //レジスタの値を64ビット(rax)に拡張する
266 ExtendTypeTo64(type,REG_RAX);
267 }
268 else if(varSize==sizeof(long)){
269 //レジスタの値を32ビット(eax)に拡張する
270 ExtendTypeTo32(type,REG_RAX);
271 }
272 else if(varSize==sizeof(short)){
273 //レジスタの値を16ビット(ax)に拡張する
274 ExtendTypeTo16(type,REG_RAX);
275 }
276 //8ビットは拡張なし
277 }
278
279 if(pRelative->dwKind==VAR_GLOBAL){
280 if(pRelative->bOffsetOffset){
281 //mov ptr[r11+offset],rax/eax/ax/al
282 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::GlobalVar );
283 }
284 else{
285 //mov ptr[offset],rax/eax/ax/al
286 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,0,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
287 }
288 }
289 else if( pRelative->dwKind == VAR_REFGLOBAL ){
290 if(pRelative->bOffsetOffset){
291 //add r11,qword ptr[offset]
292 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_NON, (int)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
293 }
294 else{
295 //mov r11,qword ptr[offset]
296 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_NON,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
297 }
298
299 goto directmem;
300 }
301 else if(pRelative->dwKind==VAR_LOCAL){
302 if(pRelative->bOffsetOffset){
303 //mov ptr[rsp+r11+offset],rax/eax/ax/al
304 compiler.codeGenerator.op_mov_MR_ex(varSize,REG_RAX,REG_RSP,REG_R11,(int)pRelative->offset,USE_OFFSET, Schedule::LocalVar );
305 }
306 else{
307 //mov ptr[rsp+offset],rax/eax/ax/al
308 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
309 }
310 }
311 else if( pRelative->dwKind == VAR_REFLOCAL ){
312 if(pRelative->bOffsetOffset){
313 //add r11,qword ptr[rsp+offset]
314 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_RSP, (int)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
315 }
316 else{
317 //mov r11,qword ptr[rsp+offset]
318 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
319 }
320
321 goto directmem;
322 }
323 else if(pRelative->dwKind==VAR_DIRECTMEM){
324directmem:
325
326 //mov ptr[r11],rax/eax/ax/al
327 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,0,MOD_BASE);
328 }
329}
Note: See TracBrowser for help on using the repository browser.