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

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

[415]を64bit版にマージ。

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