source: dev/BasicCompiler64/Compile_Var.cpp@ 19

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

64ビットコンパイラでローカル変数の0初期化に対応。
raxまたはrdxを一時保存レジスタとして利用する際に、Mod演算子、\演算子が正常に動作しないバグを修正。

File size: 31.1 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 //Const修飾子のメソッド内でメンバ書き込みアクセスが発生したとき
476 //(コンストラクタ、デストラクタ内を除く)
477 CMethod *pMethod = pobj_DBClass->GetNowCompilingMethodInfo();
478 if( isWriteAccess &&
479 pMethod->isConst &&
480 pobj_CompilingClass->IsCompilingConstructor() == false &&
481 pobj_CompilingClass->IsCompilingDestructor() == false
482 ){
483 SetError(131, NULL, cp );
484 }
485
486 //自身のオブジェクトのThisポインタをr11にコピー
487 SetThisPtrToReg(REG_R11);
488
489 pRelativeVar->dwKind=VAR_DIRECTMEM;
490 if(!GetMemberOffset(
491 isErrorEnabled,
492 isWriteAccess,
493 pobj_CompilingClass,
494 variable,
495 pType,
496 pRelativeVar,
497 &lpIndex,1)) return 0;
498 if(plpIndex) *plpIndex=lpIndex;
499 return 1;
500 }
501
502NonClassMember:
503
504 //////////////////////////
505 // 静的ローカル変数
506 // ※"Static.Object.Method.Variable"
507 //////////////////////////
508
509 char temporary[VN_SIZE];
510 extern SUBINFO *pCompilingSubInfo;
511 if(pCompilingSubInfo){
512 GetNowStaticVarFullName(VarName,temporary);
513
514 for(i=0;i<MaxGlobalVarNum;i++){
515 if(lstrcmp(temporary,GlobalVar[i].name)==0) break;
516 }
517 if(i!=MaxGlobalVarNum){
518 goto GlobalOk;
519 }
520 }
521
522 /////////////////////
523 // グローバル変数
524 /////////////////////
525
526 for(i=MaxGlobalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ
527 if(GlobalVar[i].bLiving){
528 if(lstrcmp(VarName,GlobalVar[i].name)==0) break;
529 }
530 }
531 if(i>=0){
532 goto GlobalOk;
533 }
534
535
536 //////////////////////////
537 // クラスの静的メンバ
538 //////////////////////////
539
540 if(member[0]){
541 lstrcpy(temporary,member);
542 GetVarFormatString(temporary,array,lpPtrOffset,member,&i);
543
544 char temp2[VN_SIZE];
545 sprintf(temp2,"%s.%s",VarName,temporary);
546 for(i=0;i<MaxGlobalVarNum;i++){
547 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
548 }
549
550 if(i!=MaxGlobalVarNum){
551 goto GlobalOk;
552 }
553 }
554
555 if(pobj_CompilingClass){
556 //自身のクラスから静的メンバを参照する場合
557 char temp2[VN_SIZE];
558 sprintf(temp2,"%s.%s",pobj_CompilingClass->name,VarName);
559 for(i=0;i<MaxGlobalVarNum;i++){
560 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
561 }
562
563 if(i!=MaxGlobalVarNum){
564 goto GlobalOk;
565 }
566 }
567
568 if(isErrorEnabled) SetError(3,variable,cp);
569 pRelativeVar->dwKind=NON_VAR;
570 return 0;
571
572
573
574GlobalOk:
575 //ポインタ変数の場合
576 if(IsPtrType(GlobalVar[i].type)){
577 if(GlobalVar[i].SubScripts[0]==-1){
578 lstrcpy(lpPtrOffset,array);
579 array[0]=0;
580 }
581 }
582 else{
583 if(lpPtrOffset[0]){
584 SetError(16,variable,cp);
585 pRelativeVar->dwKind=NON_VAR;
586 return 0;
587 }
588 }
589
590 pRelativeVar->offset=GlobalVar[i].offset;
591 pRelativeVar->bOffsetOffset=0;
592 pRelativeVar->dwKind=VAR_GLOBAL;
593 *pType=GlobalVar[i].type;
594 lpIndex=GlobalVar[i].u.index;
595 if(plpIndex) *plpIndex=lpIndex;
596 pSubScripts=GlobalVar[i].SubScripts;
597 bConst = GlobalVar[i].bConst;
598
599
600
601ok:
602
603 if( bConst && isWriteAccess ){
604 //Const定義の変数に書き込みアクセスをしようとした場合
605 if( *pType == DEF_OBJECT ){
606 //オブジェクト定数
607 SetError(130, VarName, cp );
608 }
609 else{
610 //一般のConst変数
611 SetError(61,VarName,cp);
612 }
613 }
614
615 if(array[0]==0&&pSubScripts[0]!=-1){
616 //配列の先頭ポインタを示す場合
617 *pType|=FLAG_PTR;
618 if(pss) memcpy(pss,pSubScripts,MAX_ARRAYDIM);
619 return 1;
620 }
621
622 if(array[0]||member[0]){
623 //xor r11,r11(r11を0に初期化する)
624 //※r11は変数ベースアドレスからの相対オフセットを示す
625 op_zero_reg(REG_R11);
626
627 pRelativeVar->bOffsetOffset=1;
628 }
629 if(array[0]){
630 if(!GetArrayOffset(pSubScripts,array,*pType,lpIndex)){
631 SetError(14,variable,cp);
632 pRelativeVar->dwKind=NON_VAR;
633 return 0;
634 }
635 }
636 if(member[0]){
637 if(*pType==DEF_OBJECT){
638 //実態オブジェクトのメンバを参照(obj.member)
639 if(RefType!=DEF_OBJECT){
640 SetError(104,VarName,cp);
641 pRelativeVar->dwKind=NON_VAR;
642 return 0;
643 }
644 }
645 else if(*pType==DEF_PTR_OBJECT){
646 //ポインタオブジェクトが示すメンバを参照
647 if(lpPtrOffset[0]){
648 //pObj[n].member
649 if(RefType!=DEF_OBJECT){
650 SetError(104,VarName,cp);
651 pRelativeVar->dwKind=NON_VAR;
652 return 0;
653 }
654 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
655 pRelativeVar->dwKind=VAR_DIRECTMEM;
656 }
657 else{
658 //pObj->member
659 if(RefType!=DEF_PTR_OBJECT){
660 SetError(104,VarName,cp);
661 pRelativeVar->dwKind=NON_VAR;
662 return 0;
663 }
664
665 SetVarPtrToReg(REG_R12,pRelativeVar);
666 pRelativeVar->dwKind=VAR_DIRECTMEM;
667
668 //mov r11,qword ptr[r12]
669 op_mov_RM(sizeof(_int64),REG_R11,REG_R12,0,MOD_BASE);
670 }
671 }
672 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2)){
673 //ポインタオブジェクトが示すメンバを参照
674 if(lpPtrOffset[0]){
675 //ppObj[n]->member
676 if(RefType!=DEF_PTR_OBJECT){
677 SetError(104,VarName,cp);
678 pRelativeVar->dwKind=NON_VAR;
679 return 0;
680 }
681
682 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
683 pRelativeVar->dwKind=VAR_DIRECTMEM;
684
685
686 SetVarPtrToReg(REG_R12,pRelativeVar);
687
688 //mov r11,qword ptr[r12]
689 op_mov_RM(sizeof(_int64),REG_R11,REG_R12,0,MOD_BASE);
690 }
691 else{
692 SetError(104,VarName,cp);
693 pRelativeVar->dwKind=NON_VAR;
694 return 0;
695 }
696 }
697 else{
698 SetError(102,VarName,cp);
699 pRelativeVar->dwKind=NON_VAR;
700 return 0;
701 }
702
703 LONG_PTR lp2;
704 if(!GetMemberOffset(
705 isErrorEnabled,
706 isWriteAccess,
707 (CClass *)lpIndex,
708 member,pType,pRelativeVar,&lp2,0)) return 0;
709 if(plpIndex) *plpIndex=lp2;
710
711 return 1;
712 }
713
714 if(lpPtrOffset[0]){
715 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
716 pRelativeVar->dwKind=VAR_DIRECTMEM;
717 }
718
719 return 1;
720}
721
722BOOL SetInitGlobalData(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf){
723 extern BYTE *initGlobalBuf;
724 int i,i2,i3,TypeSize;
725 char temporary[VN_SIZE];
726
727 if(InitBuf[0]=='['){
728 SlideString(InitBuf+1,-1);
729 InitBuf[lstrlen(InitBuf)-1]=0;
730
731 TypeSize=GetTypeSize(type,lpIndex);
732
733 if(SubScripts[0]!=-1){
734 TypeSize*=JumpSubScripts(SubScripts+1);
735 i=0;
736 i2=0;
737 while(1){
738 if(SubScripts[0]<i2){
739 SetError(41,0,cp);
740 return 0;
741 }
742 i=GetOneParameter(InitBuf,i,temporary);
743 if(!SetInitGlobalData(
744 offset+i2*TypeSize,
745 type,
746 lpIndex,
747 SubScripts+1,
748 temporary)) return 0;
749 i2++;
750 if(InitBuf[i]=='\0') break;
751 }
752 return -1;
753 }
754
755 if(type==DEF_OBJECT){
756 CClass *pobj_c;
757 pobj_c=(CClass *)lpIndex;
758
759 for(i=0,i2=0;i2<pobj_c->iMemberNum;i2++){
760 i=GetOneParameter(InitBuf,i,temporary);
761
762 i3=GetSizeOfClassMember(pobj_c,pobj_c->ppobj_Member[i2]->name,NULL);
763
764 if(!SetInitGlobalData(offset+i3,
765 pobj_c->ppobj_Member[i2]->TypeInfo.type,
766 pobj_c->ppobj_Member[i2]->TypeInfo.u.lpIndex,
767 pobj_c->ppobj_Member[i2]->SubScripts,
768 temporary)) return 0;
769
770 if(InitBuf[i]=='\0') break;
771 }
772 if(i2+1!=pobj_c->iMemberNum){
773 SetError(41,0,cp);
774 return 0;
775 }
776 return 1;
777 }
778
779 SetError(41,0,cp);
780 return 0;
781 }
782
783 if(SubScripts[0]!=-1){
784 SetError(41,0,cp);
785 return 0;
786 }
787
788 double dbl;
789 _int64 i64data;
790 int CalcType;
791 LONG_PTR lpCalcIndex;
792
793 CalcType=StaticCalculation(false, InitBuf,type,&i64data,&lpCalcIndex);
794 if(!CalcType){
795 //動的データだった場合
796 return 0;
797 }
798 if(IsRealNumberType(CalcType)){
799 memcpy(&dbl,&i64data,sizeof(double));
800 i64data=(_int64)dbl;
801 }
802 else dbl=(double)i64data;
803
804 //型チェック
805 CheckDifferentType(
806 type,
807 lpIndex,
808 CalcType,
809 lpCalcIndex,
810 0,0);
811
812 if(type==DEF_DOUBLE)
813 *(double *)(initGlobalBuf+offset)=(double)dbl;
814 else if(type==DEF_SINGLE)
815 *(float *)(initGlobalBuf+offset)=(float)dbl;
816 else if(type==DEF_INT64||type==DEF_QWORD||IsPtrType(type)){
817 if(type==DEF_PTR_BYTE&&lpCalcIndex==LITERAL_STRING){
818 //文字列定数のとき
819
820 char *temp;
821 temp=(char *)i64data;
822 i2=AddDataTable(temp,lstrlen(temp));
823 HeapDefaultFree(temp);
824
825 //mov rax,DataPos
826 op_mov_RV(sizeof(_int64),REG_RAX,i2);
827 obp-=sizeof(long);
828 pobj_DataTableSchedule->add();
829 obp+=sizeof(long);
830
831 //mov qword ptr[offset],rax
832 op_mov_MR(sizeof(_int64),REG_RAX,0,offset,MOD_DISP32);
833 obp-=sizeof(long);
834 pobj_GlobalVarSchedule->add();
835 obp+=sizeof(long);
836 }
837 else{
838 *(_int64 *)(initGlobalBuf+offset)=i64data;
839 }
840 }
841 else if(type==DEF_LONG||type==DEF_DWORD)
842 *(DWORD *)(initGlobalBuf+offset)=(DWORD)i64data;
843 else if(type==DEF_INTEGER||type==DEF_WORD)
844 *(WORD *)(initGlobalBuf+offset)=(WORD)i64data;
845 else if(type==DEF_CHAR||type==DEF_BYTE)
846 *(BYTE *)(initGlobalBuf+offset)=(BYTE)i64data;
847
848 return 1;
849}
850BOOL InitLocalVar(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf){
851 int i,i2,i3,TypeSize;
852 char temporary[VN_SIZE];
853
854 if(InitBuf[0]=='['){
855 SlideString(InitBuf+1,-1);
856 InitBuf[lstrlen(InitBuf)-1]=0;
857
858 TypeSize=GetTypeSize(type,lpIndex);
859
860 if(SubScripts[0]!=-1){
861 TypeSize*=JumpSubScripts(SubScripts+1);
862 i=0;
863 i2=0;
864 while(1){
865 if(SubScripts[0]<i2){
866 SetError(41,0,cp);
867 return 0;
868 }
869 i=GetOneParameter(InitBuf,i,temporary);
870 if(!InitLocalVar(
871 offset+i2*TypeSize,
872 type,
873 lpIndex,
874 SubScripts+1,
875 temporary)) return 0;
876 i2++;
877 if(InitBuf[i]=='\0') break;
878 }
879 return -1;
880 }
881
882 if(type==DEF_OBJECT){
883 CClass *pobj_c;
884 pobj_c=(CClass *)lpIndex;
885
886 for(i=0,i2=0;i2<pobj_c->iMemberNum;i2++){
887 i=GetOneParameter(InitBuf,i,temporary);
888
889 i3=GetSizeOfClassMember(pobj_c,pobj_c->ppobj_Member[i2]->name,NULL);
890
891 if(!InitLocalVar(offset+i3,
892 pobj_c->ppobj_Member[i2]->TypeInfo.type,
893 pobj_c->ppobj_Member[i2]->TypeInfo.u.lpIndex,
894 pobj_c->ppobj_Member[i2]->SubScripts,
895 temporary)) return 0;
896
897 if(InitBuf[i]=='\0') break;
898 }
899 if(i2+1!=pobj_c->iMemberNum){
900 SetError(41,0,cp);
901 return 0;
902 }
903 return 1;
904 }
905
906 SetError(41,0,cp);
907 return 0;
908 }
909
910 if(SubScripts[0]!=-1){
911 SetError(41,0,cp);
912 return 0;
913 }
914
915 double dbl;
916 _int64 i64data;
917 int CalcType;
918 LONG_PTR lpCalcIndex;
919 CalcType=StaticCalculation(false, InitBuf,type,&i64data,&lpCalcIndex);
920 if(!CalcType){
921 //動的データだった場合
922 return 0;
923 }
924 if(IsRealNumberType(CalcType)){
925 memcpy(&dbl,&i64data,sizeof(double));
926 i64data=(_int64)dbl;
927 }
928 else dbl=(double)i64data;
929
930 //型チェック
931 CheckDifferentType(
932 type,
933 lpIndex,
934 CalcType,
935 lpCalcIndex,
936 0,0);
937
938 if(type==DEF_DOUBLE){
939 memcpy(&i64data,&dbl,sizeof(double));
940
941 //mov rax,i64data
942 op_mov64_ToReg(REG_RAX,i64data);
943
944 //mov qword ptr[rsp+offset],rax
945 op_mov_MR(sizeof(_int64),REG_RAX,REG_RSP,offset,MOD_BASE_DISP32);
946 obp-=sizeof(long);
947 AddLocalVarAddrSchedule();
948 obp+=sizeof(long);
949 }
950 else if(type==DEF_SINGLE){
951 float flt;
952 flt=(float)dbl;
953
954 //mov dword ptr[rsp+offset],value
955 op_mov_MV(sizeof(long),REG_RSP,offset,USE_OFFSET,*(int *)&flt);
956 obp-=sizeof(long)+sizeof(long);
957 AddLocalVarAddrSchedule();
958 obp+=sizeof(long)+sizeof(long);
959 }
960 else if(type==DEF_INT64||type==DEF_QWORD||IsPtrType(type)){
961 if(type==DEF_PTR_BYTE&&lpCalcIndex==LITERAL_STRING){
962 //文字列定数のとき
963
964 char *temp;
965 temp=(char *)i64data;
966 i2=AddDataTable(temp,lstrlen(temp));
967 HeapDefaultFree(temp);
968
969 //mov rax,i2
970 op_mov_RV(sizeof(_int64),REG_RAX,i2);
971 obp-=sizeof(long);
972 pobj_DataTableSchedule->add();
973 obp+=sizeof(long);
974
975 //mov qword ptr[rsp+offset],rax
976 op_mov_MR(sizeof(_int64),REG_RAX,REG_RSP,offset,MOD_BASE_DISP32);
977 obp-=sizeof(long);
978 AddLocalVarAddrSchedule();
979 obp+=sizeof(long);
980 }
981 else{
982 if(i64data&0xFFFFFFFF00000000){
983 //mov rax,i64data
984 op_mov64_ToReg(REG_RAX,i64data);
985
986 //mov qword ptr[rsp+offset],rax
987 op_mov_MR(sizeof(_int64),REG_RAX,REG_RSP,offset,MOD_BASE_DISP32);
988 obp-=sizeof(long);
989 AddLocalVarAddrSchedule();
990 obp+=sizeof(long);
991 }
992 else{
993 //mov qword ptr[rsp+offset],value
994 op_mov_MV(sizeof(_int64),REG_RSP,offset,USE_OFFSET,(int)i64data);
995 obp-=sizeof(long)+sizeof(long);
996 AddLocalVarAddrSchedule();
997 obp+=sizeof(long)+sizeof(long);
998 }
999 }
1000 }
1001 else if(type==DEF_LONG||type==DEF_DWORD){
1002 //mov dword ptr[rsp+offset],value
1003 op_mov_MV(sizeof(long),REG_RSP,offset,USE_OFFSET,(int)i64data);
1004 obp-=sizeof(long)+sizeof(long);
1005 AddLocalVarAddrSchedule();
1006 obp+=sizeof(long)+sizeof(long);
1007 }
1008 else if(type==DEF_INTEGER||type==DEF_WORD){
1009 //mov word ptr[rsp+offset],value
1010 op_mov_MV(sizeof(short),REG_RSP,offset,USE_OFFSET,(int)i64data);
1011 obp-=sizeof(long)+sizeof(short);
1012 AddLocalVarAddrSchedule();
1013 obp+=sizeof(long)+sizeof(short);
1014 }
1015 else if(type==DEF_CHAR||type==DEF_BYTE){
1016 //mov byte ptr[rsp+offset],value
1017 op_mov_MV(sizeof(char),REG_RSP,offset,USE_OFFSET,(int)i64data);
1018 obp-=sizeof(long)+sizeof(char);
1019 AddLocalVarAddrSchedule();
1020 obp+=sizeof(long)+sizeof(char);
1021 }
1022 return 1;
1023}
1024
1025
1026void dim(char *Parameter,DWORD dwFlag){
1027 extern BOOL bCompilingGlobal;
1028 extern HANDLE hHeap;
1029 int i2,i3,VarSize;
1030 char VarName[VN_SIZE];
1031
1032
1033 if(dwFlag & DIMFLAG_CONST){
1034 //////////////////////////////////
1035 // 定数変数の場合を考慮
1036 //////////////////////////////////
1037 for(i2=0;;i2++){
1038 if(Parameter[i2] == '=' ||
1039 Parameter[i2] == 1 && Parameter[i2] == ESC_AS ||
1040 Parameter[i2] =='('){
1041 VarName[i2] = 0;
1042 break;
1043 }
1044 VarName[i2] = Parameter[i2];
1045 }
1046
1047 //定数と2重定義されていないる場合は抜け出す
1048 if(CDBConst::obj.GetType(VarName)){
1049 return;
1050 }
1051
1052 //定数マクロとして定義されている場合は抜け出す
1053 if(GetConstHash(VarName)){
1054 return;
1055 }
1056 }
1057
1058
1059 //構文を解析
1060 int SubScripts[MAX_ARRAYDIM];
1061 TYPEINFO TypeInfo;
1062 char InitBuf[8192];
1063 char ConstractParameter[VN_SIZE];
1064 if(!GetDimentionFormat(Parameter,VarName,SubScripts,&TypeInfo,InitBuf,ConstractParameter))
1065 return;
1066
1067
1068 //定数と2重定義されていないかを調べる
1069 if(CDBConst::obj.GetType(VarName)){
1070 SetError(15,VarName,cp);
1071 return;
1072 }
1073
1074 //定数マクロとして定義されている場合
1075 if(GetConstHash(VarName)){
1076 SetError(15,VarName,cp);
1077 return;
1078 }
1079
1080
1081 //タイプサイズを取得
1082 int TypeSize;
1083 TypeSize=GetTypeSize(TypeInfo.type,TypeInfo.u.lpIndex);
1084
1085 if(dwFlag&DIMFLAG_STATIC){
1086 if(bCompilingGlobal){
1087 SetError(60,NULL,cp);
1088 return;
1089 }
1090
1091 /////////////////////
1092 // Static変数
1093 // ※"Static.Object.Method.Variable"
1094 /////////////////////
1095
1096 char temporary[VN_SIZE];
1097 GetNowStaticVarFullName(VarName,temporary);
1098
1099 AddGlobalVariable(temporary,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1100
1101 /*
1102 Note: 静的変数のコンストラクタ呼び出しは
1103 _System_InitStaticLocalVariables関数内で一括して行う
1104 */
1105 }
1106 else{
1107 if(bCompilingGlobal){
1108 /////////////////////////
1109 // グローバル変数
1110 /////////////////////////
1111
1112 AddGlobalVariable(VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1113 }
1114 else{
1115 /////////////////
1116 // ローカル変数
1117 /////////////////
1118
1119 for(i2=0;i2<MaxLocalVarNum;i2++){
1120 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){
1121 if(lstrcmp(LocalVar[i2].name,VarName)==0){
1122 //2重定義のエラー
1123 SetError(15,VarName,cp);
1124 return;
1125 }
1126 }
1127 }
1128
1129 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE));
1130
1131 for(i2=1,i3=0;i3<255;i3++){
1132 //配列要素数
1133 LocalVar[MaxLocalVarNum].SubScripts[i3]=SubScripts[i3];
1134
1135 if(SubScripts[i3]==-1) break;
1136 i2*=SubScripts[i3]+1;
1137 }
1138 VarSize=TypeSize*i2;
1139 if(VarSize%8) VarSize+=8-(VarSize%8);
1140
1141 VARIABLE *pVar = &LocalVar[MaxLocalVarNum];
1142
1143 MaxLocalVarNum++;
1144
1145 //変数データを追加
1146 lstrcpy(pVar->name,VarName);
1147 pVar->fRef=0;
1148 if(dwFlag & DIMFLAG_CONST) pVar->bConst = true;
1149 else pVar->bConst = false;
1150 if(SubScripts[0]==-1) pVar->bArray=0;
1151 else pVar->bArray=1;
1152 pVar->type=TypeInfo.type;
1153 pVar->u.index=TypeInfo.u.lpIndex;
1154 AllLocalVarSize+=VarSize;
1155 pVar->offset=AllLocalVarSize;
1156
1157 //レキシカルスコープ
1158 pVar->ScopeLevel=obj_LexScopes.GetNowLevel();
1159 pVar->ScopeStartAddress=obj_LexScopes.GetStartAddress();
1160 pVar->bLiving=TRUE;
1161
1162 if(InitBuf[0]){
1163 //初期代入時のみ、書き込みアクセスを許可する
1164 bool bConstBack = pVar->bConst;
1165 pVar->bConst = false;
1166
1167 int result = InitLocalVar(-pVar->offset,
1168 pVar->type,
1169 pVar->u.index,
1170 pVar->SubScripts,
1171 InitBuf);
1172
1173 if(!result){
1174 //動的な式だった場合は代入演算を行う
1175 char temporary[8192];
1176 sprintf(temporary,"%s=%s",VarName,InitBuf);
1177 OpcodeCalc(temporary);
1178 }
1179
1180 pVar->bConst = bConstBack;
1181 }
1182 else{
1183 //0初期化
1184
1185 //mov r8, 0
1186 op_zero_reg( REG_R8 );
1187
1188 //mov rdx, VarSize
1189 op_mov_RV( sizeof(_int64), REG_RDX, VarSize );
1190
1191 //mov rcx, rsp
1192 op_mov_RR( REG_RCX, REG_RSP );
1193
1194 //add rcx, offset
1195 op_add64_value( REG_RCX, -pVar->offset );
1196 obp-=sizeof(long);
1197 AddLocalVarAddrSchedule();
1198 obp+=sizeof(long);
1199
1200 //call FillMemory
1201 DECLAREINFO *pdi;
1202 pdi=GetDeclareHash("FillMemory");
1203 op_call( pdi );
1204 }
1205 }
1206
1207 //コンストラクタ呼び出し
1208 if(TypeInfo.type==DEF_OBJECT&&(dwFlag&DIMFLAG_NONCALL_CONSTRACTOR)==0){
1209 CallConstractor(VarName,SubScripts,TypeInfo,ConstractParameter);
1210 }
1211 }
1212
1213 if(TypeInfo.type==DEF_OBJECT){
1214 if(TypeInfo.u.pobj_Class->IsHoldAbstractFunction()){
1215 //抽象クラスだったとき
1216 SetError(125,TypeInfo.u.pobj_Class->name,cp);
1217 }
1218 }
1219}
1220void OpcodeDim(char *Parameter,DWORD dwFlag){
1221 int i,i2,i3,IsStr=0;
1222 char temporary[8192];
1223
1224 for(i=0,i2=0;;i++,i2++){
1225 if(Parameter[i]=='\"') IsStr^=1;
1226 if(Parameter[i]=='('&&IsStr==0){
1227 i3=GetStringInPare(temporary+i2,Parameter+i);
1228 i+=i3-1;
1229 i2+=i3-1;
1230 continue;
1231 }
1232 if(Parameter[i]=='['&&IsStr==0){
1233 i3=GetStringInBracket(temporary+i2,Parameter+i);
1234 i+=i3-1;
1235 i2+=i3-1;
1236 continue;
1237 }
1238 if((Parameter[i]==','&&IsStr==0)||
1239 Parameter[i]=='\0'){
1240 temporary[i2]=0;
1241
1242 dim(temporary,dwFlag);
1243
1244 if(Parameter[i]=='\0') break;
1245 i2=-1;
1246 continue;
1247 }
1248 temporary[i2]=Parameter[i];
1249 }
1250}
1251
1252void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar){
1253 if(!IsGeneralReg(reg)) SetError(300,NULL,cp);
1254
1255 if(pRelativeVar->dwKind==VAR_GLOBAL){
1256 if(pRelativeVar->bOffsetOffset){
1257 //add r11,offset
1258 OpBuffer[obp++]=(char)0x49;
1259 OpBuffer[obp++]=(char)0x81;
1260 OpBuffer[obp++]=(char)0xC3;
1261 *((long *)(OpBuffer+obp))=(int)pRelativeVar->offset;
1262 pobj_GlobalVarSchedule->add();
1263 obp+=sizeof(long);
1264
1265 //mov reg,r11
1266 op_mov64_ToReg_FromReg(reg,REG_R11);
1267 }
1268 else{
1269 //mov reg,offset
1270 op_mov64_ToReg(reg,(int)pRelativeVar->offset);
1271 obp-=sizeof(long);
1272 pobj_GlobalVarSchedule->add();
1273 obp+=sizeof(long);
1274 }
1275 }
1276 else if(pRelativeVar->dwKind==VAR_LOCAL){
1277 if(pRelativeVar->bOffsetOffset){
1278 //add r11,offset
1279 OpBuffer[obp++]=(char)0x49;
1280 OpBuffer[obp++]=(char)0x81;
1281 OpBuffer[obp++]=(char)0xC3;
1282 *((long *)(OpBuffer+obp))=(int)pRelativeVar->offset;
1283 AddLocalVarAddrSchedule();
1284 obp+=sizeof(long);
1285
1286 //add r11,rsp
1287 op_add64_reg(REG_R11,REG_RSP);
1288
1289 //mov reg,r11
1290 op_mov64_ToReg_FromReg(reg,REG_R11);
1291 }
1292 else{
1293 //mov reg,rsp
1294 op_mov64_ToReg_FromReg(reg,REG_RSP);
1295
1296 //add reg,offset
1297 op_add64_value(reg,(int)pRelativeVar->offset);
1298 obp-=sizeof(long);
1299 AddLocalVarAddrSchedule();
1300 obp+=sizeof(long);
1301 }
1302 }
1303 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
1304 if(pRelativeVar->bOffsetOffset){
1305 //add r11,qword ptr[rsp+offset]
1306 OpBuffer[obp++]=(char)0x4C;
1307 OpBuffer[obp++]=(char)0x03;
1308 OpBuffer[obp++]=(char)0x9C;
1309 OpBuffer[obp++]=(char)0x24;
1310 *((long *)(OpBuffer+obp))=(int)pRelativeVar->offset;
1311 AddLocalVarAddrSchedule();
1312 obp+=sizeof(long);
1313 }
1314 else{
1315 //mov r11,qword ptr[rsp+offset]
1316 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
1317 obp-=sizeof(long);
1318 AddLocalVarAddrSchedule();
1319 obp+=sizeof(long);
1320 }
1321
1322 goto directmem;
1323 }
1324 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
1325directmem:
1326 //mov reg,r11
1327 op_mov64_ToReg_FromReg(reg,REG_R11);
1328 }
1329}
Note: See TracBrowser for help on using the repository browser.