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

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

[585][586]をリバース。NativeCodeクラスとMetaクラスは依存関係があるので分離しない方針とする。

File size: 4.5 KB
Line 
1#include "stdafx.h"
2
3using namespace ActiveBasic::Compiler;
4
5LONG_PTR 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 LONG_PTR 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 LONG_PTR 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 LONG_PTR 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 GenerateFullVTables( *classes.Iterator_GetNext() );
99 }
100}
101
102void VtblGenerator::ActionVtblSchedule( CClass &_class, LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
103{
104 if( _class.IsAbstract() )
105 {
106 // 抽象クラスは無視
107 return;
108 }
109 if( !_class.IsUsing() )
110 {
111 // 使われていないクラスは無視
112 return;
113 }
114 if( _class.GetVtblOffset() == -1 )
115 {
116 return;
117 }
118
119 // 自身のクラスのvtbl
120 {
121 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblOffset());
122
123 for( int i=0; i<_class.GetVtblNum(); i++ ){
124 const UserProc *pUserProc = (UserProc *)pVtbl[i];
125 if(!pUserProc) continue;
126
127 if( pUserProc->GetBeginOpAddress() == 0
128 && pUserProc->GetEndOpAddress() == 0 )
129 {
130 Jenga::Throw( "未解決の仮想関数が存在する" );
131 }
132
133 pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
134 }
135 }
136
137 // インターフェイスのvtbl
138 BOOST_FOREACH( const ::Interface *pInterface, _class.GetInterfaces() )
139 {
140 LONG_PTR *pVtbl = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + pInterface->GetVtblOffset());
141
142 for( int i=0; i<pInterface->GetClass().GetVtblNum(); i++ ){
143 const UserProc *pUserProc = (UserProc *)pVtbl[i];
144 if(!pUserProc) continue;
145
146 if( pUserProc->GetBeginOpAddress() == 0
147 && pUserProc->GetEndOpAddress() == 0 )
148 {
149 Jenga::Throw( "未解決の仮想関数が存在する" );
150 }
151
152 pVtbl[i] = pUserProc->GetBeginOpAddress() + ImageBase + MemPos_CodeSection;
153 }
154 }
155
156 // vtblマスターリスト
157 LONG_PTR *pVtblMasterList = (LONG_PTR *)((char *)compiler.GetObjectModule().dataTable.GetPtr() + _class.GetVtblMasterListOffset() );
158 for( int i=0; i<static_cast<int>(_class.vtblMasterList.size()); i++ )
159 {
160 pVtblMasterList[i] = _class.vtblMasterList[i] + ImageBase + MemPos_DataSection;
161 }
162}
163
164void VtblGenerator::ActionVtblScheduleForAllClasses( Classes &classes, LONG_PTR ImageBase, LONG_PTR MemPos_CodeSection, LONG_PTR MemPos_DataSection )
165{
166 classes.Iterator_Reset();
167 while( classes.Iterator_HasNext() )
168 {
169 ActionVtblSchedule( *classes.Iterator_GetNext(), ImageBase, MemPos_CodeSection, MemPos_DataSection );
170 }
171}
Note: See TracBrowser for help on using the repository browser.