-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMilBackend.cpp
More file actions
283 lines (255 loc) · 9.98 KB
/
MilBackend.cpp
File metadata and controls
283 lines (255 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright 2025 Rochus Keller <mailto:me@rochus-keller.ch>
*
* This file is part of the Micron language project.
*
* The following is the license that applies to this copy of the
* file. For a license to use the file under conditions
* other than those described here, please email to me@rochus-keller.ch.
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include "MilBackend.h"
#include "EiGen/debugging.hpp"
#include "EiGen/driver.hpp"
#include "EiGen/stdcharset.hpp"
#include "EiGen/stringpool.hpp"
#include "EiGen/cdgenerator.hpp"
#include "EiGen/amd64.hpp"
#include "EiGen/amd64generator.hpp"
#include "EiGen/arma64generator.hpp"
#include "EiGen/armt32generator.hpp"
#include "EiGen/arma32generator.hpp"
#include "EiGen/asmparser.hpp"
#include "EiGen/assembly.hpp"
#include "EiGen/cdchecker.hpp"
#include "EiGen/objlinker.hpp"
#include "EiGen/dbgdwarfconverter.hpp"
#include <QtDebug>
using namespace Mil;
using namespace ECS;
static ASCIICharset charset;
static StringPool stringPool;
static StreamDiagnostics diagnostics {std::cerr};
static std::string extensionOf(const std::string& path)
{
const size_t pos = path.find_last_of(".");
std::string extension;
if( pos != std::string::npos )
extension = path.substr(pos);
return extension;
}
static void generate(const Assembly::Program& program, Assembly::Generator& generator, const char* output, bool debug)
{
Code::Sections sections;
Code::Checker checker {diagnostics, charset, generator.platform};
checker.Check (program, sections);
Object::Binaries binaries;
Debugging::Information information;
std::ostream listing {nullptr};
generator.Generate (sections, program.source, binaries, information, listing);
std::string path(output);
if( debug )
{
Debugging::DWARFConverter converter(diagnostics, charset);
Object::Binaries dbfobj;
converter.Convert (information, program.source, dbfobj);
ECS::File dbf_file {path, ".dbf"};
dbf_file << dbfobj;
// TODO register_for_cleanup(dbf_file.getPath().c_str(),1);
}
ECS::File object( path, extensionOf(path) ); // File constructor wants to replace the extension of path in any case, even if empty
object << binaries;
}
static void generate_amd16(const Assembly::Program& program, const char* output, bool debug)
{
AMD64::Generator generator {diagnostics, stringPool, charset, AMD64::RealMode, false, true};
generate(program, generator, output, debug);
}
static void generate_amd32(const Assembly::Program& program, const char* output, bool debug)
{
AMD64::Generator generator {diagnostics, stringPool, charset, AMD64::ProtectedMode, true, true}; // RK: was false, true
// use media instead of legacy float instructions, see https://software.openbrace.org/boards/3/topics/44?r=60#message-60
// speed-up is factor 10
generate(program, generator, output, debug);
}
static void generate_amd64(const Assembly::Program& program, const char* output, bool debug)
{
AMD64::Generator generator {diagnostics, stringPool, charset, AMD64::LongMode, true, true};
generate(program, generator, output, debug);
}
static void generate_arma32(const Assembly::Program& program, const char* output, bool debug)
{
ARM::A32::Generator generator {diagnostics, stringPool, charset, true};
generate(program, generator, output, debug);
}
static void generate_arma64(const Assembly::Program& program, const char* output, bool debug)
{
ARM::A64::Generator generator {diagnostics, stringPool, charset, false};
generate(program, generator, output, debug);
}
static void generate_armt32(const Assembly::Program& program, const char* output, bool debug)
{
ARM::T32::Generator generator {diagnostics, stringPool, charset, false};
generate(program, generator, output, debug);
}
static void generate_armt32fpe(const Assembly::Program& program, const char* output, bool debug)
{
ARM::T32::Generator generator {diagnostics, stringPool, charset, true};
generate(program, generator, output,debug);
}
bool Backend::generate(const QString &inFile, const QString &outFile, EiGen::TargetCode target, bool debug)
{
try
{
const std::string input = inFile.toUtf8().toStdString();
Assembly::Program program{input};
Assembly::Parser parser {diagnostics, stringPool, true};
std::ifstream file;
file.open (input, file.binary);
if (!file.is_open ())
qCritical() << "failed to open input file " << inFile;
parser.Parse (file, GetLine (Position(file, input, 1, 1)), program);
const QByteArray output = outFile.toUtf8();
const QByteArray backend = EiGen::backend(target);
if( backend == "amd16" )
generate_amd16(program,output.constData(),debug);
else if( backend == "amd32" )
generate_amd32(program,output.constData(),debug);
else if( backend == "amd64" )
generate_amd64(program,output.constData(),debug);
else if( backend == "arma32" )
generate_arma32(program,output.constData(),debug);
else if( backend == "arma64" )
generate_arma64(program,output.constData(),debug);
else if( backend == "armt32" )
generate_armt32(program,output.constData(),debug);
else if( backend == "armt32fpe" )
generate_armt32fpe(program,output.constData(),debug);
else
qCritical() << "no generator available for " << backend;
}catch(...)
{
// already reported
return false;
}
return true;
}
#if defined Q_OS_LINUX || defined Q_OS_UNIX
#include <sys/stat.h>
#endif
#include <deque>
bool Backend::link(const QStringList &inFiles, const QStringList &searchDirs, const QString &outFile, EiGen::TargetCode target, bool linkLib, bool debug)
{
try
{
Object::Linker linker(diagnostics);
Object::Binaries binaries;
std::deque<std::string> paths;
std::deque<std::string> libs;
for (int i = 0; i < searchDirs.size(); i++)
paths.push_back(searchDirs[i].toUtf8().constData());
// add internal libs
if( !linkLib && target > EiGen::NoTarget && target <= EiGen::Win64 )
{
std::string lib = EiGen::name(target);
lib += "run.obf";
libs.push_back(lib);
lib = EiGen::name(target);
lib += "mic.lib";
libs.push_back(lib);
}
for (int i = 0; i < inFiles.size(); i++)
{
std::ifstream file;
file.open (inFiles[i].toUtf8().constData(), file.binary);
if (!file.is_open ())
qCritical() << "failed to open input file" << inFiles[i];
file >> binaries;
if (!file)
qCritical() << "invalid object file" << inFiles[i];
}
if( debug )
{
for (int i = 0; i < inFiles.size(); i++)
{
std::ifstream dbfin;
const std::string dbf = ECS::File::replace_extension(inFiles[i].toUtf8().constData(),".dbf");
dbfin.open (dbf, dbfin.binary);
if (!dbfin.is_open ())
qCritical() << "failed to open input file" << dbf.c_str();
dbfin >> binaries;
const int s = binaries.size();
if (!dbfin)
qCritical() << "invalid object file" << dbf.c_str();
}
}
for( int i = 0; i < libs.size(); i++ )
{
bool found = false;
for( int j = 0; j < paths.size(); j++ )
{
std::string path = paths[j];
if( path.empty() )
continue;
const char last = path[path.size()-1];
if( last != '/' && last != '\\' )
path += '/';
path += libs[i];
std::ifstream file;
file.open(path, file.binary);
if (!file.is_open ())
continue;
file >> binaries;
if (!file)
qCritical() << "invalid object file" << path.c_str();
else
found = true;
break;
}
if( !found )
qCritical() << "could not find library" << libs[i].c_str();
}
std::string path = outFile.toUtf8().constData();
if( target >= EiGen::BareAmd16 && target <= EiGen::BareArmA64)
{
Object::MappedByteArrangement ram, rom;
linker.Link (binaries, rom, ram, rom);
ECS::File ramfile {path, ".ram", ramfile.binary};
Object::WriteBinary (ramfile, ram.bytes);
ECS::File romfile {path, ".rom", romfile.binary};
Object::WriteBinary (romfile, rom.bytes);
ECS::File map {path, ".map"};
map << ram.map << rom.map;
}else if( linkLib )
{
linker.Combine (binaries);
ECS::File library {path, ".lib"};
library << binaries;
}else
{
Object::MappedByteArrangement arrangement;
linker.Link (binaries, arrangement);
std::string ext = GetContents ("_extension", binaries, charset, ".bin");
if( ext.empty() )
ext = extensionOf(path);
ECS::File file(path, ext, ECS::File::binary);
Object::WriteBinary (file, arrangement.bytes);
#if defined Q_OS_LINUX || defined Q_OS_UNIX
chmod(file.getPath().c_str (), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
#endif
}
}catch(...)
{
// already reported
return false;
}
return true;
}