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

Last change on this file since 232 was 232, checked in by dai_9181, 17 years ago
File size: 9.5 KB
RevLine 
[206]1#include "stdafx.h"
2
[226]3#include <Compiler.h>
4
[3]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
[75]13void SetStructVariableFromRax( const Type &varType, const Type &calcType, RELATIVE_VAR *pRelativeVar,BOOL bUseHeap){
[3]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 //右辺
[75]23 if( calcType.IsReal() ){
[3]24 RightTermReg=pobj_reg->LockXmmReg();
25
[75]26 if( calcType.IsDouble() ){
[3]27 //movlsd RightTermReg,xmm0
[226]28 compiler.codeGenerator.op_movsd_RR(RightTermReg,REG_XMM0);
[3]29 }
[75]30 else if( calcType.IsSingle() ){
[3]31 //movlss RightTermReg,xmm0
[226]32 compiler.codeGenerator.op_movss_RR(RightTermReg,REG_XMM0);
[3]33 }
34 }
35 else{
36 RightTermReg=pobj_reg->LockReg();
37
38 //mov RightTermReg,rax
[226]39 compiler.codeGenerator.op_mov_RR(RightTermReg,REG_RAX);
[3]40 }
41
42 //右辺用レジスタを解除
[75]43 if( calcType.IsReal() ) pobj_reg->UnlockXmmReg();
[3]44 else pobj_reg->UnlockReg();
45
46 //左辺用レジスタを解除
47 pobj_reg->UnlockReg();
48
49 //レジスタ管理オブジェクトを破棄
50 delete pobj_reg;
51 pobj_reg=0;
52
53
[75]54 if( calcType.IsStruct() ){
55 if( varType.GetClass().IsEquals( &calcType.GetClass() ) ){ //等しい
[3]56
[28]57 //双方のオブジェクト型が一致、または派生・継承関係にあるとき
58 //※コピーを行う
[3]59
[28]60 //mov rsi,RightTermReg
[226]61 compiler.codeGenerator.op_mov_RR(REG_RSI,RightTermReg);
[3]62
[28]63 //mov rdi,VarReg
[226]64 compiler.codeGenerator.op_mov_RR(REG_RDI,VarReg);
[3]65
[75]66 int object_size = varType.GetClass().GetSize();
[3]67
[28]68 //mov rcx,object_size
[226]69 compiler.codeGenerator.op_mov_RV(sizeof(_int64),REG_RCX,object_size);
[3]70
[28]71 if(bUseHeap){
72 //mov rax,rsi
[226]73 compiler.codeGenerator.op_mov_RR(REG_RAX,REG_RSI);
[28]74 }
[3]75
[28]76 //rep movs byte ptr[rdi],byte ptr[rsi]
[226]77 compiler.codeGenerator.op_rep_movs(sizeof(BYTE));
[28]78
79 if(bUseHeap){
80 //mov rcx,rax
[226]81 compiler.codeGenerator.op_mov_RR(REG_RCX,REG_RAX);
[28]82
83 //call free
[206]84 extern const UserProc *pSub_free;
[226]85 compiler.codeGenerator.op_call(pSub_free);
[28]86 }
87
88 return;
[3]89 }
[28]90 }
[3]91
[28]92 SetError(1,NULL,cp);
[3]93}
94
[64]95
[3]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
[232]107 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_R11, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::GlobalVar );
[3]108 }
109 else{
110 //movsd qword ptr[offset],xmm0
[232]111 compiler.codeGenerator.op_movsd_MR( REG_XMM0, 0, (long)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
[3]112 }
113 }
[62]114 else if(pRelative->dwKind==VAR_REFGLOBAL){
115 SetError(300,NULL,cp);
116 }
[3]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
[232]132 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
[3]133 }
134 }
[40]135 else if( pRelative->dwKind == VAR_REFLOCAL ){
[3]136 if(pRelative->bOffsetOffset){
137 //add r11,qword ptr[rsp+offset]
[232]138 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]139 }
140 else{
141 //mov r11,qword ptr[rsp+offset]
[232]142 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]143 }
144
145 goto directmem;
146 }
147 else if(pRelative->dwKind==VAR_DIRECTMEM){
148directmem:
149 //movsd qword ptr[r11],xmm0
[228]150 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
[3]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
[232]164 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::GlobalVar );
[3]165 }
166 else{
167 //movss dword ptr[offset],xmm0
[232]168 compiler.codeGenerator.op_movss_MR( REG_XMM0, 0, (long)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
[3]169 }
170 }
[62]171 else if(pRelative->dwKind==VAR_REFGLOBAL){
172 SetError(300,NULL,cp);
173 }
[3]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
[232]189 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
[3]190 }
191 }
[40]192 else if( pRelative->dwKind == VAR_REFLOCAL ){
[3]193 if(pRelative->bOffsetOffset){
194 //add r11,qword ptr[rsp+offset]
[232]195 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]196 }
197 else{
198 //mov r11,qword ptr[rsp+offset]
[232]199 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]200 }
201
202 goto directmem;
203 }
204 else if(pRelative->dwKind==VAR_DIRECTMEM){
205directmem:
206 //movss dword ptr[r11],xmm0
[228]207 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
[3]208 }
209}
[64]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}
[36]220void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
221 if(type==DEF_DOUBLE){
222 //Double型
223
224 //cvttsd2si rax,xmm0
[226]225 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[36]226 }
227 else if(type==DEF_SINGLE){
228 //Single型
229
230 //cvttss2si rax,xmm0
[226]231 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[36]232 }
233
234 //cmp rax,0
[226]235 compiler.codeGenerator.op_cmp_value(GetTypeSize(type,-1),REG_RAX,0);
[36]236
237 //setne al
[226]238 compiler.codeGenerator.op_setne( REG_RAX );
[36]239
240 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative);
241}
[66]242void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative){
[3]243 if(type==DEF_DOUBLE){
244 //Double型
245
246 //cvttsd2si rax,xmm0
[226]247 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[3]248 }
249 else if(type==DEF_SINGLE){
250 //Single型
251
252 //cvttss2si rax,xmm0
[226]253 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[3]254 }
255 else{
256 //その他の整数
257
[66]258 if(varSize==sizeof(_int64)){
[3]259 //レジスタの値を64ビット(rax)に拡張する
260 ExtendTypeTo64(type,REG_RAX);
261 }
[66]262 else if(varSize==sizeof(long)){
[3]263 //レジスタの値を32ビット(eax)に拡張する
264 ExtendTypeTo32(type,REG_RAX);
265 }
[66]266 else if(varSize==sizeof(short)){
[3]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
[232]276 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::GlobalVar );
[3]277 }
278 else{
279 //mov ptr[offset],rax/eax/ax/al
[232]280 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,0,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
[3]281 }
282 }
[62]283 else if( pRelative->dwKind == VAR_REFGLOBAL ){
284 if(pRelative->bOffsetOffset){
285 //add r11,qword ptr[offset]
[232]286 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_NON, (int)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
[62]287 }
288 else{
289 //mov r11,qword ptr[offset]
[232]290 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_NON,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
[62]291 }
292
293 goto directmem;
294 }
[3]295 else if(pRelative->dwKind==VAR_LOCAL){
296 if(pRelative->bOffsetOffset){
297 //mov ptr[rsp+r11+offset],rax/eax/ax/al
[232]298 compiler.codeGenerator.op_mov_MR_ex(varSize,REG_RAX,REG_RSP,REG_R11,(int)pRelative->offset,USE_OFFSET, Schedule::LocalVar );
[3]299 }
300 else{
301 //mov ptr[rsp+offset],rax/eax/ax/al
[232]302 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]303 }
304 }
[40]305 else if( pRelative->dwKind == VAR_REFLOCAL ){
[3]306 if(pRelative->bOffsetOffset){
307 //add r11,qword ptr[rsp+offset]
[232]308 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_RSP, (int)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
[3]309 }
310 else{
311 //mov r11,qword ptr[rsp+offset]
[232]312 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]313 }
314
315 goto directmem;
316 }
317 else if(pRelative->dwKind==VAR_DIRECTMEM){
318directmem:
319
320 //mov ptr[r11],rax/eax/ax/al
[226]321 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,0,MOD_BASE);
[3]322 }
323}
Note: See TracBrowser for help on using the repository browser.