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

Last change on this file since 276 was 276, checked in by dai_9181, 17 years ago
File size: 27.0 KB
RevLine 
[206]1#include "stdafx.h"
2
[183]3#include <jenga/include/smoothie/Smoothie.h>
4#include <jenga/include/smoothie/LexicalAnalysis.h>
5
6#include <Compiler.h>
7
[3]8#include "../BasicCompiler_Common/common.h"
9#include "Opcode.h"
10
11void PushReturnValue(int type){
12 //関数の戻り値をスタックへプッシュする
13 //※この処理内では、esi、ediは使用不可
14
[64]15 if(type==DEF_OBJECT || type==DEF_STRUCT){
[3]16 //push eax
[225]17 compiler.codeGenerator.op_push(REG_EAX);
[3]18 }
19 else if(type==DEF_DOUBLE){
20 //sub esp,8
[225]21 compiler.codeGenerator.op_sub_esp(8);
[3]22
23 //fstp qword ptr[esp]
[225]24 compiler.codeGenerator.op_fstp_basereg( DEF_DOUBLE, REG_ESP );
[3]25 }
26 else if(type==DEF_SINGLE){
27 //sub esp,4
[225]28 compiler.codeGenerator.op_sub_esp(4);
[3]29
30 //fstp dword ptr[esp]
[225]31 compiler.codeGenerator.op_fstp_basereg( DEF_SINGLE, REG_ESP );
[3]32 }
33 else if(type==DEF_INT64||type==DEF_QWORD){
34 //push edx
[225]35 compiler.codeGenerator.op_push(REG_EDX);
[3]36
37 //push eax
[225]38 compiler.codeGenerator.op_push(REG_EAX);
[3]39 }
40 else if(type==DEF_LONG){
41 //push eax
[225]42 compiler.codeGenerator.op_push(REG_EAX);
[3]43 }
[183]44 else if(type==DEF_INTEGER || (Smoothie::IsUnicode()&&type==DEF_CHAR)){
[3]45 //movsx ebx,ax
[225]46 compiler.codeGenerator.op_movsx_R32R16( REG_EBX, REG_EAX );
[3]47
48 //push ebx
[225]49 compiler.codeGenerator.op_push(REG_EBX);
[3]50 }
[183]51 else if(type==DEF_SBYTE || (Smoothie::IsUnicode()==false&&type==DEF_CHAR)){
[3]52 //movsx ebx,al
[225]53 compiler.codeGenerator.op_movsx_R32R8( REG_EBX, REG_EAX );
[3]54
55 //push ebx
[225]56 compiler.codeGenerator.op_push(REG_EBX);
[3]57 }
[36]58 else if(type==DEF_DWORD||type==DEF_WORD||type==DEF_BYTE||type==DEF_BOOLEAN||
[3]59 IsPtrType(type)){
60 //push eax
[225]61 compiler.codeGenerator.op_push(REG_EAX);
[3]62 }
[64]63 else{
64 SetError();
[3]65 }
66}
67
[76]68void NewStringObject( const char *str ){
[3]69 ///////////////////////////////////////////////////////
70 // lpszTextを元にStringオブジェクトを生成し、
[64]71 // オブジェクトポインタをregに格納する
[3]72 ///////////////////////////////////////////////////////
73
[76]74 char *parameter = (char *)malloc( lstrlen( str ) + 32 );
75 sprintf( parameter, "\"%s\"%c%c*Char", str, 1, ESC_AS );
[64]76 SetStringQuotes( parameter );
77
[265]78 Operator_New( *compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr(), "", parameter, Type( DEF_OBJECT, *compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr() ) );
[3]79
[64]80 free( parameter );
[3]81}
82
[97]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}
[3]99
[97]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にコピー
[225]151 compiler.codeGenerator.op_mov_RR( REG_ECX, useReg );
[97]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 ///////////////////////////////////////////////////////////////////
[206]175 vector<const UserProc *> userProcs;
[97]176
177 char methodName[VN_SIZE], lpPtrOffset[VN_SIZE], parameter[VN_SIZE], dummy[1];
[206]178 ReferenceKind refType;
[97]179 lstrcpy( methodName, member );
180 GetVarFormatString(methodName,parameter,lpPtrOffset,dummy,refType);
181
[135]182 objClass.GetMethods().Enum( methodName, userProcs );
[97]183 if(userProcs.size()){
184 //オーバーロードを解決
[206]185 const UserProc *pUserProc = OverloadSolutionWithStrParam(termFull,userProcs,parameter,termLeft);
[97]186
187 if( pUserProc ){
188
189 resultType = pUserProc->ReturnType();
190
191 {
192 //オブジェクトポインタをスタックに入れておく
193 //push reg
[225]194 compiler.codeGenerator.op_push( useReg );
[97]195
196 if( !Opcode_CallProc(parameter,pUserProc,PROCFLAG_NEW,termLeft,0 ) ){
197
198 return false;
199 }
200
[225]201 compiler.codeGenerator.op_pop();
[97]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}
[128]227bool TermOpe( const char *term, const Type &baseType, Type &resultType, bool &isLiteral, BOOL *pbUseHeap, bool isWantObject, bool *pIsClassName, bool isProcedureCallOnly ){
[97]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];
[206]243 ReferenceKind refType;
244 if( SplitMemberName( termFull, termLeft, member, refType ) ){
[97]245 ///////////////////////////////////////////////////////////////////
246 // オブジェクトとメンバに分解できるとき
247 // termLeft.member
248 ///////////////////////////////////////////////////////////////////
249
250 isLiteral = false;
251
252 // オブジェクト側の型を取得
253 bool isClassName = false;
254 Type leftType;
[128]255 if( GetTermType( termLeft, leftType, isLiteral, &isClassName ) ){
[265]256 if( isClassName == false && compiler.GetObjectModule().meta.GetBlittableTypes().IsExist( leftType ) ){
[128]257 // 左側のオブジェクト部分がBlittable型のとき
258
259 char temporary[VN_SIZE];
260 lstrcpy( temporary, termLeft );
261 sprintf( termLeft, "%s(%s)",
[265]262 compiler.GetObjectModule().meta.GetBlittableTypes().Find( leftType ).GetCreateStaticMethodFullName().c_str(),
[128]263 temporary );
264 }
265 }
266
267 if( !TermOpe( termLeft, baseType, leftType, isLiteral, pbUseHeap, true, &isClassName ) ){
[103]268 goto globalArea;
[97]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 }
[106]283globalArea:
[97]284
285
286 //////////////////////////////////////////////
287 // クラス名かどうかをチェック(静的メンバ用)
288 //////////////////////////////////////////////
289
290 if( pIsClassName ){
[265]291 if( compiler.GetObjectModule().meta.GetClasses().Find( termFull ) ){
[97]292 *pIsClassName = true;
293 return true;
294 }
295 }
296
297
298 /////////////////////////////////////////////////////////////////
299 // グローバル属性エリア
300 /////////////////////////////////////////////////////////////////
301
302 const int useReg = REG_EAX;
303
304
[122]305 if(lstrcmpi(termFull,"This")==0 && isProcedureCallOnly == false ){
[97]306 //Thisオブジェクト
[206]307 resultType.SetType( DEF_OBJECT, compiler.pCompilingClass );
[97]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 }
[208]381
[265]382 ConstMacro *pConstMacro = compiler.GetObjectModule().meta.GetGlobalConstMacros().Find( procName );
[206]383 if( pConstMacro )
384 {
385 if( pConstMacro->GetCalcBuffer( parameter, temporary ) )
386 {
387 /////////////////////////
388 // マクロ関数
389 /////////////////////////
[97]390
[206]391 //閉じカッコ")"に続く文字がNULLでないときはエラーにする
392 if(termFull[i2+1+i4+1]!='\0') SetError(42,NULL,cp);
[97]393
[206]394 //マクロ関数の場合
395 NumOpe(useReg, temporary,Type(),resultType);
[97]396
[206]397 if(!IS_LITERAL(resultType.GetIndex())){
398 //リテラル値ではなかったとき
399 isLiteral = false;
400 }
401
402 return true;
[97]403 }
404 }
405 }
[122]406 else if( isProcedureCallOnly ){
407 // 関数呼び出し以外は受け付けない
408 return false;
409 }
[97]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]
[225]515 compiler.codeGenerator.op_fld_ptr_esp( resultType.GetBasicType() );
[97]516
517 //add esp,size
[225]518 compiler.codeGenerator.op_add_esp( resultType.GetBasicSize() );
[97]519 }
520 else{
521 //pop eax
[225]522 compiler.codeGenerator.op_pop(REG_EAX);
[97]523
524 if( resultType.Is64() ){
525 //pop edx
[225]526 compiler.codeGenerator.op_pop(REG_EDX);
[97]527 }
528 }
529 return true;
530}
[76]531bool NumOpe( const char *expression,
532 const Type &baseType,
533 Type &resultType,
[254]534 BOOL *pbUseHeap )
535{
[97]536 int i,i2,i3;
537 char temporary[1024],temp2[1024];
[3]538
[76]539 if(expression[0]=='\0'){
[3]540 SetError(1,NULL,cp);
[76]541 return false;
[3]542 }
543
[93]544 if( !baseType.IsNull() && expression[0] == '[' ){
545 // リテラル配列の場合
[3]546
[93]547 if( !baseType.IsPointer() ){
548 SetError(1,NULL,cp);
549 return false;
550 }
551 Type tempBaseType( baseType );
552 tempBaseType.PtrLevelDown();
553
554 char *buffer = (char *)malloc( lstrlen( expression ) + 1 );
555 lstrcpy( buffer, expression );
556 RemoveStringBracket( buffer );
557
558 void *binary = malloc( 1 );
559 int num = 0;
560
561 i = 0;
562 while( buffer[i] ){
563 i = GetOneParameter( buffer, i, temporary );
564 if( buffer[i] == ',' ){
565 i++;
566 }
567
568 Type resultType;
569 _int64 i64data;
570 if( !StaticCalculation( true, temporary, tempBaseType.GetBasicType(), &i64data, resultType ) ){
571 return false;
572 }
573 if( !resultType.IsWhole() ){
574 // TODO: 実数に未対応
575 SetError();
576 return false;
577 }
578
579 binary = realloc( binary, ( num + 1 ) * tempBaseType.GetSize() );
580 memcpy( (char *)binary + (num * tempBaseType.GetSize()), &i64data, tempBaseType.GetSize() );
581 num++;
582 }
583
[265]584 i2 = compiler.GetObjectModule().dataTable.AddBinary( binary, num * tempBaseType.GetSize() );
[93]585
586 //mov eax,i2
[237]587 compiler.codeGenerator.op_mov_RV(REG_EAX,i2, Schedule::DataTable );
[93]588
589 free( buffer );
590
591 resultType = baseType;
592
593 //push eax
[225]594 compiler.codeGenerator.op_push( REG_EAX );
[93]595
596 return true;
597 }
598
[254]599 bool isLiteralCalculation;
600 if( !NumOpe_GetType( expression, baseType, resultType, &isLiteralCalculation ) )
601 {
602 return false;
603 }
604 if( isLiteralCalculation )
605 {
606 //右辺値が数値の定数式の場合
607 _int64 i64data;
608 StaticCalculation(true, expression,baseType.GetBasicType(),&i64data,resultType);
[93]609
[254]610 if( resultType.GetBasicSize() == sizeof(_int64) ){
611 //64ビット(符号有り整数/実数)
612
613 //push HILONG(i64data)
614 compiler.codeGenerator.op_push_V((long)*(long *)(((char *)(&i64data))+4));
615
616 //push LOLONG(i64data)
617 compiler.codeGenerator.op_push_V(*(long *)(&i64data));
618 }
619 else if( resultType.IsSingle() ){
620 //single実数
621
622 double dbl;
623 memcpy(&dbl,&i64data,sizeof(_int64));
624
625 float flt;
626 flt=(float)dbl;
627 long l;
628 memcpy(&l,&flt,sizeof(long));
629
630 //push flt
631 compiler.codeGenerator.op_push_V(l);
632 }
633 else{
634 //整数(符号有り/無し)
635
636 long l = (long)i64data;
637
638 if(resultType.GetBasicSize()==sizeof(char)) l = l & 0x000000FF;
639 if(resultType.GetBasicSize()==sizeof(short)) l = l & 0x0000FFFF;
640
641 //push term
642 compiler.codeGenerator.op_push_V(l);
643 }
644 return true;
645 }
646
647 if(expression[0]==1&& expression[1]==ESC_NEW ){
648 //New演算子(オブジェクト生成)
649
650 if( !Operator_New( expression+2, baseType, resultType ) ){
651 return false;
652 }
653
654 return true;
655 }
656
657
[3]658 /////////////////////////////////
659 // 式要素を逆ポーランド式で取得
660 /////////////////////////////////
661
662 char *values[255];
663 long calc[255];
664 long stack[255];
665 int pnum;
[76]666 if(!GetNumOpeElements(expression,&pnum,values,calc,stack)){
[3]667 for(i=0;i<pnum;i++){
668 if(values[i]) HeapDefaultFree(values[i]);
669 }
[76]670 return false;
[3]671 }
672
673
674 BOOL bError;
675 bError=0;
676
677 //リテラル値のみの計算かどうかを判別するためのフラグ
678 BOOL bLiteralCalculation=1;
679
680 //リテラル演算の場合を考慮した演算前の再配置スケジュール
681 CReloc *pobj_BackReloc;
682 pobj_BackReloc=new CReloc();
683 pobj_BackReloc->copy(pobj_Reloc);
684
685 double dbl;
686 int sp;
[76]687 int type_stack[255];
[79]688 bool isNothing_stack[255];
[3]689 LONG_PTR index_stack[255];
690 BOOL bUseHeap[255];
691 _int64 i64data;
692 for(i=0,sp=0;i<pnum;i++){
693 int idCalc;
694 idCalc=calc[i]%100;
695
696 if(idCalc){
[76]697 if(type_stack[sp-2]==DEF_OBJECT){
[79]698 if( idCalc == CALC_AS
699 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
700 && index_stack[sp-1] == index_stack[sp-2]
701 || isNothing_stack[sp-2] ){
702 // 同一の型、またはNothingに対するAsはAs演算子を呼び出さない
[3]703 }
[94]704 else if( idCalc == CALC_AS
705 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
706 && ( ((CClass *)index_stack[sp-1])->IsEqualsOrSubClass( (CClass *)index_stack[sp-2] ) || ((CClass *)index_stack[sp-2])->IsEqualsOrSubClass( (CClass *)index_stack[sp-1] )
707 )){
708 // ダウンキャストを許可する
709 }
[79]710 else{
711 //オーバーロードされたオペレータを呼び出す
712 i2=CallOperatorProc(idCalc,baseType,type_stack,index_stack,bUseHeap,sp);
713 if(i2==0){
714 if(idCalc==CALC_EQUAL) lstrcpy(temp2,"==");
715 else GetCalcName(idCalc,temp2);
716 sprintf(temporary,"Operator %s",temp2);
717 SetError(27,temporary,cp);
718 goto error;
719 }
720 else if(i2==-1) goto error;
[3]721
[79]722 continue;
723 }
[3]724 }
725
[76]726 if(!CheckCalcType(idCalc,type_stack,sp)) goto error;
[3]727 }
728
729 switch(idCalc){
730 //数値
731 case 0:
732 index_stack[sp]=-1;
[79]733 isNothing_stack[sp] = false;
[3]734 bUseHeap[sp]=0;
735
736 char *term;
737 term=values[i];
738
[97]739 if( calc[i+1]%100 == CALC_AS ){
740 // As演算子の右辺値
741 //型名
[193]742 if( Compiler::StringToType( term, resultType ) ){
[97]743 resultType.SetBasicType( resultType.GetBasicType() | FLAG_CAST );
744 }
745 else{
746 SetError(3, term, cp );
747 goto error;
748 }
749
750 type_stack[sp] = resultType.GetBasicType();
751 index_stack[sp] = resultType.GetIndex();
752 sp++;
753
754 break;
755 }
756
[3]757 if(term[0]=='\"'){
758 //リテラル文字列
759 if(!RemoveStringQuotes(term)){
760 SetError(43,NULL,cp);
761 goto error;
762 }
763 i3=lstrlen(term);
764StrLiteral:
765
[97]766 if( baseType.IsObject() || baseType.IsNull() ){
767 //要求タイプがオブジェクト、または未定のとき
[3]768
[97]769 //String型オブジェクトを生成
770 NewStringObject(term);
[3]771
[97]772 type_stack[sp]=DEF_OBJECT;
[265]773 index_stack[sp]=(LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr();
[97]774 bLiteralCalculation=0;
[3]775
[97]776 sp++;
777 break;
[3]778 }
779
780
[76]781 type_stack[sp]=typeOfPtrChar;
[3]782 bLiteralCalculation=0;
783
[265]784 i2=compiler.GetObjectModule().dataTable.AddString(term,i3);
[3]785
786 //push DataSize
[237]787 compiler.codeGenerator.op_push_V( i2, Schedule::DataTable );
[3]788 }
789 else if((term[0]=='e'||term[0]=='E')&&
790 (term[1]=='x'||term[1]=='X')&&
791 term[2]=='\"'){
792 //拡張版リテラル文字列(エスケープシーケンス可能)
793 if(!RemoveStringQuotes(term+2)){
794 SetError(43,NULL,cp);
795 goto error;
796 }
797 i3=FormatString_EscapeSequence(term+2);
798 term+=2;
799
800 goto StrLiteral;
801 }
802 else if(IsVariableTopChar(term[0])||
803 term[0]=='*'||
804 (term[0]=='.'&&IsVariableTopChar(term[1]))){
805 //////////////////
806 // 何らかの識別子
807
[97]808 bool isLiteral;
809 if( TermOpe( term, baseType, resultType, isLiteral, &bUseHeap[sp] ) ){
810 if(resultType.IsNull()){
811 //戻り値が存在しないとき
812 for(i2=0;;i2++){
813 if(term[i2]=='('||term[i2]=='\0'){
814 term[i2]=0;
815 break;
[49]816 }
817 }
[97]818 SetError(38,term,cp);
[3]819
[97]820 goto error;
821 }
[3]822
[97]823 type_stack[sp] = resultType.GetBasicType();
824 index_stack[sp] = resultType.GetIndex();
[3]825
[97]826 if( !isLiteral ){
[3]827 bLiteralCalculation=0;
[97]828 }
[3]829
[97]830 if( resultType.GetBasicType() & FLAG_CAST ){
831 // 型名のみ
832 SetError();
833 }
834 else{
835 if( resultType.IsReal() ){
836 //sub esp,size
837 //fstp ptr[esp]
[225]838 compiler.codeGenerator.op_fstp_push( resultType );
[3]839 }
[97]840 else{
841 if( resultType.Is64() ){
842 //push edx
[225]843 compiler.codeGenerator.op_push( REG_EDX );
[97]844 }
845 else{
846 ExtendTypeTo32( resultType.GetBasicType(), REG_EAX );
847 }
[3]848
[97]849 //push eax
[225]850 compiler.codeGenerator.op_push( REG_EAX );
[3]851 }
852 }
853
[97]854 sp++;
855 break;
[3]856 }
857
858
[67]859 // Nothing
860 if( lstrcmp( term, "Nothing" ) == 0 ){
[79]861 isNothing_stack[sp] = true;
862
[76]863 type_stack[sp] = DEF_OBJECT;
864 if( baseType.IsObject() ){
865 index_stack[sp] = baseType.GetIndex();
[67]866 }
867 else{
[265]868 index_stack[sp] = (LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetObjectClassPtr();
[67]869 }
[3]870
[67]871 bLiteralCalculation = 0;
872
873 //push 0
[225]874 compiler.codeGenerator.op_push_V( 0 );
[67]875
876 sp++;
877 break;
878 }
879
[3]880
881 //////////////
882 // 定数の場合
883 //////////////
884
[265]885 i3 = compiler.GetObjectModule().meta.GetGlobalConsts().GetBasicType(term);
[8]886 if(i3){
[265]887 if( compiler.GetObjectModule().meta.GetGlobalConsts().IsStringPtr( term ) ){
[103]888 //リテラル文字列
889
[265]890 double dbl = compiler.GetObjectModule().meta.GetGlobalConsts().GetDoubleData(term);
[103]891 memcpy(&i64data,&dbl,sizeof(double));
892
893 //バイト数
894 i3=lstrlen((char *)i64data);
895
896 memcpy(term,(char *)i64data,i3);
897 term[i3]=0;
898 goto StrLiteral;
899 }
900
[76]901 type_stack[sp]=i3;
[3]902 if(IsRealNumberType(i3)){
903 //実数
[265]904 double dbl = compiler.GetObjectModule().meta.GetGlobalConsts().GetDoubleData(term);
[3]905 memcpy(&i64data,&dbl,sizeof(double));
906 goto Literal;
907 }
908 else if(IsWholeNumberType(i3)){
909 //整数
[265]910 i64data = compiler.GetObjectModule().meta.GetGlobalConsts().GetWholeData(term);
[3]911 goto Literal;
912 }
913 else{
914 SetError(300,NULL,cp);
915 goto error;
916 }
917 }
918
919
920 //該当する識別子が見当たらないときはエラー扱いにする
921 bError=1;
922 SetError(3,term,cp);
[76]923 type_stack[sp]=DEF_DOUBLE;
[3]924 }
925 else{
926 //リテラル値
[76]927 type_stack[sp]=GetLiteralValue(term,&i64data,baseType.GetBasicType());
[3]928Literal:
[76]929 if(type_stack[sp]==DEF_INT64||
930 type_stack[sp]==DEF_QWORD||
931 type_stack[sp]==DEF_DOUBLE){
[3]932 //64ビット(符号有り整数/実数)
933
934 //push HILONG(dbl)
[225]935 compiler.codeGenerator.op_push_V((long)*(long *)(((char *)(&i64data))+4));
[3]936
937 //push LOLONG(dbl)
[225]938 compiler.codeGenerator.op_push_V(*(long *)(&i64data));
[3]939 }
[76]940 else if(type_stack[sp]==DEF_SINGLE){
[3]941 //single実数
942
943 float flt;
944 memcpy(&dbl,&i64data,sizeof(double));
945 flt=(float)dbl;
946 memcpy(&i3,&flt,sizeof(long));
947
948 //push term
[225]949 compiler.codeGenerator.op_push_V(i3);
[3]950 }
951 else{
952 //その他
953
954 //push term
[225]955 compiler.codeGenerator.op_push_V((long)i64data);
[3]956
957 if((long)i64data==0) index_stack[sp]=LITERAL_NULL;
958 }
959
960
961 //リテラル値の種類
[76]962 if(Is64Type(type_stack[sp])==0&&IsRealNumberType(type_stack[sp])==0){
[3]963 //整数(符号有り/無し)
964
965 index_stack[sp]=GetLiteralIndex(i64data);
966 }
967 }
968 sp++;
969 break;
970
971 //論理演算子
972 case CALC_XOR:
973 //value[sp-2] xor= value[sp-1]
974 //xor演算
[76]975 if(!Calc_Xor(type_stack,index_stack,&sp)) goto error;
[3]976 break;
977 case CALC_OR:
978 //value[sp-2] or= value[sp-1]
979 //or演算
[76]980 if(!Calc_Or(type_stack,index_stack,&sp)) goto error;
[3]981 break;
982 case CALC_AND:
983 //value[sp-2] and= value[sp-1]
984 //and演算
[76]985 if(!Calc_And(type_stack,index_stack,&sp)) goto error;
[3]986 break;
987 case CALC_NOT:
988 //value[sp-1]=Not value[sp-1]
989 //NOT演算子
[76]990 if(!Calc_Not(type_stack,sp)) goto error;
[3]991 break;
992
993 //比較演算子
994 case CALC_PE:
995 //value[sp-2]<=value[sp-1]
[76]996 if(!Calc_Relation_PE(type_stack,index_stack,&sp)) goto error;
[3]997 break;
998 case CALC_QE:
999 //value[sp-2]>=value[sp-1]
[76]1000 if(!Calc_Relation_QE(type_stack,index_stack,&sp)) goto error;
[3]1001 break;
1002 case CALC_P:
1003 //value[sp-2]<value[sp-1]
[76]1004 if(!Calc_Relation_P(type_stack,index_stack,&sp)) goto error;
[3]1005 break;
1006 case CALC_Q:
1007 //value[sp-2]>value[sp-1]
[76]1008 if(!Calc_Relation_Q(type_stack,index_stack,&sp)) goto error;
[3]1009 break;
1010 case CALC_NOTEQUAL:
1011 //value[sp-2]<>value[sp-1]
[76]1012 if(!Calc_Relation_NotEqual(type_stack,&sp)) goto error;
[3]1013 break;
1014 case CALC_EQUAL:
1015 //value[sp-2]=value[sp-1]
[76]1016 if(!Calc_Relation_Equal(type_stack,&sp)) goto error;
[3]1017 break;
1018
1019 //ビットシフト
1020 case CALC_SHL:
1021 //value[sp-2]=value[sp-2]<<value[sp-1]
[76]1022 if(!Calc_SHL(type_stack,&sp)) goto error;
[3]1023 break;
1024 case CALC_SHR:
1025 //value[sp-2]=value[sp-2]>>value[sp-1]
[76]1026 if(!Calc_SHR(type_stack,&sp)) goto error;
[3]1027 break;
1028
1029 //算術演算
1030 case CALC_ADDITION:
1031 case CALC_SUBTRACTION:
1032 case CALC_PRODUCT:
[76]1033 if(!CalcTwoTerm_Arithmetic(idCalc,type_stack,index_stack,&sp)) goto error;
[3]1034 break;
1035
1036 case CALC_MOD:
1037 //value[sp-2]%=value[sp-1]
1038 //剰余演算
[76]1039 if(!Calc_Mod(type_stack,&sp)) goto error;
[3]1040 break;
1041 case CALC_QUOTIENT:
1042 //value[sp-2]/=value[sp-1];
1043 //除算
[76]1044 if(!Calc_Divide(type_stack,&sp,baseType.GetBasicType())) goto error;
[3]1045 break;
1046 case CALC_INTQUOTIENT:
1047 //value[sp-2]/=value[sp-1]
1048 //整数除算
[76]1049 if(!Calc_IntDivide(type_stack,index_stack,&sp)) goto error;
[3]1050 break;
1051 case CALC_MINUSMARK:
1052 //value[sp-1]=-value[sp-1]
1053 //符号反転
[76]1054 if(!Calc_MinusMark(type_stack,sp)) goto error;
[3]1055 index_stack[sp-1]=-1;
1056 break;
1057 case CALC_POWER:
1058 //べき乗演算(浮動小数点演算のみ)
[76]1059 if(!Calc_Power(type_stack,&sp)) goto error;
[3]1060 break;
1061 case CALC_AS:
1062 //キャスト
[76]1063 if(!Calc_Cast(type_stack,index_stack,&sp)) goto error;
[3]1064 break;
1065
[41]1066 case CALC_BYVAL:
1067 //ポインタ型→参照型
[76]1068 if( PTR_LEVEL( type_stack[sp-1] ) <= 0 ){
[41]1069 //ポインタ型ではないとき
1070 SetError( 3, NULL, cp );
1071 goto error;
1072 }
1073
[76]1074 type_stack[sp-1] = PTR_LEVEL_DOWN( type_stack[sp-1] );
[41]1075
1076 break;
1077
[3]1078 default:
1079 SetError(300,NULL,cp);
[41]1080 goto error;
[3]1081 }
1082 }
1083
1084 if(bError) goto error;
1085
1086 if(sp!=1){
1087 SetError(1,NULL,cp);
1088 goto error;
1089 }
1090
1091 if(bLiteralCalculation){
1092 //右辺値が数値の定数式の場合
[254]1093 SetError();
[3]1094 }
1095 else{
1096 //右辺値が数値の定数式ではないとき
1097 if(IS_LITERAL(index_stack[0])) index_stack[0]=-1;
1098 }
1099
1100 if(pbUseHeap) *pbUseHeap=bUseHeap[0];
1101
[76]1102 resultType.SetType( type_stack[0], index_stack[0] );
1103
1104 bool isSuccessful = true;
[3]1105 goto finish;
1106
1107
1108error:
[76]1109 isSuccessful = false;
[3]1110 goto finish;
1111
1112
1113finish:
1114
1115 for(i=0;i<pnum;i++){
1116 if(values[i]) HeapDefaultFree(values[i]);
1117 }
1118
1119 //再配置スケジュールバックアップ情報を解放
1120 delete pobj_BackReloc;
1121
[76]1122 return isSuccessful;
[3]1123}
Note: See TracBrowser for help on using the repository browser.