source: dev/BasicCompiler32/Compile_Var.cpp@ 101

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

関数の戻り値オブジェクトのメンバ・メソッドを一時オブジェクトを介さずに参照できるようにした。

File size: 31.5 KB
Line 
1#include "../BasicCompiler_Common/common.h"
2#include "Opcode.h"
3
4//変数
5Variables globalVars;
6int AllGlobalVarSize;
7int AllInitGlobalVarSize;
8
9int AllLocalVarSize;
10
11
12void SetRelativeOffset( Type &resultType, RELATIVE_VAR *pRelativeVar,const char *lpPtrOffset){
13 PushLongVariable(pRelativeVar);
14
15 Type type;
16 NumOpe( lpPtrOffset, Type(), type );
17 ChangeTypeToLong( type.GetBasicType() );
18
19 //pop ebx
20 op_pop(REG_EBX);
21
22 if( resultType.PtrLevel() ){
23 resultType.PtrLevelDown();
24
25 int typeSize = resultType.GetSize();
26 if(typeSize>=2){
27 //imul ebx,i2
28 op_imul_RV( REG_EBX, typeSize );
29 }
30 }
31 else{
32 //エラー
33 SetError(1,NULL,cp);
34 return;
35 }
36
37 //pop ecx
38 op_pop(REG_ECX);
39
40 //add ecx,ebx
41 OpBuffer[obp++]=(char)0x03;
42 OpBuffer[obp++]=(char)0xCB;
43}
44void SetRelativeOffset( RELATIVE_VAR &relativeVar ){
45 if(relativeVar.dwKind==VAR_DIRECTMEM){
46 //mov ecx,dword ptr[ecx]
47 op_mov_RM( sizeof(long), REG_ECX, REG_ECX, 0, MOD_BASE );
48 }
49 else{
50 //直接参照に切り替え
51 SetVarPtrToEax(&relativeVar);
52 relativeVar.dwKind=VAR_DIRECTMEM;
53
54 //mov ecx,dword ptr[eax]
55 op_mov_RM( sizeof(long), REG_ECX, REG_EAX, 0, MOD_BASE );
56 }
57}
58bool GetArrayOffset(const int *SubScripts,char *array, const Type &type){
59 extern HANDLE hHeap;
60 int i,i2,i3,i4;
61 char temporary[VN_SIZE],*pParm[MAX_PARMS];
62
63 for(i=0,i2=0,i3=0;;i++,i2++){
64 if(array[i]=='('){
65 i4=GetStringInPare(temporary+i2,array+i);
66 i+=i4-1;
67 i2+=i4-1;
68 continue;
69 }
70 if(array[i]=='['){
71 i4=GetStringInBracket(temporary+i2,array+i);
72 i+=i4-1;
73 i2+=i4-1;
74 continue;
75 }
76 if(array[i]==','||array[i]=='\0'){
77 if(SubScripts[i3]==-1){
78 for(i3--;i3>=0;i3--) HeapDefaultFree(pParm[i3]);
79 return 0;
80 }
81
82 temporary[i2]=0;
83
84 pParm[i3]=(char *)HeapAlloc(hHeap,0,lstrlen(temporary)+1);
85 lstrcpy(pParm[i3],temporary);
86
87 i3++;
88
89 if(array[i]=='\0'){
90 if(SubScripts[i3]!=-1){
91 for(i3--;i3>=0;i3--) HeapDefaultFree(pParm[i3]);
92 return 0;
93 }
94 break;
95 }
96
97 i2=-1;
98 continue;
99 }
100 temporary[i2]=array[i];
101 }
102
103 //push ecx
104 op_push(REG_ECX);
105
106 //push 0
107 op_push_V(0);
108
109 for(i=i3-1;i>=0;i--){
110 Type tempType;
111 BOOL bUseHeap;
112 NumOpe( pParm[i], Type( DEF_LONG ), tempType, &bUseHeap );
113 if( tempType.IsObject() ){
114 //キャスト演算子のオーバーロードに対応する
115 CallCastOperatorProc(
116 tempType,
117 bUseHeap, Type(DEF_LONG) );
118 tempType.SetBasicType( DEF_LONG );
119 }
120 ChangeTypeToLong( tempType.GetBasicType() );
121
122 //pop eax
123 op_pop(REG_EAX);
124
125 for(i2=i+1,i4=1;i2<i3;i2++) i4*=SubScripts[i2]+1;
126
127 //imul eax,i4
128 OpBuffer[obp++]=(char)0x69;
129 OpBuffer[obp++]=(char)0xC0;
130 *((long *)(OpBuffer+obp))=i4;
131 obp+=sizeof(long);
132
133 //add dword ptr[esp],eax
134 OpBuffer[obp++]=(char)0x01;
135 OpBuffer[obp++]=(char)0x04;
136 OpBuffer[obp++]=(char)0x24;
137
138 HeapDefaultFree(pParm[i]);
139 }
140
141 //pop eax
142 op_pop(REG_EAX);
143
144 //imul eax,TypeSize
145 OpBuffer[obp++]=(char)0x69;
146 OpBuffer[obp++]=(char)0xC0;
147 *((long *)(OpBuffer+obp)) = type.GetSize();
148 obp+=sizeof(long);
149
150 //pop ecx
151 op_pop(REG_ECX);
152
153 //add ecx,eax
154 OpBuffer[obp++]=(char)0x03;
155 OpBuffer[obp++]=(char)0xC8;
156
157 return 1;
158}
159bool _member_offset(bool isErrorEnabled, bool isWriteAccess, const CClass &objClass, const char *member, RELATIVE_VAR *pRelativeVar, Type &resultType, BOOL bPrivateAccess){
160 int i;
161
162
163 //////////////////////////////////////
164 // クラス、配列の構成要素を解析する
165 //////////////////////////////////////
166
167 char VarName[VN_SIZE]; //変数名
168 char array[VN_SIZE]; //第1次配列
169 char lpPtrOffset[VN_SIZE]; //第2次配列
170 char NestMember[VN_SIZE]; //入れ子メンバ
171 CClass::RefType refType;
172 lstrcpy(VarName,member);
173 if(!GetVarFormatString(VarName,array,lpPtrOffset,NestMember,refType)) return false;
174
175
176 ////////////////////////////
177 // メンバオフセットを取得
178 ////////////////////////////
179
180 int offset = objClass.GetMemberOffset( VarName, &i );
181 if(i==objClass.iMemberNum){
182 if(isErrorEnabled) SetError(103,VarName,cp);
183 return false;
184 }
185
186 CMember *pMember=objClass.ppobj_Member[i];
187
188
189 //アクセシビリティをチェック
190 if(&objClass==pobj_CompilingClass){
191 //同一クラスオブジェクトの場合はプライベートアクセスを容認する
192 if(pMember->dwAccess==ACCESS_NON){
193 if(isErrorEnabled) SetError(107,VarName,cp);
194 return false;
195 }
196 }
197 else{
198 if((bPrivateAccess==0&&pMember->dwAccess==ACCESS_PRIVATE)||
199 pMember->dwAccess==ACCESS_NON){
200 if(isErrorEnabled) SetError(107,VarName,cp);
201 return false;
202 }
203 else if(bPrivateAccess==0&&pMember->dwAccess==ACCESS_PROTECTED){
204 if(isErrorEnabled) SetError(108,VarName,cp);
205 return false;
206 }
207 }
208
209 //Const定義の場合は書き込みアクセスを制限する
210 //※コンストラクタをコンパイル中の場合は例外的に許可する
211 if( pMember->IsConst() && //定数メンバである
212 isWriteAccess && //書き込みアクセスを要求されている
213 objClass.IsCompilingConstructor() == false //コンストラクタ コンパイル中を除く
214 ){
215 //Const定義の変数に書き込みアクセスをしようとした場合
216 SetError(61,VarName,cp);
217 }
218
219 resultType = *pMember;
220
221 //ポインタ変数の場合
222 if( resultType.IsPointer() ){
223 if(pMember->SubScripts[0]==-1){
224 lstrcpy(lpPtrOffset,array);
225 array[0]=0;
226 }
227 }
228 else{
229 if(lpPtrOffset[0]){
230 if(isErrorEnabled) SetError(16,member,cp);
231 return false;
232 }
233 }
234
235 if(offset){
236 //add ecx,offset
237 OpBuffer[obp++]=(char)0x81;
238 OpBuffer[obp++]=(char)0xC1;
239 *((long *)(OpBuffer+obp))=offset;
240 obp+=sizeof(long);
241 }
242
243 if(array[0]){
244 //配列オフセット
245 if(!GetArrayOffset(pMember->SubScripts,array,*pMember)){
246 if(isErrorEnabled) SetError(14,member,cp);
247 }
248 }
249 else if(pMember->SubScripts[0]!=-1){
250 resultType.SetBasicType( resultType.GetBasicType() | FLAG_PTR );
251 }
252
253 if(NestMember[0]){
254 //入れ子構造の場合
255
256 if( resultType.IsObject() || resultType.IsStruct() ){
257 if( refType != CClass::Dot ){
258 if(isErrorEnabled) SetError(104,member,cp);
259 return false;
260 }
261
262 if( resultType.IsObject() ){
263 // 参照内容へのポインタを抽出
264 SetRelativeOffset( *pRelativeVar );
265 }
266 }
267 else if( resultType.IsObjectPtr() || resultType.IsStructPtr() ){
268 //構造体ポインタ型メンバ変数
269
270 if(lpPtrOffset[0]){
271 //pObj[n].member
272 if( ( resultType.IsObjectPtr() || resultType.IsStructPtr() )
273 && refType != CClass::Dot ){
274 if(isErrorEnabled) SetError(104,member,cp);
275 return false;
276 }
277
278 //直接参照に切り替え
279 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
280 pRelativeVar->dwKind=VAR_DIRECTMEM;
281
282 lpPtrOffset[0]=0;
283 }
284 else{
285 //pObj->member
286 if( ( resultType.IsObjectPtr() || resultType.IsStructPtr() )
287 && refType != CClass::Pointer ){
288 if(isErrorEnabled) SetError(104,member,cp);
289 return false;
290 }
291
292 SetRelativeOffset( *pRelativeVar );
293 }
294 }
295 else if( resultType.GetBasicType() == MAKE_PTR_TYPE(DEF_OBJECT,2)
296 || resultType.GetBasicType() == MAKE_PTR_TYPE(DEF_STRUCT,2)){
297 //構造体ポインタのポインタ型メンバ変数
298
299 if(lpPtrOffset[0]){
300 //ppObj[n]->member
301 if( refType != CClass::Pointer ){
302 if(isErrorEnabled) SetError(104,member,cp);
303 return false;
304 }
305
306 //直接参照に切り替え
307 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
308 pRelativeVar->dwKind=VAR_DIRECTMEM;
309
310 lpPtrOffset[0]=0;
311
312 //mov ecx,dword ptr[ecx]
313 OpBuffer[obp++]=(char)0x8B;
314 OpBuffer[obp++]=(char)0x09;
315 }
316 else{
317 if(isErrorEnabled) SetError(104,member,cp);
318 return false;
319 }
320 }
321
322 if(!_member_offset(
323 isErrorEnabled,
324 isWriteAccess,
325 pMember->GetClass(),
326 NestMember,
327 pRelativeVar,
328 resultType,
329 0)) return false;
330 }
331
332 if(lpPtrOffset[0]){
333 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
334 pRelativeVar->dwKind=VAR_DIRECTMEM;
335 }
336
337 return true;
338}
339
340int LocalVar_ThisPtrOffset;
341void SetThisPtrToReg(int reg){
342 //自身のオブジェクトのThisポインタをregにコピー
343
344 RELATIVE_VAR RelativeVar;
345 RelativeVar.dwKind=VAR_LOCAL;
346 RelativeVar.bOffsetOffset=0;
347 RelativeVar.offset=-LocalVar_ThisPtrOffset;
348
349 SetReg_WholeVariable(DEF_PTR_VOID,&RelativeVar,reg);
350}
351
352bool GetVarOffset(bool isErrorEnabled,bool isWriteAccess,const char *NameBuffer,RELATIVE_VAR *pRelativeVar,Type &resultType,int *pss){
353 int i;
354 char variable[VN_SIZE],member[VN_SIZE],VarName[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE];
355
356 if(NameBuffer[0]=='.'){
357 GetWithName(variable);
358 lstrcat(variable,NameBuffer);
359 }
360 else lstrcpy(variable,NameBuffer);
361
362 CClass::RefType refType;
363 if( FormatUseProcReturnObject( variable, VarName, array, refType, member ) ){
364 // 戻り値オブジェクトのメンバを直接参照しているとき
365 //例: func().member
366
367 void *pInfo;
368 int idProc=GetProc(VarName,(void **)&pInfo);
369
370 if(idProc){
371 pRelativeVar->dwKind=VAR_DIRECTMEM;
372
373 Type type;
374
375
376 ////////////////
377 // 呼び出し
378 ////////////////
379
380 CallProc(idProc,pInfo,VarName,array,type);
381
382 //戻り値をecxにコピー
383 op_mov_RR( REG_ECX, REG_EAX );
384
385
386 if(!_member_offset(
387 isErrorEnabled,
388 isWriteAccess,
389 type.GetClass(),
390 member,pRelativeVar,resultType,0)) return false;
391
392 return true;
393 }
394 }
395
396
397 lstrcpy(VarName,variable);
398 GetVarFormatString(VarName,array,lpPtrOffset,member,refType);
399
400 const int *pSubScripts;
401 bool bConst = false;
402
403
404 if( UserProc::IsLocalAreaCompiling() ){
405 //////////////////
406 // ローカル変数
407 //////////////////
408
409 const Variable *pVar = UserProc::CompilingUserProc().localVars.BackSearch( VarName );
410 if( pVar ){
411 //ポインタ変数の場合
412 if( pVar->IsPointer() ){
413 if( !pVar->IsArray() ){
414 lstrcpy(lpPtrOffset,array);
415 array[0]=0;
416 }
417 }
418 else{
419 if(lpPtrOffset[0]){
420 SetError(16,variable,cp);
421 pRelativeVar->dwKind=NON_VAR;
422 return false;
423 }
424 }
425
426 pRelativeVar->offset=-pVar->offset;
427 pRelativeVar->bOffsetOffset=0;
428 if( pVar->IsRef() ){
429 // 参照型
430 pRelativeVar->dwKind = VAR_REFLOCAL;
431 }
432 else pRelativeVar->dwKind=VAR_LOCAL;
433 resultType = *pVar;
434 pSubScripts=pVar->GetSubScriptsPtr();
435 bConst = pVar->IsConst();
436
437 goto ok;
438 }
439 }
440
441 if(pobj_CompilingClass){
442 //////////////////////
443 // クラスメンバの参照
444 //////////////////////
445
446 if(lstrcmpi(variable,"This")==0){
447 //Thisオブジェクト
448
449 //Thisポインタをecxにコピー
450 SetThisPtrToReg(REG_ECX);
451
452 pRelativeVar->dwKind=VAR_DIRECTMEM;
453
454 resultType.SetType( DEF_OBJECT, pobj_CompilingClass );
455 return true;
456 }
457
458 if(memicmp(variable,"This.",5)==0){
459 //Thisオブジェクトのメンバを参照するとき
460 SlideString(variable+5,-5);
461 lstrcpy(VarName,variable);
462 }
463 else{
464 //クラス内メンバを参照するとき(通常)
465
466 for(i=0;i<pobj_CompilingClass->iMemberNum;i++){
467 if(lstrcmp(VarName,pobj_CompilingClass->ppobj_Member[i]->name)==0) break;
468 }
469 if(i==pobj_CompilingClass->iMemberNum) goto NonClassMember;
470 }
471
472 //Const修飾子のメソッド内でメンバ書き込みアクセスが発生したとき
473 //(コンストラクタ、デストラクタ内を除く)
474 CMethod *pMethod = pobj_DBClass->GetNowCompilingMethodInfo();
475 if( isWriteAccess &&
476 pMethod->isConst &&
477 pobj_CompilingClass->IsCompilingConstructor() == false &&
478 pobj_CompilingClass->IsCompilingDestructor() == false
479 ){
480 SetError(131, NULL, cp );
481 }
482
483 /////////////////////////////
484 // thisポインタをecxにセット
485
486 //Thisポインタをecxにコピー
487 SetThisPtrToReg(REG_ECX);
488
489 pRelativeVar->dwKind=VAR_DIRECTMEM;
490 if(!_member_offset(
491 isErrorEnabled,
492 isWriteAccess,
493 *pobj_CompilingClass,
494 variable,
495 pRelativeVar,
496 resultType,1)) return false;
497 return true;
498 }
499
500NonClassMember:
501
502 {
503 const Variable *pVar;
504
505 //////////////////////////
506 // 静的ローカル変数
507 // ※"Static.Object.Method.Variable"
508 //////////////////////////
509
510 char temporary[VN_SIZE];
511 if( UserProc::IsLocalAreaCompiling() ){
512 GetNowStaticVarFullName(VarName,temporary);
513
514 pVar = globalVars.Find( temporary );
515 if( pVar ){
516 goto GlobalOk;
517 }
518 }
519
520
521 //////////////////////////
522 // クラスの静的メンバ
523 //////////////////////////
524
525 if(member[0]){
526 lstrcpy(temporary,member);
527
528 char tempMember[VN_SIZE];
529 char tempArray[VN_SIZE];
530 {
531 CClass::RefType refType;
532 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember, refType );
533 }
534
535 char temp2[VN_SIZE];
536 sprintf(temp2,"%s.%s",VarName,temporary);
537 pVar = globalVars.Find( temp2 );
538 if( pVar ){
539 lstrcpy(member,tempMember);
540 lstrcpy(array,tempArray);
541 goto GlobalOk;
542 }
543 }
544
545 if(pobj_CompilingClass){
546 //自身のクラスから静的メンバを参照する場合
547 char temp2[VN_SIZE];
548 sprintf(temp2,"%s.%s",pobj_CompilingClass->name,VarName);
549 pVar = globalVars.Find( temp2 );
550 if( pVar ){
551 goto GlobalOk;
552 }
553 }
554
555 /////////////////////
556 // グローバル変数
557 /////////////////////
558
559 pVar = globalVars.BackSearch( VarName );
560 if( pVar ){
561 goto GlobalOk;
562 }
563
564 if(isErrorEnabled) SetError(3,variable,cp);
565 pRelativeVar->dwKind=NON_VAR;
566 return false;
567
568
569
570GlobalOk:
571 //ポインタ変数の場合
572 if( pVar->IsPointer() ){
573 if( !pVar->IsArray() ){
574 lstrcpy(lpPtrOffset,array);
575 array[0]=0;
576 }
577 }
578 else{
579 if(lpPtrOffset[0]){
580 SetError(16,variable,cp);
581 pRelativeVar->dwKind=NON_VAR;
582 return false;
583 }
584 }
585
586 pRelativeVar->offset=pVar->offset;
587 pRelativeVar->bOffsetOffset=0;
588 if( pVar->IsRef() ){
589 // 参照型
590 pRelativeVar->dwKind = VAR_REFGLOBAL;
591 }
592 else pRelativeVar->dwKind=VAR_GLOBAL;
593 resultType = *pVar;
594 pSubScripts=pVar->GetSubScriptsPtr();
595 bConst = pVar->IsConst();
596 }
597
598
599
600ok:
601
602 if( bConst && isWriteAccess ){
603 //Const定義の変数に書き込みアクセスをしようとした場合
604 if( resultType.IsObject() ){
605 //オブジェクト定数
606 SetError(130, VarName, cp );
607 }
608 else{
609 //一般のConst変数
610 SetError(61,VarName,cp);
611 }
612 }
613
614 if(array[0]==0&&pSubScripts[0]!=-1){
615 //配列の先頭ポインタを示す場合
616 resultType.SetBasicType( resultType.GetBasicType() | FLAG_PTR );
617 if(pss) memcpy(pss,pSubScripts,MAX_ARRAYDIM);
618 return true;
619 }
620
621 if(array[0]||member[0]){
622 //xor ecx,ecx(ecxを0に初期化する)
623 //※ecxは変数ベースアドレスからの相対オフセットを示す
624 op_zero_reg(REG_ECX);
625
626 pRelativeVar->bOffsetOffset=1;
627 }
628 if(array[0]){
629 if(!GetArrayOffset(pSubScripts,array,resultType)){
630 SetError(14,variable,cp);
631 pRelativeVar->dwKind=NON_VAR;
632 return false;
633 }
634 }
635 if(member[0]){
636 if( resultType.IsObject() || resultType.IsStruct() ){
637 //実態オブジェクトのメンバを参照(obj.member)
638 if( refType != CClass::Dot ){
639 SetError(104,VarName,cp);
640 pRelativeVar->dwKind=NON_VAR;
641 return false;
642 }
643
644 if( resultType.IsObject() ){
645 // 参照内容へのポインタを抽出
646 SetRelativeOffset( *pRelativeVar );
647 }
648 }
649 else if( resultType.IsObjectPtr() || resultType.IsStructPtr() ){
650 //ポインタオブジェクトが示すメンバを参照
651 if(lpPtrOffset[0]){
652 //pObj[n].member
653 if( refType != CClass::Dot ){
654 SetError(104,VarName,cp);
655 pRelativeVar->dwKind=NON_VAR;
656 return false;
657 }
658 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
659 pRelativeVar->dwKind=VAR_DIRECTMEM;
660 }
661 else{
662 //pObj->member
663 if( refType != CClass::Pointer ){
664 SetError(104,VarName,cp);
665 pRelativeVar->dwKind=NON_VAR;
666 return false;
667 }
668
669 SetVarPtrToEax(pRelativeVar);
670 pRelativeVar->dwKind=VAR_DIRECTMEM;
671
672 //mov ecx,dword ptr[eax]
673 OpBuffer[obp++]=(char)0x8B;
674 OpBuffer[obp++]=(char)0x08;
675 }
676 }
677 else if( resultType.GetBasicType()==MAKE_PTR_TYPE(DEF_OBJECT,2) || resultType.GetBasicType()==MAKE_PTR_TYPE(DEF_STRUCT,2)){
678 //ポインタオブジェクトが示すメンバを参照
679 if(lpPtrOffset[0]){
680 //ppObj[n]->member
681 if( refType != CClass::Pointer ){
682 SetError(104,VarName,cp);
683 pRelativeVar->dwKind=NON_VAR;
684 return false;
685 }
686
687 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
688 pRelativeVar->dwKind=VAR_DIRECTMEM;
689
690
691 SetVarPtrToEax(pRelativeVar);
692
693 //mov ecx,dword ptr[eax]
694 OpBuffer[obp++]=(char)0x8B;
695 OpBuffer[obp++]=(char)0x08;
696 }
697 else{
698 SetError(104,VarName,cp);
699 pRelativeVar->dwKind=NON_VAR;
700 return false;
701 }
702 }
703 else{
704 SetError(102,VarName,cp);
705 pRelativeVar->dwKind=NON_VAR;
706 return false;
707 }
708
709 if(!_member_offset(
710 isErrorEnabled,
711 isWriteAccess,
712 resultType.GetClass(),
713 member,pRelativeVar,resultType,0)) return false;
714
715 return true;
716 }
717
718 if(lpPtrOffset[0]){
719 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
720 pRelativeVar->dwKind=VAR_DIRECTMEM;
721 }
722
723 return true;
724}
725
726bool SetInitGlobalData(int offset,const Type &type,const int *SubScripts,char *InitBuf){
727 extern BYTE *initGlobalBuf;
728 int i,i2,i3;
729 char temporary[VN_SIZE];
730
731 if(InitBuf[0]=='['){
732 SlideString(InitBuf+1,-1);
733 InitBuf[lstrlen(InitBuf)-1]=0;
734
735 int typeSize = type.GetSize();
736
737 if(SubScripts[0]!=-1){
738 typeSize*=JumpSubScripts(SubScripts+1);
739 i=0;
740 i2=0;
741 while(1){
742 if(SubScripts[0]<i2){
743 SetError(41,0,cp);
744 return 0;
745 }
746 i=GetOneParameter(InitBuf,i,temporary);
747 if(!SetInitGlobalData(
748 offset+i2*typeSize,
749 type,
750 SubScripts+1,
751 temporary)) return false;
752 i2++;
753 if(InitBuf[i]=='\0') break;
754 }
755 return true;
756 }
757
758 if(type.IsStruct()){
759 const CClass &objClass = type.GetClass();
760
761 for(i=0,i2=0;i2<objClass.iMemberNum;i2++){
762 i=GetOneParameter(InitBuf,i,temporary);
763
764 i3=objClass.GetMemberOffset( objClass.ppobj_Member[i2]->name, NULL );
765
766 if(!SetInitGlobalData(offset+i3,
767 *objClass.ppobj_Member[i2],
768 objClass.ppobj_Member[i2]->SubScripts,
769 temporary)) return false;
770
771 if(InitBuf[i]=='\0') break;
772 }
773 if(i2+1!=objClass.iMemberNum){
774 SetError(41,0,cp);
775 return false;
776 }
777 return true;
778 }
779
780 SetError(41,0,cp);
781 return false;
782 }
783
784
785 ///////////////////////////////////////
786 // 単発式([]で囲まれていない)
787 ///////////////////////////////////////
788
789 if( type.IsObject() || type.IsStruct() ){
790 //オブジェクトまたは構造体の場合はありえない
791 SetError(300,NULL,cp);
792 return false;
793 }
794
795 if(SubScripts[0]!=-1){
796 SetError(41,0,cp);
797 return false;
798 }
799
800 double dbl;
801 _int64 i64data;
802 Type calcType;
803
804 if( !StaticCalculation(false, InitBuf,type.GetBasicType(),&i64data,calcType) ){
805 //動的データだった場合
806 return false;
807 }
808 if( calcType.IsReal() ){
809 memcpy(&dbl,&i64data,sizeof(double));
810 i64data=(_int64)dbl;
811 }
812 else dbl=(double)i64data;
813
814 //型チェック
815 CheckDifferentType(
816 type,
817 calcType,
818 0,0);
819
820 if( type.IsDouble() ){
821 *(double *)(initGlobalBuf+offset)=(double)dbl;
822 }
823 else if( type.IsSingle() ){
824 *(float *)(initGlobalBuf+offset)=(float)dbl;
825 }
826 else if( type.Is64() ){
827 *(_int64 *)(initGlobalBuf+offset)=i64data;
828 }
829 else if( type.IsLong() || type.IsDWord() || type.IsPointer() ){
830 if(type.GetBasicType()==typeOfPtrChar){
831 //文字列定数のとき
832
833 char *temp;
834 temp=(char *)i64data;
835 i2=dataTable.AddString(temp,lstrlen(temp));
836 HeapDefaultFree(temp);
837
838 //mov eax,DataPos
839 OpBuffer[obp++]=(char)0xB8;
840 *((long *)(OpBuffer+obp))=(long)i2;
841 pobj_DataTableSchedule->add();
842 obp+=sizeof(long);
843
844 //mov dword ptr[offset],eax
845 OpBuffer[obp++]=(char)0xA3;
846 *((long *)(OpBuffer+obp))=offset;
847 pobj_GlobalVarSchedule->add();
848 obp+=sizeof(long);
849 }
850 else{
851 *(DWORD *)(initGlobalBuf+offset)=(DWORD)i64data;
852 }
853 }
854 else if( type.IsWord() || type.IsInteger() ){
855 *(WORD *)(initGlobalBuf+offset)=(WORD)i64data;
856 }
857 else if( type.IsSByte() || type.IsByte() || type.IsBoolean() ){
858 *(BYTE *)(initGlobalBuf+offset)=(BYTE)i64data;
859 }
860
861 return true;
862}
863bool InitLocalVar(int offset,const Type &type,const int *SubScripts,char *InitBuf){
864 int i,i2,i3;
865 char temporary[VN_SIZE];
866
867 if(InitBuf[0]=='['){
868 SlideString(InitBuf+1,-1);
869 InitBuf[lstrlen(InitBuf)-1]=0;
870
871 int typeSize = type.GetSize();
872
873 if(SubScripts[0]!=-1){
874 typeSize*=JumpSubScripts(SubScripts+1);
875 i=0;
876 i2=0;
877 while(1){
878 if(SubScripts[0]<i2){
879 SetError(41,0,cp);
880 return false;
881 }
882 i=GetOneParameter(InitBuf,i,temporary);
883 if(!InitLocalVar(
884 offset+i2*typeSize,
885 type,
886 SubScripts+1,
887 temporary)) return false;
888 i2++;
889 if(InitBuf[i]=='\0') break;
890 }
891 return true;
892 }
893
894 if(type.IsStruct()){
895 const CClass &objClass = type.GetClass();
896
897 for(i=0,i2=0;i2<objClass.iMemberNum;i2++){
898 i=GetOneParameter(InitBuf,i,temporary);
899
900 i3=objClass.GetMemberOffset( objClass.ppobj_Member[i2]->name, NULL );
901
902 if(!InitLocalVar(offset+i3,
903 *objClass.ppobj_Member[i2],
904 objClass.ppobj_Member[i2]->SubScripts,
905 temporary)) return false;
906
907 if(InitBuf[i]=='\0') break;
908 }
909 if(i2+1!=objClass.iMemberNum){
910 SetError(41,0,cp);
911 return 0;
912 }
913 return true;
914 }
915
916 SetError(41,0,cp);
917 return false;
918 }
919
920
921 ///////////////////////////////////////
922 // 単発式([]で囲まれていない)
923 ///////////////////////////////////////
924
925 if(SubScripts[0]!=-1){
926 SetError(41,0,cp);
927 return false;
928 }
929
930 double dbl;
931 _int64 i64data;
932 Type calcType;
933
934 if( !StaticCalculation(false, InitBuf,type.GetBasicType(),&i64data,calcType) ){
935 //動的データだった場合
936 return false;
937 }
938 if( calcType.IsReal() ){
939 memcpy(&dbl,&i64data,sizeof(double));
940 i64data=(_int64)dbl;
941 }
942 else dbl=(double)i64data;
943
944 //型チェック
945 CheckDifferentType(
946 type,
947 calcType,
948 0,0);
949
950 if( type.IsDouble() ){
951 //mov eax,HILONG(dbl)
952 OpBuffer[obp++]=(char)0xB8;
953 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&dbl))+4);
954 obp+=sizeof(long);
955
956 //mov dword ptr[ebp+offset+sizeof(long)],eax
957 OpBuffer[obp++]=(char)0x89;
958 OpBuffer[obp++]=(char)0x85;
959 *((long *)(OpBuffer+obp))=offset+sizeof(long);
960 AddLocalVarAddrSchedule();
961 obp+=sizeof(long);
962
963 //mov eax,LOLONG(dbl)
964 OpBuffer[obp++]=(char)0xB8;
965 *((long *)(OpBuffer+obp))=*(long *)(&dbl);
966 obp+=sizeof(long);
967
968 //mov dword ptr[ebp+offset],eax
969 OpBuffer[obp++]=(char)0x89;
970 OpBuffer[obp++]=(char)0x85;
971 *((long *)(OpBuffer+obp))=offset;
972 AddLocalVarAddrSchedule();
973 obp+=sizeof(long);
974 }
975 else if( type.IsSingle() ){
976 float flt;
977 flt=(float)dbl;
978 //mov eax,InitValue
979 OpBuffer[obp++]=(char)0xB8;
980 *((long *)(OpBuffer+obp))=*(long *)&flt;
981 obp+=sizeof(long);
982
983 //mov dword ptr[ebp+offset],eax
984 OpBuffer[obp++]=(char)0x89;
985 OpBuffer[obp++]=(char)0x85;
986 *((long *)(OpBuffer+obp))=offset;
987 AddLocalVarAddrSchedule();
988 obp+=sizeof(long);
989 }
990 else if( type.Is64() ){
991 //mov eax,HILONG(i64data)
992 OpBuffer[obp++]=(char)0xB8;
993 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&i64data))+4);
994 obp+=sizeof(long);
995
996 //mov dword ptr[ebp+offset+sizeof(long)],eax
997 OpBuffer[obp++]=(char)0x89;
998 OpBuffer[obp++]=(char)0x85;
999 *((long *)(OpBuffer+obp))=offset+sizeof(long);
1000 AddLocalVarAddrSchedule();
1001 obp+=sizeof(long);
1002
1003 //mov eax,LOLONG(i64data)
1004 OpBuffer[obp++]=(char)0xB8;
1005 *((long *)(OpBuffer+obp))=*(long *)(&i64data);
1006 obp+=sizeof(long);
1007
1008 //mov dword ptr[ebp+offset],eax
1009 OpBuffer[obp++]=(char)0x89;
1010 OpBuffer[obp++]=(char)0x85;
1011 *((long *)(OpBuffer+obp))=offset;
1012 AddLocalVarAddrSchedule();
1013 obp+=sizeof(long);
1014 }
1015 else if( type.IsDWord() || type.IsLong() || type.IsPointer() ){
1016 if(type.GetBasicType()==typeOfPtrChar){
1017 //文字列定数のとき
1018
1019 char *temp;
1020 temp=(char *)i64data;
1021 i2=dataTable.AddString(temp,lstrlen(temp));
1022 HeapDefaultFree(temp);
1023
1024 //mov eax,DataPos
1025 OpBuffer[obp++]=(char)0xB8;
1026 *((long *)(OpBuffer+obp))=(long)i2;
1027 pobj_DataTableSchedule->add();
1028 obp+=sizeof(long);
1029 }
1030 else{
1031 //mov eax,InitValue
1032 OpBuffer[obp++]=(char)0xB8;
1033 *((long *)(OpBuffer+obp))=(long)i64data;
1034 obp+=sizeof(long);
1035 }
1036
1037 //mov dword ptr[ebp+offset],eax
1038 OpBuffer[obp++]=(char)0x89;
1039 OpBuffer[obp++]=(char)0x85;
1040 *((long *)(OpBuffer+obp))=offset;
1041 AddLocalVarAddrSchedule();
1042 obp+=sizeof(long);
1043 }
1044 else if( type.IsWord() || type.IsInteger() ){
1045 //mov ax,InitValue
1046 OpBuffer[obp++]=(char)0x66;
1047 OpBuffer[obp++]=(char)0xB8;
1048 *((WORD *)(OpBuffer+obp))=(WORD)i64data;
1049 obp+=sizeof(WORD);
1050
1051 //mov word ptr[ebp+offset],ax
1052 OpBuffer[obp++]=(char)0x66;
1053 OpBuffer[obp++]=(char)0x89;
1054 OpBuffer[obp++]=(char)0x85;
1055 *((long *)(OpBuffer+obp))=offset;
1056 AddLocalVarAddrSchedule();
1057 obp+=sizeof(long);
1058 }
1059 else if( type.IsSByte() || type.IsByte() || type.IsBoolean() ){
1060 //mov byte ptr[ebp+offset],InitValue
1061 OpBuffer[obp++]=(char)0xC6;
1062 OpBuffer[obp++]=(char)0x85;
1063 *((long *)(OpBuffer+obp))=offset;
1064 AddLocalVarAddrSchedule();
1065 obp+=sizeof(long);
1066 *((BYTE *)(OpBuffer+obp))=(BYTE)i64data;
1067 obp+=sizeof(BYTE);
1068 }
1069
1070 return true;
1071}
1072
1073void dim( char *VarName,int *SubScripts,Type &type,char *InitBuf,char *ConstractParameter,DWORD dwFlags){
1074 if( UserProc::IsGlobalAreaCompiling() ){
1075 /////////////////////////
1076 // グローバル変数
1077 /////////////////////////
1078
1079 AddGlobalVariable(VarName,SubScripts,type,InitBuf,ConstractParameter,dwFlags);
1080 }
1081 else{
1082 /////////////////
1083 // ローカル変数
1084 /////////////////
1085
1086 if( UserProc::CompilingUserProc().localVars.DuplicateCheck( VarName ) ){
1087 //2重定義のエラー
1088 SetError(15,VarName,cp);
1089 return;
1090 }
1091
1092 bool isConst = ( dwFlags & DIMFLAG_CONST ) ? true:false;
1093
1094 Variable *pVar = new Variable( VarName, type, isConst );
1095
1096 if( SubScripts[0] != -1 ){
1097 //配列あり
1098 pVar->SetArray( SubScripts );
1099 }
1100
1101 //コンストラクタ用パラメータ
1102 pVar->paramStrForConstructor = ConstractParameter;
1103
1104 //レキシカルスコープ
1105 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
1106 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
1107 pVar->bLiving=TRUE;
1108
1109 //エラー用
1110 pVar->source_code_address=cp;
1111
1112 // 変数を追加
1113 UserProc::CompilingUserProc().localVars.push_back( pVar );
1114
1115 //アラインメントを考慮
1116 if( pVar->IsStruct() ){
1117 int alignment = pVar->GetClass().iAlign;
1118 if( alignment ){
1119 if( AllLocalVarSize % alignment ){
1120 AllLocalVarSize += alignment - (AllLocalVarSize % alignment);
1121 }
1122 }
1123 }
1124
1125 AllLocalVarSize += pVar->GetMemorySize();
1126 pVar->offset = AllLocalVarSize;
1127
1128 //レキシカルスコープ
1129 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
1130 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
1131 pVar->bLiving=TRUE;
1132
1133 if(InitBuf[0]){
1134 //初期代入時のみ、書き込みアクセスを許可する
1135 if( isConst ){
1136 pVar->ConstOff();
1137 }
1138
1139 int result = 0;
1140 if( !pVar->IsObject() ){
1141 result = InitLocalVar(-pVar->offset,
1142 *pVar,
1143 pVar->GetSubScriptsPtr(),
1144 InitBuf);
1145 }
1146
1147 if(!result){
1148 //動的な式だった場合は代入演算を行う
1149 char temporary[8192];
1150 sprintf(temporary,"%s=%s",VarName,InitBuf);
1151 OpcodeCalc(temporary);
1152 }
1153
1154 if( isConst ){
1155 pVar->ConstOn();
1156 }
1157 }
1158 else{
1159 //push 0
1160 op_push_V(0);
1161
1162 //push VarSize
1163 op_push_V( pVar->GetMemorySize() );
1164
1165 //mov eax,ebp
1166 OpBuffer[obp++]=(char)0x8B;
1167 OpBuffer[obp++]=(char)0xC5;
1168
1169 //add eax,offset
1170 OpBuffer[obp++]=(char)0x05;
1171 *((long *)(OpBuffer+obp))=-pVar->offset;
1172 AddLocalVarAddrSchedule();
1173 obp+=sizeof(long);
1174
1175 //push eax
1176 op_push(REG_EAX);
1177
1178 //call FillMemory
1179 OpBuffer[obp++]=(char)0xFF;
1180 OpBuffer[obp++]=(char)0x15;
1181 DllProc *pDllProc = GetDeclareHash("FillMemory");
1182 pDllProc->Using();
1183 pobj_ImportAddrSchedule->add(pDllProc);
1184 obp+=sizeof(long);
1185 }
1186 }
1187
1188 //New呼び出し
1189 if( type.IsObject() &&(dwFlags&DIMFLAG_NONCALL_CONSTRACTOR)==0&&InitBuf[0]=='\0'){
1190 char objectSize[255];
1191 if( SubScripts[0] == -1 ){
1192 objectSize[0] = 0;
1193 }
1194 else{
1195 if( SubScripts[1] != -1 ){
1196 SetError(300,NULL,cp);
1197 }
1198 sprintf( objectSize, "%d", SubScripts[0] );
1199 }
1200 Operator_New( type.GetClass(), objectSize, ConstractParameter, type );
1201
1202 //pop eax
1203 op_pop( REG_EAX );
1204
1205 RELATIVE_VAR RelativeVar;
1206 GetVarOffset( true, false, VarName, &RelativeVar, Type() );
1207 if( RelativeVar.dwKind == VAR_DIRECTMEM ){
1208 SetError();
1209 }
1210 SetVariableFromEax( DEF_OBJECT, DEF_OBJECT, &RelativeVar );
1211 }
1212}
1213
1214void SetVarPtrToEax(RELATIVE_VAR *pRelativeVar){
1215 if(pRelativeVar->dwKind==VAR_GLOBAL){
1216 if(pRelativeVar->bOffsetOffset){
1217 //lea eax,dword ptr[ecx+offset]
1218 OpBuffer[obp++]=(char)0x8D;
1219 OpBuffer[obp++]=(char)0x81;
1220 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1221 pobj_GlobalVarSchedule->add();
1222 obp+=sizeof(long);
1223 }
1224 else{
1225 //mov eax,offset
1226 OpBuffer[obp++]=(char)0xB8;
1227 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1228 pobj_GlobalVarSchedule->add();
1229 obp+=sizeof(long);
1230 }
1231 }
1232 else if(pRelativeVar->dwKind==VAR_REFGLOBAL){
1233 if(pRelativeVar->bOffsetOffset){
1234 //mov eax,ecx
1235 OpBuffer[obp++]=(char)0x8B;
1236 OpBuffer[obp++]=(char)0xC1;
1237
1238 //add eax,dword ptr[offset]
1239 op_add_RM( sizeof(long), REG_EAX, REG_NON, (int)pRelativeVar->offset, MOD_DISP32 );
1240 }
1241 else{
1242 //mov eax,dword ptr[offset]
1243 op_mov_RM( sizeof(long), REG_EAX, REG_NON, (int)pRelativeVar->offset, MOD_DISP32 );
1244 }
1245 obp-=sizeof(long);
1246 pobj_GlobalVarSchedule->add();
1247 obp+=sizeof(long);
1248 }
1249 else if(pRelativeVar->dwKind==VAR_LOCAL){
1250 if(pRelativeVar->bOffsetOffset){
1251 //add ecx,offset
1252 OpBuffer[obp++]=(char)0x81;
1253 OpBuffer[obp++]=(char)0xC1;
1254 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1255 AddLocalVarAddrSchedule();
1256 obp+=sizeof(long);
1257
1258 //lea eax,dword ptr[ebp+ecx]
1259 OpBuffer[obp++]=(char)0x8D;
1260 OpBuffer[obp++]=(char)0x44;
1261 OpBuffer[obp++]=(char)0x0D;
1262 OpBuffer[obp++]=(char)0x00;
1263 }
1264 else{
1265 //lea eax,dword ptr[ebp+offset]
1266 OpBuffer[obp++]=(char)0x8D;
1267 OpBuffer[obp++]=(char)0x85;
1268 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1269 AddLocalVarAddrSchedule();
1270 obp+=sizeof(long);
1271 }
1272 }
1273 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
1274 if(pRelativeVar->bOffsetOffset){
1275 //mov eax,ecx
1276 OpBuffer[obp++]=(char)0x8B;
1277 OpBuffer[obp++]=(char)0xC1;
1278
1279 //add eax,dword ptr[ebp+offset]
1280 OpBuffer[obp++]=(char)0x03;
1281 OpBuffer[obp++]=(char)0x85;
1282 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1283 AddLocalVarAddrSchedule();
1284 obp+=sizeof(long);
1285 }
1286 else{
1287 //mov eax,dword ptr[ebp+offset]
1288 OpBuffer[obp++]=(char)0x8B;
1289 OpBuffer[obp++]=(char)0x85;
1290 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1291 AddLocalVarAddrSchedule();
1292 obp+=sizeof(long);
1293 }
1294 }
1295 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
1296 //mov eax,ecx
1297 OpBuffer[obp++]=(char)0x8B;
1298 OpBuffer[obp++]=(char)0xC1;
1299 }
1300}
1301void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar){
1302 if( reg != REG_EAX ){
1303 SetError();
1304 //TODO: 未完成
1305 }
1306 SetVarPtrToEax( pRelativeVar );
1307}
1308
1309bool Compile_AddGlobalRootsForGc(){
1310 UserProc *pUserProc_AddGlobalRootPtr = GetClassMethod( "_System_CGarbageCollection", "AddGlobalRootPtr" );
1311 if( !pUserProc_AddGlobalRootPtr ){
1312 SetError(3, "_System_CGarbageCollection.AddGlobalRootPtr", -1 );
1313 return false;
1314 }
1315
1316 BOOST_FOREACH( const Variable *pVar, globalVars ){
1317 if( pVar->IsObject() || pVar->IsPointer() || pVar->IsStruct() ){
1318 // オブジェクトまたはポインタだったとき
1319 // ※構造体も含む(暫定対応)
1320
1321 // 変数領域に要するLONG_PTR単位の個数を引き渡す
1322 op_push_V( pVar->GetMemorySize()/PTR_SIZE );
1323
1324
1325 /////////////////////////////
1326 // ルートポインタを引き渡す
1327
1328 //mov eax,offset
1329 op_mov_RV(REG_EAX,(int)pVar->offset);
1330 obp-=sizeof(long);
1331 pobj_GlobalVarSchedule->add();
1332 obp+=sizeof(long);
1333
1334 //push eax
1335 op_push( REG_EAX );
1336
1337 //
1338 /////////////////////////////
1339
1340
1341 /////////////////////////////
1342 // Thisポインタを引き渡す
1343
1344 SetThisPtrToReg(REG_EAX);
1345
1346 //push eax
1347 op_push( REG_EAX );
1348
1349 //
1350 /////////////////////////////
1351
1352
1353 // call AddGlobalRootPtr
1354 op_call( pUserProc_AddGlobalRootPtr );
1355
1356 ReallocNativeCodeBuffer();
1357 }
1358 }
1359
1360 return true;
1361}
Note: See TracBrowser for help on using the repository browser.