source: dev/trunk/ab5.0/abdev/BasicCompiler_Common/src/ObjectModule.cpp@ 585

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

NativeSectionクラスを追加(64bit版だけ一旦コミット)。

File size: 5.3 KB
Line 
1#pragma warning(disable : 4996)
2
3#include <map>
4#include <string>
5#include <vector>
6#include <fstream>
7#include <iostream>
8#include <iomanip>
9#include <ios>
10#include <streambuf>
11#include <sstream>
12
13#include <windows.h>
14#include <stdio.h>
15#include <string.h>
16#include <math.h>
17#include <commctrl.h>
18#include <time.h>
19#include <limits.h>
20#include <shlobj.h>
21#include <process.h>
22#include <fcntl.h>
23#include <io.h>
24#include <shlwapi.h>
25#include <tchar.h>
26#include <stdarg.h>
27
28//boost libraries
29#include <boost/foreach.hpp>
30
31
32// ObjectModuleの内容をXmlで生成する場合はこの行を有効にする
33//#define OBJECT_MODULE_IS_NOT_BINARY
34
35
36#ifdef OBJECT_MODULE_IS_NOT_BINARY
37#include <boost/archive/xml_oarchive.hpp>
38#include <boost/archive/xml_iarchive.hpp>
39#else
40#include <boost/archive/binary_oarchive.hpp>
41#include <boost/archive/binary_iarchive.hpp>
42#endif
43#include <boost/serialization/string.hpp>
44#include <boost/serialization/access.hpp>
45#include <boost/serialization/level.hpp>
46#include <boost/serialization/vector.hpp>
47#include <boost/serialization/map.hpp>
48#include <boost/serialization/version.hpp>
49#include <boost/serialization/is_abstract.hpp>
50#include <boost/serialization/serialization.hpp>
51#include <boost/serialization/nvp.hpp>
52#include <boost/serialization/export.hpp>
53
54#include <jenga/include/jenga.h>
55
56#include "../common.h"
57#include "../BasicFixed.h"
58
59#include <option.h>
60
61#include <abdev/ab_common/include/ab_common.h>
62
63using namespace ActiveBasic::Common::Lexical;
64
65#include <NativeCode.h>
66#include <Source.h>
67#include <Type.h>
68#include <Method.h>
69#include <Interface.h>
70#include <Member.h>
71#include <Class.h>
72#include <Parameter.h>
73#include <Variable.h>
74#include <Procedure.h>
75#include <TypeDef.h>
76#include <Const.h>
77#include <Delegate.h>
78#include <Enum.h>
79#include <Exception.h>
80#include <Meta.h>
81
82#include <logger.h>
83#include <Configuration.h>
84#include <CodeGenerator.h>
85#include <Messenger.h>
86#include <DataTable.h>
87#include <ObjectModule.h>
88#include <Linker.h>
89#include <Compiler.h>
90#include <Debugger.h>
91#include <Program.h>
92#include <LexicalAnalyzer.h>
93
94
95
96void ObjectModule::StaticLink( ObjectModule &objectModule )
97{
98 long dataSectionBaseOffset = this->nativeSection.dataTable.GetSize();
99 int sourceIndexBase = (int)nativeSection.GetSources().size();
100
101 // メタ情報を結合
102 meta.StaticLink( objectModule.meta, dataSectionBaseOffset, sourceIndexBase );
103
104 // グローバル ネイティブコードを結合
105 objectModule.nativeSection.globalNativeCode.ResetDataSectionBaseOffset( dataSectionBaseOffset );
106 objectModule.nativeSection.globalNativeCode.ResetSourceIndexes( sourceIndexBase );
107 nativeSection.globalNativeCode.PutEx( objectModule.nativeSection.globalNativeCode );
108
109 // データテーブルを結合
110 objectModule.nativeSection.dataTable.ResetDataSectionBaseOffset( dataSectionBaseOffset );
111 this->nativeSection.dataTable.Add( objectModule.nativeSection.dataTable );
112
113 // ソースコードを結合
114 BOOST_FOREACH( const BasicSource &source, objectModule.nativeSection.GetSources() )
115 {
116 this->nativeSection.GetSources().push_back( source );
117 }
118
119 // TODO: basbufがいらなくなったら消す
120 extern char *basbuf;
121 basbuf = this->nativeSection.GetSources()[0].GetBuffer();
122}
123
124bool ObjectModule::Read( const std::string &filePath )
125{
126 Jenga::Common::File file( filePath );
127
128 Jenga::Common::Binary binary;
129 if( !file.ReadBinary( binary ) )
130 {
131 return false;
132 }
133
134 return Load( binary );
135}
136bool ObjectModule::Write( const std::string &filePath ) const
137{
138 Jenga::Common::File file( filePath );
139
140 Jenga::Common::Binary binary;
141 if( !Save( binary ) )
142 {
143 return false;
144 }
145
146 return file.WriteBinary( binary );
147}
148bool ObjectModule::Load( const Jenga::Common::Binary &binary )
149{
150 bool isSuccessful = false;
151
152 // 入力アーカイブの作成
153
154 try{
155 std::string str( binary.GetBuffer(), binary.GetSize() );
156
157#ifdef OBJECT_MODULE_IS_NOT_BINARY
158 std::istringstream iss( str );
159 boost::archive::xml_iarchive ia(iss);
160#else
161 std::istringstream iss( str, std::ios::in | std::ios::binary );
162 boost::archive::binary_iarchive ia(iss);
163#endif
164
165 // 文字列ストリームから読込
166 ia >> boost::serialization::make_nvp( RootTagName(), *this );
167
168 isSuccessful = true;
169 }
170 catch( boost::archive::archive_exception e )
171 {
172 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
173 }
174 catch(...){
175 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
176 }
177
178 if( !isSuccessful )
179 {
180 return false;
181 }
182
183 return true;
184}
185bool ObjectModule::Save( Jenga::Common::Binary &binary ) const
186{
187 // 出力アーカイブの作成
188
189 bool isSuccessful = false;
190 try{
191#ifdef OBJECT_MODULE_IS_NOT_BINARY
192 std::ostringstream oss;
193 boost::archive::xml_oarchive oa(oss);
194#else
195 std::ostringstream oss( "", std::ios::out | std::ios::binary );
196 boost::archive::binary_oarchive oa(oss);
197#endif
198
199 // 文字列ストリームに書き出し
200 oa << boost::serialization::make_nvp( RootTagName(), *this );
201
202 binary.Put( oss.str().c_str(), static_cast<int>(oss.str().size()) );
203
204 isSuccessful = true;
205 }
206 catch( boost::archive::archive_exception e )
207 {
208 MessageBox( NULL, e.what(), "XMLシリアライズの例外", MB_OK );
209 }
210 catch(...){
211 MessageBox( NULL, "archive_exception以外の不明な例外", "XMLシリアライズの例外", MB_OK );
212 }
213
214 return isSuccessful;
215}
Note: See TracBrowser for help on using the repository browser.