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

Last change on this file since 254 was 254, checked in by dai_9181, 17 years ago
File size: 27.2 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( !baseType.IsNull() && expression[0] == '[' ){
545 // リテラル配列の場合
546
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
584 i2 = compiler.GetDataTable().AddBinary( binary, num * tempBaseType.GetSize() );
585
586 //mov eax,i2
587 compiler.codeGenerator.op_mov_RV(REG_EAX,i2, Schedule::DataTable );
588
589 free( buffer );
590
591 resultType = baseType;
592
593 //push eax
594 compiler.codeGenerator.op_push( REG_EAX );
595
596 return true;
597 }
598
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);
609
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
658 /////////////////////////////////
659 // 式要素を逆ポーランド式で取得
660 /////////////////////////////////
661
662 char *values[255];
663 long calc[255];
664 long stack[255];
665 int pnum;
666 if(!GetNumOpeElements(expression,&pnum,values,calc,stack)){
667 for(i=0;i<pnum;i++){
668 if(values[i]) HeapDefaultFree(values[i]);
669 }
670 return false;
671 }
672
673
674 BOOL bError;
675 bError=0;
676
677 //リテラル値のみの計算かどうかを判別するためのフラグ
678 BOOL bLiteralCalculation=1;
679
680 //リテラル演算の場合を考慮した演算前のバッファ位置
681 extern int obp;
682 int BeforeObp = obp;
683
684 //リテラル演算の場合を考慮した演算前のプロシージャスケジュール位置
685 //※64ビットの掛け算、除算などで特殊関数が呼ばれるため
686 int Before_ProcAddrScheduleNum;
687 Before_ProcAddrScheduleNum=pobj_SubAddrSchedule->num;
688
689 //リテラル演算の場合を考慮した演算前のデータテーブルスケジュール位置
690 int Before_DataTableScheduleNum;
691 Before_DataTableScheduleNum=pobj_DataTableSchedule->num;
692
693 //リテラル演算の場合を考慮した演算前の再配置スケジュール
694 CReloc *pobj_BackReloc;
695 pobj_BackReloc=new CReloc();
696 pobj_BackReloc->copy(pobj_Reloc);
697
698 double dbl;
699 int sp;
700 int type_stack[255];
701 bool isNothing_stack[255];
702 LONG_PTR index_stack[255];
703 BOOL bUseHeap[255];
704 _int64 i64data;
705 for(i=0,sp=0;i<pnum;i++){
706 int idCalc;
707 idCalc=calc[i]%100;
708
709 if(idCalc){
710 if(type_stack[sp-2]==DEF_OBJECT){
711 if( idCalc == CALC_AS
712 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
713 && index_stack[sp-1] == index_stack[sp-2]
714 || isNothing_stack[sp-2] ){
715 // 同一の型、またはNothingに対するAsはAs演算子を呼び出さない
716 }
717 else if( idCalc == CALC_AS
718 && type_stack[sp-1] == ( DEF_OBJECT | FLAG_CAST )
719 && ( ((CClass *)index_stack[sp-1])->IsEqualsOrSubClass( (CClass *)index_stack[sp-2] ) || ((CClass *)index_stack[sp-2])->IsEqualsOrSubClass( (CClass *)index_stack[sp-1] )
720 )){
721 // ダウンキャストを許可する
722 }
723 else{
724 //オーバーロードされたオペレータを呼び出す
725 i2=CallOperatorProc(idCalc,baseType,type_stack,index_stack,bUseHeap,sp);
726 if(i2==0){
727 if(idCalc==CALC_EQUAL) lstrcpy(temp2,"==");
728 else GetCalcName(idCalc,temp2);
729 sprintf(temporary,"Operator %s",temp2);
730 SetError(27,temporary,cp);
731 goto error;
732 }
733 else if(i2==-1) goto error;
734
735 continue;
736 }
737 }
738
739 if(!CheckCalcType(idCalc,type_stack,sp)) goto error;
740 }
741
742 switch(idCalc){
743 //数値
744 case 0:
745 index_stack[sp]=-1;
746 isNothing_stack[sp] = false;
747 bUseHeap[sp]=0;
748
749 char *term;
750 term=values[i];
751
752 if( calc[i+1]%100 == CALC_AS ){
753 // As演算子の右辺値
754 //型名
755 if( Compiler::StringToType( term, resultType ) ){
756 resultType.SetBasicType( resultType.GetBasicType() | FLAG_CAST );
757 }
758 else{
759 SetError(3, term, cp );
760 goto error;
761 }
762
763 type_stack[sp] = resultType.GetBasicType();
764 index_stack[sp] = resultType.GetIndex();
765 sp++;
766
767 break;
768 }
769
770 if(term[0]=='\"'){
771 //リテラル文字列
772 if(!RemoveStringQuotes(term)){
773 SetError(43,NULL,cp);
774 goto error;
775 }
776 i3=lstrlen(term);
777StrLiteral:
778
779 if( baseType.IsObject() || baseType.IsNull() ){
780 //要求タイプがオブジェクト、または未定のとき
781
782 //String型オブジェクトを生成
783 NewStringObject(term);
784
785 type_stack[sp]=DEF_OBJECT;
786 index_stack[sp]=(LONG_PTR)compiler.GetMeta().GetClasses().GetStringClassPtr();
787 bLiteralCalculation=0;
788
789 sp++;
790 break;
791 }
792
793
794 type_stack[sp]=typeOfPtrChar;
795 bLiteralCalculation=0;
796
797 i2=compiler.GetDataTable().AddString(term,i3);
798
799 //push DataSize
800 compiler.codeGenerator.op_push_V( i2, Schedule::DataTable );
801 }
802 else if((term[0]=='e'||term[0]=='E')&&
803 (term[1]=='x'||term[1]=='X')&&
804 term[2]=='\"'){
805 //拡張版リテラル文字列(エスケープシーケンス可能)
806 if(!RemoveStringQuotes(term+2)){
807 SetError(43,NULL,cp);
808 goto error;
809 }
810 i3=FormatString_EscapeSequence(term+2);
811 term+=2;
812
813 goto StrLiteral;
814 }
815 else if(IsVariableTopChar(term[0])||
816 term[0]=='*'||
817 (term[0]=='.'&&IsVariableTopChar(term[1]))){
818 //////////////////
819 // 何らかの識別子
820
821 bool isLiteral;
822 if( TermOpe( term, baseType, resultType, isLiteral, &bUseHeap[sp] ) ){
823 if(resultType.IsNull()){
824 //戻り値が存在しないとき
825 for(i2=0;;i2++){
826 if(term[i2]=='('||term[i2]=='\0'){
827 term[i2]=0;
828 break;
829 }
830 }
831 SetError(38,term,cp);
832
833 goto error;
834 }
835
836 type_stack[sp] = resultType.GetBasicType();
837 index_stack[sp] = resultType.GetIndex();
838
839 if( !isLiteral ){
840 bLiteralCalculation=0;
841 }
842
843 if( resultType.GetBasicType() & FLAG_CAST ){
844 // 型名のみ
845 SetError();
846 }
847 else{
848 if( resultType.IsReal() ){
849 //sub esp,size
850 //fstp ptr[esp]
851 compiler.codeGenerator.op_fstp_push( resultType );
852 }
853 else{
854 if( resultType.Is64() ){
855 //push edx
856 compiler.codeGenerator.op_push( REG_EDX );
857 }
858 else{
859 ExtendTypeTo32( resultType.GetBasicType(), REG_EAX );
860 }
861
862 //push eax
863 compiler.codeGenerator.op_push( REG_EAX );
864 }
865 }
866
867 sp++;
868 break;
869 }
870
871
872 // Nothing
873 if( lstrcmp( term, "Nothing" ) == 0 ){
874 isNothing_stack[sp] = true;
875
876 type_stack[sp] = DEF_OBJECT;
877 if( baseType.IsObject() ){
878 index_stack[sp] = baseType.GetIndex();
879 }
880 else{
881 index_stack[sp] = (LONG_PTR)compiler.GetMeta().GetClasses().GetObjectClassPtr();
882 }
883
884 bLiteralCalculation = 0;
885
886 //push 0
887 compiler.codeGenerator.op_push_V( 0 );
888
889 sp++;
890 break;
891 }
892
893
894 //////////////
895 // 定数の場合
896 //////////////
897
898 i3 = compiler.GetMeta().GetGlobalConsts().GetBasicType(term);
899 if(i3){
900 if( compiler.GetMeta().GetGlobalConsts().IsStringPtr( term ) ){
901 //リテラル文字列
902
903 double dbl = compiler.GetMeta().GetGlobalConsts().GetDoubleData(term);
904 memcpy(&i64data,&dbl,sizeof(double));
905
906 //バイト数
907 i3=lstrlen((char *)i64data);
908
909 memcpy(term,(char *)i64data,i3);
910 term[i3]=0;
911 goto StrLiteral;
912 }
913
914 type_stack[sp]=i3;
915 if(IsRealNumberType(i3)){
916 //実数
917 double dbl = compiler.GetMeta().GetGlobalConsts().GetDoubleData(term);
918 memcpy(&i64data,&dbl,sizeof(double));
919 goto Literal;
920 }
921 else if(IsWholeNumberType(i3)){
922 //整数
923 i64data = compiler.GetMeta().GetGlobalConsts().GetWholeData(term);
924 goto Literal;
925 }
926 else{
927 SetError(300,NULL,cp);
928 goto error;
929 }
930 }
931
932
933 //該当する識別子が見当たらないときはエラー扱いにする
934 bError=1;
935 SetError(3,term,cp);
936 type_stack[sp]=DEF_DOUBLE;
937 }
938 else{
939 //リテラル値
940 type_stack[sp]=GetLiteralValue(term,&i64data,baseType.GetBasicType());
941Literal:
942 if(type_stack[sp]==DEF_INT64||
943 type_stack[sp]==DEF_QWORD||
944 type_stack[sp]==DEF_DOUBLE){
945 //64ビット(符号有り整数/実数)
946
947 //push HILONG(dbl)
948 compiler.codeGenerator.op_push_V((long)*(long *)(((char *)(&i64data))+4));
949
950 //push LOLONG(dbl)
951 compiler.codeGenerator.op_push_V(*(long *)(&i64data));
952 }
953 else if(type_stack[sp]==DEF_SINGLE){
954 //single実数
955
956 float flt;
957 memcpy(&dbl,&i64data,sizeof(double));
958 flt=(float)dbl;
959 memcpy(&i3,&flt,sizeof(long));
960
961 //push term
962 compiler.codeGenerator.op_push_V(i3);
963 }
964 else{
965 //その他
966
967 //push term
968 compiler.codeGenerator.op_push_V((long)i64data);
969
970 if((long)i64data==0) index_stack[sp]=LITERAL_NULL;
971 }
972
973
974 //リテラル値の種類
975 if(Is64Type(type_stack[sp])==0&&IsRealNumberType(type_stack[sp])==0){
976 //整数(符号有り/無し)
977
978 index_stack[sp]=GetLiteralIndex(i64data);
979 }
980 }
981 sp++;
982 break;
983
984 //論理演算子
985 case CALC_XOR:
986 //value[sp-2] xor= value[sp-1]
987 //xor演算
988 if(!Calc_Xor(type_stack,index_stack,&sp)) goto error;
989 break;
990 case CALC_OR:
991 //value[sp-2] or= value[sp-1]
992 //or演算
993 if(!Calc_Or(type_stack,index_stack,&sp)) goto error;
994 break;
995 case CALC_AND:
996 //value[sp-2] and= value[sp-1]
997 //and演算
998 if(!Calc_And(type_stack,index_stack,&sp)) goto error;
999 break;
1000 case CALC_NOT:
1001 //value[sp-1]=Not value[sp-1]
1002 //NOT演算子
1003 if(!Calc_Not(type_stack,sp)) goto error;
1004 break;
1005
1006 //比較演算子
1007 case CALC_PE:
1008 //value[sp-2]<=value[sp-1]
1009 if(!Calc_Relation_PE(type_stack,index_stack,&sp)) goto error;
1010 break;
1011 case CALC_QE:
1012 //value[sp-2]>=value[sp-1]
1013 if(!Calc_Relation_QE(type_stack,index_stack,&sp)) goto error;
1014 break;
1015 case CALC_P:
1016 //value[sp-2]<value[sp-1]
1017 if(!Calc_Relation_P(type_stack,index_stack,&sp)) goto error;
1018 break;
1019 case CALC_Q:
1020 //value[sp-2]>value[sp-1]
1021 if(!Calc_Relation_Q(type_stack,index_stack,&sp)) goto error;
1022 break;
1023 case CALC_NOTEQUAL:
1024 //value[sp-2]<>value[sp-1]
1025 if(!Calc_Relation_NotEqual(type_stack,&sp)) goto error;
1026 break;
1027 case CALC_EQUAL:
1028 //value[sp-2]=value[sp-1]
1029 if(!Calc_Relation_Equal(type_stack,&sp)) goto error;
1030 break;
1031
1032 //ビットシフト
1033 case CALC_SHL:
1034 //value[sp-2]=value[sp-2]<<value[sp-1]
1035 if(!Calc_SHL(type_stack,&sp)) goto error;
1036 break;
1037 case CALC_SHR:
1038 //value[sp-2]=value[sp-2]>>value[sp-1]
1039 if(!Calc_SHR(type_stack,&sp)) goto error;
1040 break;
1041
1042 //算術演算
1043 case CALC_ADDITION:
1044 case CALC_SUBTRACTION:
1045 case CALC_PRODUCT:
1046 if(!CalcTwoTerm_Arithmetic(idCalc,type_stack,index_stack,&sp)) goto error;
1047 break;
1048
1049 case CALC_MOD:
1050 //value[sp-2]%=value[sp-1]
1051 //剰余演算
1052 if(!Calc_Mod(type_stack,&sp)) goto error;
1053 break;
1054 case CALC_QUOTIENT:
1055 //value[sp-2]/=value[sp-1];
1056 //除算
1057 if(!Calc_Divide(type_stack,&sp,baseType.GetBasicType())) goto error;
1058 break;
1059 case CALC_INTQUOTIENT:
1060 //value[sp-2]/=value[sp-1]
1061 //整数除算
1062 if(!Calc_IntDivide(type_stack,index_stack,&sp)) goto error;
1063 break;
1064 case CALC_MINUSMARK:
1065 //value[sp-1]=-value[sp-1]
1066 //符号反転
1067 if(!Calc_MinusMark(type_stack,sp)) goto error;
1068 index_stack[sp-1]=-1;
1069 break;
1070 case CALC_POWER:
1071 //べき乗演算(浮動小数点演算のみ)
1072 if(!Calc_Power(type_stack,&sp)) goto error;
1073 break;
1074 case CALC_AS:
1075 //キャスト
1076 if(!Calc_Cast(type_stack,index_stack,&sp)) goto error;
1077 break;
1078
1079 case CALC_BYVAL:
1080 //ポインタ型→参照型
1081 if( PTR_LEVEL( type_stack[sp-1] ) <= 0 ){
1082 //ポインタ型ではないとき
1083 SetError( 3, NULL, cp );
1084 goto error;
1085 }
1086
1087 type_stack[sp-1] = PTR_LEVEL_DOWN( type_stack[sp-1] );
1088
1089 break;
1090
1091 default:
1092 SetError(300,NULL,cp);
1093 goto error;
1094 }
1095 }
1096
1097 if(bError) goto error;
1098
1099 if(sp!=1){
1100 SetError(1,NULL,cp);
1101 goto error;
1102 }
1103
1104 if(bLiteralCalculation){
1105 //右辺値が数値の定数式の場合
1106 SetError();
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.