source: dev/BasicCompiler32/Compile_Var.cpp@ 14

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

Const変数の書き込み規制を有効化(グローバル/ローカル変数のみ)
定数オブジェクトと定数メンバは未実装。

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