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

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

32ビットコンパイラが落ちるので一時的な対処をした

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