source: dev/trunk/ab5.0/abdev/compiler_x86/NumOpe.cpp@ 591

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

DataTable::AddWStringメソッドを追加。

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