source: dev/BasicCompiler32/Compile_Var.cpp@ 138

Last change on this file since 138 was 138, checked in by dai_9181, 17 years ago

Member::initializeExpressionのリファクタリング

File size: 32.1 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->IsNoneAccess()){
193 if(isErrorEnabled) SetError(107,VarName,cp);
194 return false;
195 }
196 }
197 else{
198 if((bPrivateAccess==0&&pMember->IsPrivate())||
199 pMember->IsNoneAccess()){
200 if(isErrorEnabled) SetError(107,VarName,cp);
201 return false;
202 }
203 else if(bPrivateAccess==0&&pMember->IsProtected()){
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->GetType();
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->GetType())){
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->GetType().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];
355
356 if(NameBuffer[0]=='.'){
357 GetWithName(variable);
358 lstrcat(variable,NameBuffer);
359 }
360 else lstrcpy(variable,NameBuffer);
361
362 // 名前空間を分離
363 char namespaceStr[VN_SIZE]="", simpleName[VN_SIZE];
364 Smoothie::Meta::namespaceScopesCollection.SplitNamespace( variable, namespaceStr, simpleName );
365
366 // 先頭オブジェクトまたはクラス名と入れ子メンバに分割
367 CClass::RefType refType;
368 char member[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE];
369 GetVarFormatString(simpleName,array,lpPtrOffset,member,refType);
370
371 // 名前空間を分離していた場合は結合
372 char VarName[VN_SIZE];
373 if( namespaceStr[0] ){
374 sprintf( VarName, "%s.%s", namespaceStr, simpleName );
375 }
376 else{
377 lstrcpy( VarName, simpleName );
378 }
379
380 const int *pSubScripts;
381 bool bConst = false;
382
383
384 if( UserProc::IsLocalAreaCompiling() ){
385 //////////////////
386 // ローカル変数
387 //////////////////
388
389 const Variable *pVar = UserProc::CompilingUserProc().localVars.BackSearch( VarName );
390 if( pVar ){
391 //ポインタ変数の場合
392 if( pVar->IsPointer() ){
393 if( !pVar->IsArray() ){
394 lstrcpy(lpPtrOffset,array);
395 array[0]=0;
396 }
397 }
398 else{
399 if(lpPtrOffset[0]){
400 SetError(16,variable,cp);
401 pRelativeVar->dwKind=NON_VAR;
402 return false;
403 }
404 }
405
406 pRelativeVar->offset=-pVar->offset;
407 pRelativeVar->bOffsetOffset=0;
408 if( pVar->IsRef() ){
409 // 参照型
410 pRelativeVar->dwKind = VAR_REFLOCAL;
411 }
412 else pRelativeVar->dwKind=VAR_LOCAL;
413 resultType = *pVar;
414 pSubScripts=pVar->GetSubScriptsPtr();
415 bConst = pVar->IsConst();
416
417 goto ok;
418 }
419 }
420
421 if(pobj_CompilingClass){
422 //////////////////////
423 // クラスメンバの参照
424 //////////////////////
425
426 if(lstrcmpi(variable,"This")==0){
427 //Thisオブジェクト
428
429 //Thisポインタをecxにコピー
430 SetThisPtrToReg(REG_ECX);
431
432 pRelativeVar->dwKind=VAR_DIRECTMEM;
433
434 resultType.SetType( DEF_OBJECT, pobj_CompilingClass );
435 return true;
436 }
437
438 if(memicmp(variable,"This.",5)==0){
439 //Thisオブジェクトのメンバを参照するとき
440 SlideString(variable+5,-5);
441 lstrcpy(VarName,variable);
442 }
443 else{
444 //クラス内メンバを参照するとき(通常)
445
446 for(i=0;i<pobj_CompilingClass->iMemberNum;i++){
447 if( pobj_CompilingClass->ppobj_Member[i]->GetName() == VarName ){
448 break;
449 }
450 }
451 if(i==pobj_CompilingClass->iMemberNum) goto NonClassMember;
452 }
453
454 //Const修飾子のメソッド内でメンバ書き込みアクセスが発生したとき
455 //(コンストラクタ、デストラクタ内を除く)
456 const CMethod *pMethod = pobj_DBClass->GetNowCompilingMethodInfo();
457 if( isWriteAccess &&
458 pMethod->IsConst() &&
459 pobj_CompilingClass->IsCompilingConstructor() == false &&
460 pobj_CompilingClass->IsCompilingDestructor() == false
461 ){
462 SetError(131, NULL, cp );
463 }
464
465 /////////////////////////////
466 // thisポインタをecxにセット
467
468 //Thisポインタをecxにコピー
469 SetThisPtrToReg(REG_ECX);
470
471 pRelativeVar->dwKind=VAR_DIRECTMEM;
472 if(!_member_offset(
473 isErrorEnabled,
474 isWriteAccess,
475 *pobj_CompilingClass,
476 variable,
477 pRelativeVar,
478 resultType,1)) return false;
479 return true;
480 }
481
482NonClassMember:
483
484 {
485 const Variable *pVar;
486
487 //////////////////////////
488 // 静的ローカル変数
489 // ※"Static.Object.Method.Variable"
490 //////////////////////////
491
492 char temporary[VN_SIZE];
493 if( UserProc::IsLocalAreaCompiling() ){
494 GetNowStaticVarFullName(VarName,temporary);
495
496 pVar = globalVars.Find( temporary );
497 if( pVar ){
498 goto GlobalOk;
499 }
500 }
501
502
503 //////////////////////////
504 // クラスの静的メンバ
505 //////////////////////////
506
507 if(member[0]){
508 lstrcpy(temporary,member);
509
510 // TODO: 名前空間を考慮したコードになっていない
511
512 char tempMember[VN_SIZE];
513 char tempArray[VN_SIZE];
514 {
515 CClass::RefType refType;
516 GetVarFormatString(temporary,tempArray,lpPtrOffset,tempMember, refType );
517 }
518
519 int typeDefIndex = Smoothie::Meta::typeDefs.GetIndex( VarName );
520 if( typeDefIndex != -1 ){
521 // TypeDef後の型名だったとき
522 lstrcpy( VarName, Smoothie::Meta::typeDefs[typeDefIndex].GetBaseName().c_str() );
523 }
524
525 char temp2[VN_SIZE];
526 sprintf(temp2,"%s.%s",VarName,temporary);
527 pVar = globalVars.Find( temp2 );
528 if( pVar ){
529 lstrcpy(member,tempMember);
530 lstrcpy(array,tempArray);
531 goto GlobalOk;
532 }
533 }
534
535 if(pobj_CompilingClass){
536 //自身のクラスから静的メンバを参照する場合
537 char temp2[VN_SIZE];
538 sprintf(temp2,"%s.%s",pobj_CompilingClass->GetName().c_str(),VarName);
539 pVar = globalVars.Find( temp2 );
540 if( pVar ){
541 goto GlobalOk;
542 }
543 }
544
545 /////////////////////
546 // グローバル変数
547 /////////////////////
548
549 pVar = globalVars.BackSearch( VarName );
550 if( pVar ){
551 goto GlobalOk;
552 }
553
554 if(isErrorEnabled) SetError(3,variable,cp);
555 pRelativeVar->dwKind=NON_VAR;
556 return false;
557
558
559
560GlobalOk:
561 //ポインタ変数の場合
562 if( pVar->IsPointer() ){
563 if( !pVar->IsArray() ){
564 lstrcpy(lpPtrOffset,array);
565 array[0]=0;
566 }
567 }
568 else{
569 if(lpPtrOffset[0]){
570 SetError(16,variable,cp);
571 pRelativeVar->dwKind=NON_VAR;
572 return false;
573 }
574 }
575
576 pRelativeVar->offset=pVar->offset;
577 pRelativeVar->bOffsetOffset=0;
578 if( pVar->IsRef() ){
579 // 参照型
580 pRelativeVar->dwKind = VAR_REFGLOBAL;
581 }
582 else pRelativeVar->dwKind=VAR_GLOBAL;
583 resultType = *pVar;
584 pSubScripts=pVar->GetSubScriptsPtr();
585 bConst = pVar->IsConst();
586 }
587
588
589
590ok:
591
592 if( bConst && isWriteAccess ){
593 //Const定義の変数に書き込みアクセスをしようとした場合
594 if( resultType.IsObject() ){
595 //オブジェクト定数
596 SetError(130, VarName, cp );
597 }
598 else{
599 //一般のConst変数
600 SetError(61,VarName,cp);
601 }
602 }
603
604 if(array[0]==0&&pSubScripts[0]!=-1){
605 //配列の先頭ポインタを示す場合
606 resultType.SetBasicType( resultType.GetBasicType() | FLAG_PTR );
607 if(pss) memcpy(pss,pSubScripts,MAX_ARRAYDIM);
608 return true;
609 }
610
611 if(array[0]||member[0]){
612 //xor ecx,ecx(ecxを0に初期化する)
613 //※ecxは変数ベースアドレスからの相対オフセットを示す
614 op_zero_reg(REG_ECX);
615
616 pRelativeVar->bOffsetOffset=1;
617 }
618 if(array[0]){
619 if(!GetArrayOffset(pSubScripts,array,resultType)){
620 SetError(14,variable,cp);
621 pRelativeVar->dwKind=NON_VAR;
622 return false;
623 }
624 }
625 if(member[0]){
626 if( resultType.IsObject() || resultType.IsStruct() ){
627 //実態オブジェクトのメンバを参照(obj.member)
628 if( refType != CClass::Dot ){
629 SetError(104,VarName,cp);
630 pRelativeVar->dwKind=NON_VAR;
631 return false;
632 }
633
634 if( resultType.IsObject() ){
635 // 参照内容へのポインタを抽出
636 SetRelativeOffset( *pRelativeVar );
637 }
638 }
639 else if( resultType.IsObjectPtr() || resultType.IsStructPtr() ){
640 //ポインタオブジェクトが示すメンバを参照
641 if(lpPtrOffset[0]){
642 //pObj[n].member
643 if( refType != CClass::Dot ){
644 SetError(104,VarName,cp);
645 pRelativeVar->dwKind=NON_VAR;
646 return false;
647 }
648 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
649 pRelativeVar->dwKind=VAR_DIRECTMEM;
650 }
651 else{
652 //pObj->member
653 if( refType != CClass::Pointer ){
654 SetError(104,VarName,cp);
655 pRelativeVar->dwKind=NON_VAR;
656 return false;
657 }
658
659 SetVarPtrToEax(pRelativeVar);
660 pRelativeVar->dwKind=VAR_DIRECTMEM;
661
662 //mov ecx,dword ptr[eax]
663 OpBuffer[obp++]=(char)0x8B;
664 OpBuffer[obp++]=(char)0x08;
665 }
666 }
667 else if( resultType.GetBasicType()==MAKE_PTR_TYPE(DEF_OBJECT,2) || resultType.GetBasicType()==MAKE_PTR_TYPE(DEF_STRUCT,2)){
668 //ポインタオブジェクトが示すメンバを参照
669 if(lpPtrOffset[0]){
670 //ppObj[n]->member
671 if( refType != CClass::Pointer ){
672 SetError(104,VarName,cp);
673 pRelativeVar->dwKind=NON_VAR;
674 return false;
675 }
676
677 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
678 pRelativeVar->dwKind=VAR_DIRECTMEM;
679
680
681 SetVarPtrToEax(pRelativeVar);
682
683 //mov ecx,dword ptr[eax]
684 OpBuffer[obp++]=(char)0x8B;
685 OpBuffer[obp++]=(char)0x08;
686 }
687 else{
688 SetError(104,VarName,cp);
689 pRelativeVar->dwKind=NON_VAR;
690 return false;
691 }
692 }
693 else{
694 SetError(102,VarName,cp);
695 pRelativeVar->dwKind=NON_VAR;
696 return false;
697 }
698
699 if(!_member_offset(
700 isErrorEnabled,
701 isWriteAccess,
702 resultType.GetClass(),
703 member,pRelativeVar,resultType,0)) return false;
704
705 return true;
706 }
707
708 if(lpPtrOffset[0]){
709 SetRelativeOffset(resultType,pRelativeVar,lpPtrOffset);
710 pRelativeVar->dwKind=VAR_DIRECTMEM;
711 }
712
713 return true;
714}
715
716bool SetInitGlobalData(int offset,const Type &type,const int *SubScripts,const char *lpszInitBuf){
717 extern BYTE *initGlobalBuf;
718 int i,i2,i3;
719 char temporary[VN_SIZE];
720 char InitBuf[VN_SIZE];
721 lstrcpy( InitBuf, lpszInitBuf );
722
723 if(InitBuf[0]=='['){
724 SlideString(InitBuf+1,-1);
725 InitBuf[lstrlen(InitBuf)-1]=0;
726
727 int typeSize = type.GetSize();
728
729 if(SubScripts[0]!=-1){
730 typeSize*=JumpSubScripts(SubScripts+1);
731 i=0;
732 i2=0;
733 while(1){
734 if(SubScripts[0]<i2){
735 SetError(41,0,cp);
736 return 0;
737 }
738 i=GetOneParameter(InitBuf,i,temporary);
739 if(!SetInitGlobalData(
740 offset+i2*typeSize,
741 type,
742 SubScripts+1,
743 temporary)) return false;
744 i2++;
745 if(InitBuf[i]=='\0') break;
746 }
747 return true;
748 }
749
750 if(type.IsStruct()){
751 const CClass &objClass = type.GetClass();
752
753 for(i=0,i2=0;i2<objClass.iMemberNum;i2++){
754 i=GetOneParameter(InitBuf,i,temporary);
755
756 i3=objClass.GetMemberOffset( objClass.ppobj_Member[i2]->GetName().c_str(), NULL );
757
758 if(!SetInitGlobalData(offset+i3,
759 objClass.ppobj_Member[i2]->GetType(),
760 objClass.ppobj_Member[i2]->SubScripts,
761 temporary)) return false;
762
763 if(InitBuf[i]=='\0') break;
764 }
765 if(i2+1!=objClass.iMemberNum){
766 SetError(41,0,cp);
767 return false;
768 }
769 return true;
770 }
771
772 SetError(41,0,cp);
773 return false;
774 }
775
776
777 ///////////////////////////////////////
778 // 単発式([]で囲まれていない)
779 ///////////////////////////////////////
780
781 if( type.IsObject() || type.IsStruct() ){
782 //オブジェクトまたは構造体の場合はありえない
783 SetError(300,NULL,cp);
784 return false;
785 }
786
787 if(SubScripts[0]!=-1){
788 SetError(41,0,cp);
789 return false;
790 }
791
792 double dbl;
793 _int64 i64data;
794 Type calcType;
795
796 if( !StaticCalculation(false, InitBuf,type.GetBasicType(),&i64data,calcType) ){
797 //動的データだった場合
798 return false;
799 }
800 if( calcType.IsReal() ){
801 memcpy(&dbl,&i64data,sizeof(double));
802 i64data=(_int64)dbl;
803 }
804 else dbl=(double)i64data;
805
806 //型チェック
807 CheckDifferentType(
808 type,
809 calcType,
810 0,0);
811
812 if( type.IsDouble() ){
813 *(double *)(initGlobalBuf+offset)=(double)dbl;
814 }
815 else if( type.IsSingle() ){
816 *(float *)(initGlobalBuf+offset)=(float)dbl;
817 }
818 else if( type.Is64() ){
819 *(_int64 *)(initGlobalBuf+offset)=i64data;
820 }
821 else if( type.IsLong() || type.IsDWord() || type.IsPointer() ){
822 if(type.GetBasicType()==typeOfPtrChar){
823 //文字列定数のとき
824
825 char *temp;
826 temp=(char *)i64data;
827 i2=dataTable.AddString(temp,lstrlen(temp));
828 HeapDefaultFree(temp);
829
830 //mov eax,DataPos
831 OpBuffer[obp++]=(char)0xB8;
832 *((long *)(OpBuffer+obp))=(long)i2;
833 pobj_DataTableSchedule->add();
834 obp+=sizeof(long);
835
836 //mov dword ptr[offset],eax
837 OpBuffer[obp++]=(char)0xA3;
838 *((long *)(OpBuffer+obp))=offset;
839 pobj_GlobalVarSchedule->add();
840 obp+=sizeof(long);
841 }
842 else{
843 *(DWORD *)(initGlobalBuf+offset)=(DWORD)i64data;
844 }
845 }
846 else if( type.IsWord() || type.IsInteger() ){
847 *(WORD *)(initGlobalBuf+offset)=(WORD)i64data;
848 }
849 else if( type.IsSByte() || type.IsByte() || type.IsBoolean() ){
850 *(BYTE *)(initGlobalBuf+offset)=(BYTE)i64data;
851 }
852
853 return true;
854}
855bool InitLocalVar(int offset,const Type &type,const int *SubScripts,const char *lpszInitBuf){
856 int i,i2,i3;
857 char temporary[VN_SIZE];
858 char InitBuf[VN_SIZE];
859 lstrcpy( InitBuf, lpszInitBuf );
860
861 if(InitBuf[0]=='['){
862 SlideString(InitBuf+1,-1);
863 InitBuf[lstrlen(InitBuf)-1]=0;
864
865 int typeSize = type.GetSize();
866
867 if(SubScripts[0]!=-1){
868 typeSize*=JumpSubScripts(SubScripts+1);
869 i=0;
870 i2=0;
871 while(1){
872 if(SubScripts[0]<i2){
873 SetError(41,0,cp);
874 return false;
875 }
876 i=GetOneParameter(InitBuf,i,temporary);
877 if(!InitLocalVar(
878 offset+i2*typeSize,
879 type,
880 SubScripts+1,
881 temporary)) return false;
882 i2++;
883 if(InitBuf[i]=='\0') break;
884 }
885 return true;
886 }
887
888 if(type.IsStruct()){
889 const CClass &objClass = type.GetClass();
890
891 for(i=0,i2=0;i2<objClass.iMemberNum;i2++){
892 i=GetOneParameter(InitBuf,i,temporary);
893
894 i3=objClass.GetMemberOffset( objClass.ppobj_Member[i2]->GetName().c_str(), NULL );
895
896 if(!InitLocalVar(offset+i3,
897 objClass.ppobj_Member[i2]->GetType(),
898 objClass.ppobj_Member[i2]->SubScripts,
899 temporary)) return false;
900
901 if(InitBuf[i]=='\0') break;
902 }
903 if(i2+1!=objClass.iMemberNum){
904 SetError(41,0,cp);
905 return 0;
906 }
907 return true;
908 }
909
910 SetError(41,0,cp);
911 return false;
912 }
913
914
915 ///////////////////////////////////////
916 // 単発式([]で囲まれていない)
917 ///////////////////////////////////////
918
919 if(SubScripts[0]!=-1){
920 SetError(41,0,cp);
921 return false;
922 }
923
924 double dbl;
925 _int64 i64data;
926 Type calcType;
927
928 if( !StaticCalculation(false, InitBuf,type.GetBasicType(),&i64data,calcType) ){
929 //動的データだった場合
930 return false;
931 }
932 if( calcType.IsReal() ){
933 memcpy(&dbl,&i64data,sizeof(double));
934 i64data=(_int64)dbl;
935 }
936 else dbl=(double)i64data;
937
938 //型チェック
939 CheckDifferentType(
940 type,
941 calcType,
942 0,0);
943
944 if( type.IsDouble() ){
945 //mov eax,HILONG(dbl)
946 OpBuffer[obp++]=(char)0xB8;
947 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&dbl))+4);
948 obp+=sizeof(long);
949
950 //mov dword ptr[ebp+offset+sizeof(long)],eax
951 OpBuffer[obp++]=(char)0x89;
952 OpBuffer[obp++]=(char)0x85;
953 *((long *)(OpBuffer+obp))=offset+sizeof(long);
954 AddLocalVarAddrSchedule();
955 obp+=sizeof(long);
956
957 //mov eax,LOLONG(dbl)
958 OpBuffer[obp++]=(char)0xB8;
959 *((long *)(OpBuffer+obp))=*(long *)(&dbl);
960 obp+=sizeof(long);
961
962 //mov dword ptr[ebp+offset],eax
963 OpBuffer[obp++]=(char)0x89;
964 OpBuffer[obp++]=(char)0x85;
965 *((long *)(OpBuffer+obp))=offset;
966 AddLocalVarAddrSchedule();
967 obp+=sizeof(long);
968 }
969 else if( type.IsSingle() ){
970 float flt;
971 flt=(float)dbl;
972 //mov eax,InitValue
973 OpBuffer[obp++]=(char)0xB8;
974 *((long *)(OpBuffer+obp))=*(long *)&flt;
975 obp+=sizeof(long);
976
977 //mov dword ptr[ebp+offset],eax
978 OpBuffer[obp++]=(char)0x89;
979 OpBuffer[obp++]=(char)0x85;
980 *((long *)(OpBuffer+obp))=offset;
981 AddLocalVarAddrSchedule();
982 obp+=sizeof(long);
983 }
984 else if( type.Is64() ){
985 //mov eax,HILONG(i64data)
986 OpBuffer[obp++]=(char)0xB8;
987 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&i64data))+4);
988 obp+=sizeof(long);
989
990 //mov dword ptr[ebp+offset+sizeof(long)],eax
991 OpBuffer[obp++]=(char)0x89;
992 OpBuffer[obp++]=(char)0x85;
993 *((long *)(OpBuffer+obp))=offset+sizeof(long);
994 AddLocalVarAddrSchedule();
995 obp+=sizeof(long);
996
997 //mov eax,LOLONG(i64data)
998 OpBuffer[obp++]=(char)0xB8;
999 *((long *)(OpBuffer+obp))=*(long *)(&i64data);
1000 obp+=sizeof(long);
1001
1002 //mov dword ptr[ebp+offset],eax
1003 OpBuffer[obp++]=(char)0x89;
1004 OpBuffer[obp++]=(char)0x85;
1005 *((long *)(OpBuffer+obp))=offset;
1006 AddLocalVarAddrSchedule();
1007 obp+=sizeof(long);
1008 }
1009 else if( type.IsDWord() || type.IsLong() || type.IsPointer() ){
1010 if(type.GetBasicType()==typeOfPtrChar){
1011 //文字列定数のとき
1012
1013 char *temp;
1014 temp=(char *)i64data;
1015 i2=dataTable.AddString(temp,lstrlen(temp));
1016 HeapDefaultFree(temp);
1017
1018 //mov eax,DataPos
1019 OpBuffer[obp++]=(char)0xB8;
1020 *((long *)(OpBuffer+obp))=(long)i2;
1021 pobj_DataTableSchedule->add();
1022 obp+=sizeof(long);
1023 }
1024 else{
1025 //mov eax,InitValue
1026 OpBuffer[obp++]=(char)0xB8;
1027 *((long *)(OpBuffer+obp))=(long)i64data;
1028 obp+=sizeof(long);
1029 }
1030
1031 //mov dword ptr[ebp+offset],eax
1032 OpBuffer[obp++]=(char)0x89;
1033 OpBuffer[obp++]=(char)0x85;
1034 *((long *)(OpBuffer+obp))=offset;
1035 AddLocalVarAddrSchedule();
1036 obp+=sizeof(long);
1037 }
1038 else if( type.IsWord() || type.IsInteger() ){
1039 //mov ax,InitValue
1040 OpBuffer[obp++]=(char)0x66;
1041 OpBuffer[obp++]=(char)0xB8;
1042 *((WORD *)(OpBuffer+obp))=(WORD)i64data;
1043 obp+=sizeof(WORD);
1044
1045 //mov word ptr[ebp+offset],ax
1046 OpBuffer[obp++]=(char)0x66;
1047 OpBuffer[obp++]=(char)0x89;
1048 OpBuffer[obp++]=(char)0x85;
1049 *((long *)(OpBuffer+obp))=offset;
1050 AddLocalVarAddrSchedule();
1051 obp+=sizeof(long);
1052 }
1053 else if( type.IsSByte() || type.IsByte() || type.IsBoolean() ){
1054 //mov byte ptr[ebp+offset],InitValue
1055 OpBuffer[obp++]=(char)0xC6;
1056 OpBuffer[obp++]=(char)0x85;
1057 *((long *)(OpBuffer+obp))=offset;
1058 AddLocalVarAddrSchedule();
1059 obp+=sizeof(long);
1060 *((BYTE *)(OpBuffer+obp))=(BYTE)i64data;
1061 obp+=sizeof(BYTE);
1062 }
1063
1064 return true;
1065}
1066
1067void dim( char *VarName,int *SubScripts,Type &type,const char *InitBuf,const char *ConstractParameter,DWORD dwFlags){
1068 if( UserProc::IsGlobalAreaCompiling() ){
1069 /////////////////////////
1070 // グローバル変数
1071 /////////////////////////
1072
1073 AddGlobalVariable(VarName,SubScripts,type,InitBuf,ConstractParameter,dwFlags);
1074 }
1075 else{
1076 /////////////////
1077 // ローカル変数
1078 /////////////////
1079
1080 if( UserProc::CompilingUserProc().localVars.DuplicateCheck( VarName ) ){
1081 //2重定義のエラー
1082 SetError(15,VarName,cp);
1083 return;
1084 }
1085
1086 bool isConst = ( dwFlags & DIMFLAG_CONST ) ? true:false;
1087
1088 Variable *pVar = new Variable( VarName, type, isConst );
1089
1090 if( SubScripts[0] != -1 ){
1091 //配列あり
1092 pVar->SetArray( SubScripts );
1093 }
1094
1095 //コンストラクタ用パラメータ
1096 pVar->paramStrForConstructor = ConstractParameter;
1097
1098 //レキシカルスコープ
1099 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
1100 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
1101 pVar->bLiving=TRUE;
1102
1103 //エラー用
1104 pVar->source_code_address=cp;
1105
1106 // 変数を追加
1107 UserProc::CompilingUserProc().localVars.push_back( pVar );
1108
1109 //アラインメントを考慮
1110 if( pVar->IsStruct() ){
1111 int alignment = pVar->GetClass().iAlign;
1112
1113 if( alignment ){
1114 if( AllLocalVarSize % alignment ){
1115 AllLocalVarSize += alignment - (AllLocalVarSize % alignment);
1116 }
1117 }
1118
1119 if( alignment == PTR_SIZE*2 ){
1120 // ポインタに要するサイズよりも一回り大きなアラインメントが指定されているとき
1121 // (例:CONTEXT構造体など)
1122 // 呼び出し側のオフセットズレを考慮する
1123
1124 if( 0 == ( UserProc::CompilingUserProc().RealParams().GetMemorySize() + PTR_SIZE/*ret分*/ ) % alignment ){
1125 AllLocalVarSize += PTR_SIZE;
1126 }
1127 }
1128 }
1129
1130 AllLocalVarSize += pVar->GetMemorySize();
1131 pVar->offset = AllLocalVarSize;
1132
1133 //レキシカルスコープ
1134 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
1135 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
1136 pVar->bLiving=TRUE;
1137
1138 if(InitBuf[0]){
1139 //初期代入時のみ、書き込みアクセスを許可する
1140 if( isConst ){
1141 pVar->ConstOff();
1142 }
1143
1144 int result = 0;
1145 if( !pVar->IsObject() ){
1146 result = InitLocalVar(-pVar->offset,
1147 *pVar,
1148 pVar->GetSubScriptsPtr(),
1149 InitBuf);
1150 }
1151
1152 if(!result){
1153 //動的な式だった場合は代入演算を行う
1154 char temporary[8192];
1155 sprintf(temporary,"%s=%s",VarName,InitBuf);
1156 OpcodeCalc(temporary);
1157 }
1158
1159 if( isConst ){
1160 pVar->ConstOn();
1161 }
1162 }
1163 else{
1164 //push 0
1165 op_push_V(0);
1166
1167 //push VarSize
1168 op_push_V( pVar->GetMemorySize() );
1169
1170 //mov eax,ebp
1171 OpBuffer[obp++]=(char)0x8B;
1172 OpBuffer[obp++]=(char)0xC5;
1173
1174 //add eax,offset
1175 OpBuffer[obp++]=(char)0x05;
1176 *((long *)(OpBuffer+obp))=-pVar->offset;
1177 AddLocalVarAddrSchedule();
1178 obp+=sizeof(long);
1179
1180 //push eax
1181 op_push(REG_EAX);
1182
1183 //call FillMemory
1184 OpBuffer[obp++]=(char)0xFF;
1185 OpBuffer[obp++]=(char)0x15;
1186 DllProc *pDllProc = GetDeclareHash("FillMemory");
1187 pDllProc->Using();
1188 pobj_ImportAddrSchedule->add(pDllProc);
1189 obp+=sizeof(long);
1190 }
1191 }
1192
1193 //New呼び出し
1194 if( type.IsObject() &&(dwFlags&DIMFLAG_NONCALL_CONSTRACTOR)==0&&InitBuf[0]=='\0'){
1195 char objectSize[255];
1196 if( SubScripts[0] == -1 ){
1197 objectSize[0] = 0;
1198 }
1199 else{
1200 if( SubScripts[1] != -1 ){
1201 SetError(300,NULL,cp);
1202 }
1203 sprintf( objectSize, "%d", SubScripts[0] );
1204 }
1205 Operator_New( type.GetClass(), objectSize, ConstractParameter, type );
1206
1207 //pop eax
1208 op_pop( REG_EAX );
1209
1210 RELATIVE_VAR RelativeVar;
1211 GetVarOffset( true, false, VarName, &RelativeVar, Type() );
1212 if( RelativeVar.dwKind == VAR_DIRECTMEM ){
1213 SetError();
1214 }
1215 SetVariableFromEax( DEF_OBJECT, DEF_OBJECT, &RelativeVar );
1216 }
1217}
1218
1219void SetVarPtrToEax(RELATIVE_VAR *pRelativeVar){
1220 if(pRelativeVar->dwKind==VAR_GLOBAL){
1221 if(pRelativeVar->bOffsetOffset){
1222 //lea eax,dword ptr[ecx+offset]
1223 OpBuffer[obp++]=(char)0x8D;
1224 OpBuffer[obp++]=(char)0x81;
1225 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1226 pobj_GlobalVarSchedule->add();
1227 obp+=sizeof(long);
1228 }
1229 else{
1230 //mov eax,offset
1231 OpBuffer[obp++]=(char)0xB8;
1232 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1233 pobj_GlobalVarSchedule->add();
1234 obp+=sizeof(long);
1235 }
1236 }
1237 else if(pRelativeVar->dwKind==VAR_REFGLOBAL){
1238 if(pRelativeVar->bOffsetOffset){
1239 //mov eax,ecx
1240 OpBuffer[obp++]=(char)0x8B;
1241 OpBuffer[obp++]=(char)0xC1;
1242
1243 //add eax,dword ptr[offset]
1244 op_add_RM( sizeof(long), REG_EAX, REG_NON, (int)pRelativeVar->offset, MOD_DISP32 );
1245 }
1246 else{
1247 //mov eax,dword ptr[offset]
1248 op_mov_RM( sizeof(long), REG_EAX, REG_NON, (int)pRelativeVar->offset, MOD_DISP32 );
1249 }
1250 obp-=sizeof(long);
1251 pobj_GlobalVarSchedule->add();
1252 obp+=sizeof(long);
1253 }
1254 else if(pRelativeVar->dwKind==VAR_LOCAL){
1255 if(pRelativeVar->bOffsetOffset){
1256 //add ecx,offset
1257 OpBuffer[obp++]=(char)0x81;
1258 OpBuffer[obp++]=(char)0xC1;
1259 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1260 AddLocalVarAddrSchedule();
1261 obp+=sizeof(long);
1262
1263 //lea eax,dword ptr[ebp+ecx]
1264 OpBuffer[obp++]=(char)0x8D;
1265 OpBuffer[obp++]=(char)0x44;
1266 OpBuffer[obp++]=(char)0x0D;
1267 OpBuffer[obp++]=(char)0x00;
1268 }
1269 else{
1270 //lea eax,dword ptr[ebp+offset]
1271 OpBuffer[obp++]=(char)0x8D;
1272 OpBuffer[obp++]=(char)0x85;
1273 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1274 AddLocalVarAddrSchedule();
1275 obp+=sizeof(long);
1276 }
1277 }
1278 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
1279 if(pRelativeVar->bOffsetOffset){
1280 //mov eax,ecx
1281 OpBuffer[obp++]=(char)0x8B;
1282 OpBuffer[obp++]=(char)0xC1;
1283
1284 //add eax,dword ptr[ebp+offset]
1285 OpBuffer[obp++]=(char)0x03;
1286 OpBuffer[obp++]=(char)0x85;
1287 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1288 AddLocalVarAddrSchedule();
1289 obp+=sizeof(long);
1290 }
1291 else{
1292 //mov eax,dword ptr[ebp+offset]
1293 OpBuffer[obp++]=(char)0x8B;
1294 OpBuffer[obp++]=(char)0x85;
1295 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1296 AddLocalVarAddrSchedule();
1297 obp+=sizeof(long);
1298 }
1299 }
1300 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
1301 //mov eax,ecx
1302 OpBuffer[obp++]=(char)0x8B;
1303 OpBuffer[obp++]=(char)0xC1;
1304 }
1305}
1306void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar){
1307 if( reg != REG_EAX ){
1308 SetError();
1309 //TODO: 未完成
1310 }
1311 SetVarPtrToEax( pRelativeVar );
1312}
1313
1314bool Compile_AddGlobalRootsForGc(){
1315 UserProc *pUserProc_AddGlobalRootPtr = GetClassMethod( "_System_CGarbageCollection", "AddGlobalRootPtr" );
1316 if( !pUserProc_AddGlobalRootPtr ){
1317 SetError(3, "_System_CGarbageCollection.AddGlobalRootPtr", -1 );
1318 return false;
1319 }
1320
1321 BOOST_FOREACH( const Variable *pVar, globalVars ){
1322 if( pVar->IsObject() || pVar->IsPointer() || pVar->IsStruct() ){
1323 // オブジェクトまたはポインタだったとき
1324 // ※構造体も含む(暫定対応)
1325
1326 // 変数領域に要するLONG_PTR単位の個数を引き渡す
1327 op_push_V( pVar->GetMemorySize()/PTR_SIZE );
1328
1329
1330 /////////////////////////////
1331 // ルートポインタを引き渡す
1332
1333 //mov eax,offset
1334 op_mov_RV(REG_EAX,(int)pVar->offset);
1335 obp-=sizeof(long);
1336 pobj_GlobalVarSchedule->add();
1337 obp+=sizeof(long);
1338
1339 //push eax
1340 op_push( REG_EAX );
1341
1342 //
1343 /////////////////////////////
1344
1345
1346 /////////////////////////////
1347 // Thisポインタを引き渡す
1348
1349 SetThisPtrToReg(REG_EAX);
1350
1351 //push eax
1352 op_push( REG_EAX );
1353
1354 //
1355 /////////////////////////////
1356
1357
1358 // call AddGlobalRootPtr
1359 op_call( pUserProc_AddGlobalRootPtr );
1360
1361 ReallocNativeCodeBuffer();
1362 }
1363 }
1364
1365 return true;
1366}
Note: See TracBrowser for help on using the repository browser.