source: dev/BasicCompiler32/Compile_Var.cpp@ 8

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

Constステートメントで定数変数を宣言できるように改良。

File size: 29.9 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 bError,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(bError) 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(bError) 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(bError) SetError(107,VarName,cp);
193 return 0;
194 }
195 else if(bPrivateAccess==0&&pobj_c->ppobj_Member[i]->dwAccess==ACCESS_PROTECTED){
196 if(bError) 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(bError) 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(bError) 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(bError) 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(bError) 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(bError) 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(bError) 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(bError) SetError(104,member,cp);
305 return 0;
306 }
307 }
308
309 if(!GetMemberOffset(bError,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 bError,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
363 if(bCompilingGlobal==0){
364 ////////////////////
365 // ローカル変数
366 ////////////////////
367
368 for(i=MaxLocalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ
369 if(LocalVar[i].bLiving){
370 if(lstrcmp(VarName,LocalVar[i].name)==0) break;
371 }
372 }
373 if(i>=0){
374 //ポインタ変数の場合
375 if(IsPtrType(LocalVar[i].type)){
376 if(LocalVar[i].SubScripts[0]==-1){
377 lstrcpy(lpPtrOffset,array);
378 array[0]=0;
379 }
380 }
381 else{
382 if(lpPtrOffset[0]){
383 SetError(16,variable,cp);
384 pRelativeVar->dwKind=NON_VAR;
385 return 0;
386 }
387 }
388
389 pRelativeVar->offset=-LocalVar[i].offset;
390 pRelativeVar->bOffsetOffset=0;
391 if(LocalVar[i].fRef) pRelativeVar->dwKind=VAR_REFLOCAL;
392 else pRelativeVar->dwKind=VAR_LOCAL;
393 *pType=LocalVar[i].type;
394 lpIndex=LocalVar[i].u.index;
395 if(plpIndex) *plpIndex=lpIndex;
396 pSubScripts=LocalVar[i].SubScripts;
397
398 goto ok;
399 }
400 }
401
402 if(pobj_CompilingClass){
403 //////////////////////
404 // クラスメンバの参照
405 //////////////////////
406
407 if(lstrcmpi(variable,"This")==0){
408 //Thisオブジェクト
409
410 //Thisポインタをecxにコピー
411 SetThisPtrToReg(REG_ECX);
412
413 *pType=DEF_OBJECT;
414 pRelativeVar->dwKind=VAR_DIRECTMEM;
415
416 if(plpIndex) *plpIndex=(LONG_PTR)pobj_CompilingClass;
417 return 1;
418 }
419
420 if(_memicmp(variable,"This.",5)==0){
421 //Thisオブジェクトのメンバを参照するとき
422 SlideString(variable+5,-5);
423 lstrcpy(VarName,variable);
424 }
425 else{
426 //クラス内メンバを参照するとき(通常)
427
428 for(i=0;i<pobj_CompilingClass->iMemberNum;i++){
429 if(lstrcmp(VarName,pobj_CompilingClass->ppobj_Member[i]->name)==0) break;
430 }
431 if(i==pobj_CompilingClass->iMemberNum) goto NonClassMember;
432 }
433
434 /////////////////////////////
435 // thisポインタをecxにセット
436
437 //Thisポインタをecxにコピー
438 SetThisPtrToReg(REG_ECX);
439
440 pRelativeVar->dwKind=VAR_DIRECTMEM;
441 if(!GetMemberOffset(bError,pobj_CompilingClass,variable,pType,pRelativeVar,&lpIndex,1)) return 0;
442 if(plpIndex) *plpIndex=lpIndex;
443 return 1;
444 }
445
446NonClassMember:
447
448 //////////////////////////
449 // 静的ローカル変数
450 // ※"Static.Object.Method.Variable"
451 //////////////////////////
452
453 char temporary[VN_SIZE];
454 extern SUBINFO *pCompilingSubInfo;
455 if(pCompilingSubInfo){
456 GetNowStaticVarFullName(VarName,temporary);
457
458 for(i=0;i<MaxGlobalVarNum;i++){
459 if(lstrcmp(temporary,GlobalVar[i].name)==0) break;
460 }
461 if(i!=MaxGlobalVarNum){
462 goto GlobalOk;
463 }
464 }
465
466
467 ////////////////////
468 // グローバル変数
469 ////////////////////
470
471 for(i=MaxGlobalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ
472 if(GlobalVar[i].bLiving){
473 if(lstrcmp(VarName,GlobalVar[i].name)==0) break;
474 }
475 }
476 if(i>=0){
477 goto GlobalOk;
478 }
479
480
481 //////////////////////////
482 // クラスの静的メンバ
483 //////////////////////////
484
485 if(member[0]){
486 lstrcpy(temporary,member);
487 GetVarFormatString(temporary,array,lpPtrOffset,member,&i);
488
489 char temp2[VN_SIZE];
490 sprintf(temp2,"%s.%s",VarName,temporary);
491 for(i=0;i<MaxGlobalVarNum;i++){
492 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
493 }
494
495 if(i!=MaxGlobalVarNum){
496 goto GlobalOk;
497 }
498 }
499
500 if(pobj_CompilingClass){
501 //自身のクラスから静的メンバを参照する場合
502 char temp2[VN_SIZE];
503 sprintf(temp2,"%s.%s",pobj_CompilingClass->name,VarName);
504 for(i=0;i<MaxGlobalVarNum;i++){
505 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
506 }
507
508 if(i!=MaxGlobalVarNum){
509 goto GlobalOk;
510 }
511 }
512
513 if(bError) SetError(3,variable,cp);
514 pRelativeVar->dwKind=NON_VAR;
515 return 0;
516
517
518GlobalOk:
519
520 //ポインタ変数の場合
521 if(IsPtrType(GlobalVar[i].type)){
522 if(GlobalVar[i].SubScripts[0]==-1){
523 lstrcpy(lpPtrOffset,array);
524 array[0]=0;
525 }
526 }
527 else{
528 if(lpPtrOffset[0]){
529 SetError(16,variable,cp);
530 pRelativeVar->dwKind=NON_VAR;
531 return 0;
532 }
533 }
534
535 pRelativeVar->offset=GlobalVar[i].offset;
536 pRelativeVar->bOffsetOffset=0;
537 pRelativeVar->dwKind=VAR_GLOBAL;
538 *pType=GlobalVar[i].type;
539 lpIndex=GlobalVar[i].u.index;
540 if(plpIndex) *plpIndex=lpIndex;
541 pSubScripts=GlobalVar[i].SubScripts;
542
543
544ok:
545
546
547 if(array[0]==0&&pSubScripts[0]!=-1){
548 //配列の先頭ポインタを示す場合
549 *pType|=FLAG_PTR;
550 if(pss) memcpy(pss,pSubScripts,MAX_ARRAYDIM);
551 return 1;
552 }
553
554 if(array[0]||member[0]){
555 //xor ecx,ecx(ecxを0に初期化する)
556 //※ecxは変数ベースアドレスからの相対オフセットを示す
557 op_zero_reg(REG_ECX);
558
559 pRelativeVar->bOffsetOffset=1;
560 }
561 if(array[0]){
562 if(!GetArrayOffset(pSubScripts,array,*pType,lpIndex)){
563 SetError(14,variable,cp);
564 pRelativeVar->dwKind=NON_VAR;
565 return 0;
566 }
567 }
568 if(member[0]){
569 if(*pType==DEF_OBJECT){
570 //実態オブジェクトのメンバを参照(obj.member)
571 if(RefType!=DEF_OBJECT){
572 SetError(104,VarName,cp);
573 pRelativeVar->dwKind=NON_VAR;
574 return 0;
575 }
576
577 LONG_PTR lp2;
578 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
579 if(plpIndex) *plpIndex=lp2;
580 }
581 else if(*pType==DEF_PTR_OBJECT){
582 //ポインタオブジェクトが示すメンバを参照
583 if(lpPtrOffset[0]){
584 //pObj[n].member
585 if(RefType!=DEF_OBJECT){
586 SetError(104,VarName,cp);
587 pRelativeVar->dwKind=NON_VAR;
588 return 0;
589 }
590 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
591 pRelativeVar->dwKind=VAR_DIRECTMEM;
592
593 LONG_PTR lp2;
594 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
595 if(plpIndex) *plpIndex=lp2;
596 }
597 else{
598 //pObj->member
599 if(RefType!=DEF_PTR_OBJECT){
600 SetError(104,VarName,cp);
601 pRelativeVar->dwKind=NON_VAR;
602 return 0;
603 }
604
605 SetVarPtrToEax(pRelativeVar);
606 pRelativeVar->dwKind=VAR_DIRECTMEM;
607
608 //mov ecx,dword ptr[eax]
609 OpBuffer[obp++]=(char)0x8B;
610 OpBuffer[obp++]=(char)0x08;
611
612 LONG_PTR lp2;
613 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
614 if(plpIndex) *plpIndex=lp2;
615 }
616 }
617 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2)){
618 //ポインタオブジェクトが示すメンバを参照
619 if(lpPtrOffset[0]){
620 //ppObj[n]->member
621 if(RefType!=DEF_PTR_OBJECT){
622 SetError(104,VarName,cp);
623 pRelativeVar->dwKind=NON_VAR;
624 return 0;
625 }
626
627 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
628 pRelativeVar->dwKind=VAR_DIRECTMEM;
629
630
631 SetVarPtrToEax(pRelativeVar);
632
633 //mov ecx,dword ptr[eax]
634 OpBuffer[obp++]=(char)0x8B;
635 OpBuffer[obp++]=(char)0x08;
636
637 LONG_PTR lp2;
638 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
639 if(plpIndex) *plpIndex=lp2;
640 }
641 else{
642 SetError(104,VarName,cp);
643 pRelativeVar->dwKind=NON_VAR;
644 return 0;
645 }
646 }
647 else{
648 SetError(102,VarName,cp);
649 pRelativeVar->dwKind=NON_VAR;
650 return 0;
651 }
652 return 1;
653 }
654
655 if(lpPtrOffset[0]){
656 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
657 pRelativeVar->dwKind=VAR_DIRECTMEM;
658 }
659
660 return 1;
661}
662
663BOOL SetInitGlobalData(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf){
664 extern BYTE *initGlobalBuf;
665 int i,i2,i3,TypeSize;
666 char temporary[VN_SIZE];
667
668 if(InitBuf[0]=='['){
669 SlideString(InitBuf+1,-1);
670 InitBuf[lstrlen(InitBuf)-1]=0;
671
672 TypeSize=GetTypeSize(type,lpIndex);
673
674 if(SubScripts[0]!=-1){
675 TypeSize*=JumpSubScripts(SubScripts+1);
676 i=0;
677 i2=0;
678 while(1){
679 if(SubScripts[0]<i2){
680 SetError(41,0,cp);
681 return 0;
682 }
683 i=GetOneParameter(InitBuf,i,temporary);
684 if(!SetInitGlobalData(
685 offset+i2*TypeSize,
686 type,
687 lpIndex,
688 SubScripts+1,
689 temporary)) return 0;
690 i2++;
691 if(InitBuf[i]=='\0') break;
692 }
693 return -1;
694 }
695
696 if(type==DEF_OBJECT){
697 CClass *pobj_c;
698 pobj_c=(CClass *)lpIndex;
699
700 for(i=0,i2=0;i2<pobj_c->iMemberNum;i2++){
701 i=GetOneParameter(InitBuf,i,temporary);
702
703 i3=GetSizeOfClassMember(pobj_c,pobj_c->ppobj_Member[i2]->name,NULL);
704
705 if(!SetInitGlobalData(offset+i3,
706 pobj_c->ppobj_Member[i2]->TypeInfo.type,
707 pobj_c->ppobj_Member[i2]->TypeInfo.u.lpIndex,
708 pobj_c->ppobj_Member[i2]->SubScripts,
709 temporary)) return 0;
710
711 if(InitBuf[i]=='\0') break;
712 }
713 if(i2+1!=pobj_c->iMemberNum){
714 SetError(41,0,cp);
715 return 0;
716 }
717 return 1;
718 }
719
720 SetError(41,0,cp);
721 return 0;
722 }
723
724 if(SubScripts[0]!=-1){
725 SetError(41,0,cp);
726 return 0;
727 }
728
729 double dbl;
730 _int64 i64data;
731 int CalcType;
732 LONG_PTR lpCalcIndex;
733 CalcType=StaticCalculation(true, InitBuf,type,&i64data,&lpCalcIndex);
734 if(IsRealNumberType(CalcType)){
735 memcpy(&dbl,&i64data,sizeof(double));
736 i64data=(_int64)dbl;
737 }
738 else dbl=(double)i64data;
739
740 //型チェック
741 CheckDifferentType(
742 type,
743 lpIndex,
744 CalcType,
745 lpCalcIndex,
746 0,0);
747
748 if(type==DEF_DOUBLE)
749 *(double *)(initGlobalBuf+offset)=(double)dbl;
750 else if(type==DEF_SINGLE)
751 *(float *)(initGlobalBuf+offset)=(float)dbl;
752 else if(type==DEF_INT64||type==DEF_QWORD)
753 *(_int64 *)(initGlobalBuf+offset)=i64data;
754 else if(type==DEF_LONG||type==DEF_DWORD||IsPtrType(type)){
755 if(type==DEF_PTR_BYTE&&lpCalcIndex==LITERAL_STRING){
756 //文字列定数のとき
757
758 char *temp;
759 temp=(char *)i64data;
760 i2=AddDataTable(temp,lstrlen(temp));
761 HeapDefaultFree(temp);
762
763 //mov eax,DataPos
764 OpBuffer[obp++]=(char)0xB8;
765 *((long *)(OpBuffer+obp))=(long)i2;
766 pobj_DataTableSchedule->add();
767 obp+=sizeof(long);
768
769 //mov dword ptr[offset],eax
770 OpBuffer[obp++]=(char)0xA3;
771 *((long *)(OpBuffer+obp))=offset;
772 pobj_GlobalVarSchedule->add();
773 obp+=sizeof(long);
774 }
775 else{
776 *(DWORD *)(initGlobalBuf+offset)=(DWORD)i64data;
777 }
778 }
779 else if(type==DEF_INTEGER||type==DEF_WORD)
780 *(WORD *)(initGlobalBuf+offset)=(WORD)i64data;
781 else if(type==DEF_CHAR||type==DEF_BYTE)
782 *(BYTE *)(initGlobalBuf+offset)=(BYTE)i64data;
783 //String型が未完成
784 return 1;
785}
786BOOL InitLocalVar(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf){
787 int i,i2,i3,TypeSize;
788 char temporary[VN_SIZE];
789
790 if(InitBuf[0]=='['){
791 SlideString(InitBuf+1,-1);
792 InitBuf[lstrlen(InitBuf)-1]=0;
793
794 TypeSize=GetTypeSize(type,lpIndex);
795
796 if(SubScripts[0]!=-1){
797 TypeSize*=JumpSubScripts(SubScripts+1);
798 i=0;
799 i2=0;
800 while(1){
801 if(SubScripts[0]<i2){
802 SetError(41,0,cp);
803 return 0;
804 }
805 i=GetOneParameter(InitBuf,i,temporary);
806 if(!InitLocalVar(
807 offset+i2*TypeSize,
808 type,
809 lpIndex,
810 SubScripts+1,
811 temporary)) return 0;
812 i2++;
813 if(InitBuf[i]=='\0') break;
814 }
815 return -1;
816 }
817
818 if(type==DEF_OBJECT){
819 CClass *pobj_c;
820 pobj_c=(CClass *)lpIndex;
821
822 for(i=0,i2=0;i2<pobj_c->iMemberNum;i2++){
823 i=GetOneParameter(InitBuf,i,temporary);
824
825 i3=GetSizeOfClassMember(pobj_c,pobj_c->ppobj_Member[i2]->name,NULL);
826
827 if(!InitLocalVar(offset+i3,
828 pobj_c->ppobj_Member[i2]->TypeInfo.type,
829 pobj_c->ppobj_Member[i2]->TypeInfo.u.lpIndex,
830 pobj_c->ppobj_Member[i2]->SubScripts,
831 temporary)) return 0;
832
833 if(InitBuf[i]=='\0') break;
834 }
835 if(i2+1!=pobj_c->iMemberNum){
836 SetError(41,0,cp);
837 return 0;
838 }
839 return 1;
840 }
841
842 SetError(41,0,cp);
843 return 0;
844 }
845
846 if(SubScripts[0]!=-1){
847 SetError(41,0,cp);
848 return 0;
849 }
850
851 double dbl;
852 _int64 i64data;
853 int CalcType;
854 LONG_PTR lpCalcIndex;
855 CalcType=StaticCalculation(false, InitBuf,type,&i64data,&lpCalcIndex);
856 if(!CalcType){
857 //動的データだった場合
858 return 0;
859 }
860 if(IsRealNumberType(CalcType)){
861 memcpy(&dbl,&i64data,sizeof(double));
862 i64data=(_int64)dbl;
863 }
864 else dbl=(double)i64data;
865
866 //型チェック
867 CheckDifferentType(
868 type,
869 lpIndex,
870 CalcType,
871 lpCalcIndex,
872 0,0);
873
874 if(type==DEF_DOUBLE){
875 //mov eax,HILONG(dbl)
876 OpBuffer[obp++]=(char)0xB8;
877 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&dbl))+4);
878 obp+=sizeof(long);
879
880 //mov dword ptr[ebp+offset+sizeof(long)],eax
881 OpBuffer[obp++]=(char)0x89;
882 OpBuffer[obp++]=(char)0x85;
883 *((long *)(OpBuffer+obp))=offset+sizeof(long);
884 AddLocalVarAddrSchedule();
885 obp+=sizeof(long);
886
887 //mov eax,LOLONG(dbl)
888 OpBuffer[obp++]=(char)0xB8;
889 *((long *)(OpBuffer+obp))=*(long *)(&dbl);
890 obp+=sizeof(long);
891
892 //mov dword ptr[ebp+offset],eax
893 OpBuffer[obp++]=(char)0x89;
894 OpBuffer[obp++]=(char)0x85;
895 *((long *)(OpBuffer+obp))=offset;
896 AddLocalVarAddrSchedule();
897 obp+=sizeof(long);
898 }
899 else if(type==DEF_SINGLE){
900 float flt;
901 flt=(float)dbl;
902 //mov eax,InitValue
903 OpBuffer[obp++]=(char)0xB8;
904 *((long *)(OpBuffer+obp))=*(long *)&flt;
905 obp+=sizeof(long);
906
907 //mov dword ptr[ebp+offset],eax
908 OpBuffer[obp++]=(char)0x89;
909 OpBuffer[obp++]=(char)0x85;
910 *((long *)(OpBuffer+obp))=offset;
911 AddLocalVarAddrSchedule();
912 obp+=sizeof(long);
913 }
914 else if(type==DEF_INT64||type==DEF_QWORD){
915 //mov eax,HILONG(i64data)
916 OpBuffer[obp++]=(char)0xB8;
917 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&i64data))+4);
918 obp+=sizeof(long);
919
920 //mov dword ptr[ebp+offset+sizeof(long)],eax
921 OpBuffer[obp++]=(char)0x89;
922 OpBuffer[obp++]=(char)0x85;
923 *((long *)(OpBuffer+obp))=offset+sizeof(long);
924 AddLocalVarAddrSchedule();
925 obp+=sizeof(long);
926
927 //mov eax,LOLONG(i64data)
928 OpBuffer[obp++]=(char)0xB8;
929 *((long *)(OpBuffer+obp))=*(long *)(&i64data);
930 obp+=sizeof(long);
931
932 //mov dword ptr[ebp+offset],eax
933 OpBuffer[obp++]=(char)0x89;
934 OpBuffer[obp++]=(char)0x85;
935 *((long *)(OpBuffer+obp))=offset;
936 AddLocalVarAddrSchedule();
937 obp+=sizeof(long);
938 }
939 else if(type==DEF_LONG||type==DEF_DWORD||IsPtrType(type)){
940 if(type==DEF_PTR_BYTE&&lpCalcIndex==LITERAL_STRING){
941 //文字列定数のとき
942
943 char *temp;
944 temp=(char *)i64data;
945 i2=AddDataTable(temp,lstrlen(temp));
946 HeapDefaultFree(temp);
947
948 //mov eax,DataPos
949 OpBuffer[obp++]=(char)0xB8;
950 *((long *)(OpBuffer+obp))=(long)i2;
951 pobj_DataTableSchedule->add();
952 obp+=sizeof(long);
953 }
954 else{
955 //mov eax,InitValue
956 OpBuffer[obp++]=(char)0xB8;
957 *((long *)(OpBuffer+obp))=(long)i64data;
958 obp+=sizeof(long);
959 }
960
961 //mov dword ptr[ebp+offset],eax
962 OpBuffer[obp++]=(char)0x89;
963 OpBuffer[obp++]=(char)0x85;
964 *((long *)(OpBuffer+obp))=offset;
965 AddLocalVarAddrSchedule();
966 obp+=sizeof(long);
967 }
968 else if(type==DEF_INTEGER||type==DEF_WORD){
969 //mov ax,InitValue
970 OpBuffer[obp++]=(char)0x66;
971 OpBuffer[obp++]=(char)0xB8;
972 *((WORD *)(OpBuffer+obp))=(WORD)i64data;
973 obp+=sizeof(WORD);
974
975 //mov word ptr[ebp+offset],ax
976 OpBuffer[obp++]=(char)0x66;
977 OpBuffer[obp++]=(char)0x89;
978 OpBuffer[obp++]=(char)0x85;
979 *((long *)(OpBuffer+obp))=offset;
980 AddLocalVarAddrSchedule();
981 obp+=sizeof(long);
982 }
983 else if(type==DEF_CHAR||type==DEF_BYTE){
984 //mov byte ptr[ebp+offset],InitValue
985 OpBuffer[obp++]=(char)0xC6;
986 OpBuffer[obp++]=(char)0x85;
987 *((long *)(OpBuffer+obp))=offset;
988 AddLocalVarAddrSchedule();
989 obp+=sizeof(long);
990 *((BYTE *)(OpBuffer+obp))=(BYTE)i64data;
991 obp+=sizeof(BYTE);
992 }
993 //String型が未完成
994 return 1;
995}
996
997void dim(char *Parameter,DWORD dwFlag){
998 extern BOOL bCompilingGlobal;
999 extern HANDLE hHeap;
1000 int i2,i3,VarSize;
1001 char VarName[VN_SIZE];
1002
1003
1004 if(dwFlag & DIMFLAG_CONST){
1005 //////////////////////////////////
1006 // 定数変数の場合を考慮
1007 //////////////////////////////////
1008 for(i2=0;;i2++){
1009 if(Parameter[i2] == '=' ||
1010 Parameter[i2] == 1 && Parameter[i2] == ESC_AS ||
1011 Parameter[i2] =='('){
1012 VarName[i2] = 0;
1013 break;
1014 }
1015 VarName[i2] = Parameter[i2];
1016 }
1017
1018 //定数と2重定義されていないる場合は抜け出す
1019 if(CDBConst::obj.GetType(VarName)){
1020 return;
1021 }
1022
1023 //定数マクロとして定義されている場合は抜け出す
1024 if(GetConstHash(VarName)){
1025 return;
1026 }
1027 }
1028
1029
1030 //構文を解析
1031 int SubScripts[MAX_ARRAYDIM];
1032 TYPEINFO TypeInfo;
1033 char InitBuf[8192];
1034 char ConstractParameter[VN_SIZE];
1035 if(!GetDimentionFormat(Parameter,VarName,SubScripts,&TypeInfo,InitBuf,ConstractParameter))
1036 return;
1037
1038
1039 //定数と2重定義されていないかを調べる
1040 if(CDBConst::obj.GetType(VarName)){
1041 SetError(15,VarName,cp);
1042 return;
1043 }
1044
1045 //定数マクロとして定義されている場合
1046 if(GetConstHash(VarName)){
1047 SetError(15,VarName,cp);
1048 return;
1049 }
1050
1051
1052 //タイプサイズを取得
1053 int TypeSize;
1054 TypeSize=GetTypeSize(TypeInfo.type,TypeInfo.u.lpIndex);
1055
1056 if(dwFlag&DIMFLAG_STATIC){
1057 if(bCompilingGlobal){
1058 SetError(60,NULL,cp);
1059 return;
1060 }
1061
1062 /////////////////////
1063 // Static変数
1064 // ※"Static.Object.Method.Variable"
1065 /////////////////////
1066
1067 char temporary[VN_SIZE];
1068 GetNowStaticVarFullName(VarName,temporary);
1069
1070 AddGlobalVariable(temporary,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1071
1072 /*
1073 Note: 静的変数のコンストラクタ呼び出しは
1074 _System_InitStaticLocalVariables関数内で一括して行う
1075 */
1076 }
1077 else{
1078 if(bCompilingGlobal){
1079 /////////////////////////
1080 // グローバル変数
1081 /////////////////////////
1082
1083 AddGlobalVariable(VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1084 }
1085 else{
1086 /////////////////
1087 // ローカル変数
1088 /////////////////
1089
1090 for(i2=0;i2<MaxLocalVarNum;i2++){
1091 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){
1092 if(lstrcmp(LocalVar[i2].name,VarName)==0){
1093 //2重定義のエラー
1094 SetError(15,VarName,cp);
1095 return;
1096 }
1097 }
1098 }
1099
1100 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE));
1101
1102 for(i2=1,i3=0;i3<255;i3++){
1103 //配列要素数
1104 LocalVar[MaxLocalVarNum].SubScripts[i3]=SubScripts[i3];
1105
1106 if(SubScripts[i3]==-1) break;
1107 i2*=SubScripts[i3]+1;
1108 }
1109 VarSize=TypeSize*i2;
1110 if(VarSize%4) VarSize+=4-(VarSize%4);
1111
1112 VARIABLE *pVar = &LocalVar[MaxLocalVarNum];
1113
1114 MaxLocalVarNum++;
1115
1116 //変数データを追加
1117 lstrcpy(pVar->name,VarName);
1118 pVar->fRef=0;
1119 if(dwFlag & DIMFLAG_CONST) pVar->bConst = 1;
1120 else pVar->bConst = 0;
1121 if(SubScripts[0]==-1) pVar->bArray=0;
1122 else pVar->bArray=1;
1123 pVar->type=TypeInfo.type;
1124 pVar->u.index=TypeInfo.u.lpIndex;
1125 AllLocalVarSize+=VarSize;
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 int result = InitLocalVar(-pVar->offset,
1135 pVar->type,
1136 pVar->u.index,
1137 pVar->SubScripts,
1138 InitBuf);
1139
1140 if(!result){
1141 //動的な式だった場合は代入演算を行う
1142 char temporary[8192];
1143 sprintf(temporary,"%s=%s",VarName,InitBuf);
1144 OpcodeCalc(temporary);
1145 }
1146 }
1147 else{
1148 //push 0
1149 op_push_value(0);
1150
1151 //push VarSize
1152 op_push_value(VarSize);
1153
1154 //mov eax,ebp
1155 OpBuffer[obp++]=(char)0x8B;
1156 OpBuffer[obp++]=(char)0xC5;
1157
1158 //add eax,offset
1159 OpBuffer[obp++]=(char)0x05;
1160 *((long *)(OpBuffer+obp))=-pVar->offset;
1161 AddLocalVarAddrSchedule();
1162 obp+=sizeof(long);
1163
1164 //push eax
1165 op_push(REG_EAX);
1166
1167 //call FillMemory
1168 OpBuffer[obp++]=(char)0xFF;
1169 OpBuffer[obp++]=(char)0x15;
1170 DECLAREINFO *pdi;
1171 pdi=GetDeclareHash("FillMemory");
1172 pdi->bUse=1;
1173 pobj_ImportAddrSchedule->add(pdi);
1174 obp+=sizeof(long);
1175 }
1176 }
1177
1178 //コンストラクタ呼び出し
1179 if(TypeInfo.type==DEF_OBJECT&&(dwFlag&DIMFLAG_NONCALL_CONSTRACTOR)==0){
1180 CallConstractor(VarName,SubScripts,TypeInfo,ConstractParameter);
1181 }
1182 }
1183
1184 if(TypeInfo.type==DEF_OBJECT){
1185 if(TypeInfo.u.pobj_Class->IsHoldAbstractFunction()){
1186 //抽象クラスだったとき
1187 SetError(125,TypeInfo.u.pobj_Class->name,cp);
1188 }
1189 }
1190}
1191void OpcodeDim(char *Parameter,DWORD dwFlag){
1192 int i,i2,i3,IsStr=0;
1193 char temporary[8192];
1194
1195 for(i=0,i2=0;;i++,i2++){
1196 if(Parameter[i]=='\"') IsStr^=1;
1197 if(Parameter[i]=='('&&IsStr==0){
1198 i3=GetStringInPare(temporary+i2,Parameter+i);
1199 i+=i3-1;
1200 i2+=i3-1;
1201 continue;
1202 }
1203 if(Parameter[i]=='['&&IsStr==0){
1204 i3=GetStringInBracket(temporary+i2,Parameter+i);
1205 i+=i3-1;
1206 i2+=i3-1;
1207 continue;
1208 }
1209 if((Parameter[i]==','&&IsStr==0)||
1210 Parameter[i]=='\0'){
1211 temporary[i2]=0;
1212
1213 dim(temporary,dwFlag);
1214
1215 if(Parameter[i]=='\0') break;
1216 i2=-1;
1217 continue;
1218 }
1219 temporary[i2]=Parameter[i];
1220 }
1221}
1222
1223void SetVarPtrToEax(RELATIVE_VAR *pRelativeVar){
1224 if(pRelativeVar->dwKind==VAR_GLOBAL){
1225 if(pRelativeVar->bOffsetOffset){
1226 //lea eax,dword ptr[ecx+offset]
1227 OpBuffer[obp++]=(char)0x8D;
1228 OpBuffer[obp++]=(char)0x81;
1229 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1230 pobj_GlobalVarSchedule->add();
1231 obp+=sizeof(long);
1232 }
1233 else{
1234 //mov eax,offset
1235 OpBuffer[obp++]=(char)0xB8;
1236 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1237 pobj_GlobalVarSchedule->add();
1238 obp+=sizeof(long);
1239 }
1240 }
1241 else if(pRelativeVar->dwKind==VAR_LOCAL){
1242 if(pRelativeVar->bOffsetOffset){
1243 //add ecx,offset
1244 OpBuffer[obp++]=(char)0x81;
1245 OpBuffer[obp++]=(char)0xC1;
1246 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1247 AddLocalVarAddrSchedule();
1248 obp+=sizeof(long);
1249
1250 //lea eax,dword ptr[ebp+ecx]
1251 OpBuffer[obp++]=(char)0x8D;
1252 OpBuffer[obp++]=(char)0x44;
1253 OpBuffer[obp++]=(char)0x0D;
1254 OpBuffer[obp++]=(char)0x00;
1255 }
1256 else{
1257 //lea eax,dword ptr[ebp+offset]
1258 OpBuffer[obp++]=(char)0x8D;
1259 OpBuffer[obp++]=(char)0x85;
1260 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1261 AddLocalVarAddrSchedule();
1262 obp+=sizeof(long);
1263 }
1264 }
1265 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
1266 if(pRelativeVar->bOffsetOffset){
1267 //mov eax,ecx
1268 OpBuffer[obp++]=(char)0x8B;
1269 OpBuffer[obp++]=(char)0xC1;
1270
1271 //add eax,dword ptr[ebp+offset]
1272 OpBuffer[obp++]=(char)0x03;
1273 OpBuffer[obp++]=(char)0x85;
1274 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1275 AddLocalVarAddrSchedule();
1276 obp+=sizeof(long);
1277 }
1278 else{
1279 //mov eax,dword ptr[ebp+offset]
1280 OpBuffer[obp++]=(char)0x8B;
1281 OpBuffer[obp++]=(char)0x85;
1282 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1283 AddLocalVarAddrSchedule();
1284 obp+=sizeof(long);
1285 }
1286 }
1287 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
1288 //mov eax,ecx
1289 OpBuffer[obp++]=(char)0x8B;
1290 OpBuffer[obp++]=(char)0xC1;
1291 }
1292}
Note: See TracBrowser for help on using the repository browser.