source: dev/trunk/abdev/BasicCompiler64/NumOpe.cpp@ 255

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