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

Last change on this file since 244 was 242, checked in by dai_9181, 17 years ago
File size: 9.4 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
[242]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 );
[3]132 }
133 else{
134 //movsd qword ptr[rsp+offset],xmm0
[232]135 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
[3]136 }
137 }
[40]138 else if( pRelative->dwKind == VAR_REFLOCAL ){
[3]139 if(pRelative->bOffsetOffset){
140 //add r11,qword ptr[rsp+offset]
[232]141 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]142 }
143 else{
144 //mov r11,qword ptr[rsp+offset]
[232]145 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]146 }
147
148 goto directmem;
149 }
150 else if(pRelative->dwKind==VAR_DIRECTMEM){
151directmem:
152 //movsd qword ptr[r11],xmm0
[228]153 compiler.codeGenerator.op_movsd_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
[3]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
[232]167 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::GlobalVar );
[3]168 }
169 else{
170 //movss dword ptr[offset],xmm0
[232]171 compiler.codeGenerator.op_movss_MR( REG_XMM0, 0, (long)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
[3]172 }
173 }
[62]174 else if(pRelative->dwKind==VAR_REFGLOBAL){
175 SetError(300,NULL,cp);
176 }
[3]177 else if(pRelative->dwKind==VAR_LOCAL){
178 if(pRelative->bOffsetOffset){
179 //movss dword ptr[rsp+r11+offset],xmm0
[242]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 );
[3]192 }
193 else{
194 //movss dword ptr[rsp+offset],xmm0
[232]195 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_RSP, (long)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
[3]196 }
197 }
[40]198 else if( pRelative->dwKind == VAR_REFLOCAL ){
[3]199 if(pRelative->bOffsetOffset){
200 //add r11,qword ptr[rsp+offset]
[232]201 compiler.codeGenerator.op_add_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]202 }
203 else{
204 //mov r11,qword ptr[rsp+offset]
[232]205 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]206 }
207
208 goto directmem;
209 }
210 else if(pRelative->dwKind==VAR_DIRECTMEM){
211directmem:
212 //movss dword ptr[r11],xmm0
[228]213 compiler.codeGenerator.op_movss_MR( REG_XMM0, REG_R11, 0, MOD_BASE );
[3]214 }
215}
[64]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}
[36]226void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
227 if(type==DEF_DOUBLE){
228 //Double型
229
230 //cvttsd2si rax,xmm0
[226]231 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[36]232 }
233 else if(type==DEF_SINGLE){
234 //Single型
235
236 //cvttss2si rax,xmm0
[226]237 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[36]238 }
239
240 //cmp rax,0
[226]241 compiler.codeGenerator.op_cmp_value(GetTypeSize(type,-1),REG_RAX,0);
[36]242
243 //setne al
[226]244 compiler.codeGenerator.op_setne( REG_RAX );
[36]245
246 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative);
247}
[66]248void SetWholeVariable(int varSize,int type,RELATIVE_VAR *pRelative){
[3]249 if(type==DEF_DOUBLE){
250 //Double型
251
252 //cvttsd2si rax,xmm0
[226]253 compiler.codeGenerator.op_cvttsd2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[3]254 }
255 else if(type==DEF_SINGLE){
256 //Single型
257
258 //cvttss2si rax,xmm0
[226]259 compiler.codeGenerator.op_cvttss2si_xmm(sizeof(_int64),REG_RAX,REG_XMM0);
[3]260 }
261 else{
262 //その他の整数
263
[66]264 if(varSize==sizeof(_int64)){
[3]265 //レジスタの値を64ビット(rax)に拡張する
266 ExtendTypeTo64(type,REG_RAX);
267 }
[66]268 else if(varSize==sizeof(long)){
[3]269 //レジスタの値を32ビット(eax)に拡張する
270 ExtendTypeTo32(type,REG_RAX);
271 }
[66]272 else if(varSize==sizeof(short)){
[3]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
[232]282 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::GlobalVar );
[3]283 }
284 else{
285 //mov ptr[offset],rax/eax/ax/al
[232]286 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,0,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
[3]287 }
288 }
[62]289 else if( pRelative->dwKind == VAR_REFGLOBAL ){
290 if(pRelative->bOffsetOffset){
291 //add r11,qword ptr[offset]
[232]292 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_NON, (int)pRelative->offset, MOD_DISP32, Schedule::GlobalVar );
[62]293 }
294 else{
295 //mov r11,qword ptr[offset]
[232]296 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_NON,(int)pRelative->offset,MOD_DISP32, Schedule::GlobalVar );
[62]297 }
298
299 goto directmem;
300 }
[3]301 else if(pRelative->dwKind==VAR_LOCAL){
302 if(pRelative->bOffsetOffset){
303 //mov ptr[rsp+r11+offset],rax/eax/ax/al
[232]304 compiler.codeGenerator.op_mov_MR_ex(varSize,REG_RAX,REG_RSP,REG_R11,(int)pRelative->offset,USE_OFFSET, Schedule::LocalVar );
[3]305 }
306 else{
307 //mov ptr[rsp+offset],rax/eax/ax/al
[232]308 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]309 }
310 }
[40]311 else if( pRelative->dwKind == VAR_REFLOCAL ){
[3]312 if(pRelative->bOffsetOffset){
313 //add r11,qword ptr[rsp+offset]
[232]314 compiler.codeGenerator.op_add_RM( sizeof(_int64), REG_R11, REG_RSP, (int)pRelative->offset, MOD_BASE_DISP32, Schedule::LocalVar );
[3]315 }
316 else{
317 //mov r11,qword ptr[rsp+offset]
[232]318 compiler.codeGenerator.op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelative->offset,MOD_BASE_DISP32, Schedule::LocalVar );
[3]319 }
320
321 goto directmem;
322 }
323 else if(pRelative->dwKind==VAR_DIRECTMEM){
324directmem:
325
326 //mov ptr[r11],rax/eax/ax/al
[226]327 compiler.codeGenerator.op_mov_MR(varSize,REG_RAX,REG_R11,0,MOD_BASE);
[3]328 }
329}
Note: See TracBrowser for help on using the repository browser.