-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
328 lines (283 loc) · 7.79 KB
/
main.cpp
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include "globals.h"
#include "boards/pico.h"
#include "boards/gamebuino.h"
#include "boards/picosystem.h"
#include <bit>
u4 countArgs(std::string str)
{
int count = 0;
if (str[0] != '(')
{
throw fmt::format("Invalid descriptor. Should start with '(', got '{}'!", str[0]);
}
for (size_t index = 1; index < str.size(); ++index)
{
switch (str[index])
{
case ')':
index = str.size();
break;
case 'L':
++count;
while (str[index] != ';') ++index;
break;
case 'I':
case 'Z':
case 'F':
case 'B':
case 'S':
++count;
break;
case '[':
break;
default:
throw fmt::format("Invalid or unhandled type used as a parameter type: '{}'.", str[index]);
}
}
return count;
}
attribute_info readAttribute(Buffer & buffer);
std::vector<Instruction> convertBytecode(Buffer & buffer, std::string function, u2 depth);
int main([[maybe_unused]] int argc, [[maybe_unused]] char** argv)
{
std::vector<std::string> javaFiles;
for (auto const& dir_entry : std::filesystem::directory_iterator{"."})
{
if (dir_entry.is_regular_file())
{
auto filename_entry = dir_entry.path().filename().string();
if (filename_entry.ends_with(".java"))
{
javaFiles.push_back(filename_entry);
}
}
}
if (!javaFiles.size())
{
fmt::print("No .java file detected. Aborting.\n");
return 0;
}
for (auto & file : javaFiles)
{
int r = system(fmt::format("javac -cp api {}", file).data());
if (r != 0)
{
fmt::print("{} has errors. Aborting.\n", file);
return 0;
}
}
try
{
std::string project_name, board_name;
for (auto & file : javaFiles)
{
ClassFile tmp(file, ""s, true);
if (tmp.hasBoard())
{
project_name = tmp.fileName;
board_name = tmp.boardName();
}
}
for (const fs::directory_entry& dir_entry :
fs::recursive_directory_iterator("."))
{
if (dir_entry.is_regular_file() && dir_entry.path().extension().string() == ".class")
{
ClassFile tmp(dir_entry.path().string(), ""s, true);
ClassFile::partialClasses.push_back(tmp);
}
}
std::vector<ClassFile> classFiles;
for (auto & file : javaFiles)
{
classFiles.push_back(ClassFile(file, project_name));
}
auto board = getBoardTypeFromString(board_name);
if (board == Board::Gamebuino)
{
build_gamebuino(project_name, classFiles);
}
else if (board == Board::Picosystem)
{
build_picosystem(project_name, classFiles);
}
else
{
build_pico(project_name, getBoardTypeFromString(board_name), classFiles);
}
fmt::print("\n");
}
catch (const std::string & str)
{
fmt::print("{}\n", str);
}
return 0;
}
u1 read8(Buffer & buffer)
{
auto byte = buffer.front();
buffer.erase(buffer.begin());
return byte;
}
u2 read16(Buffer & buffer)
{
auto MSB = read8(buffer);
auto LSB = read8(buffer);
return MSB << 8 | LSB;
}
u4 read32(Buffer & buffer)
{
auto MSB = read16(buffer);
auto LSB = read16(buffer);
return MSB << 16 | LSB;
}
s1 reads8(Buffer & buffer)
{
return r8();
}
s2 reads16(Buffer & buffer)
{
return r16();
}
s4 reads32(Buffer & buffer)
{
return r32();
}
std::string getReturnType(std::string descriptor, u1 flags)
{
auto paren = descriptor.find(')');
auto type = descriptor.substr(paren + 1);
std::string prefix;
std::string suffix;
if (flags & CONST_TYPE) prefix += "const ";
if (flags & UNSIGNED_TYPE) prefix += "u";
if (flags & POINTER_TYPE) suffix += "*";
if (type == "V")
{
return "void";
}
if (type == "I")
{
return prefix + "int32_t" + suffix;
}
if (type == "B")
{
return prefix + "int8_t" + suffix;
}
if (type == "S")
{
return prefix + "int16_t" + suffix;
}
if (type == "Z")
{
return "bool" + suffix;
}
if (type == "D")
{
return prefix + "double" + suffix;
}
if (type.starts_with("L") && type.ends_with(";"))
{
auto jt = type.substr(1, type.size() - 2);
if (jt == "java/lang/String")
{
return prefix + "std::string" + suffix;
}
else
{
return prefix + javaToCpp(jt) + suffix;
}
}
throw fmt::format("Invalid or unhandled type used as a return type: '{}'.", type);
}
Board getBoardTypeFromString(std::string board_name)
{
std::transform(begin(board_name), end(board_name), begin(board_name), [](auto c) { return std::tolower(c); });
if (board_name == "pico") return Board::Pico;
if (board_name == "picow") return Board::PicoW;
if (board_name == "tiny2040") return Board::Tiny2040;
if (board_name == "tiny2040_2mb") return Board::Tiny2040_2mb;
if (board_name == "badger2040") return Board::Badger2040;
if (board_name == "gamebuino") return Board::Gamebuino;
if (board_name == "picosystem") return Board::Picosystem;
throw fmt::format("Invalid board: '{}'.", board_name);
}
std::string generateParameters(std::string descriptor, std::vector<u1> flags, bool isMethod)
{
std::string ret;
int count = isMethod ? 1 : 0;
if (descriptor[0] != '(')
{
throw fmt::format("Invalid descriptor. Should start with '(', got '{}'!", descriptor[0]);
}
int arrayCount = 0;
for (size_t index = 1; index < descriptor.size(); ++index)
{
switch (descriptor[index])
{
case ')':
index = descriptor.size();
break;
case 'L':
{
std::string type;
++index;
while (descriptor[index] != ';')
{
type += descriptor[index];
++index;
}
if (flags[count] & POINTER_TYPE) ++arrayCount;
if (type == "java/lang/String")
{
ret += fmt::format(", std::string {}local_{}", std::string(arrayCount, '*'), count);
}
else if (type == "pimoroni/buffer")
{
ret += fmt::format(", pimoroni::buffer {}local_{}", std::string(arrayCount, '*'), count);
}
else
{
throw fmt::format("classes are not supported as function arguments.");
}
break;
}
case 'I':
case 'Z':
{
ret += fmt::format(", {} {}local_{}", getTypeFromDescriptor(descriptor[index]+""s, flags[count]), std::string(arrayCount, '*'), count);
arrayCount = 0;
++count;
break;
}
case '[':
++arrayCount;
break;
default:
throw fmt::format("Invalid character in descriptor: '{}'.", descriptor[index]);
}
}
if (ret.size() > 0)
{
ret = ret.substr(2);
}
return ret;
}
std::string javaToCpp(std::string name)
{
return boost::replace_all_copy(name, "/"s, "::"s);
}
void copyUserFiles(fs::path currentPath)
{
for (auto ext : { ".h"s, ".cpp"s })
{
if (fs::exists(currentPath / (USER_FILE + ext)))
{
fs::copy_file(currentPath / (USER_FILE + ext), fs::current_path() / (USER_FILE + ext), fs::copy_options::overwrite_existing);
}
else
{
fs::remove(fs::current_path() / (USER_FILE + ext));
}
}
}