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

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

ジェネリッククラスの型パラメータに値型が指定されたときに限り、テンプレート展開を行うようにした。

TODO: libファイルを跨ってテンプレート展開ができていないため、ソースコード管理部分に手を加える必要あり。

File size: 5.4 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 BOOST_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 BOOST_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 classes.Iterator_Reset();
96 while( classes.Iterator_HasNext() )
97 {
98 CClass *pClass = classes.Iterator_GetNext();
99 GenerateFullVTables( *pClass );
100
101 // テンプレート展開されたクラスも
102 BOOST_FOREACH( ActiveBasic::Common::Lexical::ExpandedTemplateClass *pExpandedTemplateClass, pClass->expandedTemplateClasses )
103 {
104 if( !pExpandedTemplateClass->GetClass().expandedTemplateClasses.empty() )
105 {
106 // テンプレート展開後のクラスが更にテンプレート展開されていることはありえない
107 throw;
108 }
109
110 GenerateFullVTables( pExpandedTemplateClass->GetClass() );
111 }
112 }
113}
114
115void VtblGenerator::ActionVtblSchedule( CClass &_class, LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
116{
117 if( _class.IsAbstract() )
118 {
119 // 抽象クラスは無視
120 return;
121 }
122 if( !_class.IsUsing() )
123 {
124 // 使われていないクラスは無視
125 return;
126 }
127 if( _class.GetVtblOffset() == -1 )
128 {
129 return;
130 }
131
132 // 自身のクラスのvtbl
133 {
134 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblOffset());
135
136 for( int i=0; i<_class.GetVtblNum(); i++ ){
137 const UserProc *pUserProc = (UserProc *)pVtbl[i];
138 if(!pUserProc) continue;
139
140 if( pUserProc->GetBeginOpAddress() == 0
141 && pUserProc->GetEndOpAddress() == 0 )
142 {
143 Jenga::Throw( "未解決の仮想関数が存在する" );
144 }
145
146 pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
147 }
148 }
149
150 // インターフェイスのvtbl
151 BOOST_FOREACH( const ::Interface *pInterface, _class.GetInterfaces() )
152 {
153 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + pInterface->GetVtblOffset());
154
155 for( int i=0; i<pInterface->GetClass().GetVtblNum(); i++ ){
156 const UserProc *pUserProc = (UserProc *)pVtbl[i];
157 if(!pUserProc) continue;
158
159 if( pUserProc->GetBeginOpAddress() == 0
160 && pUserProc->GetEndOpAddress() == 0 )
161 {
162 Jenga::Throw( "未解決の仮想関数が存在する" );
163 }
164
165 pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
166 }
167 }
168
169 // vtblマスターリスト
170 LONG_PTR *pVtblMasterList = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblMasterListOffset() );
171 for( int i=0; i<static_cast<int>(_class.vtblMasterList.size()); i++ )
172 {
173 pVtblMasterList[i] = _class.vtblMasterList[i] + ImageBase + MemPos_DataSection;
174 }
175}
176
177void VtblGenerator::ActionVtblScheduleForAllClasses( Classes &classes, LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
178{
179 classes.Iterator_Reset();
180 while( classes.Iterator_HasNext() )
181 {
182 CClass *pClass = classes.Iterator_GetNext();
183 ActionVtblSchedule( *pClass, ImageBase, MemPos_CodeSection, MemPos_DataSection );
184
185 // テンプレート展開されたクラスも
186 BOOST_FOREACH( ActiveBasic::Common::Lexical::ExpandedTemplateClass *pExpandedTemplateClass, pClass->expandedTemplateClasses )
187 {
188 if( !pExpandedTemplateClass->GetClass().expandedTemplateClasses.empty() )
189 {
190 // テンプレート展開後のクラスが更にテンプレート展開されていることはありえない
191 throw;
192 }
193
194 ActionVtblSchedule( pExpandedTemplateClass->GetClass(), ImageBase, MemPos_CodeSection, MemPos_DataSection );
195 }
196 }
197}
Note: See TracBrowser for help on using the repository browser.