source: dev/trunk/abdev/BasicCompiler32/NumOpe.cpp@ 290

Last change on this file since 290 was 290, checked in by dai_9181, 17 years ago

ジェネリクスのベースを実装

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