source: dev/BasicCompiler32/Compile_Set_Var.cpp@ 66

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

参照型の整数型パラメータに実数を代入すると「内部エラー」になってしまうバグを修正。
参照型の整数型にリテラル値を直接指定すると「内部エラー」になってしまうバグを修正。
バージョンをβ15にした。

File size: 30.0 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4BOOL IsUse_ecx(RELATIVE_VAR *pRelativeVar){
5 if(pRelativeVar->bOffsetOffset||pRelativeVar->dwKind==VAR_DIRECTMEM) return 1;
6 return 0;
7}
8
9void SetStructVariable(LONG_PTR lpVarIndex,int CalcType,LONG_PTR lpCalcIndex,BOOL bUseHeap){
10/*
11 TODO: 消す
12 ///////////////////////////////////////////////////////////////////
13 // オペレータ '=' のオーバーロード関数を呼ぶ
14 ///////////////////////////////////////////////////////////////////
15
16 int type[10];
17 LONG_PTR index_stack[10];
18 BOOL array_bUseHeap[10];
19 int sp=2;
20
21 //左辺
22 type[0]=DEF_OBJECT;
23 index_stack[0]=lpVarIndex;
24 array_bUseHeap[0]=0;
25
26 //右辺
27 type[1]=CalcType;
28 index_stack[1]=lpCalcIndex;
29 array_bUseHeap[1]=bUseHeap;
30
31 int iRet;
32 iRet=CallOperatorProc(CALC_SUBSITUATION,NULL,type,index_stack,array_bUseHeap,sp);
33 if(iRet==-1||iRet==1){
34 //成功したとき、またはエラーが発行されたとき
35 return;
36 }*/
37
38
39 if( CalcType == DEF_STRUCT ){
40 CClass *pVarClass = (CClass *)lpVarIndex;
41 CClass *pCalcClass = (CClass *)lpCalcIndex;
42
43
44 if( pVarClass->IsEquals( pCalcClass ) ){ //等しい
45
46 //双方のオブジェクト型が一致、または派生・継承関係にあるとき
47 //※コピーを行う
48
49 int object_size = pVarClass->GetSize();
50
51 //mov ecx,object_size
52 op_mov_RV(REG_ECX,object_size);
53
54 //pop esi
55 op_pop(REG_ESI);
56
57 //pop edi
58 op_pop(REG_EDI);
59
60 if(bUseHeap){
61 //mov eax,esi
62 op_mov_RR(REG_EAX,REG_ESI);
63 }
64
65 //rep movs byte ptr[edi],byte ptr[esi]
66 op_rep_movs(sizeof(BYTE));
67
68 if(bUseHeap){
69 //push eax
70 op_push(REG_EAX);
71
72 //call free
73 extern SUBINFO *pSub_free;
74 op_call(pSub_free);
75 }
76
77 return;
78 }
79 }
80
81 SetError(1,NULL,cp);
82}
83
84
85void SetRealVariable(int VarType,int CalcType,RELATIVE_VAR *pRelativeVar){
86 if( !IsRealNumberType( CalcType ) ){
87 // 実数へ変換
88 // 64bit edx:eax -> st(0)
89 // 32bit eax -> st(0)
90
91 if( Is64Type( CalcType ) ){
92 //64ビット整数型
93
94 //push edx
95 op_push( REG_EDX );
96
97 //push eax
98 op_push( REG_EAX );
99
100 //fild qword ptr[esp]
101 op_fld_ptr_esp(DEF_INT64);
102
103 //pop
104 op_pop( REG_NON );
105
106 //pop
107 op_pop( REG_NON );
108 }
109 else{
110 //push eax
111 op_push( REG_EAX );
112
113 //fild qword ptr[esp]
114 op_fld_ptr_esp(DEF_LONG);
115
116 //pop
117 op_pop( REG_NON );
118 }
119 }
120
121 if(pRelativeVar->dwKind==VAR_GLOBAL){
122 if(pRelativeVar->bOffsetOffset){
123 //fstp ptr[ecx+offset]
124 op_fstp_base_offset(VarType,REG_ECX,(int)pRelativeVar->offset);
125 obp-=sizeof(long);
126 pobj_GlobalVarSchedule->add();
127 obp+=sizeof(long);
128 }
129 else{
130 //mov ecx,offset
131 op_mov_RV(REG_ECX,(int)pRelativeVar->offset);
132 obp-=sizeof(long);
133 pobj_GlobalVarSchedule->add();
134 obp+=sizeof(long);
135
136 //fstp ptr[ecx]
137 op_fstp_basereg(VarType,REG_ECX);
138 }
139 }
140 else if(pRelativeVar->dwKind==VAR_REFGLOBAL){
141 if(pRelativeVar->bOffsetOffset){
142 //add ecx,qword ptr[offset]
143 op_add_RM(sizeof(long),REG_ECX,REG_NON,(int)pRelativeVar->offset,MOD_DISP32);
144 }
145 else{
146 //mov ecx,qword ptr[offset]
147 op_mov_RM(sizeof(long),REG_ECX,REG_NON,(int)pRelativeVar->offset,MOD_DISP32);
148 }
149 obp-=sizeof(long);
150 pobj_GlobalVarSchedule->add();
151 obp+=sizeof(long);
152
153 goto directmem;
154 }
155 else if(pRelativeVar->dwKind==VAR_LOCAL){
156 if(pRelativeVar->bOffsetOffset){
157 //fstp ptr[ebp+ecx+offset]
158 op_fstp_base_offset_ex(VarType,REG_EBP,REG_ECX,(int)pRelativeVar->offset,USE_OFFSET);
159 }
160 else{
161 //fstp ptr[ebp+offset]
162 op_fstp_base_offset(VarType,REG_EBP,(int)pRelativeVar->offset);
163 }
164 obp-=sizeof(long);
165 AddLocalVarAddrSchedule();
166 obp+=sizeof(long);
167 }
168 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
169 if(pRelativeVar->bOffsetOffset){
170 //add ecx,qword ptr[ebp+offset]
171 op_add_RM(sizeof(long),REG_ECX,REG_EBP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
172 }
173 else{
174 //mov ecx,qword ptr[ebp+offset]
175 op_mov_RM(sizeof(long),REG_ECX,REG_EBP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
176 }
177 obp-=sizeof(long);
178 AddLocalVarAddrSchedule();
179 obp+=sizeof(long);
180
181 goto directmem;
182 }
183 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
184directmem:
185 //fstp ptr[ecx]
186 op_fstp_basereg(VarType,REG_ECX);
187 }
188}
189
190void SetBooleanVariable(int type,RELATIVE_VAR *pRelative){
191 if(type==DEF_DOUBLE){
192 // TODO: 実装
193 SetError();
194 }
195 else if(type==DEF_SINGLE){
196 // TODO: 実装
197 SetError();
198 }
199 else if(type==DEF_INT64||type==DEF_QWORD){
200 //cmp eax,0
201 op_cmp_value(GetTypeSize(type,-1),REG_EAX,0);
202
203 //setne al
204 op_setne( REG_EAX );
205
206 //cmp edx,0
207 op_cmp_value(GetTypeSize(type,-1),REG_EDX,0);
208
209 //setne cl
210 op_setne( REG_ECX );
211
212 //or al,cl
213 op_or_RR( sizeof( _int8 ), REG_EAX, REG_ECX );
214 }
215 else{
216 if(!IsWholeNumberType(type)){
217 //不正な型の場合
218 SetError(9,NULL,cp);
219 return;
220 }
221 }
222
223 //cmp eax,0
224 op_cmp_value(GetTypeSize(type,-1),REG_EAX,0);
225
226 //setne al
227 op_setne( REG_EAX );
228
229 SetWholeVariable( sizeof(char), DEF_BYTE, pRelative );
230}
231
232void ExtendTypeTo32(int type,int reg);
233void ExtendTypeTo64(int type){
234 if(Is64Type(type)) return;
235
236 ExtendTypeTo32(type,REG_EAX);
237
238 if(IsSignedType(type)){
239 //cdq
240 op_cdq();
241 }
242 else{
243 //xor edx,edx
244 op_zero_reg(REG_EDX);
245 }
246}
247void ExtendTypeTo32(int type,int reg){
248 if(type==DEF_INTEGER || (isUnicode&&type==DEF_CHAR)){
249 //movsx reg32,reg16
250 op_movsx_R32R16(reg,reg);
251 }
252 else if(type==DEF_WORD){
253 //and reg,0000FFFFh
254 op_and_RV(reg,(int)0x0000FFFF);
255 }
256 else if(type==DEF_SBYTE || (isUnicode==false&&type==DEF_CHAR)){
257 //movsx reg32,reg8
258 op_movsx_R32R8(reg,reg);
259 }
260 else if(type==DEF_BYTE||type==DEF_BOOLEAN){
261 //and reg,000000FFh
262 op_and_RV(reg,(int)0xFF);
263 }
264}
265void ExtendTypeTo16(int type,int reg){
266 if(type==DEF_SBYTE || (isUnicode==false&&type==DEF_CHAR)){
267 //movsx reg16,reg8
268 op_movsx_R16R8(reg,reg);
269 }
270 else if(type==DEF_BYTE||type==DEF_BOOLEAN){
271 //and reg,000000FFh
272 op_and_RV(reg,(int)0xFF);
273 }
274}
275
276void SetWholeVariable(int varSize,int calcType,RELATIVE_VAR *pRelative){
277 if( IsRealNumberType( calcType ) ){
278 // 実数型から整数型へ変換する
279
280 if( varSize == sizeof(_int64) ){
281 // 64bitへ
282 // st(0) -> edx:eax
283 breakpoint;
284
285 //push
286 //push
287 op_sub_esp( PTR_SIZE * 2 );
288
289 //fistp qword ptr[esp]
290 op_fistp_ptr_esp( sizeof(_int64) );
291
292 //pop eax
293 op_pop( REG_EAX );
294
295 //pop edx
296 op_pop( REG_EDX );
297 }
298 else{
299 // 32bit
300 // st(0) -> eax
301
302 //push
303 op_push( REG_NON );
304
305 //fistp dword ptr[esp]
306 op_fistp_ptr_esp( sizeof(long) );
307
308 //pop eax
309 op_pop( REG_EAX );
310 }
311 }
312 else{
313 //その他の整数
314
315 if(varSize==sizeof(_int64)){
316 //eaxの値を64ビット(edx:eax)に拡張する
317 ExtendTypeTo64(calcType);
318 }
319 else if(varSize==sizeof(long)){
320 //レジスタの値を32ビット(eax)に拡張する
321 ExtendTypeTo32(calcType,REG_EAX);
322 }
323 else if(varSize==sizeof(short)){
324 //レジスタの値を16ビット(ax)に拡張する
325 ExtendTypeTo16(calcType,REG_EAX);
326 }
327 //8ビットは拡張なし
328 }
329
330 if(varSize==sizeof(_int64)){
331 //下位32ビット
332 SetWholeVariable(sizeof(long),DEF_LONG,pRelative);
333
334 //上位32ビット
335
336 //直接参照に切り替え
337 SetVarPtrToEax(pRelative);
338 pRelative->dwKind=VAR_DIRECTMEM;
339
340 //mov ecx,eax
341 op_mov_RR( REG_ECX, REG_EAX );
342
343 //add ecx,sizeof(long)
344 op_add_RV8( REG_ECX, sizeof(long) );
345
346 //mov eax,edx
347 op_mov_RR( REG_EAX, REG_EDX );
348
349 SetWholeVariable(sizeof(long),DEF_LONG,pRelative);
350
351 return;
352 }
353
354 if(pRelative->dwKind==VAR_GLOBAL){
355 if(pRelative->bOffsetOffset){
356 //mov ptr[ecx+offset],eax/ax/al
357 op_mov_MR(varSize,REG_EAX,REG_ECX,(int)pRelative->offset,MOD_BASE_DISP32);
358 }
359 else{
360 //mov ptr[offset],eax/ax/al
361 op_mov_MR(varSize,REG_EAX,0,(int)pRelative->offset,MOD_DISP32);
362 }
363 obp-=sizeof(long);
364 pobj_GlobalVarSchedule->add();
365 obp+=sizeof(long);
366 }
367 else if(pRelative->dwKind==VAR_REFGLOBAL){
368 if(pRelative->bOffsetOffset){
369 //add ecx,qword ptr[offset]
370 op_add_RM(varSize,REG_ECX,REG_NON,(int)pRelative->offset,MOD_DISP32);
371 }
372 else{
373 //mov ecx,qword ptr[offset]
374 op_mov_RM(varSize,REG_ECX,REG_NON,(int)pRelative->offset,MOD_DISP32);
375 }
376 obp-=sizeof(long);
377 pobj_GlobalVarSchedule->add();
378 obp+=sizeof(long);
379
380 goto directmem;
381 }
382 else if(pRelative->dwKind==VAR_LOCAL){
383 if(pRelative->bOffsetOffset){
384 //mov ptr[ebp+ecx+offset],eax/ax/al
385 op_mov_MR_ex(varSize,REG_EAX,REG_EBP,REG_ECX,(int)pRelative->offset,USE_OFFSET);
386 }
387 else{
388 //mov ptr[ebp+offset],eax/ax/al
389 op_mov_MR(varSize,REG_EAX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
390 }
391 obp-=sizeof(long);
392 AddLocalVarAddrSchedule();
393 obp+=sizeof(long);
394 }
395 else if(pRelative->dwKind==VAR_REFLOCAL){
396 if(pRelative->bOffsetOffset){
397 //add ecx,qword ptr[ebp+offset]
398 op_add_RM(varSize,REG_ECX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
399 }
400 else{
401 //mov ecx,qword ptr[ebp+offset]
402 op_mov_RM(varSize,REG_ECX,REG_EBP,(int)pRelative->offset,MOD_BASE_DISP32);
403 }
404 obp-=sizeof(long);
405 AddLocalVarAddrSchedule();
406 obp+=sizeof(long);
407
408 goto directmem;
409 }
410 else if(pRelative->dwKind==VAR_DIRECTMEM){
411directmem:
412
413 //mov ptr[ecx],eax/ax/al
414 op_mov_MR(varSize,REG_EAX,REG_ECX,0,MOD_BASE);
415 }
416}
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442/*
443TODO: 消す
444void SetDoubleVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset){
445 ChangeTypeToDouble(type);
446
447 //pop ebx
448 op_pop(REG_EBX);
449
450 //pop eax
451 op_pop(REG_EAX);
452
453 if(VarKind==VAR_GLOBAL){
454 if(bOffsetOffset){
455 //mov dword ptr[ecx+offset],ebx
456 OpBuffer[obp++]=(char)0x89;
457 OpBuffer[obp++]=(char)0x99;
458 *((long *)(OpBuffer+obp))=offset;
459 pobj_GlobalVarSchedule->add();
460 obp+=sizeof(long);
461
462 //mov dword ptr[ecx+offset+sizeof(long)],eax
463 OpBuffer[obp++]=(char)0x89;
464 OpBuffer[obp++]=(char)0x81;
465 *((long *)(OpBuffer+obp))=offset+sizeof(long);
466 pobj_GlobalVarSchedule->add();
467 obp+=sizeof(long);
468 }
469 else{
470 //mov dword ptr[offset],ebx
471 OpBuffer[obp++]=(char)0x89;
472 OpBuffer[obp++]=(char)0x1D;
473 *((long *)(OpBuffer+obp))=offset;
474 pobj_GlobalVarSchedule->add();
475 obp+=sizeof(long);
476
477 //mov dword ptr[offset+sizeof(long)],eax
478 OpBuffer[obp++]=(char)0xA3;
479 *((long *)(OpBuffer+obp))=offset+sizeof(long);
480 pobj_GlobalVarSchedule->add();
481 obp+=sizeof(long);
482 }
483 }
484 else if( VarKind==VAR_REFGLOBAL ){
485 SetError(300,NULL,cp);
486 }
487 else if(VarKind==VAR_LOCAL){
488 if(bOffsetOffset){
489 //add ecx,offset
490 OpBuffer[obp++]=(char)0x81;
491 OpBuffer[obp++]=(char)0xC1;
492 *((long *)(OpBuffer+obp))=offset;
493 AddLocalVarAddrSchedule();
494 obp+=sizeof(long);
495
496 //mov dword ptr[ebp+ecx],ebx
497 OpBuffer[obp++]=(char)0x89;
498 OpBuffer[obp++]=(char)0x5C;
499 OpBuffer[obp++]=(char)0x0D;
500 OpBuffer[obp++]=(char)0x00;
501
502 //add ecx,sizeof(long)
503 OpBuffer[obp++]=(char)0x83;
504 OpBuffer[obp++]=(char)0xC1;
505 OpBuffer[obp++]=(char)0x04;
506
507 //mov dword ptr[ebp+ecx],eax
508 OpBuffer[obp++]=(char)0x89;
509 OpBuffer[obp++]=(char)0x44;
510 OpBuffer[obp++]=(char)0x0D;
511 OpBuffer[obp++]=(char)0x00;
512 }
513 else{
514 //mov dword ptr[ebp+offset],ebx
515 OpBuffer[obp++]=(char)0x89;
516 OpBuffer[obp++]=(char)0x9D;
517 *((long *)(OpBuffer+obp))=offset;
518 AddLocalVarAddrSchedule();
519 obp+=sizeof(long);
520
521 //mov dword ptr[ebp+offset+sizeof(long)],eax
522 OpBuffer[obp++]=(char)0x89;
523 OpBuffer[obp++]=(char)0x85;
524 *((long *)(OpBuffer+obp))=offset+sizeof(long);
525 AddLocalVarAddrSchedule();
526 obp+=sizeof(long);
527 }
528 }
529 else if(VarKind==VAR_REFLOCAL){
530 if(bOffsetOffset){
531 //add ecx,dword ptr[ebp+offset]
532 OpBuffer[obp++]=(char)0x03;
533 OpBuffer[obp++]=(char)0x8D;
534 *((long *)(OpBuffer+obp))=offset;
535 AddLocalVarAddrSchedule();
536 obp+=sizeof(long);
537 }
538 else{
539 //mov ecx,dword ptr[ebp+offset]
540 OpBuffer[obp++]=(char)0x8B;
541 OpBuffer[obp++]=(char)0x8D;
542 *((long *)(OpBuffer+obp))=offset;
543 AddLocalVarAddrSchedule();
544 obp+=sizeof(long);
545 }
546
547 //mov dword ptr[ecx],ebx
548 OpBuffer[obp++]=(char)0x89;
549 OpBuffer[obp++]=(char)0x19;
550
551 //mov dword ptr[ecx+sizeof(long)],eax
552 OpBuffer[obp++]=(char)0x89;
553 OpBuffer[obp++]=(char)0x41;
554 OpBuffer[obp++]=(char)0x04;
555 }
556 else if(VarKind==VAR_DIRECTMEM){
557 //mov dword ptr[ecx],ebx
558 OpBuffer[obp++]=(char)0x89;
559 OpBuffer[obp++]=(char)0x19;
560
561 //mov dword ptr[ecx+sizeof(long)],eax
562 OpBuffer[obp++]=(char)0x89;
563 OpBuffer[obp++]=(char)0x41;
564 OpBuffer[obp++]=(char)0x04;
565 }
566}
567void SetSingleVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset){
568 if(type==DEF_SINGLE){
569 //32ビット変数としてそのままコピーする
570 SetDWordVariable(DEF_DWORD,VarKind,offset,bOffsetOffset);
571 return;
572 }
573
574 if(type==DEF_DOUBLE){
575 //fld qword ptr[esp]
576 op_fld_ptr_esp(DEF_DOUBLE);
577
578 //add esp,8
579 op_add_esp(8);
580 }
581 else if(type==DEF_INT64||type==DEF_QWORD){
582 //64ビット整数
583
584 //fild qword ptr[esp]
585 op_fld_ptr_esp(DEF_INT64);
586
587 //add esp,8
588 op_add_esp(8);
589 }
590 else if(IsSignedType(type)){
591 //符号あり整数
592
593 //fild dword ptr[esp]
594 op_fld_ptr_esp(DEF_LONG);
595
596 //add esp,4
597 op_add_esp(4);
598 }
599 else{
600 if(!IsWholeNumberType(type)){
601 //不正な型の場合
602 SetError(9,NULL,cp);
603 return;
604 }
605
606 //符号なし整数
607
608 //pop eax
609 op_pop(REG_EAX);
610
611 //push 0
612 op_push_value(0);
613
614 //push eax
615 op_push(REG_EAX);
616
617 //fild qword ptr[esp]
618 OpBuffer[obp++]=(char)0xDF;
619 OpBuffer[obp++]=(char)0x2C;
620 OpBuffer[obp++]=(char)0x24;
621
622 //add esp,8
623 op_add_esp(8);
624 }
625
626 if(VarKind==VAR_GLOBAL){
627 if(bOffsetOffset){
628 //fstp dword ptr[ecx+offset]
629 OpBuffer[obp++]=(char)0xD9;
630 OpBuffer[obp++]=(char)0x99;
631 *((long *)(OpBuffer+obp))=offset;
632 pobj_GlobalVarSchedule->add();
633 obp+=sizeof(long);
634 }
635 else{
636 //fstp dword ptr[offset]
637 OpBuffer[obp++]=(char)0xD9;
638 OpBuffer[obp++]=(char)0x1D;
639 *((long *)(OpBuffer+obp))=offset;
640 pobj_GlobalVarSchedule->add();
641 obp+=sizeof(long);
642 }
643 }
644 else if( VarKind==VAR_REFGLOBAL ){
645 SetError(300,NULL,cp);
646 }
647 else if(VarKind==VAR_LOCAL){
648 if(bOffsetOffset){
649 //add ecx,offset
650 OpBuffer[obp++]=(char)0x81;
651 OpBuffer[obp++]=(char)0xC1;
652 *((long *)(OpBuffer+obp))=offset;
653 AddLocalVarAddrSchedule();
654 obp+=sizeof(long);
655
656 //fstp dword ptr[ebp+ecx]
657 OpBuffer[obp++]=(char)0xD9;
658 OpBuffer[obp++]=(char)0x5C;
659 OpBuffer[obp++]=(char)0x0D;
660 OpBuffer[obp++]=(char)0x00;
661 }
662 else{
663 //fstp dword ptr[ebp+offset]
664 OpBuffer[obp++]=(char)0xD9;
665 OpBuffer[obp++]=(char)0x9D;
666 *((long *)(OpBuffer+obp))=offset;
667 AddLocalVarAddrSchedule();
668 obp+=sizeof(long);
669 }
670 }
671 else if(VarKind==VAR_REFLOCAL){
672 //mov eax,dword ptr[ebp+offset]
673 OpBuffer[obp++]=(char)0x8B;
674 OpBuffer[obp++]=(char)0x85;
675 *((long *)(OpBuffer+obp))=offset;
676 AddLocalVarAddrSchedule();
677 obp+=sizeof(long);
678
679 if(bOffsetOffset){
680 //add eax,ecx
681 OpBuffer[obp++]=(char)0x03;
682 OpBuffer[obp++]=(char)0xC1;
683 }
684
685 //fstp dword ptr[eax]
686 OpBuffer[obp++]=(char)0xD9;
687 OpBuffer[obp++]=(char)0x18;
688 }
689 else if(VarKind==VAR_DIRECTMEM){
690 //fstp dword ptr[ecx]
691 OpBuffer[obp++]=(char)0xD9;
692 OpBuffer[obp++]=(char)0x19;
693 }
694}
695void SetInt64Variable(int type,RELATIVE_VAR *pRelative){
696 if(type==DEF_DOUBLE){
697 //fld qword ptr[esp]
698 op_fld_ptr_esp(DEF_DOUBLE);
699
700 //fistp qword ptr[esp]
701 fpu_cast();
702 OpBuffer[obp++]=(char)0xDF;
703 OpBuffer[obp++]=(char)0x3C;
704 OpBuffer[obp++]=(char)0x24;
705 fpu_cast_end();
706
707 //pop eax
708 op_pop(REG_EAX);
709
710 //pop edx
711 op_pop(REG_EDX);
712 }
713 else if(type==DEF_SINGLE){
714 //fld dword ptr[esp]
715 op_fld_ptr_esp(DEF_SINGLE);
716
717 //sub esp,4
718 op_sub_esp(4);
719
720 //fistp qword ptr[esp]
721 fpu_cast();
722 OpBuffer[obp++]=(char)0xDF;
723 OpBuffer[obp++]=(char)0x3C;
724 OpBuffer[obp++]=(char)0x24;
725 fpu_cast_end();
726
727 //pop eax
728 op_pop(REG_EAX);
729
730 //pop edx
731 op_pop(REG_EDX);
732 }
733 else if(type==DEF_INT64||type==DEF_QWORD){
734 //pop eax
735 op_pop(REG_EAX);
736
737 //pop edx
738 op_pop(REG_EDX);
739 }
740 else if(IsSignedType(type)){
741 //符号拡張
742 //edx:eax ← eax
743
744 //pop eax
745 op_pop(REG_EAX);
746
747 //cdq
748 op_cdq();
749 }
750 else{
751 if(!IsWholeNumberType(type)){
752 //不正な型の場合
753 SetError(9,NULL,cp);
754 return;
755 }
756
757 //符号拡張
758 //edx=0:eax
759
760 //pop eax
761 op_pop(REG_EAX);
762
763 //xor edx,edx
764 op_zero_reg(REG_EDX);
765 }
766
767 if(pRelative->dwKind==VAR_GLOBAL){
768 if(pRelative->bOffsetOffset){
769 //mov dword ptr[ecx+offset],eax
770 OpBuffer[obp++]=(char)0x89;
771 OpBuffer[obp++]=(char)0x81;
772 *((long *)(OpBuffer+obp))=pRelative->offset;
773 pobj_GlobalVarSchedule->add();
774 obp+=sizeof(long);
775
776 //mov dword ptr[ecx+offset+sizeof(long)],edx
777 OpBuffer[obp++]=(char)0x89;
778 OpBuffer[obp++]=(char)0x91;
779 *((long *)(OpBuffer+obp))=pRelative->offset+sizeof(long);
780 pobj_GlobalVarSchedule->add();
781 obp+=sizeof(long);
782 }
783 else{
784 //mov dword ptr[offset],eax
785 OpBuffer[obp++]=(char)0xA3;
786 *((long *)(OpBuffer+obp))=pRelative->offset;
787 pobj_GlobalVarSchedule->add();
788 obp+=sizeof(long);
789
790 //mov dword ptr[offset+sizeof(long)],edx
791 OpBuffer[obp++]=(char)0x89;
792 OpBuffer[obp++]=(char)0x15;
793 *((long *)(OpBuffer+obp))=pRelative->offset+sizeof(DWORD);
794 pobj_GlobalVarSchedule->add();
795 obp+=sizeof(long);
796 }
797 }
798 else if(pRelative->dwKind==VAR_REFLOCAL){
799 if(pRelative->bOffsetOffset){
800 //add ecx,dword ptr[offset]
801 op_add_RM( sizeof(long), REG_ECX, REG_NON, (int)pRelative->offset, MOD_DISP32 );
802 }
803 else{
804 //mov ecx,dword ptr[offset]
805 op_mov_RM( sizeof(long), REG_ECX, REG_NON, (int)pRelative->offset, MOD_DISP32 );
806 }
807 obp-=sizeof(long);
808 pobj_GlobalVarSchedule->add();
809 obp+=sizeof(long);
810
811 //mov dword ptr[ecx],eax
812 OpBuffer[obp++]=(char)0x89;
813 OpBuffer[obp++]=(char)0x01;
814
815 //mov dword ptr[ecx+sizeof(long)],edx
816 OpBuffer[obp++]=(char)0x89;
817 OpBuffer[obp++]=(char)0x51;
818 OpBuffer[obp++]=(char)0x04;
819 }
820 else if(pRelative->dwKind==VAR_LOCAL){
821 if(pRelative->bOffsetOffset){
822 //add ecx,offset
823 OpBuffer[obp++]=(char)0x81;
824 OpBuffer[obp++]=(char)0xC1;
825 *((long *)(OpBuffer+obp))=pRelative->offset;
826 AddLocalVarAddrSchedule();
827 obp+=sizeof(long);
828
829 //mov dword ptr[ebp+ecx],eax
830 OpBuffer[obp++]=(char)0x89;
831 OpBuffer[obp++]=(char)0x44;
832 OpBuffer[obp++]=(char)0x0D;
833 OpBuffer[obp++]=(char)0x00;
834
835 //add ecx,sizeof(long)
836 OpBuffer[obp++]=(char)0x83;
837 OpBuffer[obp++]=(char)0xC1;
838 OpBuffer[obp++]=(char)0x04;
839
840 //mov dword ptr[ebp+ecx],edx
841 OpBuffer[obp++]=(char)0x89;
842 OpBuffer[obp++]=(char)0x54;
843 OpBuffer[obp++]=(char)0x0D;
844 OpBuffer[obp++]=(char)0x00;
845 }
846 else{
847 //mov dword ptr[ebp+offset],eax
848 OpBuffer[obp++]=(char)0x89;
849 OpBuffer[obp++]=(char)0x85;
850 *((long *)(OpBuffer+obp))=pRelative->offset;
851 AddLocalVarAddrSchedule();
852 obp+=sizeof(long);
853
854 //mov dword ptr[ebp+offset+sizeof(long)],edx
855 OpBuffer[obp++]=(char)0x89;
856 OpBuffer[obp++]=(char)0x95;
857 *((long *)(OpBuffer+obp))=pRelative->offset+sizeof(long);
858 AddLocalVarAddrSchedule();
859 obp+=sizeof(long);
860 }
861 }
862 else if(pRelative->dwKind==VAR_REFLOCAL){
863 if(pRelative->bOffsetOffset){
864 //add ecx,dword ptr[ebp+offset]
865 OpBuffer[obp++]=(char)0x03;
866 OpBuffer[obp++]=(char)0x8D;
867 *((long *)(OpBuffer+obp))=pRelative->offset;
868 AddLocalVarAddrSchedule();
869 obp+=sizeof(long);
870 }
871 else{
872 //mov ecx,dword ptr[ebp+offset]
873 OpBuffer[obp++]=(char)0x8B;
874 OpBuffer[obp++]=(char)0x8D;
875 *((long *)(OpBuffer+obp))=pRelative->offset;
876 AddLocalVarAddrSchedule();
877 obp+=sizeof(long);
878 }
879
880 //mov dword ptr[ecx],eax
881 OpBuffer[obp++]=(char)0x89;
882 OpBuffer[obp++]=(char)0x01;
883
884 //mov dword ptr[ecx+sizeof(long)],edx
885 OpBuffer[obp++]=(char)0x89;
886 OpBuffer[obp++]=(char)0x51;
887 OpBuffer[obp++]=(char)0x04;
888 }
889 else if(pRelative->dwKind==VAR_DIRECTMEM){
890 //mov dword ptr[ecx],eax
891 OpBuffer[obp++]=(char)0x89;
892 OpBuffer[obp++]=(char)0x01;
893
894 //mov dword ptr[ecx+sizeof(long)],edx
895 OpBuffer[obp++]=(char)0x89;
896 OpBuffer[obp++]=(char)0x51;
897 OpBuffer[obp++]=(char)0x04;
898 }
899}
900void SetDWordVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset){
901 if(type==DEF_DOUBLE||type==DEF_SINGLE){
902 if(type==DEF_DOUBLE){
903 //fld qword ptr[esp]
904 op_fld_ptr_esp(DEF_DOUBLE);
905 }
906 else if(type==DEF_SINGLE){
907 //fld dword ptr[esp]
908 op_fld_ptr_esp(DEF_SINGLE);
909
910 //sub esp,4
911 op_sub_esp(4);
912 }
913
914 //fistp qword ptr[esp]
915 fpu_cast();
916 OpBuffer[obp++]=(char)0xDF;
917 OpBuffer[obp++]=(char)0x3C;
918 OpBuffer[obp++]=(char)0x24;
919 fpu_cast_end();
920
921 //pop eax
922 op_pop(REG_EAX);
923
924 //add esp,4
925 op_add_esp(4);
926 }
927 else if(type==DEF_INT64||type==DEF_QWORD){
928 //pop eax
929 op_pop(REG_EAX);
930
931 //add esp,4
932 op_add_esp(4);
933 }
934 else{
935 if(!IsWholeNumberType(type)){
936 //不正な型の場合
937 SetError(9,NULL,cp);
938 return;
939 }
940
941 ChangeTypeToWhole(type,DEF_LONG);
942
943 //pop eax
944 op_pop(REG_EAX);
945 }
946
947 if(VarKind==VAR_GLOBAL){
948 if(bOffsetOffset){
949 //mov dword ptr[ecx+offset],eax
950 OpBuffer[obp++]=(char)0x89;
951 OpBuffer[obp++]=(char)0x81;
952 *((long *)(OpBuffer+obp))=offset;
953 pobj_GlobalVarSchedule->add();
954 obp+=sizeof(long);
955 }
956 else{
957 //mov dword ptr[offset],eax
958 OpBuffer[obp++]=(char)0xA3;
959 *((long *)(OpBuffer+obp))=offset;
960 pobj_GlobalVarSchedule->add();
961 obp+=sizeof(long);
962 }
963 }
964 else if(VarKind==VAR_REFGLOBAL){
965 if(bOffsetOffset){
966 //add ecx,dword ptr[offset]
967 op_add_RM( sizeof(long), REG_ECX, REG_NON, (int)offset, MOD_DISP32 );
968 }
969 else{
970 //mov ecx,dword ptr[offset]
971 op_mov_RM( sizeof(long), REG_ECX, REG_NON, (int)offset, MOD_DISP32 );
972 }
973 obp-=sizeof(long);
974 pobj_GlobalVarSchedule->add();
975 obp+=sizeof(long);
976
977 //mov dword ptr[ecx],eax
978 OpBuffer[obp++]=(char)0x89;
979 OpBuffer[obp++]=(char)0x01;
980 }
981 else if(VarKind==VAR_LOCAL){
982 if(bOffsetOffset){
983 //add ecx,offset
984 OpBuffer[obp++]=(char)0x81;
985 OpBuffer[obp++]=(char)0xC1;
986 *((long *)(OpBuffer+obp))=offset;
987 AddLocalVarAddrSchedule();
988 obp+=sizeof(long);
989
990 //mov dword ptr[ebp+ecx],eax
991 OpBuffer[obp++]=(char)0x89;
992 OpBuffer[obp++]=(char)0x44;
993 OpBuffer[obp++]=(char)0x0D;
994 OpBuffer[obp++]=(char)0x00;
995 }
996 else{
997 //mov dword ptr[ebp+offset],eax
998 OpBuffer[obp++]=(char)0x89;
999 OpBuffer[obp++]=(char)0x85;
1000 *((long *)(OpBuffer+obp))=offset;
1001 AddLocalVarAddrSchedule();
1002 obp+=sizeof(long);
1003 }
1004 }
1005 else if(VarKind==VAR_REFLOCAL){
1006 if(bOffsetOffset){
1007 //add ecx,dword ptr[ebp+offset]
1008 OpBuffer[obp++]=(char)0x03;
1009 OpBuffer[obp++]=(char)0x8D;
1010 *((long *)(OpBuffer+obp))=offset;
1011 AddLocalVarAddrSchedule();
1012 obp+=sizeof(long);
1013 }
1014 else{
1015 //mov ecx,dword ptr[ebp+offset]
1016 OpBuffer[obp++]=(char)0x8B;
1017 OpBuffer[obp++]=(char)0x8D;
1018 *((long *)(OpBuffer+obp))=offset;
1019 AddLocalVarAddrSchedule();
1020 obp+=sizeof(long);
1021 }
1022
1023 //mov dword ptr[ecx],eax
1024 OpBuffer[obp++]=(char)0x89;
1025 OpBuffer[obp++]=(char)0x01;
1026 }
1027 else if(VarKind==VAR_DIRECTMEM){
1028 //mov dword ptr[ecx],eax
1029 OpBuffer[obp++]=(char)0x89;
1030 OpBuffer[obp++]=(char)0x01;
1031 }
1032}
1033void SetLongVariable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset){
1034 if(type==DEF_DOUBLE||type==DEF_SINGLE){
1035 if(type==DEF_DOUBLE){
1036 //fld qword ptr[esp]
1037 op_fld_ptr_esp(DEF_DOUBLE);
1038
1039 //add esp,8
1040 op_add_esp(8);
1041 }
1042 else if(type==DEF_SINGLE){
1043 //fld dword ptr[esp]
1044 op_fld_ptr_esp(DEF_SINGLE);
1045
1046 //add esp,4
1047 op_add_esp(4);
1048 }
1049
1050 if(VarKind==VAR_GLOBAL){
1051 if(bOffsetOffset){
1052 //fistp dword ptr[ecx+offset]
1053 fpu_cast();
1054 OpBuffer[obp++]=(char)0xDB;
1055 OpBuffer[obp++]=(char)0x99;
1056 *((long *)(OpBuffer+obp))=offset;
1057 pobj_GlobalVarSchedule->add();
1058 obp+=sizeof(long);
1059 fpu_cast_end();
1060 }
1061 else{
1062 //fistp dword ptr[offset]
1063 fpu_cast();
1064 OpBuffer[obp++]=(char)0xDB;
1065 OpBuffer[obp++]=(char)0x1D;
1066 *((long *)(OpBuffer+obp))=offset;
1067 pobj_GlobalVarSchedule->add();
1068 obp+=sizeof(long);
1069 fpu_cast_end();
1070 }
1071 }
1072 else if(VarKind==VAR_REFGLOBAL){
1073 //mov eax,dword ptr[offset]
1074 op_mov_RM( sizeof(long), REG_EAX, REG_NON, (int)offset, MOD_DISP32 );
1075 obp-=sizeof(long);
1076 pobj_GlobalVarSchedule->add();
1077 obp+=sizeof(long);
1078
1079 if(bOffsetOffset){
1080 //add eax,ecx
1081 OpBuffer[obp++]=(char)0x03;
1082 OpBuffer[obp++]=(char)0xC1;
1083 }
1084
1085 //fistp dword ptr[eax]
1086 fpu_cast();
1087 OpBuffer[obp++]=(char)0xDB;
1088 OpBuffer[obp++]=(char)0x18;
1089 fpu_cast_end();
1090 }
1091 else if(VarKind==VAR_LOCAL){
1092 if(bOffsetOffset){
1093 //add ecx,offset
1094 OpBuffer[obp++]=(char)0x81;
1095 OpBuffer[obp++]=(char)0xC1;
1096 *((long *)(OpBuffer+obp))=offset;
1097 AddLocalVarAddrSchedule();
1098 obp+=sizeof(long);
1099
1100 //fistp dword ptr[ebp+ecx]
1101 fpu_cast();
1102 OpBuffer[obp++]=(char)0xDB;
1103 OpBuffer[obp++]=(char)0x5C;
1104 OpBuffer[obp++]=(char)0x0D;
1105 OpBuffer[obp++]=(char)0x00;
1106 fpu_cast_end();
1107 }
1108 else{
1109 //fistp dword ptr[ebp+offset]
1110 fpu_cast();
1111 OpBuffer[obp++]=(char)0xDB;
1112 OpBuffer[obp++]=(char)0x9D;
1113 *((long *)(OpBuffer+obp))=offset;
1114 AddLocalVarAddrSchedule();
1115 obp+=sizeof(long);
1116 fpu_cast_end();
1117 }
1118 }
1119 else if(VarKind==VAR_REFLOCAL){
1120 //mov eax,dword ptr[ebp+offset]
1121 OpBuffer[obp++]=(char)0x8B;
1122 OpBuffer[obp++]=(char)0x85;
1123 *((long *)(OpBuffer+obp))=offset;
1124 AddLocalVarAddrSchedule();
1125 obp+=sizeof(long);
1126
1127 if(bOffsetOffset){
1128 //add eax,ecx
1129 OpBuffer[obp++]=(char)0x03;
1130 OpBuffer[obp++]=(char)0xC1;
1131 }
1132
1133 //fistp dword ptr[eax]
1134 fpu_cast();
1135 OpBuffer[obp++]=(char)0xDB;
1136 OpBuffer[obp++]=(char)0x18;
1137 fpu_cast_end();
1138 }
1139 else if(VarKind==VAR_DIRECTMEM){
1140 //fistp dword ptr[ecx]
1141 fpu_cast();
1142 OpBuffer[obp++]=(char)0xDB;
1143 OpBuffer[obp++]=(char)0x19;
1144 fpu_cast_end();
1145 }
1146 }
1147 else{
1148 //実数以外の型からの代入処理はDWordのものと同様
1149 SetDWordVariable(type,VarKind,offset,bOffsetOffset);
1150 }
1151}
1152void Set16Variable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset){
1153 ChangeTypeToWhole(type,DEF_INTEGER);
1154
1155 //pop eax
1156 op_pop(REG_EAX);
1157
1158 if(VarKind==VAR_GLOBAL){
1159 if(bOffsetOffset){
1160 //mov word ptr[ecx+offset],ax
1161 OpBuffer[obp++]=(char)0x66;
1162 OpBuffer[obp++]=(char)0x89;
1163 OpBuffer[obp++]=(char)0x81;
1164 *((long *)(OpBuffer+obp))=offset;
1165 pobj_GlobalVarSchedule->add();
1166 obp+=sizeof(long);
1167 }
1168 else{
1169 //mov word ptr[offset],ax
1170 OpBuffer[obp++]=(char)0x66;
1171 OpBuffer[obp++]=(char)0xA3;
1172 *((long *)(OpBuffer+obp))=offset;
1173 pobj_GlobalVarSchedule->add();
1174 obp+=sizeof(long);
1175 }
1176 }
1177 else if(VarKind==VAR_REFGLOBAL){
1178 //mov ebx,dword ptr[offset]
1179 op_mov_RM( sizeof(long), REG_EBX, REG_NON, (int)offset, MOD_DISP32 );
1180 obp-=sizeof(long);
1181 pobj_GlobalVarSchedule->add();
1182 obp+=sizeof(long);
1183
1184 if(bOffsetOffset){
1185 //add ebx,ecx
1186 OpBuffer[obp++]=(char)0x03;
1187 OpBuffer[obp++]=(char)0xD9;
1188 }
1189
1190 //mov word ptr[ebx],ax
1191 OpBuffer[obp++]=(char)0x66;
1192 OpBuffer[obp++]=(char)0x89;
1193 OpBuffer[obp++]=(char)0x03;
1194 }
1195 else if(VarKind==VAR_LOCAL){
1196 if(bOffsetOffset){
1197 //add ecx,offset
1198 OpBuffer[obp++]=(char)0x81;
1199 OpBuffer[obp++]=(char)0xC1;
1200 *((long *)(OpBuffer+obp))=offset;
1201 AddLocalVarAddrSchedule();
1202 obp+=sizeof(long);
1203
1204 //mov word ptr[ebp+ecx],ax
1205 OpBuffer[obp++]=(char)0x66;
1206 OpBuffer[obp++]=(char)0x89;
1207 OpBuffer[obp++]=(char)0x44;
1208 OpBuffer[obp++]=(char)0x0D;
1209 OpBuffer[obp++]=(char)0x00;
1210 }
1211 else{
1212 //mov word ptr[ebp+offset],ax
1213 OpBuffer[obp++]=(char)0x66;
1214 OpBuffer[obp++]=(char)0x89;
1215 OpBuffer[obp++]=(char)0x85;
1216 *((long *)(OpBuffer+obp))=offset;
1217 AddLocalVarAddrSchedule();
1218 obp+=sizeof(long);
1219 }
1220 }
1221 else if(VarKind==VAR_REFLOCAL){
1222 //mov ebx,dword ptr[ebp+offset]
1223 OpBuffer[obp++]=(char)0x8B;
1224 OpBuffer[obp++]=(char)0x9D;
1225 *((long *)(OpBuffer+obp))=offset;
1226 AddLocalVarAddrSchedule();
1227 obp+=sizeof(long);
1228
1229 if(bOffsetOffset){
1230 //add ebx,ecx
1231 OpBuffer[obp++]=(char)0x03;
1232 OpBuffer[obp++]=(char)0xD9;
1233 }
1234
1235 //mov word ptr[ebx],ax
1236 OpBuffer[obp++]=(char)0x66;
1237 OpBuffer[obp++]=(char)0x89;
1238 OpBuffer[obp++]=(char)0x03;
1239 }
1240 else if(VarKind==VAR_DIRECTMEM){
1241 //mov word ptr[ecx],ax
1242 OpBuffer[obp++]=(char)0x66;
1243 OpBuffer[obp++]=(char)0x89;
1244 OpBuffer[obp++]=(char)0x01;
1245 }
1246}
1247void Set8Variable(int type,DWORD VarKind,DWORD offset,BOOL bOffsetOffset){
1248 ChangeTypeToWhole(type,DEF_SBYTE);
1249
1250 //pop eax
1251 op_pop(REG_EAX);
1252
1253 if(VarKind==VAR_GLOBAL){
1254 if(bOffsetOffset){
1255 //mov byte ptr[ecx+offset],al
1256 OpBuffer[obp++]=(char)0x88;
1257 OpBuffer[obp++]=(char)0x81;
1258 *((long *)(OpBuffer+obp))=offset;
1259 pobj_GlobalVarSchedule->add();
1260 obp+=sizeof(long);
1261 }
1262 else{
1263 //mov byte ptr[offset],al
1264 OpBuffer[obp++]=(char)0xA2;
1265 *((long *)(OpBuffer+obp))=offset;
1266 pobj_GlobalVarSchedule->add();
1267 obp+=sizeof(long);
1268 }
1269 }
1270 else if(VarKind==VAR_REFGLOBAL){
1271 //mov ebx,dword ptr[offset]
1272 op_mov_RM( sizeof(long), REG_EBX, REG_NON, (int)offset, MOD_DISP32 );
1273 obp-=sizeof(long);
1274 pobj_GlobalVarSchedule->add();
1275 obp+=sizeof(long);
1276
1277 if(bOffsetOffset){
1278 //add ebx,ecx
1279 OpBuffer[obp++]=(char)0x03;
1280 OpBuffer[obp++]=(char)0xD9;
1281 }
1282
1283 //mov byte ptr[ebx],al
1284 OpBuffer[obp++]=(char)0x88;
1285 OpBuffer[obp++]=(char)0x03;
1286 }
1287 else if(VarKind==VAR_LOCAL){
1288 if(bOffsetOffset){
1289 //add ecx,offset
1290 OpBuffer[obp++]=(char)0x81;
1291 OpBuffer[obp++]=(char)0xC1;
1292 *((long *)(OpBuffer+obp))=offset;
1293 AddLocalVarAddrSchedule();
1294 obp+=sizeof(long);
1295
1296 //mov byte ptr[ebp+ecx],al
1297 OpBuffer[obp++]=(char)0x88;
1298 OpBuffer[obp++]=(char)0x44;
1299 OpBuffer[obp++]=(char)0x0D;
1300 OpBuffer[obp++]=(char)0x00;
1301 }
1302 else{
1303 //mov byte ptr[ebp+offset],al
1304 OpBuffer[obp++]=(char)0x88;
1305 OpBuffer[obp++]=(char)0x85;
1306 *((long *)(OpBuffer+obp))=offset;
1307 AddLocalVarAddrSchedule();
1308 obp+=sizeof(long);
1309 }
1310 }
1311 else if(VarKind==VAR_REFLOCAL){
1312 //mov ebx,dword ptr[ebp+offset]
1313 OpBuffer[obp++]=(char)0x8B;
1314 OpBuffer[obp++]=(char)0x9D;
1315 *((long *)(OpBuffer+obp))=offset;
1316 AddLocalVarAddrSchedule();
1317 obp+=sizeof(long);
1318
1319 if(bOffsetOffset){
1320 //add ebx,ecx
1321 OpBuffer[obp++]=(char)0x03;
1322 OpBuffer[obp++]=(char)0xD9;
1323 }
1324
1325 //mov byte ptr[ebx],al
1326 OpBuffer[obp++]=(char)0x88;
1327 OpBuffer[obp++]=(char)0x03;
1328 }
1329 else if(VarKind==VAR_DIRECTMEM){
1330 //mov byte ptr[ecx],al
1331 OpBuffer[obp++]=(char)0x88;
1332 OpBuffer[obp++]=(char)0x01;
1333 }
1334}
1335*/
Note: See TracBrowser for help on using the repository browser.