source: dev/BasicCompiler32/Compile_Set_Var.cpp@ 82

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

TYPEINFO→Typeへのリファクタリングを実施。64bitはほぼ完了。32bitが全般的に未完成。

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