source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/VtblGenerator.cpp@ 829

Last change on this file since 829 was 829, checked in by イグトランス (egtra), 12 years ago

svn:eol-styleとsvn:mime-type(文字コード指定含む)の設定

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/plain; charset=Shift_JIS
File size: 5.1 KB
Line 
1#include "stdafx.h"
2
3using namespace ActiveBasic::Compiler;
4
5int VtblGenerator::GenerateVTablePart( const Methods &methods )
6{
7 const UserProc **ppsi = (const UserProc **)malloc(methods.GetVtblNum()*sizeof(UserProc *));
8
9 //関数テーブルに値をセット
10 int i2 = 0;
11 foreach( const CMethod *pMethod, methods )
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
31 int vtableDataTableOffset = compiler.GetObjectModule().dataTable.AddBinary( (void *)ppsi, methods.GetVtblNum()*sizeof(LONG_PTR) );
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
43void 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を生成
63 int thisClassVtblOffset = GenerateVTablePart( _class.GetDynamicMethods() );
64 _class.SetVtblOffset( thisClassVtblOffset );
65 _class.vtblMasterList.push_back( thisClassVtblOffset );
66
67 // インターフェイスのvtblを生成
68 foreach( const ::Interface *pInterface, _class.GetInterfaces() )
69 {
70 int tempVtblOffset = GenerateVTablePart( pInterface->GetDynamicMethods() );
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マスターリストを生成
86 int offset = compiler.GetObjectModule().dataTable.AddBinary(
87 (void *)&_class.vtblMasterList[0],
88 static_cast<int>(_class.vtblMasterList.size()*sizeof(LONG_PTR))
89 );
90 _class.SetVtblMasterListOffset( offset );
91}
92
93void VtblGenerator::GenerateVTablesForAllClasses( Classes &classes )
94{
95 foreach (auto pClass, classes)
96 {
97 GenerateFullVTables( *pClass );
98
99 // テンプレート展開されたクラスも
100 foreach( ActiveBasic::Common::Lexical::ExpandedTemplateClass *pExpandedTemplateClass, pClass->expandedTemplateClasses )
101 {
102 if( !pExpandedTemplateClass->GetClass().expandedTemplateClasses.empty() )
103 {
104 // テンプレート展開後のクラスが更にテンプレート展開されていることはありえない
105 throw;
106 }
107
108 GenerateFullVTables( pExpandedTemplateClass->GetClass() );
109 }
110 }
111}
112
113void 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 {
132 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblOffset());
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
149 foreach( const ::Interface *pInterface, _class.GetInterfaces() )
150 {
151 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + pInterface->GetVtblOffset());
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マスターリスト
168 LONG_PTR *pVtblMasterList = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblMasterListOffset() );
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
175void VtblGenerator::ActionVtblScheduleForAllClasses( Classes &classes, LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
176{
177 foreach(auto pClass, classes)
178 {
179 ActionVtblSchedule( *pClass, ImageBase, MemPos_CodeSection, MemPos_DataSection );
180
181 // テンプレート展開されたクラスも
182 foreach( ActiveBasic::Common::Lexical::ExpandedTemplateClass *pExpandedTemplateClass, pClass->expandedTemplateClasses )
183 {
184 if( !pExpandedTemplateClass->GetClass().expandedTemplateClasses.empty() )
185 {
186 // テンプレート展開後のクラスが更にテンプレート展開されていることはありえない
187 throw;
188 }
189
190 ActionVtblSchedule( pExpandedTemplateClass->GetClass(), ImageBase, MemPos_CodeSection, MemPos_DataSection );
191 }
192 }
193}
Note: See TracBrowser for help on using the repository browser.