Changeset 248 in dev for trunk/abdev/BasicCompiler_Common
- Timestamp:
- Jul 29, 2007, 12:33:04 PM (17 years ago)
- Location:
- trunk/abdev/BasicCompiler_Common
- Files:
-
- 6 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/abdev/BasicCompiler_Common/Compile.cpp
r247 r248 5 5 #include <jenga/include/smoothie/SmoothieException.h> 6 6 7 #include <LexicalScop ingImpl.h>7 #include <LexicalScope.h> 8 8 #include <CodeGenerator.h> 9 9 #include <Compiler.h> … … 173 173 case ESC_EXITWHILE: 174 174 { 175 CScope *pScope = GetLexicalScopes().SearchScope(SCOPE_TYPE_WHILE );175 LexicalScope *pScope = compiler.codeGenerator.lexicalScopes.SearchScope( LexicalScope::SCOPE_TYPE_WHILE ); 176 176 if( !pScope ){ 177 177 SetError(12,"Exit While",cp); … … 183 183 case ESC_EXITFOR: 184 184 { 185 CScope *pScope = GetLexicalScopes().SearchScope(SCOPE_TYPE_FOR );185 LexicalScope *pScope = compiler.codeGenerator.lexicalScopes.SearchScope( LexicalScope::SCOPE_TYPE_FOR ); 186 186 if( !pScope ){ 187 187 SetError(12,"Exit For",cp); … … 193 193 case ESC_EXITDO: 194 194 { 195 CScope *pScope = GetLexicalScopes().SearchScope(SCOPE_TYPE_DO );195 LexicalScope *pScope = compiler.codeGenerator.lexicalScopes.SearchScope( LexicalScope::SCOPE_TYPE_DO ); 196 196 if( !pScope ){ 197 197 SetError(12,"Exit Do",cp); -
trunk/abdev/BasicCompiler_Common/VariableOpe.cpp
r232 r248 5 5 6 6 #include <Compiler.h> 7 #include <LexicalScop ingImpl.h>7 #include <LexicalScope.h> 8 8 #include <Variable.h> 9 9 #include <NamespaceSupporter.h> … … 989 989 990 990 //レキシカルスコープ 991 pVar->SetScopeLevel( GetLexicalScopes().GetNowLevel() );992 pVar->SetScopeStartAddress( GetLexicalScopes().GetStartAddress() );991 pVar->SetScopeLevel( compiler.codeGenerator.lexicalScopes.GetNowLevel() ); 992 pVar->SetScopeStartAddress( compiler.codeGenerator.lexicalScopes.GetStartAddress() ); 993 993 pVar->bLiving=TRUE; 994 994 -
trunk/abdev/BasicCompiler_Common/include/CodeGenerator.h
r247 r248 2 2 3 3 #include <NativeCode.h> 4 #include <LexicalScope.h> 4 5 5 6 #ifdef _AMD64_ … … 8 9 #include "../../BasicCompiler32/MachineFixed.h" 9 10 #endif 10 11 11 12 12 void ReallocNativeCodeBuffer(); … … 51 51 }; 52 52 53 // コード生成時の部分的なスケジューリング 54 class PertialSchedule 55 { 56 int codePos; // バッファ位置 57 int typeSize; // 対象サイズ(一般的には8bit/32bit) 58 59 int _obpOld; // 未完成 60 public: 61 PertialSchedule( int codePos, int typeSize ) 62 : codePos( codePos ) 63 , typeSize( typeSize ) 64 { 65 extern int obp; 66 _obpOld = obp; 67 } 68 ~PertialSchedule() 69 { 70 } 71 72 int GetCodePos() const 73 { 74 return codePos; 75 } 76 int GetTypeSize() const 77 { 78 return typeSize; 79 } 80 int GetObpOld() const 81 { 82 return _obpOld; 83 } 84 }; 85 typedef std::vector<const PertialSchedule *> PertialSchedules; 86 87 class LexicalScope 88 { 89 public: 90 enum SCOPE_TYPE{ 91 //ベース 92 SCOPE_TYPE_BASE, 93 94 //分岐 95 SCOPE_TYPE_IF, 96 97 //ループ 98 SCOPE_TYPE_DO, 99 SCOPE_TYPE_FOR, 100 SCOPE_TYPE_WHILE, 101 102 //ケース分け 103 SCOPE_TYPE_SELECT, 104 }; 105 106 private: 107 int level; 108 int StartAddress; 109 SCOPE_TYPE TypeOfStatement; 110 111 PertialSchedules breakPertialSchedules; 112 113 public: 114 LexicalScope( int level, int addr, SCOPE_TYPE TypeOfStatement ) 115 : level( level ) 116 , StartAddress( addr ) 117 , TypeOfStatement( TypeOfStatement ) 118 { 119 } 120 ~LexicalScope() 121 { 122 } 123 124 int GetStartAddress() 125 { 126 return StartAddress; 127 } 128 SCOPE_TYPE GetTypeOfStatement() 129 { 130 return TypeOfStatement; 131 } 132 133 void Break(); 134 void RunScheduleOfBreak(); 135 }; 136 137 class LexicalScopes 138 { 139 LexicalScope **ppScopes; 140 int level; 141 142 public: 143 144 LexicalScopes(){ 145 ppScopes = (LexicalScope **)malloc( 1 ); 146 level=0; 147 } 148 ~LexicalScopes(){ 149 free( ppScopes ); 150 } 151 152 //初期化(関数コンパイルの開始時に呼び出される) 153 void Init(int addr); 154 155 // スコープを開始 156 void Start( int addr, LexicalScope::SCOPE_TYPE TypeOfStatement ); 157 158 // スコープを検索 159 LexicalScope *SearchScope( LexicalScope::SCOPE_TYPE TypeOfStatement ); 160 161 int GetNowLevel(void); 162 void SetNowLevel( int level ); 163 int GetStartAddress(void); 164 165 void End(); 166 167 //スコープ終了時のデストラクタ呼び出し 168 void CallDestructorsOfScopeEnd(); 169 170 //Returnステートメント用のデストラクタ呼び出し 171 void CallDestructorsOfReturn( int BaseLevel = 0 ); 172 }; 173 53 174 class CodeGenerator 54 175 { 55 176 NativeCode *pNativeCode; 56 177 57 public:58 59 // コード生成時の部分的なスケジューリング60 class PertialSchedule61 {62 int codePos; // バッファ位置63 int typeSize; // 対象サイズ(一般的には8bit/32bit)64 65 int _obpOld; // 未完成66 public:67 PertialSchedule( int codePos, int typeSize )68 : codePos( codePos )69 , typeSize( typeSize )70 {71 extern int obp;72 _obpOld = obp;73 }74 ~PertialSchedule()75 {76 }77 78 int GetCodePos() const79 {80 return codePos;81 }82 int GetTypeSize() const83 {84 return typeSize;85 }86 int GetObpOld() const87 {88 return _obpOld;89 }90 };91 92 178 private: 93 179 // 部分スケジュールの管理 94 typedef std::vector<const PertialSchedule *> PertialSchedules;95 180 PertialSchedules pertialSchedules; 96 181 … … 107 192 // Gotoスケジュールの管理 108 193 std::vector<GotoLabelSchedule> gotoLabelSchedules; 194 195 // レキシカルスコープの管理 196 LexicalScopes lexicalScopes; 109 197 110 198 CodeGenerator() -
trunk/abdev/BasicCompiler_Common/include/LexicalScope.h
r245 r248 2 2 3 3 #include <jenga/include/smoothie/Smoothie.h> 4 #include <jenga/include/smoothie/LexicalScoping.h>5 4 6 class ScopeImpl : public CScope 7 { 8 public: 9 ScopeImpl( int level, int addr, SCOPE_TYPE TypeOfStatement ) 10 : CScope( level, addr, TypeOfStatement ) 11 { 12 } 13 ~ScopeImpl(); 5 #include <CodeGenerator.h> 14 6 15 virtual void Break();16 virtual void RunScheduleOfBreak();17 };18 19 class LexicalScopesImpl : public CLexicalScopes20 {21 virtual CScope *CreateScope( int level, int addr, SCOPE_TYPE TypeOfStatement )22 {23 return new ScopeImpl( level, addr, TypeOfStatement );24 }25 26 public:27 28 virtual void End();29 30 //スコープ終了時のデストラクタ呼び出し31 virtual void CallDestructorsOfScopeEnd();32 33 //Returnステートメント用のデストラクタ呼び出し34 virtual void CallDestructorsOfReturn( int BaseLevel = 0 );35 };36 37 LexicalScopesImpl &GetLexicalScopes(); -
trunk/abdev/BasicCompiler_Common/src/CommonCodeGenerator.cpp
r247 r248 33 33 bool isSuccessful = false; 34 34 35 CodeGenerator::PertialSchedules::iterator it = pertialSchedules.begin();35 PertialSchedules::iterator it = pertialSchedules.begin(); 36 36 while( it != pertialSchedules.end() ) 37 37 { … … 84 84 } 85 85 } 86 const CodeGenerator::PertialSchedule *CodeGenerator::__jmp_op_format( char opcode, long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )86 const PertialSchedule *CodeGenerator::__jmp_op_format( char opcode, long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset ) 87 87 { 88 88 long beginCodePos = pNativeCode->GetSize(); … … 158 158 return pPertialSchedule; 159 159 } 160 const CodeGenerator::PertialSchedule *CodeGenerator::op_jle( long offset, int op_size, bool isPertialSchedule )160 const PertialSchedule *CodeGenerator::op_jle( long offset, int op_size, bool isPertialSchedule ) 161 161 { 162 162 return __jmp_op_format( (char)0x0E, offset, op_size, isPertialSchedule ); 163 163 } 164 const CodeGenerator::PertialSchedule *CodeGenerator::op_jbe( long offset, int op_size, bool isPertialSchedule )164 const PertialSchedule *CodeGenerator::op_jbe( long offset, int op_size, bool isPertialSchedule ) 165 165 { 166 166 return __jmp_op_format( (char)0x06, offset, op_size, isPertialSchedule ); 167 167 } 168 const CodeGenerator::PertialSchedule *CodeGenerator::op_jge( long offset, int op_size, bool isPertialSchedule )168 const PertialSchedule *CodeGenerator::op_jge( long offset, int op_size, bool isPertialSchedule ) 169 169 { 170 170 return __jmp_op_format( (char)0x0D, offset, op_size, isPertialSchedule ); 171 171 } 172 const CodeGenerator::PertialSchedule *CodeGenerator::op_jae( long offset, int op_size, bool isPertialSchedule )172 const PertialSchedule *CodeGenerator::op_jae( long offset, int op_size, bool isPertialSchedule ) 173 173 { 174 174 return __jmp_op_format( (char)0x03, offset, op_size, isPertialSchedule ); 175 175 } 176 const CodeGenerator::PertialSchedule *CodeGenerator::op_jl( long offset, int op_size, bool isPertialSchedule )176 const PertialSchedule *CodeGenerator::op_jl( long offset, int op_size, bool isPertialSchedule ) 177 177 { 178 178 return __jmp_op_format( (char)0x0C, offset, op_size, isPertialSchedule ); 179 179 } 180 const CodeGenerator::PertialSchedule *CodeGenerator::op_jb( long offset, int op_size, bool isPertialSchedule )180 const PertialSchedule *CodeGenerator::op_jb( long offset, int op_size, bool isPertialSchedule ) 181 181 { 182 182 return __jmp_op_format( (char)0x02, offset, op_size, isPertialSchedule ); 183 183 } 184 const CodeGenerator::PertialSchedule *CodeGenerator::op_jg( long offset, int op_size, bool isPertialSchedule )184 const PertialSchedule *CodeGenerator::op_jg( long offset, int op_size, bool isPertialSchedule ) 185 185 { 186 186 return __jmp_op_format( (char)0x0F, offset, op_size, isPertialSchedule ); 187 187 } 188 const CodeGenerator::PertialSchedule *CodeGenerator::op_ja( long offset, int op_size, bool isPertialSchedule )188 const PertialSchedule *CodeGenerator::op_ja( long offset, int op_size, bool isPertialSchedule ) 189 189 { 190 190 return __jmp_op_format( (char)0x07, offset, op_size, isPertialSchedule ); 191 191 } 192 const CodeGenerator::PertialSchedule *CodeGenerator::op_jne( long offset, int op_size, bool isPertialSchedule )192 const PertialSchedule *CodeGenerator::op_jne( long offset, int op_size, bool isPertialSchedule ) 193 193 { 194 194 return __jmp_op_format( (char)0x05, offset, op_size, isPertialSchedule ); 195 195 } 196 const CodeGenerator::PertialSchedule *CodeGenerator::op_je( long offset, int op_size, bool isPertialSchedule )196 const PertialSchedule *CodeGenerator::op_je( long offset, int op_size, bool isPertialSchedule ) 197 197 { 198 198 return __jmp_op_format( (char)0x04, offset, op_size, isPertialSchedule ); 199 199 } 200 const CodeGenerator::PertialSchedule *CodeGenerator::op_jmp( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset )200 const PertialSchedule *CodeGenerator::op_jmp( long offset, int op_size, bool isPertialSchedule, bool isSelfOpcodeOffset ) 201 201 { 202 202 return __jmp_op_format( (char)0xEB, offset, op_size, isPertialSchedule, isSelfOpcodeOffset ); -
trunk/abdev/BasicCompiler_Common/src/LexicalScope.cpp
r245 r248 1 1 #include "stdafx.h" 2 2 3 #include <LexicalScop ingImpl.h>3 #include <LexicalScope.h> 4 4 #include <Compiler.h> 5 5 … … 13 13 14 14 15 void ScopeImpl::Break(){15 void LexicalScope::Break(){ 16 16 //未解放のローカルオブジェクトを解放する 17 GetLexicalScopes().CallDestructorsOfReturn( level );17 compiler.codeGenerator.lexicalScopes.CallDestructorsOfReturn( level ); 18 18 19 19 //jmp ...(Next addr) 20 OpBuffer[obp++]=(char)0xE9; 21 22 pBreakSchedule=(DWORD *)realloc( pBreakSchedule, ( nBreakSchedule + 1 ) * sizeof(DWORD) ); 23 pBreakSchedule[nBreakSchedule]=obp; 24 nBreakSchedule++; 25 26 obp+=sizeof(long); 20 breakPertialSchedules.push_back( 21 compiler.codeGenerator.op_jmp( 0, sizeof(long), true ) 22 ); 27 23 } 28 void ScopeImpl::RunScheduleOfBreak(){ 29 for(int i=0;i<nBreakSchedule;i++){ 30 *((long *)(OpBuffer+pBreakSchedule[i]))=obp-(pBreakSchedule[i]+sizeof(long)); 24 void LexicalScope::RunScheduleOfBreak(){ 25 BOOST_FOREACH( const PertialSchedule *pBreakPertialSchedule, breakPertialSchedules ) 26 { 27 compiler.codeGenerator.opfix_JmpPertialSchedule( pBreakPertialSchedule ); 31 28 } 32 29 } 33 30 34 void LexicalScopesImpl::End(){ 31 32 33 LexicalScope *LexicalScopes::SearchScope( LexicalScope::SCOPE_TYPE TypeOfStatement ){ 34 for( int i = level; i>=0; i-- ){ 35 if( ppScopes[i]->GetTypeOfStatement() == TypeOfStatement ){ 36 return ppScopes[i]; 37 } 38 } 39 return NULL; 40 } 41 42 void LexicalScopes::Init(int addr){ 43 // TODO: エラーチェック 44 45 level = -1; 46 Start( addr, LexicalScope::SCOPE_TYPE_BASE ); 47 } 48 void LexicalScopes::Start( int addr, LexicalScope::SCOPE_TYPE TypeOfStatement ){ 49 level++; 50 ppScopes = (LexicalScope **)realloc( ppScopes, ( level + 1 ) * sizeof( LexicalScope * ) ); 51 ppScopes[level] = new LexicalScope( level, addr, TypeOfStatement ); 52 } 53 54 int LexicalScopes::GetNowLevel(){ 55 return level; 56 } 57 void LexicalScopes::SetNowLevel( int level ){ 58 this->level = level; 59 } 60 int LexicalScopes::GetStartAddress(){ 61 return ppScopes[level]->GetStartAddress(); 62 } 63 64 void LexicalScopes::End(){ 35 65 if( level <= 0 ){ 36 66 SetError(); … … 65 95 66 96 // スコープ終了時のデストラクタ呼び出し 67 void LexicalScopes Impl::CallDestructorsOfScopeEnd(){97 void LexicalScopes::CallDestructorsOfScopeEnd(){ 68 98 69 99 Variables &vars = UserProc::IsGlobalAreaCompiling() ? … … 134 164 135 165 // Returnステートメントで発行されるデストラクタを生成 136 void LexicalScopes Impl::CallDestructorsOfReturn( int BaseLevel ){166 void LexicalScopes::CallDestructorsOfReturn( int BaseLevel ){ 137 167 //現在のスコープレベルを退避 138 168 int backupScopeLevel = GetNowLevel(); … … 147 177 SetNowLevel( backupScopeLevel ); 148 178 } 149 150 LexicalScopesImpl &GetLexicalScopes()151 {152 static LexicalScopesImpl *pTemp = NULL;153 if( !pTemp )154 {155 pTemp = (LexicalScopesImpl *)Smoothie::Temp::pLexicalScopes;156 }157 return *pTemp;158 } -
trunk/abdev/BasicCompiler_Common/src/SmoothieImpl.cpp
r206 r248 6 6 #include <Class.h> 7 7 #include <Procedure.h> 8 #include <LexicalScopingImpl.h> 9 10 CLexicalScopes *Smoothie::Temp::pLexicalScopes = new LexicalScopesImpl(); 8 #include <LexicalScope.h> -
trunk/abdev/BasicCompiler_Common/src/Variable.cpp
r206 r248 35 35 Variable &var = *(*this)[i]; 36 36 if( var.bLiving //現在のスコープで有効なもの 37 && var.GetScopeLevel() == Smoothie::Temp::pLexicalScopes->GetNowLevel() //現在のスコープと同一レベル 38 ){ 39 if( var.IsEqualSymbol( symbol ) ){ 40 return true; 41 } 37 && var.GetScopeLevel() == compiler.codeGenerator.lexicalScopes.GetNowLevel() //現在のスコープと同一レベル 38 ) 39 { 40 if( var.IsEqualSymbol( symbol ) ){ 41 return true; 42 } 42 43 } 43 44 } … … 51 52 Variable &var = *(*this)[i]; 52 53 if( var.bLiving //現在のスコープで有効なもの 53 && var.GetScopeLevel() <= Smoothie::Temp::pLexicalScopes->GetNowLevel() //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮)54 && var.GetScopeLevel() <= compiler.codeGenerator.lexicalScopes.GetNowLevel() //現在のスコープレベルを超さないもの(Returnによる解放処理中を考慮) 54 55 ){ 55 56 if( var.IsEqualSymbol( symbol ) ){
Note:
See TracChangeset
for help on using the changeset viewer.