#include "stdafx.h" #include #include "../BasicCompiler_Common/common.h" #include "Opcode.h" void NewStringObject(int reg, const char *str){ /////////////////////////////////////////////////////// // lpszTextを元にStringオブジェクトを生成し、 // オブジェクトポインタをregに格納する /////////////////////////////////////////////////////// ////////////////////////////////////////////////////// ///// レジスタ資源のバックアップ { BACKUP_REGISTER_RESOURCE ////////////////////////////////////////////////////// char *parameter = (char *)malloc( lstrlen( str ) + 32 ); sprintf( parameter, "%s%c%c*Char", str, 1, ESC_AS ); Operator_New( *compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr(), "", parameter, Type( DEF_OBJECT, *compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr() ) ); free( parameter ); //mov reg,rax compiler.codeGenerator.op_mov_RR( reg, REG_RAX ); ///////////////////////////////////////////// ////// レジスタ資源を復元 RESTORE_REGISTER_RESOURCE }//////////////////////////////////////////// } void SetUseRegFromRax(int type,int UseReg,int XmmReg){ if(IsRealNumberType(type)){ //実数型 if(XmmReg==REG_XMM4){ if(type==DEF_DOUBLE){ //movsd qword ptr[rsp+offset],xmm0 ※スタックフレームを利用 pobj_sf->push(REG_XMM0,sizeof(double)); } if(type==DEF_SINGLE){ //movss dword ptr[rsp+offset],xmm0 ※スタックフレームを利用 pobj_sf->push(REG_XMM0,sizeof(float)); } } else{ if(type==DEF_DOUBLE){ //movsd xmm_reg,xmm0 compiler.codeGenerator.op_movsd_RR(XmmReg,REG_XMM0); } else if(type==DEF_SINGLE){ //movss xmm_reg,xmm0 compiler.codeGenerator.op_movss_RR(XmmReg,REG_XMM0); } } } else{ //整数型 if(UseReg==REG_R14){ //mov qword ptr[rsp+offset],rax ※スタックフレームを利用 pobj_sf->push(REG_RAX); } else{ //mov reg,rax compiler.codeGenerator.op_mov_RR(UseReg,REG_RAX); } } } void ExtendRegToBigType( int reg, int bigBasicType, int baseBasicType ){ switch( Type::GetBasicSize( bigBasicType ) ){ case sizeof(_int64): ExtendTypeTo64(baseBasicType,reg); break; case sizeof(long): ExtendTypeTo32(baseBasicType,reg); break; case sizeof(short): ExtendTypeTo16(baseBasicType,reg); break; } } bool VarToReg( RELATIVE_VAR &relativeVar, const Type &baseType, Type &resultType ){ int UseReg=pobj_reg->GetNextReg(); int XmmReg=pobj_reg->GetNextXmmReg(); //大きな型への暗黙の変換 int bigType = AutoBigCast(baseType.GetBasicType(),resultType.GetBasicType()); if(resultType.GetBasicType()&FLAG_PTR){ //配列ポインタ resultType.SetBasicType( GetPtrType(resultType.GetBasicType()^FLAG_PTR) ); SetVarPtrToReg(UseReg,&relativeVar); } else if(resultType.IsReal()){ //実数型 if( resultType.IsDouble() ) SetXmmReg_DoubleVariable(&relativeVar,XmmReg); if( resultType.IsSingle() ) SetXmmReg_SingleVariable(&relativeVar,XmmReg); } else if( resultType.IsWhole() || resultType.IsObject()){ //整数型 SetReg_WholeVariable(resultType,&relativeVar,UseReg); } else if( resultType.IsStruct() ){ //構造体ポインタをUseRegへ格納(構造体は値型) SetVarPtrToReg(UseReg,&relativeVar); } else{ return false; } if( resultType.GetBasicType() != bigType ){ // 大きな型へ変換された場合 // ※レジスタの値をキャストする ExtendRegToBigType( UseReg, bigType, resultType.GetBasicType() ); resultType.SetBasicType( bigType ); } return true; } bool 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 ) { const CClass &objClass = leftType.GetClass(); const int useReg=pobj_reg->GetNextReg(); const int xmmReg=pobj_reg->GetNextXmmReg(); //////////////////////////////// // インデクサ(getアクセサ) //////////////////////////////// char VarName[VN_SIZE],ArrayElements[VN_SIZE]; GetArrayElement(member,VarName,ArrayElements); if(ArrayElements[0]){ Type classType; if( VarName[0] == '\0' ) { classType = leftType; if( classType.IsObject() ) { // 既にuseRegにオブジェクトポインタが格納されており、それに対するインデクサを呼び出す場合 // ※「プロパティ値として返ってきたオブジェクトインスタンスのインデクサを呼び出す」場合にここにくる // オブジェクトメンバのポインタは既にraxに入っている } } else { GetMemberType( leftType, VarName, classType, 0, false ); if( classType.IsObject() ) { //オブジェクトポインタをr11にコピー compiler.codeGenerator.op_mov_RR( REG_R11, useReg ); RELATIVE_VAR tempRelativeVar; tempRelativeVar.dwKind=VAR_DIRECTMEM; if( !_member_offset( true, //エラー表示あり false, //読み込み専用 leftType, VarName,&tempRelativeVar,classType,0)){ return false; } // オブジェクトメンバのポインタをraxにコピー if( !VarToReg( tempRelativeVar, baseType, resultType ) ){ compiler.errorMessenger.Output(11,termFull,cp); } } } if( classType.IsObject() ) { ////////////////////////////////////////////////////// ///// レジスタ資源のバックアップ { BACKUP_REGISTER_RESOURCE ////////////////////////////////////////////////////// //オブジェクトポインタをスタックに入れておく //mov qword ptr[rsp+offset],reg ※スタックフレームを利用 pobj_sf->push( useReg ); char objectFullName[VN_SIZE], dummyArrayElements[VN_SIZE]; GetArrayElement(termFull,objectFullName,dummyArrayElements); CallIndexerGetterProc(useReg,classType,objectFullName, ArrayElements,resultType, PROCFLAG_NEW ); pobj_sf->pop(); ///////////////////////////////////////////// ////// レジスタ資源を復元 RESTORE_REGISTER_RESOURCE }//////////////////////////////////////////// return true; } } /////////////////////////////////////////////////////////////////// // メンバを検索 /////////////////////////////////////////////////////////////////// if( GetMemberType( leftType, member, resultType, 0, false ) ){ // メンバが見つかったとき if( isNeedHeapFreeStructure ) { if( !leftType.IsStruct() ) { compiler.errorMessenger.OutputFatalError(); } pobj_reg->LockReg(); // 親となる構造体が一時メモリに存在していた場合、後ほど解放する必要がある compiler.codeGenerator.op_AddNeedFreeTempStructure( useReg ); isNeedHeapFreeStructure = false; pobj_reg->UnlockReg(); } //オブジェクトポインタをr11にコピー compiler.codeGenerator.op_mov_RR( REG_R11, useReg ); relativeVar.dwKind=VAR_DIRECTMEM; if( !_member_offset( true, //エラー表示あり false, //読み込み専用 leftType, member,&relativeVar,resultType,0)){ return false; } // 変数として扱う isVariable = true; return true; } /////////////////////////////////////////////////////////////////// // 動的メソッドを検索 /////////////////////////////////////////////////////////////////// std::vector userProcs; char methodName[VN_SIZE], lpPtrOffset[VN_SIZE], parameter[VN_SIZE], dummy[1]; ReferenceKind refType; PareOrBracket pareOrBracket = None; lstrcpy( methodName, member ); GetVarFormatString( methodName, parameter, lpPtrOffset, dummy, refType, &pareOrBracket ); objClass.EnumDynamicMethodsOrInterfaceMethods( methodName, userProcs ); if(userProcs.size()){ //オーバーロードを解決 const UserProc *pUserProc = OverloadSolutionWithStrParam(termFull,userProcs,parameter,termLeft); if( pUserProc ) { if( pUserProc->Params().size() == 0 // 仮引数の個数は0 && parameter[0] // 実引数は1つ以上 && pUserProc->ReturnType().IsObject() // 戻り値がクラス型の場合 && pareOrBracket == Bracket ) // 実引数は[]で囲まれている { // プロパティ値として返ってきたオブジェクトインスタンスのインデクサを呼び出す // まずはプロパティ値を取得 bool dummyIsVariable; RELATIVE_VAR dummyRelativeVar; TermMemberOpe( leftType, isNeedHeapFreeStructure, baseType, resultType, termFull, termLeft, methodName, dummyIsVariable, dummyRelativeVar ); // 戻り値のオブジェクトインスタンスのインデクサを呼び出す char temporary[VN_SIZE], temp2[VN_SIZE]; sprintf( temporary, "[%s]", parameter ); sprintf( temp2, "%s.%s", termLeft, methodName ); Type classType = resultType; return TermMemberOpe( classType, isNeedHeapFreeStructure, baseType, resultType, termFull, temp2, temporary, isVariable, relativeVar ); } resultType = pUserProc->ReturnType(); ////////////////////////////////////////////////////// ///// レジスタ資源のバックアップ { BACKUP_REGISTER_RESOURCE ////////////////////////////////////////////////////// //オブジェクトポインタをスタックに入れておく //mov qword ptr[rsp+offset],reg ※スタックフレームを利用 pobj_sf->push( useReg ); if( !Opcode_CallProc(parameter,pUserProc,PROCFLAG_NEW,termLeft ) ){ //レジスタ資源を復元 RESTORE_REGISTER_RESOURCE return false; } pobj_sf->pop(); ///////////////////// // 戻り値の処理 ///////////////////// //大きな型への暗黙の変換 int bigType = AutoBigCast(baseType.GetBasicType(), resultType.GetBasicType() ); if( resultType.GetBasicType() != bigType ){ // 大きな型へ変換された場合 // ※レジスタの値をキャストする ExtendRegToBigType( REG_RAX, bigType, resultType.GetBasicType() ); resultType.SetBasicType( bigType ); } SetUseRegFromRax(resultType.GetBasicType(),useReg,xmmReg); // 型パラメータを解決 ResolveFormalGenericTypeParameter( resultType, leftType, pUserProc ); ///////////////////////////////////////////// ////// レジスタ資源を復元 RESTORE_REGISTER_RESOURCE }//////////////////////////////////////////// return true; } } else if( pareOrBracket == Pare ) { // 関数ポインタ compiler.errorMessenger.OutputFatalError(); /////////////////////////////////////////////////////////////////// // メンバを検索 /////////////////////////////////////////////////////////////////// if( GetMemberType( leftType, methodName, resultType, 0, false ) ){ // メンバが見つかったとき } } compiler.errorMessenger.OutputFatalError(); return false; } bool _TermOpe( const char *term, const Type &baseType, Type &resultType, bool &isLiteral, bool &isNeedHeapFreeStructure, bool *pIsClassName, bool isProcedureCallOnly, bool &isVariable, RELATIVE_VAR &relativeVar, bool isWriteAccess ) { char parameter[VN_SIZE]; // Withを解決 char termFull[VN_SIZE]; if(term[0]=='.'){ GetWithName(termFull); lstrcat(termFull,term); } else lstrcpy(termFull,term); char termLeft[VN_SIZE]; lstrcpy(termLeft,termFull); // パース char member[VN_SIZE]; ReferenceKind refType; if( SplitMemberName( termFull, termLeft, member, refType ) ){ /////////////////////////////////////////////////////////////////// // オブジェクトとメンバに分解できるとき // termLeft.member /////////////////////////////////////////////////////////////////// isLiteral = false; // オブジェクト側の型を取得 bool isClassName = false; Type leftType; if( GetTermType( termLeft, Type(), leftType, isLiteral, &isClassName ) ){ if( isClassName == false && compiler.GetObjectModule().meta.GetBlittableTypes().IsExist( leftType ) ){ // 左側のオブジェクト部分がBlittable型のとき char temporary[VN_SIZE]; lstrcpy( temporary, termLeft ); sprintf( termLeft, "%s(%s)", compiler.GetObjectModule().meta.GetBlittableTypes().Find( leftType ).GetCreateStaticMethodFullName().c_str(), temporary ); } } if( !TermOpe( termLeft, baseType, leftType, isLiteral, isNeedHeapFreeStructure, &isClassName ) ){ goto globalArea; } if( isClassName ){ // 静的メンバ/メソッドの場合 goto globalArea; } if( !leftType.HasMember() ){ // メンバを持たない型の場合 if( isProcedureCallOnly ) { compiler.errorMessenger.Output(1,NULL,cp); } return false; } return TermMemberOpe( leftType, isNeedHeapFreeStructure, baseType, resultType, termFull, termLeft, member, isVariable, relativeVar ); } globalArea: ////////////////////////////////////////////// // クラス名かどうかをチェック(静的メンバ用) ////////////////////////////////////////////// if( pIsClassName ){ if( compiler.GetObjectModule().meta.FindClassSupportedTypeDef( termFull ) ){ *pIsClassName = true; return true; } } ///////////////////////////////////////////////////////////////// // グローバル属性エリア ///////////////////////////////////////////////////////////////// int UseReg=pobj_reg->GetNextReg(); int XmmReg=pobj_reg->GetNextXmmReg(); if(lstrcmpi(termFull,"This")==0 && isProcedureCallOnly == false ){ if( !compiler.IsCompilingClass() ) { compiler.errorMessenger.Output(142,NULL,cp); return false; } //Thisオブジェクト resultType.SetType( DEF_OBJECT, &compiler.GetCompilingClass() ); SetThisPtrToReg( UseReg ); isLiteral = false; return true; } ////////////////////////////////////// // 関数(DLL、ユーザー定義、組み込み) ////////////////////////////////////// char procName[VN_SIZE]; char temporary[8192]; int i2=GetCallProcName(termFull,procName); if(termFull[i2]=='('){ int i4=GetStringInPare_RemovePare(parameter,termFull+i2+1); void *pInfo; int idProc=GetProc(procName,(void **)&pInfo); if(idProc){ //閉じカッコ")"に続く文字がNULLでないとき if(termFull[i2+1+i4+1]!='\0'){ compiler.errorMessenger.Output(42,NULL,cp); } ////////////////////////////////////////////////////// ///// レジスタ資源のバックアップ { BACKUP_REGISTER_RESOURCE ////////////////////////////////////////////////////// //////////////// // 呼び出し //////////////// CallProc(idProc,pInfo,procName,parameter, baseType,resultType); ///////////////////// // 戻り値の処理 ///////////////////// //大きな型への暗黙の変換 int bigType = AutoBigCast(baseType.GetBasicType(), resultType.GetBasicType() ); if( resultType.GetBasicType() != bigType ){ // 大きな型へ変換された場合 // ※レジスタの値をキャストする ExtendRegToBigType( REG_RAX, bigType, resultType.GetBasicType() ); resultType.SetBasicType( bigType ); } SetUseRegFromRax(resultType.GetBasicType(),UseReg,XmmReg); ///////////////////////////////////////////// ////// レジスタ資源を復元 RESTORE_REGISTER_RESOURCE }//////////////////////////////////////////// if(resultType.IsStruct()) { //構造体が戻ったときはヒープ領域にインスタンスが格納されている //※後にfreeする必要あり // TODO: 解放はGCに任せる isNeedHeapFreeStructure = true; } isLiteral = false; return true; } ConstMacro *pConstMacro = compiler.GetObjectModule().meta.GetGlobalConstMacros().Find( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( procName ) ); if( pConstMacro ) { if( ActiveBasic::Compiler::LexicalAnalyzer::ConstMacroToExpression( *pConstMacro, parameter, temporary ) ) { ///////////////////////// // マクロ関数 ///////////////////////// //閉じカッコ")"に続く文字がNULLでないときはエラーにする if(termFull[i2+1+i4+1]!='\0') compiler.errorMessenger.Output(42,NULL,cp); //マクロ関数の場合 NumOpe(&UseReg, temporary,Type(),resultType); if(!IS_LITERAL(resultType.GetIndex())){ //リテラル値ではなかったとき isLiteral = false; } return true; } } } else if( isProcedureCallOnly ){ // 関数呼び出し以外は受け付けない return false; } //////////////////////////////// // インデクサ(getアクセサ) //////////////////////////////// char VarName[VN_SIZE],ArrayElements[VN_SIZE]; GetArrayElement(termFull,VarName,ArrayElements); if(ArrayElements[0]){ Type classType; GetVarType(VarName,classType,false); if( classType.IsObject() ) { CallIndexerGetterProc(UseReg,classType,VarName,ArrayElements,resultType); isLiteral = false; return true; } } //////////////////////////////// // 変数 //////////////////////////////// if(GetVarOffset( false, //エラー表示なし isWriteAccess, termFull, &relativeVar,resultType)){ ////////// // 変数 ////////// // 変数として扱う isVariable = true; isLiteral = false; return true; } /* //////////////////////////////// // 型名 //////////////////////////////// if( compiler.StringToType( termFull, resultType ) ){ resultType.SetBasicType( resultType.GetBasicType() | FLAG_CAST ); return true; }*/ ///////////////////////////////// // プロパティ用のメソッド ///////////////////////////////// //配列要素を排除 GetArrayElement(termFull,VarName,ArrayElements); if(GetSubHash(VarName,0)){ ////////////////////////////////////////////////////// ///// レジスタ資源のバックアップ { BACKUP_REGISTER_RESOURCE ////////////////////////////////////////////////////// CallPropertyMethod(termFull,NULL,resultType); //大きな型への暗黙の変換 int bigType = AutoBigCast(baseType.GetBasicType(), resultType.GetBasicType() ); if( resultType.GetBasicType() != bigType ){ // 大きな型へ変換された場合 // ※レジスタの値をキャストする ExtendRegToBigType( REG_RAX, bigType, resultType.GetBasicType() ); resultType.SetBasicType( bigType ); } SetUseRegFromRax(resultType.GetBasicType(),UseReg,XmmReg); ///////////////////////////////////////////// ////// レジスタ資源を復元 RESTORE_REGISTER_RESOURCE }//////////////////////////////////////////// if(resultType.IsStruct()) { //構造体が戻ったときはヒープ領域にインスタンスが格納されている //※後にfreeする必要あり // TODO: 解放はGCに任せる isNeedHeapFreeStructure = true; } isLiteral = false; return true; } if( isProcedureCallOnly ) { compiler.errorMessenger.Output(3, termLeft, cp ); } return false; } bool TermOpe( const char *term, const Type &baseType, Type &resultType, bool &isLiteral, bool &isNeedHeapFreeStructure, bool *pIsClassName, bool isProcedureCallOnly, bool isWriteAccess ) { bool isInitRegSwitch = false; if( !pobj_reg ) { isInitRegSwitch = true; //作業用レジスタを取得 pobj_reg = new CRegister( REG_RAX ); } //エラー時の復旧用 CRegister objReg_Backup = *pobj_reg; RELATIVE_VAR relativeVar; bool isVariable = false; bool result = _TermOpe( term, baseType, resultType, isLiteral, isNeedHeapFreeStructure, pIsClassName, isProcedureCallOnly, isVariable, relativeVar, isWriteAccess ); if( isVariable ) { // 変数の場合はeaxに変数ポインタを格納する if( !VarToReg( relativeVar, baseType, resultType ) ){ compiler.errorMessenger.Output(11,term,cp); } } if( !result ) { *pobj_reg = objReg_Backup; } if( isInitRegSwitch ){ //整合性をチェック(バグ回避) if( result ) { pobj_reg->bug_check(); } //作業レジスタを解放 delete pobj_reg; pobj_reg = NULL; } return result; } bool TermOpeOnlyVariable( const char *term, Type &resultType, RELATIVE_VAR &relativeVar, bool isWriteAccess ) { if( pobj_reg ) { compiler.errorMessenger.OutputFatalError(); } //作業用レジスタを取得 pobj_reg = new CRegister( REG_NON ); bool isLiteral, isVariable = false, isNeedHeapFreeStructure = false; bool result = _TermOpe( term, Type(), resultType, isLiteral, isNeedHeapFreeStructure, NULL, false, isVariable, relativeVar, isWriteAccess ); if( !isVariable ) { compiler.errorMessenger.OutputFatalError(); } //整合性をチェック(バグ回避) if( result ) { pobj_reg->bug_check(); } //作業レジスタを解放 delete pobj_reg; pobj_reg=0; return result; } bool _numope( int *pReg, const char *expression, const Type &baseType, Type &resultType, bool *pbIsNeedHeapFreeStructure ) { int i,i2,i3; char temporary[1024],temp2[1024]; if(expression[0]=='\0'){ compiler.errorMessenger.Output(1,NULL,cp); return false; } if( !baseType.IsNull() && expression[0] == '[' ){ // リテラル配列の場合 int dataTableOffset; if( !compiler.GetObjectModule().dataTable.MakeLiteralArrayBuffer( expression, baseType, dataTableOffset ) ) { return false; } //mov reg,i2 compiler.codeGenerator.op_mov_RV( sizeof(_int64), *pReg, dataTableOffset, Schedule::DataTable ); resultType = baseType; return true; } bool isLiteralCalculation; if( NumOpe_GetType( expression, baseType, resultType, &isLiteralCalculation ) ) { if( isLiteralCalculation ) { //右辺値が数値の定数式の場合 _int64 i64data; StaticCalculation(true, expression,baseType.GetBasicType(),&i64data,resultType); if(resultType.IsReal()){ if(baseType.IsReal()) resultType=baseType; int xmmReg = pobj_reg->GetNextXmmReg(); *pReg = xmmReg; if(resultType.IsDouble()){ i3 = compiler.GetObjectModule().dataTable.Add( i64data ); //movlpd xmm_reg,qword ptr[data table offset] compiler.codeGenerator.op_movlpd_RM( xmmReg, 0, i3, MOD_DISP32, Schedule::DataTable ); } if(resultType.IsSingle()){ double dbl; memcpy(&dbl,&i64data,sizeof(_int64)); float flt; int i32data; flt=(float)dbl; memcpy(&i32data,&flt,sizeof(long)); i3 = compiler.GetObjectModule().dataTable.Add( i32data ); //movss xmm_reg,dword ptr[data table offset] compiler.codeGenerator.op_movss_RM( xmmReg, 0, i3, MOD_DISP32, Schedule::DataTable ); } } else{ if(!resultType.Is64()){ //整数(符号有り/無し) i3=(long)i64data; if(resultType.GetBasicSize()==sizeof(char)) i3=i3&0x000000FF; if(resultType.GetBasicSize()==sizeof(short)) i3=i3&0x0000FFFF; i64data=(_int64)i3; } //mov reg,i64data compiler.codeGenerator.op_mov_RV64(*pReg,i64data); } return true; } } if( expression[0] == 1 ) { if( expression[1]==ESC_NEW ) { //New演算子(オブジェクト生成) if( pobj_BlockReg->check(REG_RAX) ){ compiler.errorMessenger.OutputFatalError(); } ////////////////////////////////////////////////////// ///// レジスタ資源のバックアップ { BACKUP_REGISTER_RESOURCE ////////////////////////////////////////////////////// if( !Operator_New( expression+2, baseType, resultType ) ){ return false; } ///////////////////////////////////////////// ////// レジスタ資源を復元 RESTORE_REGISTER_RESOURCE }//////////////////////////////////////////// //mov reg,rax compiler.codeGenerator.op_mov_RR( *pReg, REG_RAX ); return true; } else if( expression[1] == ESC_SYSTEM_STATIC_NEW ) { // 静的領域にオブジェクトを作る // 静的領域にオブジェクトを生成 int dataTableOffset; if( !compiler.GetObjectModule().dataTable.MakeConstObjectToProcessStaticBuffer( expression + 2, resultType, dataTableOffset ) ) { return false; } //mov reg,i2 compiler.codeGenerator.op_mov_RV( sizeof(_int64), *pReg, dataTableOffset, Schedule::DataTable); return true; } } ///////////////////////////////// // 式要素を逆ポーランド式で取得 ///////////////////////////////// char *values[255]; long calc[255]; long stack[255]; int pnum; if(!GetNumOpeElements(expression,&pnum,values,calc,stack)){ for(i=0;iIsEqualsOrSubClass( (CClass *)index_stack[sp-2] ) || ((CClass *)index_stack[sp-2])->IsEqualsOrSubClass( (CClass *)index_stack[sp-1] ) )){ // ダウンキャストを許可する } else{ //オーバーロードされたオペレータを呼び出す i2=CallOperatorProc(idCalc,baseType,type_stack,index_stack,isNeedHeapFreeStructureStack,sp); if(i2==0){ if(idCalc==CALC_EQUAL) lstrcpy(temp2,"=="); else GetCalcName(idCalc,temp2); sprintf(temporary,"Operator %s",temp2); compiler.errorMessenger.Output(27,temporary,cp); goto error; } else if(i2==-1) goto error; continue; } } if(!CheckCalcType(idCalc,type_stack,sp)) goto error; } switch(idCalc){ //数値 case 0: index_stack[sp]=-1; isNothing_stack[sp] = false; isNeedHeapFreeStructureStack[sp] = false; UseReg=pobj_reg->GetNextReg(); XmmReg=pobj_reg->GetNextXmmReg(); bXmm=0; char *term; term=values[i]; if( calc[i+1]%100 == CALC_AS ){ // As演算子の右辺値 //型名 if( compiler.StringToType( term, resultType ) ){ resultType.SetBasicType( resultType.GetBasicType() | FLAG_CAST ); } else{ compiler.errorMessenger.Output(3, term, cp ); goto error; } type_stack[sp] = resultType.GetBasicType(); index_stack[sp] = resultType.GetIndex(); sp++; break; } if( (term[0]=='e'||term[0]=='E') && (term[1]=='x'||term[1]=='X') && term[2]=='\"' || term[0] == '\"' ) { bool isEx = true; if( term[0] == '\"' ) { isEx = false; } if( isEx ) { // 拡張版リテラル文字列(エスケープシーケンス可能) if(!RemoveStringQuotes(term+2)){ compiler.errorMessenger.Output(43,NULL,cp); goto error; } i3=FormatString_EscapeSequence(term+2); term+=2; } else { // 通常文字列 if(!RemoveStringQuotes(term)){ compiler.errorMessenger.Output(43,NULL,cp); goto error; } i3=lstrlen(term); } if( !baseType.IsPointer() ) { //要求タイプがオブジェクト、または未定のとき //String型オブジェクトを生成 i2 = compiler.GetObjectModule().dataTable.MakeConstStringObjectToProcessStaticBuffer( term ); //mov reg,i2 compiler.codeGenerator.op_mov_RV(sizeof(_int64),UseReg,i2, Schedule::DataTable); type_stack[sp]=DEF_OBJECT; index_stack[sp]=(LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr(); bLiteralCalculation=0; if(bXmm) pobj_reg->LockXmmReg(); else pobj_reg->LockReg(); sp++; break; } StrLiteral: type_stack[sp]=typeOfPtrChar; bLiteralCalculation=0; i2 = compiler.GetObjectModule().dataTable.AddString( term, i3 ); //mov reg,i2 compiler.codeGenerator.op_mov_RV(sizeof(_int64),UseReg,i2, Schedule::DataTable); if(UseReg==REG_R14){ //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用 pobj_sf->push(REG_R14); } } else if(IsVariableTopChar(term[0])|| term[0]=='*'|| (term[0]=='.'&&IsVariableTopChar(term[1]))) { ////////////////// // 何らかの識別子 bool isLiteral; if( TermOpe( term, baseType, resultType, isLiteral, isNeedHeapFreeStructureStack[sp] ) ){ if(resultType.IsNull()){ //戻り値が存在しないとき for(i2=0;;i2++){ if(term[i2]=='('||term[i2]=='\0'){ term[i2]=0; break; } } compiler.errorMessenger.Output(38,term,cp); goto error; } type_stack[sp] = resultType.GetBasicType(); index_stack[sp] = resultType.GetIndex(); if( !isLiteral ){ bLiteralCalculation=0; } if( resultType.GetBasicType() & FLAG_CAST ){ // 型名のみ compiler.errorMessenger.OutputFatalError(); } else{ if( resultType.IsReal() == false && UseReg==REG_R14 ){ //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用 pobj_sf->push(REG_R14); } if( resultType.IsReal() && XmmReg==REG_XMM4 ){ if(resultType.IsDouble()){ //movsd qword ptr[rsp+offset],xmm4 ※スタックフレームを利用 pobj_sf->push(REG_XMM4,sizeof(double)); } if(resultType.IsSingle()){ //movss dword ptr[rsp+offset],xmm4 ※スタックフレームを利用 pobj_sf->push(REG_XMM4,sizeof(float)); } } if( resultType.IsReal() ){ pobj_reg->LockXmmReg(); } else{ pobj_reg->LockReg(); } } sp++; break; } // Nothing if( lstrcmp( term, "Nothing" ) == 0 ){ isNothing_stack[sp] = true; type_stack[sp] = DEF_OBJECT; if( baseType.IsObject() ){ index_stack[sp] = baseType.GetIndex(); } else{ index_stack[sp] = (LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetObjectClassPtr(); } bLiteralCalculation = 0; //xor reg,reg compiler.codeGenerator.op_zero_reg( UseReg ); if(UseReg==REG_R14){ //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用 pobj_sf->push(REG_R14); } pobj_reg->LockReg(); sp++; break; } ////////////// // 定数の場合 ////////////// i3 = compiler.GetObjectModule().meta.GetGlobalConsts().GetBasicType( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term ) ); if(i3){ if( compiler.GetObjectModule().meta.GetGlobalConsts().IsStringPtr( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term ) ) ){ //リテラル文字列 if( baseType.IsObject() || baseType.IsNull() ) { //要求タイプがオブジェクト、または未定のとき //String型オブジェクトを生成 NewStringObject(UseReg,term); type_stack[sp]=DEF_OBJECT; index_stack[sp]=(LONG_PTR)compiler.GetObjectModule().meta.GetClasses().GetStringClassPtr(); bLiteralCalculation=0; if(bXmm) pobj_reg->LockXmmReg(); else pobj_reg->LockReg(); sp++; break; } double dbl = compiler.GetObjectModule().meta.GetGlobalConsts().GetDoubleData( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term ) ); memcpy(&i64data,&dbl,sizeof(double)); //バイト数 i3=lstrlen((char *)i64data); memcpy(term,(char *)i64data,i3); term[i3]=0; goto StrLiteral; } type_stack[sp] = i3; if(IsRealNumberType(i3)){ //実数 double dbl = compiler.GetObjectModule().meta.GetGlobalConsts().GetDoubleData( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term ) ); memcpy(&i64data,&dbl,sizeof(double)); goto Literal; } else if(IsWholeNumberType(i3)){ //整数 i64data = compiler.GetObjectModule().meta.GetGlobalConsts().GetWholeData( ActiveBasic::Compiler::LexicalAnalyzer::FullNameToSymbol( term ) ); goto Literal; } else{ compiler.errorMessenger.Output(1,NULL,0); goto error; } } //該当する識別子が見当たらないときはエラー扱いにする bError=1; compiler.errorMessenger.Output(3,term,cp); type_stack[sp]=DEF_DOUBLE; } else{ //リテラル値 type_stack[sp]=GetLiteralValue(term,&i64data,baseType.GetBasicType()); Literal: if(type_stack[sp]==DEF_DOUBLE){ //64ビット浮動小数型 bXmm=1; if(XmmReg==REG_XMM4){ //mov r14,i64data compiler.codeGenerator.op_mov_RV64(REG_R14,i64data); //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用 pobj_sf->push(REG_R14); } else{ i3 = compiler.GetObjectModule().dataTable.Add( i64data ); //movlpd xmm_reg,qword ptr[data table offset] compiler.codeGenerator.op_movlpd_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable ); } } else if(type_stack[sp]==DEF_SINGLE){ //32ビット浮動小数型 bXmm=1; float flt; int i32data; memcpy(&dbl,&i64data,sizeof(double)); flt=(float)dbl; memcpy(&i32data,&flt,sizeof(long)); if(XmmReg==REG_XMM4){ compiler.errorMessenger.OutputFatalError(); // TODO: 未実装 //push term //compiler.codeGenerator.op_push_value(i32data); } else{ i3=compiler.GetObjectModule().dataTable.Add( i32data ); //movss xmm_reg,dword ptr[data table offset] compiler.codeGenerator.op_movss_RM( XmmReg, 0, i3, MOD_DISP32, Schedule::DataTable ); } } else{ //整数 index_stack[sp]=GetLiteralIndex(i64data); //mov reg,i64data compiler.codeGenerator.op_mov_RV64(UseReg,i64data); if(UseReg==REG_R14){ //mov qword ptr[rsp+offset],r14 ※スタックフレームを利用 pobj_sf->push(REG_R14); } } } if(bXmm) pobj_reg->LockXmmReg(); else pobj_reg->LockReg(); sp++; break; //論理演算子 case CALC_XOR: case CALC_OR: case CALC_AND: if(!CalcTwoTerm_Logical(idCalc,type_stack,index_stack,&sp)) goto error; break; case CALC_NOT: //value[sp-1]=Not value[sp-1] //NOT演算子 if(!Calc_Not(type_stack,sp)) goto error; break; //比較演算子 case CALC_PE: //value[sp-2] <= value[sp-1] case CALC_QE: //value[sp-2] >= value[sp-1] case CALC_P: //value[sp-2] < value[sp-1] case CALC_Q: //value[sp-2] > value[sp-1] case CALC_NOTEQUAL: //value[sp-2] <> value[sp-1] case CALC_EQUAL: //value[sp-2] = value[sp-1] if(!CalcTwoTerm_Relational(idCalc,type_stack,index_stack,&sp)) goto error; break; //ビットシフト case CALC_SHL: //value[sp-2] << value[sp-1] case CALC_SHR: //value[sp-2] >> value[sp-1] if(!Calc_Shift(idCalc,type_stack,&sp)) goto error; break; //算術演算 case CALC_ADDITION: case CALC_SUBTRACTION: case CALC_PRODUCT: if(!CalcTwoTerm_Arithmetic(idCalc,type_stack,index_stack,&sp)) goto error; break; case CALC_MOD: //value[sp-2]%=value[sp-1] //剰余演算 if(!Calc_Mod(type_stack,index_stack,&sp)) goto error; break; case CALC_QUOTIENT: //value[sp-2]/=value[sp-1]; //除算 if(!Calc_Divide(type_stack,&sp,baseType.GetBasicType())) goto error; break; case CALC_INTQUOTIENT: //value[sp-2]/=value[sp-1] //整数除算 if(!Calc_IntDivide(type_stack,index_stack,&sp)) goto error; break; case CALC_MINUSMARK: //value[sp-1]=-value[sp-1] //符号反転 if(!Calc_MinusMark(type_stack,sp)) goto error; break; case CALC_POWER: //べき乗演算(浮動小数点演算のみ) if(!Calc_Power(type_stack,&sp)) goto error; break; case CALC_AS: //キャスト if(!Calc_Cast(type_stack,index_stack,&sp)) goto error; break; case CALC_BYVAL: //ポインタ型→参照型 if( PTR_LEVEL( type_stack[sp-1] ) <= 0 ){ //ポインタ型ではないとき compiler.errorMessenger.Output( 3, NULL, cp ); goto error; } type_stack[sp-1] = PTR_LEVEL_DOWN( type_stack[sp-1] ); break; default: compiler.errorMessenger.Output(300,NULL,cp); goto error; } } if(bError) goto error; if(sp!=1){ compiler.errorMessenger.Output(1,NULL,cp); goto error; } if(bLiteralCalculation){ compiler.errorMessenger.OutputFatalError(); } else{ //右辺値が数値の定数式ではないとき if(IS_LITERAL(index_stack[0])) index_stack[0]=-1; } if( pbIsNeedHeapFreeStructure ) { *pbIsNeedHeapFreeStructure = isNeedHeapFreeStructureStack[0]; } if(IsRealNumberType(type_stack[0])) *pReg=pobj_reg->UnlockXmmReg(); else *pReg=pobj_reg->UnlockReg(); resultType.SetType( type_stack[0], index_stack[0] ); bool isSuccessful = true; goto finish; ////////////////// // エラー処理 ////////////////// error: isSuccessful = false; goto finish; finish: for(i=0;iGetNextReg(); bool result = _numope( pReg, expression, baseType, resultType, pbIsNeedHeapFreeStructure ); if( !result ) { *pobj_reg = objReg_Backup; } if( isInitRegSwitch ){ //整合性をチェック(バグ回避) if( result ) { pobj_reg->bug_check(); } //作業レジスタを解放 delete pobj_reg; pobj_reg = NULL; } return result; }