source: dev/BasicCompiler64/Compile_Var.cpp@ 6

Last change on this file since 6 was 3, checked in by dai_9181, 18 years ago
File size: 28.8 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 bError,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(bError) 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(bError) 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(bError) SetError(107,VarName,cp);
221 return 0;
222 }
223 else if(bPrivateAccess==0&&pobj_c->ppobj_Member[i]->dwAccess==ACCESS_PROTECTED){
224 if(bError) SetError(108,VarName,cp);
225 return 0;
226 }
227 }
228
229 *pType=pobj_c->ppobj_Member[i]->TypeInfo.type;
230 *plpNestIndex=pobj_c->ppobj_Member[i]->TypeInfo.u.lpIndex;
231
232 //ポインタ変数の場合
233 if(IsPtrType(*pType)){
234 if(pobj_c->ppobj_Member[i]->SubScripts[0]==-1){
235 lstrcpy(lpPtrOffset,array);
236 array[0]=0;
237 }
238 }
239 else{
240 if(lpPtrOffset[0]){
241 if(bError) SetError(16,member,cp);
242 return 0;
243 }
244 }
245
246 if(offset){
247 //add r11,offset
248 OpBuffer[obp++]=(char)0x49;
249 OpBuffer[obp++]=(char)0x81;
250 OpBuffer[obp++]=(char)0xC3;
251 *((long *)(OpBuffer+obp))=offset;
252 obp+=sizeof(long);
253 }
254
255 if(array[0]){
256 //配列オフセット
257 if(!GetArrayOffset(pobj_c->ppobj_Member[i]->SubScripts,array,*pType,pobj_c->ppobj_Member[i]->TypeInfo.u.lpIndex)){
258 if(bError) SetError(14,member,cp);
259 }
260 }
261 else if(pobj_c->ppobj_Member[i]->SubScripts[0]!=-1){
262 *pType|=FLAG_PTR;
263 }
264
265 if(NestMember[0]){
266 //入れ子構造の場合
267
268 if(*pType==DEF_OBJECT){
269 if(RefType!=DEF_OBJECT){
270 if(bError) SetError(104,member,cp);
271 return 0;
272 }
273 }
274 else if(*pType==DEF_PTR_OBJECT){
275 //構造体ポインタ型メンバ変数
276
277 if(lpPtrOffset[0]){
278 //pObj[n].member
279 if(RefType!=DEF_OBJECT){
280 if(bError) SetError(104,member,cp);
281 return 0;
282 }
283
284 //直接参照に切り替え
285 SetRelativeOffset(pType,*plpNestIndex,pRelativeVar,lpPtrOffset);
286 pRelativeVar->dwKind=VAR_DIRECTMEM;
287
288 lpPtrOffset[0]=0;
289 }
290 else{
291 //pObj->member
292 if(RefType!=DEF_PTR_OBJECT){
293 if(bError) SetError(104,member,cp);
294 return 0;
295 }
296
297 if(pRelativeVar->dwKind==VAR_DIRECTMEM){
298 //mov r11,qword ptr[r11]
299 op_mov_RM(sizeof(_int64),REG_R11,REG_R11,0,MOD_BASE);
300 }
301 else{
302 //直接参照に切り替え
303 SetVarPtrToReg(REG_R12,pRelativeVar);
304 pRelativeVar->dwKind=VAR_DIRECTMEM;
305
306 //mov r11,qword ptr[r12]
307 op_mov_RM(sizeof(_int64),REG_R11,REG_R12,0,MOD_BASE);
308 }
309 }
310 }
311 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2)){
312 //構造体ポインタのポインタ型メンバ変数
313
314 if(lpPtrOffset[0]){
315 //ppObj[n]->member
316 if(RefType!=DEF_PTR_OBJECT){
317 if(bError) SetError(104,member,cp);
318 return 0;
319 }
320
321 //直接参照に切り替え
322 SetRelativeOffset(pType,*plpNestIndex,pRelativeVar,lpPtrOffset);
323 pRelativeVar->dwKind=VAR_DIRECTMEM;
324
325 lpPtrOffset[0]=0;
326
327 //mov r11,qword ptr[r11]
328 op_mov_RM(sizeof(_int64),REG_R11,REG_R11,0,MOD_BASE);
329 }
330 else{
331 if(bError) SetError(104,member,cp);
332 return 0;
333 }
334 }
335
336 if(!GetMemberOffset(bError,
337 pobj_c->ppobj_Member[i]->TypeInfo.u.pobj_Class,
338 NestMember,
339 pType,
340 pRelativeVar,
341 plpNestIndex,
342 0)) return 0;
343 }
344
345 if(lpPtrOffset[0]){
346 SetRelativeOffset(pType,*plpNestIndex,pRelativeVar,lpPtrOffset);
347 pRelativeVar->dwKind=VAR_DIRECTMEM;
348 }
349
350 return 1;
351}
352void GetWithName(char *buffer){
353 extern WITHINFO WithInfo;
354 int i;
355
356 buffer[0]=0;
357 for(i=0;i<WithInfo.num;i++)
358 lstrcat(buffer,WithInfo.ppName[i]);
359}
360
361int LocalVar_ThisPtrOffset;
362void SetThisPtrToReg(int reg){
363 //自身のオブジェクトのThisポインタをregにコピー
364
365 extern VARIABLE *LocalVar;
366 RELATIVE_VAR RelativeVar;
367 RelativeVar.dwKind=VAR_LOCAL;
368 RelativeVar.bOffsetOffset=0;
369 RelativeVar.offset=-LocalVar_ThisPtrOffset;
370
371 SetReg_WholeVariable(DEF_PTR_VOID,&RelativeVar,reg);
372}
373BOOL GetVarOffset(BOOL bError,char *NameBuffer,int *pType,RELATIVE_VAR *pRelativeVar,LONG_PTR *plpIndex,int *pss){
374 extern BOOL bCompilingGlobal;
375 int i,RefType;
376 char variable[VN_SIZE],member[VN_SIZE],VarName[VN_SIZE],array[VN_SIZE],lpPtrOffset[VN_SIZE];
377
378 if(NameBuffer[0]=='.'){
379 GetWithName(variable);
380 lstrcat(variable,NameBuffer);
381 }
382 else lstrcpy(variable,NameBuffer);
383
384 lstrcpy(VarName,variable);
385 GetVarFormatString(VarName,array,lpPtrOffset,member,&RefType);
386
387 LONG_PTR lpIndex;
388 int *pSubScripts;
389
390
391 if(bCompilingGlobal==0){
392 //////////////////
393 // ローカル変数
394 //////////////////
395
396 for(i=MaxLocalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ
397 if(LocalVar[i].bLiving){
398 if(lstrcmp(VarName,LocalVar[i].name)==0) break;
399 }
400 }
401 if(i>=0){
402 //ポインタ変数の場合
403 if(IsPtrType(LocalVar[i].type)){
404 if(LocalVar[i].SubScripts[0]==-1){
405 lstrcpy(lpPtrOffset,array);
406 array[0]=0;
407 }
408 }
409 else{
410 if(lpPtrOffset[0]){
411 SetError(16,variable,cp);
412 pRelativeVar->dwKind=NON_VAR;
413 return 0;
414 }
415 }
416
417 pRelativeVar->offset=-LocalVar[i].offset;
418 pRelativeVar->bOffsetOffset=0;
419 if(LocalVar[i].fRef) pRelativeVar->dwKind=VAR_REFLOCAL;
420 else pRelativeVar->dwKind=VAR_LOCAL;
421 *pType=LocalVar[i].type;
422 lpIndex=LocalVar[i].u.index;
423 if(plpIndex) *plpIndex=lpIndex;
424 pSubScripts=LocalVar[i].SubScripts;
425
426 goto ok;
427 }
428 }
429
430
431 if(pobj_CompilingClass){
432 //////////////////////
433 // クラスメンバの参照
434 //////////////////////
435
436 if(lstrcmpi(variable,"This")==0){
437 //自身のオブジェクトのThisポインタをr11にコピー
438 SetThisPtrToReg(REG_R11);
439
440 *pType=DEF_OBJECT;
441 pRelativeVar->dwKind=VAR_DIRECTMEM;
442
443 if(plpIndex) *plpIndex=(LONG_PTR)pobj_CompilingClass;
444 return 1;
445 }
446
447 if(memicmp(variable,"This.",5)==0){
448 //Thisオブジェクトのメンバを参照するとき
449 SlideString(variable+5,-5);
450 lstrcpy(VarName,variable);
451 }
452 else{
453 //クラス内メンバを参照するとき(通常)
454
455 for(i=0;i<pobj_CompilingClass->iMemberNum;i++){
456 if(lstrcmp(VarName,pobj_CompilingClass->ppobj_Member[i]->name)==0) break;
457 }
458 if(i==pobj_CompilingClass->iMemberNum) goto NonClassMember;
459 }
460
461 //自身のオブジェクトのThisポインタをr11にコピー
462 SetThisPtrToReg(REG_R11);
463
464 pRelativeVar->dwKind=VAR_DIRECTMEM;
465 if(!GetMemberOffset(bError,pobj_CompilingClass,variable,pType,pRelativeVar,&lpIndex,1)) return 0;
466 if(plpIndex) *plpIndex=lpIndex;
467 return 1;
468 }
469
470NonClassMember:
471
472 //////////////////////////
473 // 静的ローカル変数
474 // ※"Static.Object.Method.Variable"
475 //////////////////////////
476
477 char temporary[VN_SIZE];
478 extern SUBINFO *pCompilingSubInfo;
479 if(pCompilingSubInfo){
480 GetNowStaticVarFullName(VarName,temporary);
481
482 for(i=0;i<MaxGlobalVarNum;i++){
483 if(lstrcmp(temporary,GlobalVar[i].name)==0) break;
484 }
485 if(i!=MaxGlobalVarNum){
486 goto GlobalOk;
487 }
488 }
489
490 /////////////////////
491 // グローバル変数
492 /////////////////////
493
494 for(i=MaxGlobalVarNum-1;i>=0;i--){ //レキシカルスコープを考慮してバックサーチ
495 if(GlobalVar[i].bLiving){
496 if(lstrcmp(VarName,GlobalVar[i].name)==0) break;
497 }
498 }
499 if(i>=0){
500 goto GlobalOk;
501 }
502
503
504 //////////////////////////
505 // クラスの静的メンバ
506 //////////////////////////
507
508 if(member[0]){
509 lstrcpy(temporary,member);
510 GetVarFormatString(temporary,array,lpPtrOffset,member,&i);
511
512 char temp2[VN_SIZE];
513 sprintf(temp2,"%s.%s",VarName,temporary);
514 for(i=0;i<MaxGlobalVarNum;i++){
515 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
516 }
517
518 if(i!=MaxGlobalVarNum){
519 goto GlobalOk;
520 }
521 }
522
523 if(pobj_CompilingClass){
524 //自身のクラスから静的メンバを参照する場合
525 char temp2[VN_SIZE];
526 sprintf(temp2,"%s.%s",pobj_CompilingClass->name,VarName);
527 for(i=0;i<MaxGlobalVarNum;i++){
528 if(lstrcmp(temp2,GlobalVar[i].name)==0) break;
529 }
530
531 if(i!=MaxGlobalVarNum){
532 goto GlobalOk;
533 }
534 }
535
536 if(bError) SetError(3,variable,cp);
537 pRelativeVar->dwKind=NON_VAR;
538 return 0;
539
540
541
542GlobalOk:
543 //ポインタ変数の場合
544 if(IsPtrType(GlobalVar[i].type)){
545 if(GlobalVar[i].SubScripts[0]==-1){
546 lstrcpy(lpPtrOffset,array);
547 array[0]=0;
548 }
549 }
550 else{
551 if(lpPtrOffset[0]){
552 SetError(16,variable,cp);
553 pRelativeVar->dwKind=NON_VAR;
554 return 0;
555 }
556 }
557
558 pRelativeVar->offset=GlobalVar[i].offset;
559 pRelativeVar->bOffsetOffset=0;
560 pRelativeVar->dwKind=VAR_GLOBAL;
561 *pType=GlobalVar[i].type;
562 lpIndex=GlobalVar[i].u.index;
563 if(plpIndex) *plpIndex=lpIndex;
564 pSubScripts=GlobalVar[i].SubScripts;
565
566
567
568ok:
569
570
571 if(array[0]==0&&pSubScripts[0]!=-1){
572 //配列の先頭ポインタを示す場合
573 *pType|=FLAG_PTR;
574 if(pss) memcpy(pss,pSubScripts,MAX_ARRAYDIM);
575 return 1;
576 }
577
578 if(array[0]||member[0]){
579 //xor r11,r11(r11を0に初期化する)
580 //※r11は変数ベースアドレスからの相対オフセットを示す
581 op_zero_reg(REG_R11);
582
583 pRelativeVar->bOffsetOffset=1;
584 }
585 if(array[0]){
586 if(!GetArrayOffset(pSubScripts,array,*pType,lpIndex)){
587 SetError(14,variable,cp);
588 pRelativeVar->dwKind=NON_VAR;
589 return 0;
590 }
591 }
592 if(member[0]){
593 if(*pType==DEF_OBJECT){
594 //実態オブジェクトのメンバを参照(obj.member)
595 if(RefType!=DEF_OBJECT){
596 SetError(104,VarName,cp);
597 pRelativeVar->dwKind=NON_VAR;
598 return 0;
599 }
600
601 LONG_PTR lp2;
602 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
603 if(plpIndex) *plpIndex=lp2;
604 }
605 else if(*pType==DEF_PTR_OBJECT){
606 //ポインタオブジェクトが示すメンバを参照
607 if(lpPtrOffset[0]){
608 //pObj[n].member
609 if(RefType!=DEF_OBJECT){
610 SetError(104,VarName,cp);
611 pRelativeVar->dwKind=NON_VAR;
612 return 0;
613 }
614 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
615 pRelativeVar->dwKind=VAR_DIRECTMEM;
616
617 LONG_PTR lp2;
618 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
619 if(plpIndex) *plpIndex=lp2;
620 }
621 else{
622 //pObj->member
623 if(RefType!=DEF_PTR_OBJECT){
624 SetError(104,VarName,cp);
625 pRelativeVar->dwKind=NON_VAR;
626 return 0;
627 }
628
629 SetVarPtrToReg(REG_R12,pRelativeVar);
630 pRelativeVar->dwKind=VAR_DIRECTMEM;
631
632 //mov r11,qword ptr[r12]
633 op_mov_RM(sizeof(_int64),REG_R11,REG_R12,0,MOD_BASE);
634
635 LONG_PTR lp2;
636 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
637 if(plpIndex) *plpIndex=lp2;
638 }
639 }
640 else if(*pType==MAKE_PTR_TYPE(DEF_OBJECT,2)){
641 //ポインタオブジェクトが示すメンバを参照
642 if(lpPtrOffset[0]){
643 //ppObj[n]->member
644 if(RefType!=DEF_PTR_OBJECT){
645 SetError(104,VarName,cp);
646 pRelativeVar->dwKind=NON_VAR;
647 return 0;
648 }
649
650 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
651 pRelativeVar->dwKind=VAR_DIRECTMEM;
652
653
654 SetVarPtrToReg(REG_R12,pRelativeVar);
655
656 //mov r11,qword ptr[r12]
657 op_mov_RM(sizeof(_int64),REG_R11,REG_R12,0,MOD_BASE);
658
659 LONG_PTR lp2;
660 if(!GetMemberOffset(bError,(CClass *)lpIndex,member,pType,pRelativeVar,&lp2,0)) return 0;
661 if(plpIndex) *plpIndex=lp2;
662 }
663 else{
664 SetError(104,VarName,cp);
665 pRelativeVar->dwKind=NON_VAR;
666 return 0;
667 }
668 }
669 else{
670 SetError(102,VarName,cp);
671 pRelativeVar->dwKind=NON_VAR;
672 return 0;
673 }
674 return 1;
675 }
676
677 if(lpPtrOffset[0]){
678 SetRelativeOffset(pType,lpIndex,pRelativeVar,lpPtrOffset);
679 pRelativeVar->dwKind=VAR_DIRECTMEM;
680 }
681
682 return 1;
683}
684
685BOOL SetInitGlobalData(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf){
686 extern BYTE *initGlobalBuf;
687 int i,i2,i3,TypeSize;
688 char temporary[VN_SIZE];
689
690 if(InitBuf[0]=='['){
691 SlideString(InitBuf+1,-1);
692 InitBuf[lstrlen(InitBuf)-1]=0;
693
694 TypeSize=GetTypeSize(type,lpIndex);
695
696 if(SubScripts[0]!=-1){
697 TypeSize*=JumpSubScripts(SubScripts+1);
698 i=0;
699 i2=0;
700 while(1){
701 if(SubScripts[0]<i2){
702 SetError(41,0,cp);
703 return 0;
704 }
705 i=GetOneParameter(InitBuf,i,temporary);
706 if(!SetInitGlobalData(
707 offset+i2*TypeSize,
708 type,
709 lpIndex,
710 SubScripts+1,
711 temporary)) return 0;
712 i2++;
713 if(InitBuf[i]=='\0') break;
714 }
715 return -1;
716 }
717
718 if(type==DEF_OBJECT){
719 CClass *pobj_c;
720 pobj_c=(CClass *)lpIndex;
721
722 for(i=0,i2=0;i2<pobj_c->iMemberNum;i2++){
723 i=GetOneParameter(InitBuf,i,temporary);
724
725 i3=GetSizeOfClassMember(pobj_c,pobj_c->ppobj_Member[i2]->name,NULL);
726
727 if(!SetInitGlobalData(offset+i3,
728 pobj_c->ppobj_Member[i2]->TypeInfo.type,
729 pobj_c->ppobj_Member[i2]->TypeInfo.u.lpIndex,
730 pobj_c->ppobj_Member[i2]->SubScripts,
731 temporary)) return 0;
732
733 if(InitBuf[i]=='\0') break;
734 }
735 if(i2+1!=pobj_c->iMemberNum){
736 SetError(41,0,cp);
737 return 0;
738 }
739 return 1;
740 }
741
742 SetError(41,0,cp);
743 return 0;
744 }
745
746 if(SubScripts[0]!=-1){
747 SetError(41,0,cp);
748 return 0;
749 }
750
751 double dbl;
752 _int64 i64data;
753 int CalcType;
754 LONG_PTR lpCalcIndex;
755
756 CalcType=StaticCalculation(true, InitBuf,type,&i64data,&lpCalcIndex);
757 if(IsRealNumberType(CalcType)){
758 memcpy(&dbl,&i64data,sizeof(double));
759 i64data=(_int64)dbl;
760 }
761 else dbl=(double)i64data;
762
763 //型チェック
764 CheckDifferentType(
765 type,
766 lpIndex,
767 CalcType,
768 lpCalcIndex,
769 0,0);
770
771 if(type==DEF_DOUBLE)
772 *(double *)(initGlobalBuf+offset)=(double)dbl;
773 else if(type==DEF_SINGLE)
774 *(float *)(initGlobalBuf+offset)=(float)dbl;
775 else if(type==DEF_INT64||type==DEF_QWORD||IsPtrType(type)){
776 if(type==DEF_PTR_BYTE&&lpCalcIndex==LITERAL_STRING){
777 //文字列定数のとき
778
779 char *temp;
780 temp=(char *)i64data;
781 i2=AddDataTable(temp,lstrlen(temp));
782 HeapDefaultFree(temp);
783
784 //mov rax,DataPos
785 op_mov_RV(sizeof(_int64),REG_RAX,i2);
786 obp-=sizeof(long);
787 pobj_DataTableSchedule->add();
788 obp+=sizeof(long);
789
790 //mov qword ptr[offset],rax
791 op_mov_MR(sizeof(_int64),REG_RAX,0,offset,MOD_DISP32);
792 obp-=sizeof(long);
793 pobj_GlobalVarSchedule->add();
794 obp+=sizeof(long);
795 }
796 else{
797 *(_int64 *)(initGlobalBuf+offset)=i64data;
798 }
799 }
800 else if(type==DEF_LONG||type==DEF_DWORD)
801 *(DWORD *)(initGlobalBuf+offset)=(DWORD)i64data;
802 else if(type==DEF_INTEGER||type==DEF_WORD)
803 *(WORD *)(initGlobalBuf+offset)=(WORD)i64data;
804 else if(type==DEF_CHAR||type==DEF_BYTE)
805 *(BYTE *)(initGlobalBuf+offset)=(BYTE)i64data;
806
807 return 1;
808}
809BOOL InitLocalVar(int offset,int type,LONG_PTR lpIndex,int *SubScripts,char *InitBuf){
810 int i,i2,i3,TypeSize;
811 char temporary[VN_SIZE];
812
813 if(InitBuf[0]=='['){
814 SlideString(InitBuf+1,-1);
815 InitBuf[lstrlen(InitBuf)-1]=0;
816
817 TypeSize=GetTypeSize(type,lpIndex);
818
819 if(SubScripts[0]!=-1){
820 TypeSize*=JumpSubScripts(SubScripts+1);
821 i=0;
822 i2=0;
823 while(1){
824 if(SubScripts[0]<i2){
825 SetError(41,0,cp);
826 return 0;
827 }
828 i=GetOneParameter(InitBuf,i,temporary);
829 if(!InitLocalVar(
830 offset+i2*TypeSize,
831 type,
832 lpIndex,
833 SubScripts+1,
834 temporary)) return 0;
835 i2++;
836 if(InitBuf[i]=='\0') break;
837 }
838 return -1;
839 }
840
841 if(type==DEF_OBJECT){
842 CClass *pobj_c;
843 pobj_c=(CClass *)lpIndex;
844
845 for(i=0,i2=0;i2<pobj_c->iMemberNum;i2++){
846 i=GetOneParameter(InitBuf,i,temporary);
847
848 i3=GetSizeOfClassMember(pobj_c,pobj_c->ppobj_Member[i2]->name,NULL);
849
850 if(!InitLocalVar(offset+i3,
851 pobj_c->ppobj_Member[i2]->TypeInfo.type,
852 pobj_c->ppobj_Member[i2]->TypeInfo.u.lpIndex,
853 pobj_c->ppobj_Member[i2]->SubScripts,
854 temporary)) return 0;
855
856 if(InitBuf[i]=='\0') break;
857 }
858 if(i2+1!=pobj_c->iMemberNum){
859 SetError(41,0,cp);
860 return 0;
861 }
862 return 1;
863 }
864
865 SetError(41,0,cp);
866 return 0;
867 }
868
869 if(SubScripts[0]!=-1){
870 SetError(41,0,cp);
871 return 0;
872 }
873
874 double dbl;
875 _int64 i64data;
876 int CalcType;
877 LONG_PTR lpCalcIndex;
878 CalcType=StaticCalculation(true, InitBuf,type,&i64data,&lpCalcIndex);
879 if(IsRealNumberType(CalcType)){
880 memcpy(&dbl,&i64data,sizeof(double));
881 i64data=(_int64)dbl;
882 }
883 else dbl=(double)i64data;
884
885 //型チェック
886 CheckDifferentType(
887 type,
888 lpIndex,
889 CalcType,
890 lpCalcIndex,
891 0,0);
892
893 if(type==DEF_DOUBLE){
894 memcpy(&i64data,&dbl,sizeof(double));
895
896 //mov rax,i64data
897 op_mov64_ToReg(REG_RAX,i64data);
898
899 //mov qword ptr[rsp+offset],rax
900 op_mov_MR(sizeof(_int64),REG_RAX,REG_RSP,offset,MOD_BASE_DISP32);
901 obp-=sizeof(long);
902 AddLocalVarAddrSchedule();
903 obp+=sizeof(long);
904 }
905 else if(type==DEF_SINGLE){
906 float flt;
907 flt=(float)dbl;
908
909 //mov dword ptr[rsp+offset],value
910 op_mov_MV(sizeof(long),REG_RSP,offset,USE_OFFSET,*(int *)&flt);
911 obp-=sizeof(long)+sizeof(long);
912 AddLocalVarAddrSchedule();
913 obp+=sizeof(long)+sizeof(long);
914 }
915 else if(type==DEF_INT64||type==DEF_QWORD||IsPtrType(type)){
916 if(type==DEF_PTR_BYTE&&lpCalcIndex==LITERAL_STRING){
917 //文字列定数のとき
918
919 char *temp;
920 temp=(char *)i64data;
921 i2=AddDataTable(temp,lstrlen(temp));
922 HeapDefaultFree(temp);
923
924 //mov rax,i2
925 op_mov_RV(sizeof(_int64),REG_RAX,i2);
926 obp-=sizeof(long);
927 pobj_DataTableSchedule->add();
928 obp+=sizeof(long);
929
930 //mov qword ptr[rsp+offset],rax
931 op_mov_MR(sizeof(_int64),REG_RAX,REG_RSP,offset,MOD_BASE_DISP32);
932 obp-=sizeof(long);
933 AddLocalVarAddrSchedule();
934 obp+=sizeof(long);
935 }
936 else{
937 if(i64data&0xFFFFFFFF00000000){
938 //mov rax,i64data
939 op_mov64_ToReg(REG_RAX,i64data);
940
941 //mov qword ptr[rsp+offset],rax
942 op_mov_MR(sizeof(_int64),REG_RAX,REG_RSP,offset,MOD_BASE_DISP32);
943 obp-=sizeof(long);
944 AddLocalVarAddrSchedule();
945 obp+=sizeof(long);
946 }
947 else{
948 //mov qword ptr[rsp+offset],value
949 op_mov_MV(sizeof(_int64),REG_RSP,offset,USE_OFFSET,(int)i64data);
950 obp-=sizeof(long)+sizeof(long);
951 AddLocalVarAddrSchedule();
952 obp+=sizeof(long)+sizeof(long);
953 }
954 }
955 }
956 else if(type==DEF_LONG||type==DEF_DWORD){
957 //mov dword ptr[rsp+offset],value
958 op_mov_MV(sizeof(long),REG_RSP,offset,USE_OFFSET,(int)i64data);
959 obp-=sizeof(long)+sizeof(long);
960 AddLocalVarAddrSchedule();
961 obp+=sizeof(long)+sizeof(long);
962 }
963 else if(type==DEF_INTEGER||type==DEF_WORD){
964 //mov word ptr[rsp+offset],value
965 op_mov_MV(sizeof(short),REG_RSP,offset,USE_OFFSET,(int)i64data);
966 obp-=sizeof(long)+sizeof(short);
967 AddLocalVarAddrSchedule();
968 obp+=sizeof(long)+sizeof(short);
969 }
970 else if(type==DEF_CHAR||type==DEF_BYTE){
971 //mov byte ptr[rsp+offset],value
972 op_mov_MV(sizeof(char),REG_RSP,offset,USE_OFFSET,(int)i64data);
973 obp-=sizeof(long)+sizeof(char);
974 AddLocalVarAddrSchedule();
975 obp+=sizeof(long)+sizeof(char);
976 }
977 return 1;
978}
979
980
981void dim(char *Parameter,DWORD dwFlag){
982 extern BOOL bCompilingGlobal;
983 extern HANDLE hHeap;
984 int i2,i3,VarSize;
985
986 //構文を解析
987 char VarName[VN_SIZE];
988 int SubScripts[MAX_ARRAYDIM];
989 TYPEINFO TypeInfo;
990 char InitBuf[8192];
991 char ConstractParameter[VN_SIZE];
992 if(!GetDimentionFormat(Parameter,VarName,SubScripts,&TypeInfo,InitBuf,ConstractParameter))
993 return;
994
995 //タイプサイズを取得
996 int TypeSize;
997 TypeSize=GetTypeSize(TypeInfo.type,TypeInfo.u.lpIndex);
998
999 //定数と2重定義されていないかを調べる
1000 if(GetConstHash(VarName)){
1001 SetError(15,VarName,cp);
1002 return;
1003 }
1004
1005 if(dwFlag&DIMFLAG_STATIC){
1006 if(bCompilingGlobal){
1007 SetError(60,NULL,cp);
1008 return;
1009 }
1010
1011 /////////////////////
1012 // Static変数
1013 // ※"Static.Object.Method.Variable"
1014 /////////////////////
1015
1016 char temporary[VN_SIZE];
1017 GetNowStaticVarFullName(VarName,temporary);
1018
1019 AddGlobalVariable(temporary,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1020
1021 /*
1022 Note: 静的変数のコンストラクタ呼び出しは
1023 _System_InitStaticLocalVariables関数内で一括して行う
1024 */
1025 }
1026 else{
1027 if(bCompilingGlobal){
1028 /////////////////////////
1029 // グローバル変数
1030 /////////////////////////
1031
1032 AddGlobalVariable(VarName,SubScripts,&TypeInfo,TypeSize,InitBuf,ConstractParameter,dwFlag);
1033 }
1034 else{
1035 /////////////////
1036 // ローカル変数
1037 /////////////////
1038
1039 for(i2=0;i2<MaxLocalVarNum;i2++){
1040 if(LocalVar[i2].bLiving&&obj_LexScopes.GetNowLevel()==LocalVar[i2].ScopeLevel){
1041 if(lstrcmp(LocalVar[i2].name,VarName)==0){
1042 //2重定義のエラー
1043 SetError(15,VarName,cp);
1044 return;
1045 }
1046 }
1047 }
1048
1049 LocalVar=(VARIABLE *)HeapReAlloc(hHeap,0,LocalVar,(MaxLocalVarNum+1)*sizeof(VARIABLE));
1050
1051 for(i2=1,i3=0;i3<255;i3++){
1052 //配列要素数
1053 LocalVar[MaxLocalVarNum].SubScripts[i3]=SubScripts[i3];
1054
1055 if(SubScripts[i3]==-1) break;
1056 i2*=SubScripts[i3]+1;
1057 }
1058 VarSize=TypeSize*i2;
1059 if(VarSize%8) VarSize+=8-(VarSize%8);
1060
1061 //変数データを追加
1062 lstrcpy(LocalVar[MaxLocalVarNum].name,VarName);
1063 LocalVar[MaxLocalVarNum].fRef=0;
1064 if(SubScripts[0]==-1) LocalVar[MaxLocalVarNum].bArray=0;
1065 else LocalVar[MaxLocalVarNum].bArray=1;
1066 LocalVar[MaxLocalVarNum].type=TypeInfo.type;
1067 LocalVar[MaxLocalVarNum].u.index=TypeInfo.u.lpIndex;
1068 AllLocalVarSize+=VarSize;
1069 LocalVar[MaxLocalVarNum].offset=AllLocalVarSize;
1070
1071 //レキシカルスコープ
1072 LocalVar[MaxLocalVarNum].ScopeLevel=obj_LexScopes.GetNowLevel();
1073 LocalVar[MaxLocalVarNum].ScopeStartAddress=obj_LexScopes.GetStartAddress();
1074 LocalVar[MaxLocalVarNum].bLiving=TRUE;
1075
1076 if(InitBuf[0]){
1077 InitLocalVar(-LocalVar[MaxLocalVarNum].offset,
1078 LocalVar[MaxLocalVarNum].type,
1079 LocalVar[MaxLocalVarNum].u.index,
1080 LocalVar[MaxLocalVarNum].SubScripts,
1081 InitBuf);
1082 }
1083 else{
1084 //0初期化未完成
1085 }
1086
1087 MaxLocalVarNum++;
1088 }
1089
1090 //コンストラクタ呼び出し
1091 if(TypeInfo.type==DEF_OBJECT&&(dwFlag&DIMFLAG_NONCALL_CONSTRACTOR)==0){
1092 CallConstractor(VarName,SubScripts,TypeInfo,ConstractParameter);
1093 }
1094 }
1095
1096 if(TypeInfo.type==DEF_OBJECT){
1097 if(TypeInfo.u.pobj_Class->IsHoldAbstractFunction()){
1098 //抽象クラスだったとき
1099 SetError(125,TypeInfo.u.pobj_Class->name,cp);
1100 }
1101 }
1102}
1103void OpcodeDim(char *Parameter,DWORD dwFlag){
1104 int i,i2,i3,IsStr=0;
1105 char temporary[8192];
1106
1107 for(i=0,i2=0;;i++,i2++){
1108 if(Parameter[i]=='\"') IsStr^=1;
1109 if(Parameter[i]=='('&&IsStr==0){
1110 i3=GetStringInPare(temporary+i2,Parameter+i);
1111 i+=i3-1;
1112 i2+=i3-1;
1113 continue;
1114 }
1115 if(Parameter[i]=='['&&IsStr==0){
1116 i3=GetStringInBracket(temporary+i2,Parameter+i);
1117 i+=i3-1;
1118 i2+=i3-1;
1119 continue;
1120 }
1121 if((Parameter[i]==','&&IsStr==0)||
1122 Parameter[i]=='\0'){
1123 temporary[i2]=0;
1124
1125 dim(temporary,dwFlag);
1126
1127 if(Parameter[i]=='\0') break;
1128 i2=-1;
1129 continue;
1130 }
1131 temporary[i2]=Parameter[i];
1132 }
1133}
1134
1135void SetVarPtrToReg(int reg,RELATIVE_VAR *pRelativeVar){
1136 if(!IsGeneralReg(reg)) SetError(300,NULL,cp);
1137
1138 if(pRelativeVar->dwKind==VAR_GLOBAL){
1139 if(pRelativeVar->bOffsetOffset){
1140 //add r11,offset
1141 OpBuffer[obp++]=(char)0x49;
1142 OpBuffer[obp++]=(char)0x81;
1143 OpBuffer[obp++]=(char)0xC3;
1144 *((long *)(OpBuffer+obp))=(int)pRelativeVar->offset;
1145 pobj_GlobalVarSchedule->add();
1146 obp+=sizeof(long);
1147
1148 //mov reg,r11
1149 op_mov64_ToReg_FromReg(reg,REG_R11);
1150 }
1151 else{
1152 //mov reg,offset
1153 op_mov64_ToReg(reg,(int)pRelativeVar->offset);
1154 obp-=sizeof(long);
1155 pobj_GlobalVarSchedule->add();
1156 obp+=sizeof(long);
1157 }
1158 }
1159 else if(pRelativeVar->dwKind==VAR_LOCAL){
1160 if(pRelativeVar->bOffsetOffset){
1161 //add r11,offset
1162 OpBuffer[obp++]=(char)0x49;
1163 OpBuffer[obp++]=(char)0x81;
1164 OpBuffer[obp++]=(char)0xC3;
1165 *((long *)(OpBuffer+obp))=(int)pRelativeVar->offset;
1166 AddLocalVarAddrSchedule();
1167 obp+=sizeof(long);
1168
1169 //add r11,rsp
1170 op_add64_reg(REG_R11,REG_RSP);
1171
1172 //mov reg,r11
1173 op_mov64_ToReg_FromReg(reg,REG_R11);
1174 }
1175 else{
1176 //mov reg,rsp
1177 op_mov64_ToReg_FromReg(reg,REG_RSP);
1178
1179 //add reg,offset
1180 op_add64_value(reg,(int)pRelativeVar->offset);
1181 obp-=sizeof(long);
1182 AddLocalVarAddrSchedule();
1183 obp+=sizeof(long);
1184 }
1185 }
1186 else if(pRelativeVar->dwKind==VAR_REFLOCAL){
1187 if(pRelativeVar->bOffsetOffset){
1188 //add r11,qword ptr[rsp+offset]
1189 OpBuffer[obp++]=(char)0x4C;
1190 OpBuffer[obp++]=(char)0x03;
1191 OpBuffer[obp++]=(char)0x9C;
1192 OpBuffer[obp++]=(char)0x24;
1193 *((long *)(OpBuffer+obp))=(int)pRelativeVar->offset;
1194 AddLocalVarAddrSchedule();
1195 obp+=sizeof(long);
1196 }
1197 else{
1198 //mov r11,qword ptr[rsp+offset]
1199 op_mov_RM(sizeof(_int64),REG_R11,REG_RSP,(int)pRelativeVar->offset,MOD_BASE_DISP32);
1200 obp-=sizeof(long);
1201 AddLocalVarAddrSchedule();
1202 obp+=sizeof(long);
1203 }
1204
1205 goto directmem;
1206 }
1207 else if(pRelativeVar->dwKind==VAR_DIRECTMEM){
1208directmem:
1209 //mov reg,r11
1210 op_mov64_ToReg_FromReg(reg,REG_R11);
1211 }
1212}
Note: See TracBrowser for help on using the repository browser.