source: dev/BasicCompiler64/Compile_Var.cpp@ 17

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

定数メンバ機能を有効にした。

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