source: dev/BasicCompiler32/Compile_Var.cpp@ 6

Last change on this file since 6 was 3, checked in by dai_9181, 18 years ago
File size: 29.1 KB
RevLine 
[3]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(true, InitBuf,type,&i64data,&lpCalcIndex);
856 if(IsRealNumberType(CalcType)){
857 memcpy(&dbl,&i64data,sizeof(double));
858 i64data=(_int64)dbl;
859 }
860 else dbl=(double)i64data;
861
862 //型チェック
863 CheckDifferentType(
864 type,
865 lpIndex,
866 CalcType,
867 lpCalcIndex,
868 0,0);
869
870 if(type==DEF_DOUBLE){
871 //mov eax,HILONG(dbl)
872 OpBuffer[obp++]=(char)0xB8;
873 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&dbl))+4);
874 obp+=sizeof(long);
875
876 //mov dword ptr[ebp+offset+sizeof(long)],eax
877 OpBuffer[obp++]=(char)0x89;
878 OpBuffer[obp++]=(char)0x85;
879 *((long *)(OpBuffer+obp))=offset+sizeof(long);
880 AddLocalVarAddrSchedule();
881 obp+=sizeof(long);
882
883 //mov eax,LOLONG(dbl)
884 OpBuffer[obp++]=(char)0xB8;
885 *((long *)(OpBuffer+obp))=*(long *)(&dbl);
886 obp+=sizeof(long);
887
888 //mov dword ptr[ebp+offset],eax
889 OpBuffer[obp++]=(char)0x89;
890 OpBuffer[obp++]=(char)0x85;
891 *((long *)(OpBuffer+obp))=offset;
892 AddLocalVarAddrSchedule();
893 obp+=sizeof(long);
894 }
895 else if(type==DEF_SINGLE){
896 float flt;
897 flt=(float)dbl;
898 //mov eax,InitValue
899 OpBuffer[obp++]=(char)0xB8;
900 *((long *)(OpBuffer+obp))=*(long *)&flt;
901 obp+=sizeof(long);
902
903 //mov dword ptr[ebp+offset],eax
904 OpBuffer[obp++]=(char)0x89;
905 OpBuffer[obp++]=(char)0x85;
906 *((long *)(OpBuffer+obp))=offset;
907 AddLocalVarAddrSchedule();
908 obp+=sizeof(long);
909 }
910 else if(type==DEF_INT64||type==DEF_QWORD){
911 //mov eax,HILONG(i64data)
912 OpBuffer[obp++]=(char)0xB8;
913 *((long *)(OpBuffer+obp))=(long)*(long *)(((char *)(&i64data))+4);
914 obp+=sizeof(long);
915
916 //mov dword ptr[ebp+offset+sizeof(long)],eax
917 OpBuffer[obp++]=(char)0x89;
918 OpBuffer[obp++]=(char)0x85;
919 *((long *)(OpBuffer+obp))=offset+sizeof(long);
920 AddLocalVarAddrSchedule();
921 obp+=sizeof(long);
922
923 //mov eax,LOLONG(i64data)
924 OpBuffer[obp++]=(char)0xB8;
925 *((long *)(OpBuffer+obp))=*(long *)(&i64data);
926 obp+=sizeof(long);
927
928 //mov dword ptr[ebp+offset],eax
929 OpBuffer[obp++]=(char)0x89;
930 OpBuffer[obp++]=(char)0x85;
931 *((long *)(OpBuffer+obp))=offset;
932 AddLocalVarAddrSchedule();
933 obp+=sizeof(long);
934 }
935 else if(type==DEF_LONG||type==DEF_DWORD||IsPtrType(type)){
936 if(type==DEF_PTR_BYTE&&lpCalcIndex==LITERAL_STRING){
937 //文字列定数のとき
938
939 char *temp;
940 temp=(char *)i64data;
941 i2=AddDataTable(temp,lstrlen(temp));
942 HeapDefaultFree(temp);
943
944 //mov eax,DataPos
945 OpBuffer[obp++]=(char)0xB8;
946 *((long *)(OpBuffer+obp))=(long)i2;
947 pobj_DataTableSchedule->add();
948 obp+=sizeof(long);
949 }
950 else{
951 //mov eax,InitValue
952 OpBuffer[obp++]=(char)0xB8;
953 *((long *)(OpBuffer+obp))=(long)i64data;
954 obp+=sizeof(long);
955 }
956
957 //mov dword ptr[ebp+offset],eax
958 OpBuffer[obp++]=(char)0x89;
959 OpBuffer[obp++]=(char)0x85;
960 *((long *)(OpBuffer+obp))=offset;
961 AddLocalVarAddrSchedule();
962 obp+=sizeof(long);
963 }
964 else if(type==DEF_INTEGER||type==DEF_WORD){
965 //mov ax,InitValue
966 OpBuffer[obp++]=(char)0x66;
967 OpBuffer[obp++]=(char)0xB8;
968 *((WORD *)(OpBuffer+obp))=(WORD)i64data;
969 obp+=sizeof(WORD);
970
971 //mov word ptr[ebp+offset],ax
972 OpBuffer[obp++]=(char)0x66;
973 OpBuffer[obp++]=(char)0x89;
974 OpBuffer[obp++]=(char)0x85;
975 *((long *)(OpBuffer+obp))=offset;
976 AddLocalVarAddrSchedule();
977 obp+=sizeof(long);
978 }
979 else if(type==DEF_CHAR||type==DEF_BYTE){
980 //mov byte ptr[ebp+offset],InitValue
981 OpBuffer[obp++]=(char)0xC6;
982 OpBuffer[obp++]=(char)0x85;
983 *((long *)(OpBuffer+obp))=offset;
984 AddLocalVarAddrSchedule();
985 obp+=sizeof(long);
986 *((BYTE *)(OpBuffer+obp))=(BYTE)i64data;
987 obp+=sizeof(BYTE);
988 }
989 //String型が未完成
990 return 1;
991}
992
993void dim(char *Parameter,DWORD dwFlag){
994 extern BOOL bCompilingGlobal;
995 extern HANDLE hHeap;
996 int i2,i3,VarSize;
997
998 //構文を解析
999 char VarName[VN_SIZE];
1000 int SubScripts[MAX_ARRAYDIM];
1001 TYPEINFO TypeInfo;
1002 char InitBuf[8192];
1003 char ConstractParameter[VN_SIZE];
1004 if(!GetDimentionFormat(Parameter,VarName,SubScripts,&TypeInfo,InitBuf,ConstractParameter))
1005 return;
1006
1007 //タイプサイズを取得
1008 int TypeSize;
1009 TypeSize=GetTypeSize(TypeInfo.type,TypeInfo.u.lpIndex);
1010
1011 //定数と2重定義されていないかを調べる
1012 if(GetConstHash(VarName)){
1013 SetError(15,VarName,cp);
1014 return;
1015 }
1016
1017 if(dwFlag&DIMFLAG_STATIC){
1018 if(bCompilingGlobal){
1019 SetError(60,NULL,cp);
1020 return;
1021 }
1022
1023 /////////////////////
1024 // Static変数
1025 // ※"Static.Object.Method.Variable"
1026 /////////////////////
1027
1028 char temporary[VN_SIZE];
1029 GetNowStaticVarFullName(VarName,temporary);
1030
1031 AddGlobalVariable(temporary,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1032
1033 /*
1034 Note: 静的変数のコンストラクタ呼び出しは
1035 _System_InitStaticLocalVariables関数内で一括して行う
1036 */
1037 }
1038 else{
1039 if(bCompilingGlobal){
1040 /////////////////////////
1041 // グローバル変数
1042 /////////////////////////
1043
1044 AddGlobalVariable(VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1045 }
1046 else{
1047 /////////////////
1048 // ローカル変数
1049 /////////////////
1050
1051 for(i2=0;i2<MaxLocalVarNum;i2++){
1052 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){
1053 if(lstrcmp(LocalVar[i2].name,VarName)==0){
1054 //2重定義のエラー
1055 SetError(15,VarName,cp);
1056 return;
1057 }
1058 }
1059 }
1060
1061 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE));
1062
1063 for(i2=1,i3=0;i3<255;i3++){
1064 //配列要素数
1065 LocalVar[MaxLocalVarNum].SubScripts[i3]=SubScripts[i3];
1066
1067 if(SubScripts[i3]==-1) break;
1068 i2*=SubScripts[i3]+1;
1069 }
1070 VarSize=TypeSize*i2;
1071 if(VarSize%4) VarSize+=4-(VarSize%4);
1072
1073 //変数データを追加
1074 lstrcpy(LocalVar[MaxLocalVarNum].name,VarName);
1075 LocalVar[MaxLocalVarNum].fRef=0;
1076 if(SubScripts[0]==-1) LocalVar[MaxLocalVarNum].bArray=0;
1077 else LocalVar[MaxLocalVarNum].bArray=1;
1078 LocalVar[MaxLocalVarNum].type=TypeInfo.type;
1079 LocalVar[MaxLocalVarNum].u.index=TypeInfo.u.lpIndex;
1080 AllLocalVarSize+=VarSize;
1081 LocalVar[MaxLocalVarNum].offset=AllLocalVarSize;
1082
1083 //レキシカルスコープ
1084 LocalVar[MaxLocalVarNum].ScopeLevel=obj_LexScopes.GetNowLevel();
1085 LocalVar[MaxLocalVarNum].ScopeStartAddress=obj_LexScopes.GetStartAddress();
1086 LocalVar[MaxLocalVarNum].bLiving=TRUE;
1087
1088 if(InitBuf[0]){
1089 InitLocalVar(-LocalVar[MaxLocalVarNum].offset,
1090 LocalVar[MaxLocalVarNum].type,
1091 LocalVar[MaxLocalVarNum].u.index,
1092 LocalVar[MaxLocalVarNum].SubScripts,
1093 InitBuf);
1094 }
1095 else{
1096 //push 0
1097 op_push_value(0);
1098
1099 //push VarSize
1100 op_push_value(VarSize);
1101
1102 //mov eax,ebp
1103 OpBuffer[obp++]=(char)0x8B;
1104 OpBuffer[obp++]=(char)0xC5;
1105
1106 //add eax,offset
1107 OpBuffer[obp++]=(char)0x05;
1108 *((long *)(OpBuffer+obp))=-LocalVar[MaxLocalVarNum].offset;
1109 AddLocalVarAddrSchedule();
1110 obp+=sizeof(long);
1111
1112 //push eax
1113 op_push(REG_EAX);
1114
1115 //call FillMemory
1116 OpBuffer[obp++]=(char)0xFF;
1117 OpBuffer[obp++]=(char)0x15;
1118 DECLAREINFO *pdi;
1119 pdi=GetDeclareHash("FillMemory");
1120 pdi->bUse=1;
1121 pobj_ImportAddrSchedule->add(pdi);
1122 obp+=sizeof(long);
1123 }
1124
1125 MaxLocalVarNum++;
1126 }
1127
1128 //コンストラクタ呼び出し
1129 if(TypeInfo.type==DEF_OBJECT&&(dwFlag&DIMFLAG_NONCALL_CONSTRACTOR)==0){
1130 CallConstractor(VarName,SubScripts,TypeInfo,ConstractParameter);
1131 }
1132 }
1133
1134 if(TypeInfo.type==DEF_OBJECT){
1135 if(TypeInfo.u.pobj_Class->IsHoldAbstractFunction()){
1136 //抽象クラスだったとき
1137 SetError(125,TypeInfo.u.pobj_Class->name,cp);
1138 }
1139 }
1140}
1141void OpcodeDim(char *Parameter,DWORD dwFlag){
1142 int i,i2,i3,IsStr=0;
1143 char temporary[8192];
1144
1145 for(i=0,i2=0;;i++,i2++){
1146 if(Parameter[i]=='\"') IsStr^=1;
1147 if(Parameter[i]=='('&&IsStr==0){
1148 i3=GetStringInPare(temporary+i2,Parameter+i);
1149 i+=i3-1;
1150 i2+=i3-1;
1151 continue;
1152 }
1153 if(Parameter[i]=='['&&IsStr==0){
1154 i3=GetStringInBracket(temporary+i2,Parameter+i);
1155 i+=i3-1;
1156 i2+=i3-1;
1157 continue;
1158 }
1159 if((Parameter[i]==','&&IsStr==0)||
1160 Parameter[i]=='\0'){
1161 temporary[i2]=0;
1162
1163 dim(temporary,dwFlag);
1164
1165 if(Parameter[i]=='\0') break;
1166 i2=-1;
1167 continue;
1168 }
1169 temporary[i2]=Parameter[i];
1170 }
1171}
1172
1173void SetVarPtrToEax(RELATIVE_VAR *pRelativeVar){
1174 if(pRelativeVar->dwKind==VAR_GLOBAL){
1175 if(pRelativeVar->bOffsetOffset){
1176 //lea eax,dword ptr[ecx+offset]
1177 OpBuffer[obp++]=(char)0x8D;
1178 OpBuffer[obp++]=(char)0x81;
1179 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1180 pobj_GlobalVarSchedule->add();
1181 obp+=sizeof(long);
1182 }
1183 else{
1184 //mov eax,offset
1185 OpBuffer[obp++]=(char)0xB8;
1186 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1187 pobj_GlobalVarSchedule->add();
1188 obp+=sizeof(long);
1189 }
1190 }
1191 else if(pRelativeVar->dwKind==VAR_LOCAL){
1192 if(pRelativeVar->bOffsetOffset){
1193 //add ecx,offset
1194 OpBuffer[obp++]=(char)0x81;
1195 OpBuffer[obp++]=(char)0xC1;
1196 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1197 AddLocalVarAddrSchedule();
1198 obp+=sizeof(long);
1199
1200 //lea eax,dword ptr[ebp+ecx]
1201 OpBuffer[obp++]=(char)0x8D;
1202 OpBuffer[obp++]=(char)0x44;
1203 OpBuffer[obp++]=(char)0x0D;
1204 OpBuffer[obp++]=(char)0x00;
1205 }
1206 else{
1207 //lea eax,dword ptr[ebp+offset]
1208 OpBuffer[obp++]=(char)0x8D;
1209 OpBuffer[obp++]=(char)0x85;
1210 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1211 AddLocalVarAddrSchedule();
1212 obp+=sizeof(long);
1213 }
1214 }
1215 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
1216 if(pRelativeVar->bOffsetOffset){
1217 //mov eax,ecx
1218 OpBuffer[obp++]=(char)0x8B;
1219 OpBuffer[obp++]=(char)0xC1;
1220
1221 //add eax,dword ptr[ebp+offset]
1222 OpBuffer[obp++]=(char)0x03;
1223 OpBuffer[obp++]=(char)0x85;
1224 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1225 AddLocalVarAddrSchedule();
1226 obp+=sizeof(long);
1227 }
1228 else{
1229 //mov eax,dword ptr[ebp+offset]
1230 OpBuffer[obp++]=(char)0x8B;
1231 OpBuffer[obp++]=(char)0x85;
1232 *((long *)(OpBuffer+obp))=pRelativeVar->offset;
1233 AddLocalVarAddrSchedule();
1234 obp+=sizeof(long);
1235 }
1236 }
1237 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
1238 //mov eax,ecx
1239 OpBuffer[obp++]=(char)0x8B;
1240 OpBuffer[obp++]=(char)0xC1;
1241 }
1242}
Note: See TracBrowser for help on using the repository browser.