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

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

プロパティ値として返ってきたオブジェクトインスタンスのインデクサを呼び出す処理をきちんと対応した。

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