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

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