source: dev/BasicCompiler64/Compile_Var.cpp@ 103

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

名前空間機能をグローバル変数、定数と列挙型に適用。
一部、クラスの静的メンバと名前空間の相性が悪いコードが潜んでいるため、要改修

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