source: dev/trunk/ab5.0/abdev/compiler_x64/NumOpe.cpp@ 584

Last change on this file since 584 was 584, checked in by dai_9181, 16 years ago

[530][583]を64bit版にマージ。

File size: 37.2 KB
Line 
1#include "stdafx.h"
2
3#include <Compiler.h>
4
5#include "../BasicCompiler_Common/common.h"
6#include "Opcode.h"
7
8void NewStringObject(int reg, const char *str){
9 ///////////////////////////////////////////////////////
10 // lpszTextを元にStringオブジェクトを生成し、
11 // オブジェクトポインタをregに格納する
12 ///////////////////////////////////////////////////////
13
14
15 //////////////////////////////////////////////////////
16 ///// レジスタ資源のバックアップ
17 { BACKUP_REGISTER_RESOURCE
18 //////////////////////////////////////////////////////
19
20 char *parameter = (char *)malloc( lstrlen( str ) + 32 );
21 sprintf( parameter, "%s%c%c*Char", str, 1, ESC_AS );
22
23 Operator_New( *compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr(), "", parameter, Type( DEF_OBJECT, *compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr() ) );
24
25 free( parameter );
26
27 //mov reg,rax
28 compiler.codeGenerator.op_mov_RR( reg, REG_RAX );
29
30 /////////////////////////////////////////////
31 ////// レジスタ資源を復元
32 RESTORE_REGISTER_RESOURCE
33 }////////////////////////////////////////////
34}
35
36void SetUseRegFromRax(int type,int UseReg,int XmmReg){
37 if(IsRealNumberType(type)){
38 //実数型
39 if(XmmReg==REG_XMM4){
40 if(type==DEF_DOUBLE){
41 //movsd qword ptr[rsp+offset],xmm0 ※スタックフレームを利用
42 pobj_sf->push(REG_XMM0,sizeof(double));
43 }
44 if(type==DEF_SINGLE){
45 //movss dword ptr[rsp+offset],xmm0 ※スタックフレームを利用
46 pobj_sf->push(REG_XMM0,sizeof(float));
47 }
48 }
49 else{
50 if(type==DEF_DOUBLE){
51 //movsd xmm_reg,xmm0
52 compiler.codeGenerator.op_movsd_RR(XmmReg,REG_XMM0);
53 }
54 else if(type==DEF_SINGLE){
55 //movss xmm_reg,xmm0
56 compiler.codeGenerator.op_movss_RR(XmmReg,REG_XMM0);
57 }
58 }
59 }
60 else{
61 //整数型
62 if(UseReg==REG_R14){
63 //mov qword ptr[rsp+offset],rax ※スタックフレームを利用
64 pobj_sf->push(REG_RAX);
65 }
66 else{
67 //mov reg,rax
68 compiler.codeGenerator.op_mov_RR(UseReg,REG_RAX);
69 }
70 }
71}
72
73void ExtendRegToBigType( int reg, int bigBasicType, int baseBasicType ){
74 switch( Type::GetBasicSize( bigBasicType ) ){
75 case sizeof(_int64):
76 ExtendTypeTo64(baseBasicType,reg);
77 break;
78 case sizeof(long):
79 ExtendTypeTo32(baseBasicType,reg);
80 break;
81 case sizeof(short):
82 ExtendTypeTo16(baseBasicType,reg);
83 break;
84 }
85}
86
87
88bool VarToReg( RELATIVE_VAR &relativeVar, const Type &baseType, Type &resultType ){
89 int UseReg=pobj_reg->GetNextReg();
90 int XmmReg=pobj_reg->GetNextXmmReg();
91
92 //大きな型への暗黙の変換
93 int bigType = AutoBigCast(baseType.GetBasicType(),resultType.GetBasicType());
94
95 if(resultType.GetBasicType()&FLAG_PTR){
96 //配列ポインタ
97 resultType.SetBasicType( GetPtrType(resultType.GetBasicType()^FLAG_PTR) );
98
99 SetVarPtrToReg(UseReg,&relativeVar);
100 }
101 else if(resultType.IsReal()){
102 //実数型
103 if( resultType.IsDouble() )
104 SetXmmReg_DoubleVariable(&relativeVar,XmmReg);
105 if( resultType.IsSingle() )
106 SetXmmReg_SingleVariable(&relativeVar,XmmReg);
107 }
108 else if( resultType.IsWhole() || resultType.IsObject()){
109 //整数型
110 SetReg_WholeVariable(resultType,&relativeVar,UseReg);
111 }
112 else if( resultType.IsStruct() ){
113 //構造体ポインタをUseRegへ格納(構造体は値型)
114 SetVarPtrToReg(UseReg,&relativeVar);
115 }
116 else{
117 return false;
118 }
119
120 if( resultType.GetBasicType() != bigType ){
121 // 大きな型へ変換された場合
122 // ※レジスタの値をキャストする
123 ExtendRegToBigType( UseReg, bigType, resultType.GetBasicType() );
124
125 resultType.SetBasicType( bigType );
126 }
127
128 return true;
129}
130bool TermMemberOpe( const Type &leftType, bool &isNeedHeapFreeStructure, const Type &baseType, Type &resultType, const char *termFull, const char *termLeft, const char *member, bool &isVariable, RELATIVE_VAR &relativeVar )
131{
132 const CClass &objClass = leftType.GetClass();
133
134 const int useReg=pobj_reg->GetNextReg();
135 const int xmmReg=pobj_reg->GetNextXmmReg();
136
137
138 ////////////////////////////////
139 // インデクサ(getアクセサ)
140 ////////////////////////////////
141 char VarName[VN_SIZE],ArrayElements[VN_SIZE];
142 GetArrayElement(member,VarName,ArrayElements);
143 if(ArrayElements[0]){
144 Type classType;
145 if( VarName[0] == '\0' )
146 {
147 classType = leftType;
148
149 if( classType.IsObject() )
150 {
151 // 既にuseRegにオブジェクトポインタが格納されており、それに対するインデクサを呼び出す場合
152 // ※「プロパティ値として返ってきたオブジェクトインスタンスのインデクサを呼び出す」場合にここにくる
153
154 // オブジェクトメンバのポインタは既にraxに入っている
155 }
156 }
157 else
158 {
159 GetMemberType( leftType, VarName, classType, 0, false );
160
161 if( classType.IsObject() )
162 {
163 //オブジェクトポインタをr11にコピー
164 compiler.codeGenerator.op_mov_RR( REG_R11, useReg );
165
166 RELATIVE_VAR tempRelativeVar;
167 tempRelativeVar.dwKind=VAR_DIRECTMEM;
168
169 if( !_member_offset(
170 true, //エラー表示あり
171 false, //読み込み専用
172 leftType,
173 VarName,&tempRelativeVar,classType,0)){
174 return false;
175 }
176
177 // オブジェクトメンバのポインタをraxにコピー
178 if( !VarToReg( tempRelativeVar, baseType, resultType ) ){
179 compiler.errorMessenger.Output(11,termFull,cp);
180 }
181 }
182 }
183
184 if( classType.IsObject() )
185 {
186 //////////////////////////////////////////////////////
187 ///// レジスタ資源のバックアップ
188 { BACKUP_REGISTER_RESOURCE
189 //////////////////////////////////////////////////////
190
191 //オブジェクトポインタをスタックに入れておく
192 //mov qword ptr[rsp+offset],reg ※スタックフレームを利用
193 pobj_sf->push( useReg );
194
195 char objectFullName[VN_SIZE], dummyArrayElements[VN_SIZE];
196 GetArrayElement(termFull,objectFullName,dummyArrayElements);
197
198 CallIndexerGetterProc(useReg,classType,objectFullName, ArrayElements,resultType, PROCFLAG_NEW );
199
200 pobj_sf->pop();
201
202 /////////////////////////////////////////////
203 ////// レジスタ資源を復元
204 RESTORE_REGISTER_RESOURCE
205 }////////////////////////////////////////////
206
207 return true;
208 }
209 }
210
211
212 ///////////////////////////////////////////////////////////////////
213 // メンバを検索
214 ///////////////////////////////////////////////////////////////////
215 if( GetMemberType( leftType, member, resultType, 0, false ) ){
216 // メンバが見つかったとき
217
218 if( isNeedHeapFreeStructure )
219 {
220 if( !leftType.IsStruct() )
221 {
222 compiler.errorMessenger.OutputFatalError();
223 }
224
225 pobj_reg->LockReg();
226
227 // 親となる構造体が一時メモリに存在していた場合、後ほど解放する必要がある
228 compiler.codeGenerator.op_AddNeedFreeTempStructure( useReg );
229 isNeedHeapFreeStructure = false;
230
231 pobj_reg->UnlockReg();
232 }
233
234 //オブジェクトポインタをr11にコピー
235 compiler.codeGenerator.op_mov_RR( REG_R11, useReg );
236
237 relativeVar.dwKind=VAR_DIRECTMEM;
238
239 if( !_member_offset(
240 true, //エラー表示あり
241 false, //読み込み専用
242 leftType,
243 member,&relativeVar,resultType,0)){
244 return false;
245 }
246
247 // 変数として扱う
248 isVariable = true;
249
250 return true;
251 }
252
253
254 ///////////////////////////////////////////////////////////////////
255 // 動的メソッドを検索
256 ///////////////////////////////////////////////////////////////////
257 std::vector<const UserProc *> userProcs;
258
259 char methodName[VN_SIZE], lpPtrOffset[VN_SIZE], parameter[VN_SIZE], dummy[1];
260 ReferenceKind refType;
261 PareOrBracket pareOrBracket = None;
262 lstrcpy( methodName, member );
263 GetVarFormatString( methodName, parameter, lpPtrOffset, dummy, refType, &pareOrBracket );
264
265 objClass.EnumDynamicMethodsOrInterfaceMethods( methodName, userProcs );
266 if(userProcs.size()){
267 //オーバーロードを解決
268 const UserProc *pUserProc = OverloadSolutionWithStrParam(termFull,userProcs,parameter,termLeft);
269
270 if( pUserProc )
271 {
272 if(
273 pUserProc->Params().size() == 0 // 仮引数の個数は0
274 && parameter[0] // 実引数は1つ以上
275 && pUserProc->ReturnType().IsObject() // 戻り値がクラス型の場合
276 && pareOrBracket == Bracket ) // 実引数は[]で囲まれている
277 {
278 // プロパティ値として返ってきたオブジェクトインスタンスのインデクサを呼び出す
279
280 // まずはプロパティ値を取得
281 bool dummyIsVariable;
282 RELATIVE_VAR dummyRelativeVar;
283 TermMemberOpe( leftType, isNeedHeapFreeStructure, baseType, resultType, termFull, termLeft, methodName, dummyIsVariable, dummyRelativeVar );
284
285 // 戻り値のオブジェクトインスタンスのインデクサを呼び出す
286 char temporary[VN_SIZE], temp2[VN_SIZE];
287 sprintf( temporary, "[%s]", parameter );
288 sprintf( temp2, "%s.%s", termLeft, methodName );
289 Type classType = resultType;
290 return TermMemberOpe( classType, isNeedHeapFreeStructure, baseType, resultType, termFull, temp2, temporary, isVariable, relativeVar );
291 }
292
293 resultType = pUserProc->ReturnType();
294
295
296 //////////////////////////////////////////////////////
297 ///// レジスタ資源のバックアップ
298 { BACKUP_REGISTER_RESOURCE
299 //////////////////////////////////////////////////////
300
301 //オブジェクトポインタをスタックに入れておく
302 //mov qword ptr[rsp+offset],reg ※スタックフレームを利用
303 pobj_sf->push( useReg );
304
305 if( !Opcode_CallProc(parameter,pUserProc,PROCFLAG_NEW,termLeft ) ){
306 //レジスタ資源を復元
307 RESTORE_REGISTER_RESOURCE
308
309 return false;
310 }
311
312 pobj_sf->pop();
313
314 /////////////////////
315 // 戻り値の処理
316 /////////////////////
317
318 //大きな型への暗黙の変換
319 int bigType = AutoBigCast(baseType.GetBasicType(), resultType.GetBasicType() );
320
321 if( resultType.GetBasicType() != bigType ){
322 // 大きな型へ変換された場合
323 // ※レジスタの値をキャストする
324 ExtendRegToBigType( REG_RAX, bigType, resultType.GetBasicType() );
325
326 resultType.SetBasicType( bigType );
327 }
328
329 SetUseRegFromRax(resultType.GetBasicType(),useReg,xmmReg);
330
331 // 型パラメータを解決
332 ResolveFormalGenericTypeParameter( resultType, leftType, pUserProc );
333
334
335 /////////////////////////////////////////////
336 ////// レジスタ資源を復元
337 RESTORE_REGISTER_RESOURCE
338 }////////////////////////////////////////////
339
340 return true;
341 }
342 }
343 else if( pareOrBracket == Pare )
344 {
345 // 関数ポインタ
346 compiler.errorMessenger.OutputFatalError();
347
348 ///////////////////////////////////////////////////////////////////
349 // メンバを検索
350 ///////////////////////////////////////////////////////////////////
351 if( GetMemberType( leftType, methodName, resultType, 0, false ) ){
352 // メンバが見つかったとき
353 }
354 }
355
356 compiler.errorMessenger.OutputFatalError();
357
358 return false;
359}
360bool _TermOpe( const char *term, const Type &baseType, Type &resultType, bool &isLiteral, bool &isNeedHeapFreeStructure, bool *pIsClassName, bool isProcedureCallOnly, bool &isVariable, RELATIVE_VAR &relativeVar, bool isWriteAccess )
361{
362 char parameter[VN_SIZE];
363
364 // Withを解決
365 char termFull[VN_SIZE];
366 if(term[0]=='.'){
367 GetWithName(termFull);
368 lstrcat(termFull,term);
369 }
370 else lstrcpy(termFull,term);
371
372 char termLeft[VN_SIZE];
373 lstrcpy(termLeft,termFull);
374
375 // パース
376 char member[VN_SIZE];
377 ReferenceKind refType;
378 if( SplitMemberName( termFull, termLeft, member, refType ) ){
379 ///////////////////////////////////////////////////////////////////
380 // オブジェクトとメンバに分解できるとき
381 // termLeft.member
382 ///////////////////////////////////////////////////////////////////
383
384 isLiteral = false;
385
386 // オブジェクト側の型を取得
387 bool isClassName = false;
388 Type leftType;
389 if( GetTermType( termLeft, Type(), leftType, isLiteral, &isClassName ) ){
390 if( isClassName == false && compiler.GetObjectModule().meta.GetBlittableTypes().IsExist( leftType ) ){
391 // 左側のオブジェクト部分がBlittable型のとき
392
393 char temporary[VN_SIZE];
394 lstrcpy( temporary, termLeft );
395 sprintf( termLeft, "%s(%s)",
396 compiler.GetObjectModule().meta.GetBlittableTypes().Find( leftType ).GetCreateStaticMethodFullName().c_str(),
397 temporary );
398 }
399 }
400
401 if( !TermOpe( termLeft, baseType, leftType, isLiteral, isNeedHeapFreeStructure, &isClassName ) ){
402 goto globalArea;
403 }
404
405 if( isClassName ){
406 // 静的メンバ/メソッドの場合
407 goto globalArea;
408 }
409
410 if( !leftType.HasMember() ){
411 // メンバを持たない型の場合
412 if( isProcedureCallOnly )
413 {
414 compiler.errorMessenger.Output(1,NULL,cp);
415 }
416 return false;
417 }
418
419 return TermMemberOpe( leftType, isNeedHeapFreeStructure, baseType, resultType, termFull, termLeft, member, isVariable, relativeVar );
420 }
421globalArea:
422
423 //////////////////////////////////////////////
424 // クラス名かどうかをチェック(静的メンバ用)
425 //////////////////////////////////////////////
426
427 if( pIsClassName ){
428 if( compiler.GetObjectModule().meta.FindClassSupportedTypeDef( termFull ) ){
429 *pIsClassName = true;
430 return true;
431 }
432 }
433
434
435 /////////////////////////////////////////////////////////////////
436 // グローバル属性エリア
437 /////////////////////////////////////////////////////////////////
438
439 int UseReg=pobj_reg->GetNextReg();
440 int XmmReg=pobj_reg->GetNextXmmReg();
441
442
443 if(lstrcmpi(termFull,"This")==0 && isProcedureCallOnly == false ){
444 if( !compiler.IsCompilingClass() )
445 {
446 compiler.errorMessenger.Output(142,NULL,cp);
447 return false;
448 }
449
450 //Thisオブジェクト
451 resultType.SetType( DEF_OBJECT, &compiler.GetCompilingClass() );
452
453 SetThisPtrToReg( UseReg );
454
455 isLiteral = false;
456
457 return true;
458 }
459
460
461 //////////////////////////////////////
462 // 関数(DLL、ユーザー定義、組み込み)
463 //////////////////////////////////////
464 char procName[VN_SIZE];
465 char temporary[8192];
466
467 int i2=GetCallProcName(termFull,procName);
468 if(termFull[i2]=='('){
469 int i4=GetStringInPare_RemovePare(parameter,termFull+i2+1);
470
471 void *pInfo;
472 int idProc=GetProc(procName,(void **)&pInfo);
473
474 if(idProc){
475 //閉じカッコ")"に続く文字がNULLでないとき
476 if(termFull[i2+1+i4+1]!='\0'){
477 compiler.errorMessenger.Output(42,NULL,cp);
478 }
479
480
481 //////////////////////////////////////////////////////
482 ///// レジスタ資源のバックアップ
483 { BACKUP_REGISTER_RESOURCE
484 //////////////////////////////////////////////////////
485
486
487 ////////////////
488 // 呼び出し
489 ////////////////
490
491 CallProc(idProc,pInfo,procName,parameter, baseType,resultType);
492
493
494 /////////////////////
495 // 戻り値の処理
496 /////////////////////
497
498 //大きな型への暗黙の変換
499 int bigType = AutoBigCast(baseType.GetBasicType(), resultType.GetBasicType() );
500
501 if( resultType.GetBasicType() != bigType ){
502 // 大きな型へ変換された場合
503 // ※レジスタの値をキャストする
504 ExtendRegToBigType( REG_RAX, bigType, resultType.GetBasicType() );
505
506 resultType.SetBasicType( bigType );
507 }
508
509 SetUseRegFromRax(resultType.GetBasicType(),UseReg,XmmReg);
510
511 /////////////////////////////////////////////
512 ////// レジスタ資源を復元
513 RESTORE_REGISTER_RESOURCE
514 }////////////////////////////////////////////
515
516 if(resultType.IsStruct())
517 {
518 //構造体が戻ったときはヒープ領域にインスタンスが格納されている
519 //※後にfreeする必要あり
520 // TODO: 解放はGCに任せる
521 isNeedHeapFreeStructure = true;
522 }
523
524 isLiteral = false;
525
526 return true;
527 }
528
529 ConstMacro *pConstMacro = compiler.GetObjectModule().meta.GetGlobalConstMacros().Find(
530 ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( procName )
531 );
532 if( pConstMacro )
533 {
534 if( ActiveBasic::Compiler::LexicalAnalyzer::ConstMacroToExpression( *pConstMacro, parameter, temporary ) )
535 {
536 /////////////////////////
537 // マクロ関数
538 /////////////////////////
539
540 //閉じカッコ")"に続く文字がNULLでないときはエラーにする
541 if(termFull[i2+1+i4+1]!='\0') compiler.errorMessenger.Output(42,NULL,cp);
542
543 //マクロ関数の場合
544 NumOpe(&UseReg, temporary,Type(),resultType);
545
546 if(!IS_LITERAL(resultType.GetIndex())){
547 //リテラル値ではなかったとき
548 isLiteral = false;
549 }
550
551 return true;
552 }
553 }
554 }
555 else if( isProcedureCallOnly ){
556 // 関数呼び出し以外は受け付けない
557 return false;
558 }
559
560
561 ////////////////////////////////
562 // インデクサ(getアクセサ)
563 ////////////////////////////////
564
565 char VarName[VN_SIZE],ArrayElements[VN_SIZE];
566 GetArrayElement(termFull,VarName,ArrayElements);
567 if(ArrayElements[0]){
568 Type classType;
569 GetVarType(VarName,classType,false);
570 if( classType.IsObject() )
571 {
572 CallIndexerGetterProc(UseReg,classType,VarName,ArrayElements,resultType);
573
574 isLiteral = false;
575
576 return true;
577 }
578 }
579
580
581 ////////////////////////////////
582 // 変数
583 ////////////////////////////////
584
585 if(GetVarOffset(
586 false, //エラー表示なし
587 isWriteAccess,
588 termFull,
589 &relativeVar,resultType)){
590 //////////
591 // 変数
592 //////////
593
594 // 変数として扱う
595 isVariable = true;
596
597 isLiteral = false;
598
599 return true;
600 }
601
602/*
603 ////////////////////////////////
604 // 型名
605 ////////////////////////////////
606
607 if( compiler.StringToType( termFull, resultType ) ){
608 resultType.SetBasicType( resultType.GetBasicType() | FLAG_CAST );
609 return true;
610 }*/
611
612
613 /////////////////////////////////
614 // プロパティ用のメソッド
615 /////////////////////////////////
616
617 //配列要素を排除
618 GetArrayElement(termFull,VarName,ArrayElements);
619
620 if(GetSubHash(VarName,0)){
621
622 //////////////////////////////////////////////////////
623 ///// レジスタ資源のバックアップ
624 { BACKUP_REGISTER_RESOURCE
625 //////////////////////////////////////////////////////
626
627 CallPropertyMethod(termFull,NULL,resultType);
628
629 //大きな型への暗黙の変換
630 int bigType = AutoBigCast(baseType.GetBasicType(), resultType.GetBasicType() );
631
632 if( resultType.GetBasicType() != bigType ){
633 // 大きな型へ変換された場合
634 // ※レジスタの値をキャストする
635 ExtendRegToBigType( REG_RAX, bigType, resultType.GetBasicType() );
636
637 resultType.SetBasicType( bigType );
638 }
639
640 SetUseRegFromRax(resultType.GetBasicType(),UseReg,XmmReg);
641
642 /////////////////////////////////////////////
643 ////// レジスタ資源を復元
644 RESTORE_REGISTER_RESOURCE
645 }////////////////////////////////////////////
646
647 if(resultType.IsStruct())
648 {
649 //構造体が戻ったときはヒープ領域にインスタンスが格納されている
650 //※後にfreeする必要あり
651 // TODO: 解放はGCに任せる
652 isNeedHeapFreeStructure = true;
653 }
654
655 isLiteral = false;
656
657 return true;
658 }
659
660 if( isProcedureCallOnly )
661 {
662 compiler.errorMessenger.Output(3, termLeft, cp );
663 }
664
665 return false;
666}
667
668bool TermOpe( const char *term, const Type &baseType, Type &resultType, bool &isLiteral, bool &isNeedHeapFreeStructure, bool *pIsClassName, bool isProcedureCallOnly, bool isWriteAccess )
669{
670 bool isInitRegSwitch = false;
671 if( !pobj_reg )
672 {
673 isInitRegSwitch = true;
674
675 //作業用レジスタを取得
676 pobj_reg = new CRegister( REG_RAX );
677 }
678
679 //エラー時の復旧用
680 CRegister objReg_Backup = *pobj_reg;
681
682
683 RELATIVE_VAR relativeVar;
684 bool isVariable = false;
685 bool result = _TermOpe( term, baseType, resultType, isLiteral, isNeedHeapFreeStructure, pIsClassName, isProcedureCallOnly, isVariable, relativeVar, isWriteAccess );
686
687 if( isVariable )
688 {
689 // 変数の場合はeaxに変数ポインタを格納する
690 if( !VarToReg( relativeVar, baseType, resultType ) ){
691 compiler.errorMessenger.Output(11,term,cp);
692 }
693 }
694
695
696 if( !result )
697 {
698 *pobj_reg = objReg_Backup;
699 }
700
701 if( isInitRegSwitch ){
702 //整合性をチェック(バグ回避)
703 if( result )
704 {
705 pobj_reg->bug_check();
706 }
707
708 //作業レジスタを解放
709 delete pobj_reg;
710 pobj_reg = NULL;
711 }
712
713 return result;
714}
715bool TermOpeOnlyVariable( const char *term, Type &resultType, RELATIVE_VAR &relativeVar, bool isWriteAccess )
716{
717 if( pobj_reg )
718 {
719 compiler.errorMessenger.OutputFatalError();
720 }
721
722 //作業用レジスタを取得
723 pobj_reg = new CRegister( REG_NON );
724
725 bool isLiteral, isVariable = false, isNeedHeapFreeStructure = false;
726 bool result = _TermOpe( term, Type(), resultType, isLiteral, isNeedHeapFreeStructure, NULL, false, isVariable, relativeVar, isWriteAccess );
727
728 if( !isVariable )
729 {
730 compiler.errorMessenger.OutputFatalError();
731 }
732
733 //整合性をチェック(バグ回避)
734 if( result )
735 {
736 pobj_reg->bug_check();
737 }
738
739 //作業レジスタを解放
740 delete pobj_reg;
741 pobj_reg=0;
742
743 return result;
744}
745
746
747bool _numope( int *pReg,
748 const char *expression,
749 const Type &baseType,
750 Type &resultType,
751 bool *pbIsNeedHeapFreeStructure )
752{
753 int i,i2,i3;
754 char temporary[1024],temp2[1024];
755
756 if(expression[0]=='\0'){
757 compiler.errorMessenger.Output(1,NULL,cp);
758 return false;
759 }
760
761 if( !baseType.IsNull() && expression[0] == '[' ){
762 // リテラル配列の場合
763
764 int dataTableOffset;
765 if( !compiler.GetObjectModule().dataTable.MakeLiteralArrayBuffer( expression, baseType, dataTableOffset ) )
766 {
767 return false;
768 }
769
770 //mov reg,i2
771 compiler.codeGenerator.op_mov_RV( sizeof(_int64), *pReg, dataTableOffset, Schedule::DataTable );
772
773 resultType = baseType;
774
775 return true;
776 }
777
778 bool isLiteralCalculation;
779 if( NumOpe_GetType( expression, baseType, resultType, &isLiteralCalculation ) )
780 {
781 if( isLiteralCalculation )
782 {
783 //右辺値が数値の定数式の場合
784 _int64 i64data;
785 StaticCalculation(true, expression,baseType.GetBasicType(),&i64data,resultType);
786
787 if(resultType.IsReal()){
788 if(baseType.IsReal()) resultType=baseType;
789
790 int xmmReg = pobj_reg->GetNextXmmReg();
791 *pReg = xmmReg;
792
793 if(resultType.IsDouble()){
794 i3 = compiler.GetObjectModule().dataTable.Add( i64data );
795
796 //movlpd xmm_reg,qword ptr[data table offset]
797 compiler.codeGenerator.op_movlpd_RM( xmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
798 }
799 if(resultType.IsSingle()){
800 double dbl;
801 memcpy(&dbl,&i64data,sizeof(_int64));
802
803 float flt;
804 int i32data;
805 flt=(float)dbl;
806 memcpy(&i32data,&flt,sizeof(long));
807
808 i3 = compiler.GetObjectModule().dataTable.Add( i32data );
809
810 //movss xmm_reg,dword ptr[data table offset]
811 compiler.codeGenerator.op_movss_RM( xmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
812 }
813 }
814 else{
815 if(!resultType.Is64()){
816 //整数(符号有り/無し)
817
818 i3=(long)i64data;
819
820 if(resultType.GetBasicSize()==sizeof(char)) i3=i3&0x000000FF;
821 if(resultType.GetBasicSize()==sizeof(short)) i3=i3&0x0000FFFF;
822
823 i64data=(_int64)i3;
824 }
825
826 //mov reg,i64data
827 compiler.codeGenerator.op_mov_RV64(*pReg,i64data);
828 }
829 return true;
830 }
831 }
832
833 if( expression[0] == 1 )
834 {
835 if( expression[1]==ESC_NEW )
836 {
837 //New演算子(オブジェクト生成)
838
839 if( pobj_BlockReg->check(REG_RAX) ){
840 compiler.errorMessenger.OutputFatalError();
841 }
842
843 //////////////////////////////////////////////////////
844 ///// レジスタ資源のバックアップ
845 { BACKUP_REGISTER_RESOURCE
846 //////////////////////////////////////////////////////
847
848 if( !Operator_New( expression+2, baseType, resultType ) ){
849 return false;
850 }
851
852 /////////////////////////////////////////////
853 ////// レジスタ資源を復元
854 RESTORE_REGISTER_RESOURCE
855 }////////////////////////////////////////////
856
857 //mov reg,rax
858 compiler.codeGenerator.op_mov_RR( *pReg, REG_RAX );
859
860 return true;
861 }
862 else if( expression[1] == ESC_SYSTEM_STATIC_NEW )
863 {
864 // 静的領域にオブジェクトを作る
865
866 // 静的領域にオブジェクトを生成
867 int dataTableOffset;
868 if( !compiler.GetObjectModule().dataTable.MakeConstObjectToProcessStaticBuffer( expression + 2, resultType, dataTableOffset ) )
869 {
870 return false;
871 }
872
873 //mov reg,i2
874 compiler.codeGenerator.op_mov_RV( sizeof(_int64), *pReg, dataTableOffset, Schedule::DataTable);
875
876 return true;
877 }
878 }
879
880
881 /////////////////////////////////
882 // 式要素を逆ポーランド式で取得
883 /////////////////////////////////
884
885 char *values[255];
886 long calc[255];
887 long stack[255];
888 int pnum;
889 if(!GetNumOpeElements(expression,&pnum,values,calc,stack)){
890 for(i=0;i<pnum;i++){
891 if(values[i]) HeapDefaultFree(values[i]);
892 }
893 return 0;
894 }
895
896
897 ////////////////////////////////
898 // 演算部分のコード生成を開始
899 ////////////////////////////////
900
901 BOOL bError;
902 bError=0;
903
904 //リテラル値のみの計算かどうかを判別するためのフラグ
905 BOOL bLiteralCalculation=1;
906
907 double dbl;
908 int sp;
909 int type_stack[255];
910 LONG_PTR index_stack[255];
911 bool isNothing_stack[255];
912 bool isNeedHeapFreeStructureStack[255];
913 _int64 i64data;
914 int UseReg,XmmReg;
915 BOOL bXmm;
916 for(i=0,sp=0;i<pnum;i++){
917 int idCalc;
918 idCalc=calc[i]%100;
919
920 if(idCalc){
921 if(type_stack[sp-2]==DEF_OBJECT){
922 if( idCalc == CALC_AS
923 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
924 && index_stack[sp-1] == index_stack[sp-2]
925 || isNothing_stack[sp-2] ){
926 // 同一の型、またはNothingに対するAsはAs演算子を呼び出さない
927 }
928 else if( idCalc == CALC_AS
929 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
930 && ( ((CClass *)index_stack[sp-1])->IsEqualsOrSubClass( (CClass *)index_stack[sp-2] ) || ((CClass *)index_stack[sp-2])->IsEqualsOrSubClass( (CClass *)index_stack[sp-1] )
931 )){
932 // ダウンキャストを許可する
933 }
934 else{
935 //オーバーロードされたオペレータを呼び出す
936 i2=CallOperatorProc(idCalc,baseType,type_stack,index_stack,isNeedHeapFreeStructureStack,sp);
937 if(i2==0){
938 if(idCalc==CALC_EQUAL) lstrcpy(temp2,"==");
939 else GetCalcName(idCalc,temp2);
940 sprintf(temporary,"Operator %s",temp2);
941 compiler.errorMessenger.Output(27,temporary,cp);
942 goto error;
943 }
944 else if(i2==-1) goto error;
945
946 continue;
947 }
948 }
949
950 if(!CheckCalcType(idCalc,type_stack,sp)) goto error;
951 }
952
953 switch(idCalc){
954 //数値
955 case 0:
956 index_stack[sp]=-1;
957 isNothing_stack[sp] = false;
958 isNeedHeapFreeStructureStack[sp] = false;
959
960 UseReg=pobj_reg->GetNextReg();
961 XmmReg=pobj_reg->GetNextXmmReg();
962
963 bXmm=0;
964
965 char *term;
966 term=values[i];
967
968 if( calc[i+1]%100 == CALC_AS ){
969 // As演算子の右辺値
970 //型名
971 if( compiler.StringToType( term, resultType ) ){
972 resultType.SetBasicType( resultType.GetBasicType() | FLAG_CAST );
973 }
974 else{
975 compiler.errorMessenger.Output(3, term, cp );
976 goto error;
977 }
978
979 type_stack[sp] = resultType.GetBasicType();
980 index_stack[sp] = resultType.GetIndex();
981 sp++;
982
983 break;
984 }
985
986 if( (term[0]=='e'||term[0]=='E')
987 && (term[1]=='x'||term[1]=='X')
988 && term[2]=='\"'
989 || term[0] == '\"' )
990 {
991 bool isEx = true;
992 if( term[0] == '\"' )
993 {
994 isEx = false;
995 }
996
997 if( isEx )
998 {
999 // 拡張版リテラル文字列(エスケープシーケンス可能)
1000 if(!RemoveStringQuotes(term+2)){
1001 compiler.errorMessenger.Output(43,NULL,cp);
1002 goto error;
1003 }
1004 i3=FormatString_EscapeSequence(term+2);
1005 term+=2;
1006 }
1007 else
1008 {
1009 // 通常文字列
1010 if(!RemoveStringQuotes(term)){
1011 compiler.errorMessenger.Output(43,NULL,cp);
1012 goto error;
1013 }
1014 i3=lstrlen(term);
1015 }
1016
1017 if( !baseType.IsPointer() )
1018 {
1019 //要求タイプがオブジェクト、または未定のとき
1020
1021 //String型オブジェクトを生成
1022 i2 = compiler.GetObjectModule().dataTable.MakeConstStringObjectToProcessStaticBuffer( term );
1023
1024 //mov reg,i2
1025 compiler.codeGenerator.op_mov_RV(sizeof(_int64),UseReg,i2, Schedule::DataTable);
1026
1027 type_stack[sp]=DEF_OBJECT;
1028 index_stack[sp]=(LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr();
1029 bLiteralCalculation=0;
1030
1031 if(bXmm) pobj_reg->LockXmmReg();
1032 else pobj_reg->LockReg();
1033
1034 sp++;
1035 break;
1036 }
1037
1038StrLiteral:
1039
1040 type_stack[sp]=typeOfPtrChar;
1041 bLiteralCalculation=0;
1042
1043 i2 = compiler.GetObjectModule().dataTable.AddString( term, i3 );
1044
1045 //mov reg,i2
1046 compiler.codeGenerator.op_mov_RV(sizeof(_int64),UseReg,i2, Schedule::DataTable);
1047
1048 if(UseReg==REG_R14){
1049 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
1050 pobj_sf->push(REG_R14);
1051 }
1052 }
1053 else if(IsVariableTopChar(term[0])||
1054 term[0]=='*'||
1055 (term[0]=='.'&&IsVariableTopChar(term[1])))
1056 {
1057 //////////////////
1058 // 何らかの識別子
1059
1060 bool isLiteral;
1061 if( TermOpe( term, baseType, resultType, isLiteral, isNeedHeapFreeStructureStack[sp] ) ){
1062 if(resultType.IsNull()){
1063 //戻り値が存在しないとき
1064 for(i2=0;;i2++){
1065 if(term[i2]=='('||term[i2]=='\0'){
1066 term[i2]=0;
1067 break;
1068 }
1069 }
1070 compiler.errorMessenger.Output(38,term,cp);
1071
1072 goto error;
1073 }
1074
1075 type_stack[sp] = resultType.GetBasicType();
1076 index_stack[sp] = resultType.GetIndex();
1077
1078 if( !isLiteral ){
1079 bLiteralCalculation=0;
1080 }
1081
1082 if( resultType.GetBasicType() & FLAG_CAST ){
1083 // 型名のみ
1084 compiler.errorMessenger.OutputFatalError();
1085 }
1086 else{
1087 if( resultType.IsReal() == false && UseReg==REG_R14 ){
1088 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
1089 pobj_sf->push(REG_R14);
1090 }
1091 if( resultType.IsReal() && XmmReg==REG_XMM4 ){
1092 if(resultType.IsDouble()){
1093 //movsd qword ptr[rsp+offset],xmm4 ※スタックフレームを利用
1094 pobj_sf->push(REG_XMM4,sizeof(double));
1095 }
1096 if(resultType.IsSingle()){
1097 //movss dword ptr[rsp+offset],xmm4 ※スタックフレームを利用
1098 pobj_sf->push(REG_XMM4,sizeof(float));
1099 }
1100 }
1101
1102 if( resultType.IsReal() ){
1103 pobj_reg->LockXmmReg();
1104 }
1105 else{
1106 pobj_reg->LockReg();
1107 }
1108 }
1109
1110 sp++;
1111 break;
1112 }
1113
1114
1115 // Nothing
1116 if( lstrcmp( term, "Nothing" ) == 0 ){
1117 isNothing_stack[sp] = true;
1118
1119 type_stack[sp] = DEF_OBJECT;
1120 if( baseType.IsObject() ){
1121 index_stack[sp] = baseType.GetIndex();
1122 }
1123 else{
1124 index_stack[sp] = (LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetObjectClassPtr();
1125 }
1126
1127 bLiteralCalculation = 0;
1128
1129 //xor reg,reg
1130 compiler.codeGenerator.op_zero_reg( UseReg );
1131
1132 if(UseReg==REG_R14){
1133 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
1134 pobj_sf->push(REG_R14);
1135 }
1136
1137 pobj_reg->LockReg();
1138 sp++;
1139 break;
1140 }
1141
1142
1143 //////////////
1144 // 定数の場合
1145 //////////////
1146
1147 i3 = compiler.GetObjectModule().meta.GetGlobalConsts().GetBasicType(
1148 ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term )
1149 );
1150 if(i3){
1151 if( compiler.GetObjectModule().meta.GetGlobalConsts().IsStringPtr( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term ) ) ){
1152 //リテラル文字列
1153
1154 if( baseType.IsObject() || baseType.IsNull() )
1155 {
1156 //要求タイプがオブジェクト、または未定のとき
1157
1158 //String型オブジェクトを生成
1159 NewStringObject(UseReg,term);
1160
1161 type_stack[sp]=DEF_OBJECT;
1162 index_stack[sp]=(LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr();
1163 bLiteralCalculation=0;
1164
1165 if(bXmm) pobj_reg->LockXmmReg();
1166 else pobj_reg->LockReg();
1167
1168 sp++;
1169 break;
1170 }
1171
1172 double dbl = compiler.GetObjectModule().meta.GetGlobalConsts().GetDoubleData(
1173 ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term )
1174 );
1175 memcpy(&i64data,&dbl,sizeof(double));
1176
1177 //バイト数
1178 i3=lstrlen((char *)i64data);
1179
1180 memcpy(term,(char *)i64data,i3);
1181 term[i3]=0;
1182 goto StrLiteral;
1183 }
1184
1185 type_stack[sp] = i3;
1186 if(IsRealNumberType(i3)){
1187 //実数
1188 double dbl = compiler.GetObjectModule().meta.GetGlobalConsts().GetDoubleData(
1189 ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term )
1190 );
1191 memcpy(&i64data,&dbl,sizeof(double));
1192 goto Literal;
1193 }
1194 else if(IsWholeNumberType(i3)){
1195 //整数
1196 i64data = compiler.GetObjectModule().meta.GetGlobalConsts().GetWholeData(
1197 ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term )
1198 );
1199 goto Literal;
1200 }
1201 else{
1202 compiler.errorMessenger.Output(1,NULL,0);
1203 goto error;
1204 }
1205 }
1206
1207
1208 //該当する識別子が見当たらないときはエラー扱いにする
1209 bError=1;
1210 compiler.errorMessenger.Output(3,term,cp);
1211 type_stack[sp]=DEF_DOUBLE;
1212 }
1213 else{
1214 //リテラル値
1215 type_stack[sp]=GetLiteralValue(term,&i64data,baseType.GetBasicType());
1216Literal:
1217 if(type_stack[sp]==DEF_DOUBLE){
1218 //64ビット浮動小数型
1219 bXmm=1;
1220
1221 if(XmmReg==REG_XMM4){
1222 //mov r14,i64data
1223 compiler.codeGenerator.op_mov_RV64(REG_R14,i64data);
1224
1225
1226 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
1227 pobj_sf->push(REG_R14);
1228 }
1229 else{
1230 i3 = compiler.GetObjectModule().dataTable.Add( i64data );
1231
1232 //movlpd xmm_reg,qword ptr[data table offset]
1233 compiler.codeGenerator.op_movlpd_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
1234 }
1235 }
1236 else if(type_stack[sp]==DEF_SINGLE){
1237 //32ビット浮動小数型
1238 bXmm=1;
1239
1240 float flt;
1241 int i32data;
1242 memcpy(&dbl,&i64data,sizeof(double));
1243 flt=(float)dbl;
1244 memcpy(&i32data,&flt,sizeof(long));
1245
1246 if(XmmReg==REG_XMM4){
1247 compiler.errorMessenger.OutputFatalError(); // TODO: 未実装
1248 //push term
1249 //compiler.codeGenerator.op_push_value(i32data);
1250 }
1251 else{
1252 i3=compiler.GetObjectModule().dataTable.Add( i32data );
1253
1254 //movss xmm_reg,dword ptr[data table offset]
1255 compiler.codeGenerator.op_movss_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
1256 }
1257 }
1258 else{
1259 //整数
1260
1261 index_stack[sp]=GetLiteralIndex(i64data);
1262
1263 //mov reg,i64data
1264 compiler.codeGenerator.op_mov_RV64(UseReg,i64data);
1265
1266 if(UseReg==REG_R14){
1267 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
1268 pobj_sf->push(REG_R14);
1269 }
1270 }
1271 }
1272
1273 if(bXmm) pobj_reg->LockXmmReg();
1274 else pobj_reg->LockReg();
1275
1276 sp++;
1277 break;
1278
1279 //論理演算子
1280 case CALC_XOR:
1281 case CALC_OR:
1282 case CALC_AND:
1283 if(!CalcTwoTerm_Logical(idCalc,type_stack,index_stack,&sp)) goto error;
1284 break;
1285 case CALC_NOT:
1286 //value[sp-1]=Not value[sp-1]
1287 //NOT演算子
1288 if(!Calc_Not(type_stack,sp)) goto error;
1289 break;
1290
1291 //比較演算子
1292 case CALC_PE: //value[sp-2] <= value[sp-1]
1293 case CALC_QE: //value[sp-2] >= value[sp-1]
1294 case CALC_P: //value[sp-2] < value[sp-1]
1295 case CALC_Q: //value[sp-2] > value[sp-1]
1296 case CALC_NOTEQUAL: //value[sp-2] <> value[sp-1]
1297 case CALC_EQUAL: //value[sp-2] = value[sp-1]
1298 if(!CalcTwoTerm_Relational(idCalc,type_stack,index_stack,&sp)) goto error;
1299 break;
1300
1301 //ビットシフト
1302 case CALC_SHL: //value[sp-2] << value[sp-1]
1303 case CALC_SHR: //value[sp-2] >> value[sp-1]
1304 if(!Calc_Shift(idCalc,type_stack,&sp)) goto error;
1305 break;
1306
1307 //算術演算
1308 case CALC_ADDITION:
1309 case CALC_SUBTRACTION:
1310 case CALC_PRODUCT:
1311 if(!CalcTwoTerm_Arithmetic(idCalc,type_stack,index_stack,&sp)) goto error;
1312 break;
1313 case CALC_MOD:
1314 //value[sp-2]%=value[sp-1]
1315 //剰余演算
1316 if(!Calc_Mod(type_stack,index_stack,&sp)) goto error;
1317 break;
1318 case CALC_QUOTIENT:
1319 //value[sp-2]/=value[sp-1];
1320 //除算
1321 if(!Calc_Divide(type_stack,&sp,baseType.GetBasicType())) goto error;
1322 break;
1323 case CALC_INTQUOTIENT:
1324 //value[sp-2]/=value[sp-1]
1325 //整数除算
1326 if(!Calc_IntDivide(type_stack,index_stack,&sp)) goto error;
1327 break;
1328 case CALC_MINUSMARK:
1329 //value[sp-1]=-value[sp-1]
1330 //符号反転
1331 if(!Calc_MinusMark(type_stack,sp)) goto error;
1332 break;
1333 case CALC_POWER:
1334 //べき乗演算(浮動小数点演算のみ)
1335 if(!Calc_Power(type_stack,&sp)) goto error;
1336 break;
1337 case CALC_AS:
1338 //キャスト
1339 if(!Calc_Cast(type_stack,index_stack,&sp)) goto error;
1340 break;
1341 case CALC_BYVAL:
1342 //ポインタ型→参照型
1343 if( PTR_LEVEL( type_stack[sp-1] ) <= 0 ){
1344 //ポインタ型ではないとき
1345 compiler.errorMessenger.Output( 3, NULL, cp );
1346 goto error;
1347 }
1348
1349 type_stack[sp-1] = PTR_LEVEL_DOWN( type_stack[sp-1] );
1350
1351 break;
1352
1353 default:
1354 compiler.errorMessenger.Output(300,NULL,cp);
1355 goto error;
1356 }
1357 }
1358
1359 if(bError) goto error;
1360
1361 if(sp!=1){
1362 compiler.errorMessenger.Output(1,NULL,cp);
1363 goto error;
1364 }
1365
1366 if(bLiteralCalculation){
1367 compiler.errorMessenger.OutputFatalError();
1368 }
1369 else{
1370 //右辺値が数値の定数式ではないとき
1371 if(IS_LITERAL(index_stack[0])) index_stack[0]=-1;
1372 }
1373
1374 if( pbIsNeedHeapFreeStructure )
1375 {
1376 *pbIsNeedHeapFreeStructure = isNeedHeapFreeStructureStack[0];
1377 }
1378
1379 if(IsRealNumberType(type_stack[0]))
1380 *pReg=pobj_reg->UnlockXmmReg();
1381 else
1382 *pReg=pobj_reg->UnlockReg();
1383
1384
1385 resultType.SetType( type_stack[0], index_stack[0] );
1386
1387 bool isSuccessful = true;
1388 goto finish;
1389
1390
1391
1392 //////////////////
1393 // エラー処理
1394 //////////////////
1395
1396error:
1397
1398 isSuccessful = false;
1399 goto finish;
1400
1401
1402
1403
1404finish:
1405
1406 for(i=0;i<pnum;i++){
1407 if(values[i]) HeapDefaultFree(values[i]);
1408 }
1409
1410 return isSuccessful;
1411}
1412
1413bool NumOpe( int *pReg,
1414 const char *expression,
1415 const Type &baseType,
1416 Type &resultType,
1417 bool *pbIsNeedHeapFreeStructure )
1418{
1419 bool isInitRegSwitch = false;
1420 if( !pobj_reg )
1421 {
1422 isInitRegSwitch = true;
1423
1424 //作業用レジスタを取得
1425 pobj_reg = new CRegister( *pReg );
1426 }
1427
1428 //エラー時の復旧用
1429 CRegister objReg_Backup = *pobj_reg;
1430
1431 *pReg = pobj_reg->GetNextReg();
1432
1433
1434 bool result = _numope( pReg, expression, baseType, resultType, pbIsNeedHeapFreeStructure );
1435
1436
1437 if( !result )
1438 {
1439 *pobj_reg = objReg_Backup;
1440 }
1441
1442 if( isInitRegSwitch ){
1443 //整合性をチェック(バグ回避)
1444 if( result )
1445 {
1446 pobj_reg->bug_check();
1447 }
1448
1449 //作業レジスタを解放
1450 delete pobj_reg;
1451 pobj_reg = NULL;
1452 }
1453
1454 return result;
1455}
Note: See TracBrowser for help on using the repository browser.