source: dev/trunk/abdev/BasicCompiler64/Compile_Var.cpp@ 225

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