[559] | 1 | #include "stdafx.h"
|
---|
| 2 |
|
---|
| 3 | using namespace ActiveBasic::Compiler;
|
---|
| 4 |
|
---|
[593] | 5 | int VtblGenerator::GenerateVTablePart( const Methods &methods )
|
---|
[559] | 6 | {
|
---|
| 7 | const UserProc **ppsi = (const UserProc **)malloc(methods.GetVtblNum()*sizeof(UserProc *));
|
---|
| 8 |
|
---|
| 9 | //関数テーブルに値をセット
|
---|
| 10 | int i2 = 0;
|
---|
[750] | 11 | foreach( const CMethod *pMethod, methods )
|
---|
[559] | 12 | {
|
---|
| 13 | if(pMethod->IsVirtual()){
|
---|
| 14 | if( !pMethod->GetUserProc().IsUsing() )
|
---|
| 15 | {
|
---|
| 16 | //ts((char *)pMethod->GetUserProc().GetFullName().c_str());
|
---|
| 17 | }
|
---|
| 18 | pMethod->GetUserProc().Using();
|
---|
| 19 |
|
---|
| 20 | if(pMethod->IsAbstract())
|
---|
| 21 | {
|
---|
| 22 | throw;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | ppsi[i2]=&pMethod->GetUserProc();
|
---|
| 26 |
|
---|
| 27 | i2++;
|
---|
| 28 | }
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[593] | 31 | int vtableDataTableOffset = compiler.GetObjectModule().dataTable.AddBinary( (void *)ppsi, methods.GetVtblNum()*sizeof(LONG_PTR) );
|
---|
[559] | 32 |
|
---|
| 33 | for( int i=0; i < methods.GetVtblNum(); i++ )
|
---|
| 34 | {
|
---|
| 35 | pobj_Reloc->AddSchedule_DataSection(static_cast<DWORD>(vtableDataTableOffset+i*sizeof(LONG_PTR)));
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | free(ppsi);
|
---|
| 39 |
|
---|
| 40 | return vtableDataTableOffset;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | void VtblGenerator::GenerateFullVTables( CClass &_class )
|
---|
| 44 | {
|
---|
| 45 | if( _class.IsAbstract() )
|
---|
| 46 | {
|
---|
| 47 | // 抽象クラスは無視
|
---|
| 48 | return;
|
---|
| 49 | }
|
---|
| 50 | if( !_class.IsUsing() )
|
---|
| 51 | {
|
---|
| 52 | // 使われていないクラスは無視
|
---|
| 53 | return;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | // vtblマスターリストの元データに不要なデータが含まれていたらエラー
|
---|
| 57 | if( _class.vtblMasterList.size() )
|
---|
| 58 | {
|
---|
| 59 | throw;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | // 自身のクラスのvtblを生成
|
---|
[593] | 63 | int thisClassVtblOffset = GenerateVTablePart( _class.GetDynamicMethods() );
|
---|
[559] | 64 | _class.SetVtblOffset( thisClassVtblOffset );
|
---|
| 65 | _class.vtblMasterList.push_back( thisClassVtblOffset );
|
---|
| 66 |
|
---|
| 67 | // インターフェイスのvtblを生成
|
---|
[750] | 68 | foreach( const ::Interface *pInterface, _class.GetInterfaces() )
|
---|
[559] | 69 | {
|
---|
[593] | 70 | int tempVtblOffset = GenerateVTablePart( pInterface->GetDynamicMethods() );
|
---|
[559] | 71 | _class.vtblMasterList.push_back( tempVtblOffset );
|
---|
| 72 |
|
---|
| 73 | pInterface->SetVtblOffset( tempVtblOffset );
|
---|
| 74 |
|
---|
| 75 | if( pInterface->GetClass().IsComInterface() )
|
---|
| 76 | {
|
---|
| 77 | if( _class.GetComVtblOffset() )
|
---|
| 78 | {
|
---|
| 79 | throw;
|
---|
| 80 | }
|
---|
| 81 | _class.SetComVtblOffset( tempVtblOffset );
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | // vtblマスターリストを生成
|
---|
[587] | 86 | int offset = compiler.GetObjectModule().dataTable.AddBinary(
|
---|
[559] | 87 | (void *)&_class.vtblMasterList[0],
|
---|
| 88 | static_cast<int>(_class.vtblMasterList.size()*sizeof(LONG_PTR))
|
---|
| 89 | );
|
---|
| 90 | _class.SetVtblMasterListOffset( offset );
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | void VtblGenerator::GenerateVTablesForAllClasses( Classes &classes )
|
---|
| 94 | {
|
---|
[803] | 95 | foreach (auto pClass, classes)
|
---|
[559] | 96 | {
|
---|
[632] | 97 | GenerateFullVTables( *pClass );
|
---|
| 98 |
|
---|
| 99 | // テンプレート展開されたクラスも
|
---|
[750] | 100 | foreach( ActiveBasic::Common::Lexical::ExpandedTemplateClass *pExpandedTemplateClass, pClass->expandedTemplateClasses )
|
---|
[632] | 101 | {
|
---|
| 102 | if( !pExpandedTemplateClass->GetClass().expandedTemplateClasses.empty() )
|
---|
| 103 | {
|
---|
| 104 | // テンプレート展開後のクラスが更にテンプレート展開されていることはありえない
|
---|
| 105 | throw;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | GenerateFullVTables( pExpandedTemplateClass->GetClass() );
|
---|
| 109 | }
|
---|
[559] | 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | void VtblGenerator::ActionVtblSchedule( CClass &_class, LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
|
---|
| 114 | {
|
---|
| 115 | if( _class.IsAbstract() )
|
---|
| 116 | {
|
---|
| 117 | // 抽象クラスは無視
|
---|
| 118 | return;
|
---|
| 119 | }
|
---|
| 120 | if( !_class.IsUsing() )
|
---|
| 121 | {
|
---|
| 122 | // 使われていないクラスは無視
|
---|
| 123 | return;
|
---|
| 124 | }
|
---|
| 125 | if( _class.GetVtblOffset() == -1 )
|
---|
| 126 | {
|
---|
| 127 | return;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | // 自身のクラスのvtbl
|
---|
| 131 | {
|
---|
[587] | 132 | LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblOffset());
|
---|
[559] | 133 |
|
---|
| 134 | for( int i=0; i<_class.GetVtblNum(); i++ ){
|
---|
| 135 | const UserProc *pUserProc = (UserProc *)pVtbl[i];
|
---|
| 136 | if(!pUserProc) continue;
|
---|
| 137 |
|
---|
| 138 | if( pUserProc->GetBeginOpAddress() == 0
|
---|
| 139 | && pUserProc->GetEndOpAddress() == 0 )
|
---|
| 140 | {
|
---|
| 141 | Jenga::Throw( "未解決の仮想関数が存在する" );
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
|
---|
| 145 | }
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | // インターフェイスのvtbl
|
---|
[750] | 149 | foreach( const ::Interface *pInterface, _class.GetInterfaces() )
|
---|
[559] | 150 | {
|
---|
[587] | 151 | LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + pInterface->GetVtblOffset());
|
---|
[559] | 152 |
|
---|
| 153 | for( int i=0; i<pInterface->GetClass().GetVtblNum(); i++ ){
|
---|
| 154 | const UserProc *pUserProc = (UserProc *)pVtbl[i];
|
---|
| 155 | if(!pUserProc) continue;
|
---|
| 156 |
|
---|
| 157 | if( pUserProc->GetBeginOpAddress() == 0
|
---|
| 158 | && pUserProc->GetEndOpAddress() == 0 )
|
---|
| 159 | {
|
---|
| 160 | Jenga::Throw( "未解決の仮想関数が存在する" );
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
|
---|
| 164 | }
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | // vtblマスターリスト
|
---|
[587] | 168 | LONG_PTR *pVtblMasterList = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblMasterListOffset() );
|
---|
[559] | 169 | for( int i=0; i<static_cast<int>(_class.vtblMasterList.size()); i++ )
|
---|
| 170 | {
|
---|
| 171 | pVtblMasterList[i] = _class.vtblMasterList[i] + ImageBase + MemPos_DataSection;
|
---|
| 172 | }
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | void VtblGenerator::ActionVtblScheduleForAllClasses( Classes &classes, LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
|
---|
| 176 | {
|
---|
[803] | 177 | foreach(auto pClass, classes)
|
---|
[559] | 178 | {
|
---|
[632] | 179 | ActionVtblSchedule( *pClass, ImageBase, MemPos_CodeSection, MemPos_DataSection );
|
---|
| 180 |
|
---|
| 181 | // テンプレート展開されたクラスも
|
---|
[750] | 182 | foreach( ActiveBasic::Common::Lexical::ExpandedTemplateClass *pExpandedTemplateClass, pClass->expandedTemplateClasses )
|
---|
[632] | 183 | {
|
---|
| 184 | if( !pExpandedTemplateClass->GetClass().expandedTemplateClasses.empty() )
|
---|
| 185 | {
|
---|
| 186 | // テンプレート展開後のクラスが更にテンプレート展開されていることはありえない
|
---|
| 187 | throw;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | ActionVtblSchedule( pExpandedTemplateClass->GetClass(), ImageBase, MemPos_CodeSection, MemPos_DataSection );
|
---|
| 191 | }
|
---|
[559] | 192 | }
|
---|
| 193 | }
|
---|