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

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

CodeGeneratorクラスのベースを実装

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