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

Last change on this file since 242 was 242, checked in by dai_9181, 17 years ago
File size: 30.5 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 int i,i2,i3;
528 char temporary[1024],temp2[1024];
529
530 if(expression[0]=='\0'){
531 SetError(1,NULL,cp);
532 return false;
533 }
534
535 if(expression[0]==1&& expression[1]==ESC_NEW ){
536 //New演算子(オブジェクト生成)
537
538 if( pobj_BlockReg->check(REG_RAX) ){
539 SetError();
540 }
541
542 //////////////////////////////////////////////////////
543 ///// レジスタ資源のバックアップ
544 { BACKUP_REGISTER_RESOURCE
545 //////////////////////////////////////////////////////
546
547 if( !Operator_New( expression+2, baseType, resultType ) ){
548 return false;
549 }
550
551 /////////////////////////////////////////////
552 ////// レジスタ資源を復元
553 RESTORE_REGISTER_RESOURCE
554 }////////////////////////////////////////////
555
556 //mov reg,rax
557 compiler.codeGenerator.op_mov_RR( *pReg, REG_RAX );
558
559 return true;
560 }
561
562 if( !baseType.IsNull() && expression[0] == '[' ){
563 // リテラル配列の場合
564
565 if( !baseType.IsPointer() ){
566 SetError(1,NULL,cp);
567 return false;
568 }
569 Type tempBaseType( baseType );
570 tempBaseType.PtrLevelDown();
571
572 char *buffer = (char *)malloc( lstrlen( expression ) + 1 );
573 lstrcpy( buffer, expression );
574 RemoveStringBracket( buffer );
575
576 void *binary = malloc( 1 );
577 int num = 0;
578
579 i = 0;
580 while( buffer[i] ){
581 i = GetOneParameter( buffer, i, temporary );
582 if( buffer[i] == ',' ){
583 i++;
584 }
585
586 Type resultType;
587 _int64 i64data;
588 if( !StaticCalculation( true, temporary, tempBaseType.GetBasicType(), &i64data, resultType ) ){
589 return false;
590 }
591 if( !resultType.IsWhole() ){
592 // TODO: 実数に未対応
593 SetError();
594 return false;
595 }
596
597 binary = realloc( binary, ( num + 1 ) * tempBaseType.GetSize() );
598 memcpy( (char *)binary + (num * tempBaseType.GetSize()), &i64data, tempBaseType.GetSize() );
599 num++;
600 }
601
602 i2 = compiler.GetDataTable().AddBinary( binary, num * tempBaseType.GetSize() );
603
604 //mov reg,i2
605 compiler.codeGenerator.op_mov_RV(sizeof(_int64),*pReg,i2, Schedule::DataTable );
606
607 free( buffer );
608
609 resultType = baseType;
610
611 return true;
612 }
613
614
615 /////////////////////////////////
616 // 式要素を逆ポーランド式で取得
617 /////////////////////////////////
618
619 char *values[255];
620 long calc[255];
621 long stack[255];
622 int pnum;
623 if(!GetNumOpeElements(expression,&pnum,values,calc,stack)){
624 for(i=0;i<pnum;i++){
625 if(values[i]) HeapDefaultFree(values[i]);
626 }
627 return 0;
628 }
629
630
631 BOOL bInitRegSwitch=0;
632 if(!pobj_reg){
633 bInitRegSwitch=1;
634
635 //作業用レジスタを取得
636 pobj_reg=new CRegister(*pReg);
637 }
638
639 //エラー時の復旧用
640 CRegister objReg_Backup;
641 objReg_Backup=*pobj_reg;
642
643
644
645 ////////////////////////////////
646 // 演算部分のコード生成を開始
647 ////////////////////////////////
648
649 BOOL bError;
650 bError=0;
651
652 //リテラル値のみの計算かどうかを判別するためのフラグ
653 BOOL bLiteralCalculation=1;
654
655 //リテラル演算の場合を考慮した演算前のバッファ位置
656 int BeforeObp;
657 BeforeObp=obp;
658
659 //リテラル演算の場合を考慮した演算前のプロシージャスケジュール位置
660 //※64ビットの掛け算、除算などで特殊関数が呼ばれるため
661 int Before_ProcAddrScheduleNum;
662 Before_ProcAddrScheduleNum=pobj_SubAddrSchedule->num;
663
664 //リテラル演算の場合を考慮した演算前のデータテーブルスケジュール位置
665 int Before_DataTableScheduleNum;
666 Before_DataTableScheduleNum=pobj_DataTableSchedule->num;
667
668 //リテラル演算の場合を考慮した演算前の再配置スケジュール
669 CReloc *pobj_BackReloc;
670 pobj_BackReloc=new CReloc();
671 pobj_BackReloc->copy(pobj_Reloc);
672
673 //リテラル演算の場合を考慮した演算前のスタックフレームスケジュール位置
674 int Before_StackFrameScheduleNum;
675 Before_StackFrameScheduleNum=pobj_sf->num;
676
677 double dbl;
678 int sp;
679 int type_stack[255];
680 LONG_PTR index_stack[255];
681 bool isNothing_stack[255];
682 BOOL bUseHeap[255];
683 _int64 i64data;
684 int UseReg,XmmReg;
685 BOOL bXmm;
686 for(i=0,sp=0;i<pnum;i++){
687 int idCalc;
688 idCalc=calc[i]%100;
689
690 if(idCalc){
691 if(type_stack[sp-2]==DEF_OBJECT){
692 if( idCalc == CALC_AS
693 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
694 && index_stack[sp-1] == index_stack[sp-2]
695 || isNothing_stack[sp-2] ){
696 // 同一の型、またはNothingに対するAsはAs演算子を呼び出さない
697 }
698 else if( idCalc == CALC_AS
699 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
700 && ( ((CClass *)index_stack[sp-1])->IsEqualsOrSubClass( (CClass *)index_stack[sp-2] ) || ((CClass *)index_stack[sp-2])->IsEqualsOrSubClass( (CClass *)index_stack[sp-1] )
701 )){
702 // ダウンキャストを許可する
703 }
704 else if( idCalc == CALC_AS
705 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST ) && ((CClass *)index_stack[sp-1])->IsInterface()
706 ){
707 // インターフェイスへのキャスト
708 // TODO: 実装
709 CastToInterface( pobj_reg->GetLockingReg(), REG_R15, *(CClass *)index_stack[sp-2], *(CClass *)index_stack[sp-1] );
710 }
711 else{
712 //オーバーロードされたオペレータを呼び出す
713 i2=CallOperatorProc(idCalc,baseType,type_stack,index_stack,bUseHeap,sp);
714 if(i2==0){
715 if(idCalc==CALC_EQUAL) lstrcpy(temp2,"==");
716 else GetCalcName(idCalc,temp2);
717 sprintf(temporary,"Operator %s",temp2);
718 SetError(27,temporary,cp);
719 goto error;
720 }
721 else if(i2==-1) goto error;
722
723 continue;
724 }
725 }
726
727 if(!CheckCalcType(idCalc,type_stack,sp)) goto error;
728 }
729
730 switch(idCalc){
731 //数値
732 case 0:
733 index_stack[sp]=-1;
734 isNothing_stack[sp] = false;
735 bUseHeap[sp]=0;
736
737 UseReg=pobj_reg->GetNextReg();
738 XmmReg=pobj_reg->GetNextXmmReg();
739
740 bXmm=0;
741
742 char *term;
743 term=values[i];
744
745 if( calc[i+1]%100 == CALC_AS ){
746 // As演算子の右辺値
747 //型名
748 if( Compiler::StringToType( term, resultType ) ){
749 resultType.SetBasicType( resultType.GetBasicType() | FLAG_CAST );
750 }
751 else{
752 SetError(3, term, cp );
753 goto error;
754 }
755
756 type_stack[sp] = resultType.GetBasicType();
757 index_stack[sp] = resultType.GetIndex();
758 sp++;
759
760 break;
761 }
762
763 if(term[0]=='\"'){
764 //リテラル文字列
765 if(!RemoveStringQuotes(term)){
766 SetError(43,NULL,cp);
767 goto error;
768 }
769 i3=lstrlen(term);
770StrLiteral:
771
772 if( baseType.IsObject() || baseType.IsNull() ){
773 //要求タイプがオブジェクト、または未定のとき
774
775 //String型オブジェクトを生成
776 NewStringObject(UseReg,term);
777
778 type_stack[sp]=DEF_OBJECT;
779 index_stack[sp]=(LONG_PTR)compiler.GetMeta().GetClasses().GetStringClassPtr();
780 bLiteralCalculation=0;
781
782 if(bXmm) pobj_reg->LockXmmReg();
783 else pobj_reg->LockReg();
784
785 sp++;
786 break;
787 }
788
789 type_stack[sp]=typeOfPtrChar;
790 bLiteralCalculation=0;
791
792 i2 = compiler.GetDataTable().AddString( term, i3 );
793
794 //mov reg,i2
795 compiler.codeGenerator.op_mov_RV(sizeof(_int64),UseReg,i2, Schedule::DataTable);
796
797 if(UseReg==REG_R14){
798 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
799 pobj_sf->push(REG_R14);
800 }
801 }
802 else if((term[0]=='e'||term[0]=='E')&&
803 (term[1]=='x'||term[1]=='X')&&
804 term[2]=='\"'){
805 //拡張版リテラル文字列(エスケープシーケンス可能)
806 if(!RemoveStringQuotes(term+2)){
807 SetError(43,NULL,cp);
808 goto error;
809 }
810 i3=FormatString_EscapeSequence(term+2);
811 term+=2;
812
813 goto StrLiteral;
814 }
815 else if(IsVariableTopChar(term[0])||
816 term[0]=='*'||
817 (term[0]=='.'&&IsVariableTopChar(term[1]))){
818 //////////////////
819 // 何らかの識別子
820
821 bool isLiteral;
822 if( TermOpe( term, baseType, resultType, isLiteral, &bUseHeap[sp] ) ){
823 if(resultType.IsNull()){
824 //戻り値が存在しないとき
825 for(i2=0;;i2++){
826 if(term[i2]=='('||term[i2]=='\0'){
827 term[i2]=0;
828 break;
829 }
830 }
831 SetError(38,term,cp);
832
833 goto error;
834 }
835
836 type_stack[sp] = resultType.GetBasicType();
837 index_stack[sp] = resultType.GetIndex();
838
839 if( !isLiteral ){
840 bLiteralCalculation=0;
841 }
842
843 if( resultType.GetBasicType() & FLAG_CAST ){
844 // 型名のみ
845 SetError();
846 }
847 else{
848 if( resultType.IsReal() == false && UseReg==REG_R14 ){
849 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
850 pobj_sf->push(REG_R14);
851 }
852 if( resultType.IsReal() && XmmReg==REG_XMM4 ){
853 if(resultType.IsDouble()){
854 //movsd qword ptr[rsp+offset],xmm4 ※スタックフレームを利用
855 pobj_sf->push(REG_XMM4,sizeof(double));
856 }
857 if(resultType.IsSingle()){
858 //movss dword ptr[rsp+offset],xmm4 ※スタックフレームを利用
859 pobj_sf->push(REG_XMM4,sizeof(float));
860 }
861 }
862
863 if( resultType.IsReal() ){
864 pobj_reg->LockXmmReg();
865 }
866 else{
867 pobj_reg->LockReg();
868 }
869 }
870
871 sp++;
872 break;
873 }
874
875
876 // Nothing
877 if( lstrcmp( term, "Nothing" ) == 0 ){
878 isNothing_stack[sp] = true;
879
880 type_stack[sp] = DEF_OBJECT;
881 if( baseType.IsObject() ){
882 index_stack[sp] = baseType.GetIndex();
883 }
884 else{
885 index_stack[sp] = (LONG_PTR)compiler.GetMeta().GetClasses().GetObjectClassPtr();
886 }
887
888 bLiteralCalculation = 0;
889
890 //xor reg,reg
891 compiler.codeGenerator.op_zero_reg( UseReg );
892
893 if(UseReg==REG_R14){
894 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
895 pobj_sf->push(REG_R14);
896 }
897
898 pobj_reg->LockReg();
899 sp++;
900 break;
901 }
902
903
904 //////////////
905 // 定数の場合
906 //////////////
907
908 i3 = compiler.GetMeta().GetGlobalConsts().GetBasicType(term);
909 if(i3){
910 if( compiler.GetMeta().GetGlobalConsts().IsStringPtr( term ) ){
911 //リテラル文字列
912
913 double dbl = compiler.GetMeta().GetGlobalConsts().GetDoubleData(term);
914 memcpy(&i64data,&dbl,sizeof(double));
915
916 //バイト数
917 i3=lstrlen((char *)i64data);
918
919 memcpy(term,(char *)i64data,i3);
920 term[i3]=0;
921 goto StrLiteral;
922 }
923
924 type_stack[sp] = i3;
925 if(IsRealNumberType(i3)){
926 //実数
927 double dbl = compiler.GetMeta().GetGlobalConsts().GetDoubleData(term);
928 memcpy(&i64data,&dbl,sizeof(double));
929 goto Literal;
930 }
931 else if(IsWholeNumberType(i3)){
932 //整数
933 i64data = compiler.GetMeta().GetGlobalConsts().GetWholeData(term);
934 goto Literal;
935 }
936 /*else if(i3==DEF_STRING){
937 //リテラル文字列
938
939 //バイト数
940 i3=(int)dbl;
941
942 memcpy(term,temporary,i3);
943 goto StrLiteral;
944 }*/
945 else{
946 SetError(1,NULL,0);
947 goto error;
948 }
949 }
950
951
952 //該当する識別子が見当たらないときはエラー扱いにする
953 bError=1;
954 SetError(3,term,cp);
955 type_stack[sp]=DEF_DOUBLE;
956 }
957 else{
958 //リテラル値
959 type_stack[sp]=GetLiteralValue(term,&i64data,baseType.GetBasicType());
960Literal:
961 if(type_stack[sp]==DEF_DOUBLE){
962 //64ビット浮動小数型
963 bXmm=1;
964
965 if(XmmReg==REG_XMM4){
966 //mov r14,i64data
967 compiler.codeGenerator.op_mov_RV64(REG_R14,i64data);
968
969
970 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
971 pobj_sf->push(REG_R14);
972 }
973 else{
974 i3 = compiler.GetDataTable().Add( i64data );
975
976 //movlpd xmm_reg,qword ptr[data table offset]
977 compiler.codeGenerator.op_movlpd_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
978 }
979 }
980 else if(type_stack[sp]==DEF_SINGLE){
981 //32ビット浮動小数型
982 bXmm=1;
983
984 float flt;
985 int i32data;
986 memcpy(&dbl,&i64data,sizeof(double));
987 flt=(float)dbl;
988 memcpy(&i32data,&flt,sizeof(long));
989
990 if(XmmReg==REG_XMM4){
991 SetError(); // TODO: 未実装
992 //push term
993 //compiler.codeGenerator.op_push_value(i32data);
994 }
995 else{
996 i3=compiler.GetDataTable().Add( i32data );
997
998 //movss xmm_reg,dword ptr[data table offset]
999 compiler.codeGenerator.op_movss_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
1000 }
1001 }
1002 else{
1003 //整数
1004
1005 index_stack[sp]=GetLiteralIndex(i64data);
1006
1007 //mov reg,i64data
1008 compiler.codeGenerator.op_mov_RV64(UseReg,i64data);
1009
1010 if(UseReg==REG_R14){
1011 //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用
1012 pobj_sf->push(REG_R14);
1013 }
1014 }
1015 }
1016
1017 if(bXmm) pobj_reg->LockXmmReg();
1018 else pobj_reg->LockReg();
1019
1020 sp++;
1021 break;
1022
1023 //論理演算子
1024 case CALC_XOR:
1025 case CALC_OR:
1026 case CALC_AND:
1027 if(!CalcTwoTerm_Logical(idCalc,type_stack,index_stack,&sp)) goto error;
1028 break;
1029 case CALC_NOT:
1030 //value[sp-1]=Not value[sp-1]
1031 //NOT演算子
1032 if(!Calc_Not(type_stack,sp)) goto error;
1033 break;
1034
1035 //比較演算子
1036 case CALC_PE: //value[sp-2] <= value[sp-1]
1037 case CALC_QE: //value[sp-2] >= value[sp-1]
1038 case CALC_P: //value[sp-2] < value[sp-1]
1039 case CALC_Q: //value[sp-2] > value[sp-1]
1040 case CALC_NOTEQUAL: //value[sp-2] <> value[sp-1]
1041 case CALC_EQUAL: //value[sp-2] = value[sp-1]
1042 if(!CalcTwoTerm_Relational(idCalc,type_stack,index_stack,&sp)) goto error;
1043 break;
1044
1045 //ビットシフト
1046 case CALC_SHL: //value[sp-2] << value[sp-1]
1047 case CALC_SHR: //value[sp-2] >> value[sp-1]
1048 if(!Calc_Shift(idCalc,type_stack,&sp)) goto error;
1049 break;
1050
1051 //算術演算
1052 case CALC_ADDITION:
1053 case CALC_SUBTRACTION:
1054 case CALC_PRODUCT:
1055 if(!CalcTwoTerm_Arithmetic(idCalc,type_stack,index_stack,&sp)) goto error;
1056 break;
1057 case CALC_MOD:
1058 //value[sp-2]%=value[sp-1]
1059 //剰余演算
1060 if(!Calc_Mod(type_stack,index_stack,&sp)) goto error;
1061 break;
1062 case CALC_QUOTIENT:
1063 //value[sp-2]/=value[sp-1];
1064 //除算
1065 if(!Calc_Divide(type_stack,&sp,baseType.GetBasicType())) goto error;
1066 break;
1067 case CALC_INTQUOTIENT:
1068 //value[sp-2]/=value[sp-1]
1069 //整数除算
1070 if(!Calc_IntDivide(type_stack,index_stack,&sp)) goto error;
1071 break;
1072 case CALC_MINUSMARK:
1073 //value[sp-1]=-value[sp-1]
1074 //符号反転
1075 if(!Calc_MinusMark(type_stack,sp)) goto error;
1076 break;
1077 case CALC_POWER:
1078 //べき乗演算(浮動小数点演算のみ)
1079 if(!Calc_Power(type_stack,&sp)) goto error;
1080 break;
1081 case CALC_AS:
1082 //キャスト
1083 if(!Calc_Cast(type_stack,index_stack,&sp)) goto error;
1084 break;
1085 case CALC_BYVAL:
1086 //ポインタ型→参照型
1087 if( PTR_LEVEL( type_stack[sp-1] ) <= 0 ){
1088 //ポインタ型ではないとき
1089 SetError( 3, NULL, cp );
1090 goto error;
1091 }
1092
1093 type_stack[sp-1] = PTR_LEVEL_DOWN( type_stack[sp-1] );
1094
1095 break;
1096
1097 default:
1098 SetError(300,NULL,cp);
1099 goto error;
1100 }
1101 }
1102
1103 if(bError) goto error;
1104
1105 if(sp!=1){
1106 SetError(1,NULL,cp);
1107 goto error;
1108 }
1109
1110 if(bLiteralCalculation){
1111 //右辺値が数値の定数式の場合
1112 Type resultType;
1113 StaticCalculation(true, expression,baseType.GetBasicType(),&i64data,resultType);
1114
1115 obp=BeforeObp;
1116 pobj_SubAddrSchedule->num=Before_ProcAddrScheduleNum;
1117 pobj_DataTableSchedule->num=Before_DataTableScheduleNum;
1118 pobj_Reloc->copy(pobj_BackReloc);
1119 pobj_sf->num=Before_StackFrameScheduleNum;
1120 *pobj_reg=objReg_Backup;
1121
1122 if(resultType.IsReal()){
1123 if(baseType.IsReal()) resultType=baseType;
1124
1125 XmmReg=pobj_reg->LockXmmReg();
1126
1127 if(resultType.IsDouble()){
1128 i3 = compiler.GetDataTable().Add( i64data );
1129
1130 //movlpd xmm_reg,qword ptr[data table offset]
1131 compiler.codeGenerator.op_movlpd_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
1132 }
1133 if(resultType.IsSingle()){
1134 memcpy(&dbl,&i64data,sizeof(_int64));
1135
1136 float flt;
1137 int i32data;
1138 flt=(float)dbl;
1139 memcpy(&i32data,&flt,sizeof(long));
1140
1141 i3 = compiler.GetDataTable().Add( i32data );
1142
1143 //movss xmm_reg,dword ptr[data table offset]
1144 compiler.codeGenerator.op_movss_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable );
1145 }
1146 }
1147 else{
1148 if(!resultType.Is64()){
1149 //整数(符号有り/無し)
1150
1151 i3=(long)i64data;
1152
1153 if(resultType.GetBasicSize()==sizeof(char)) i3=i3&0x000000FF;
1154 if(resultType.GetBasicSize()==sizeof(short)) i3=i3&0x0000FFFF;
1155
1156 i64data=(_int64)i3;
1157 }
1158
1159 UseReg=pobj_reg->LockReg();
1160
1161 //mov reg,i64data
1162 compiler.codeGenerator.op_mov_RV64(UseReg,i64data);
1163 }
1164
1165 type_stack[0]=resultType.GetBasicType();
1166 index_stack[0]=resultType.GetIndex();
1167 }
1168 else{
1169 //右辺値が数値の定数式ではないとき
1170 if(IS_LITERAL(index_stack[0])) index_stack[0]=-1;
1171 }
1172
1173 if(pbUseHeap) *pbUseHeap=bUseHeap[0];
1174
1175 if(IsRealNumberType(type_stack[0]))
1176 *pReg=pobj_reg->UnlockXmmReg();
1177 else
1178 *pReg=pobj_reg->UnlockReg();
1179
1180
1181 if(bInitRegSwitch){
1182 //整合性をチェック(バグ回避)
1183 pobj_reg->bug_check();
1184
1185 //作業レジスタを解放
1186 delete pobj_reg;
1187 pobj_reg=0;
1188 }
1189
1190 resultType.SetType( type_stack[0], index_stack[0] );
1191
1192 bool isSuccessful = true;
1193 goto finish;
1194
1195
1196
1197 //////////////////
1198 // エラー処理
1199 //////////////////
1200
1201error:
1202
1203 *pobj_reg=objReg_Backup;
1204
1205 if(bInitRegSwitch){
1206 //作業レジスタを解放
1207 delete pobj_reg;
1208 pobj_reg=0;
1209 }
1210
1211 isSuccessful = false;
1212 goto finish;
1213
1214
1215
1216
1217finish:
1218
1219 for(i=0;i<pnum;i++){
1220 if(values[i]) HeapDefaultFree(values[i]);
1221 }
1222
1223 //再配置スケジュールバックアップ情報を解放
1224 delete pobj_BackReloc;
1225
1226 return isSuccessful;
1227}
Note: See TracBrowser for help on using the repository browser.