Skip to content

Commit 15bdb76

Browse files
committed
VM (new pixeltower): getstatic 指令实现
1 parent 1273486 commit 15bdb76

6 files changed

Lines changed: 122 additions & 50 deletions

File tree

include/openminecraft/vm/pixeltower/v0/om_pixeltower.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class OMPixelTower
1919
void load(std::string path);
2020
void init(std::vector<std::shared_ptr<std::istream>> &streams);
2121
void boot(OMMethod *method);
22-
void mainLoop();
2322
OMKlassLoader *loader;
2423
OMPixelTowerHeap *heap;
2524
OMPixelTowerHeap *metaspace;

include/openminecraft/vm/pixeltower/v0/om_pixeltower_interpreter.hpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
namespace openminecraft::vm::pixeltower::v0
99
{
1010
class OMPixelTower;
11+
#define EXEC_OK 0
12+
#define EXEC_FAIL 1
13+
#define EXEC_RETURN 2
1114
class OMInterpreter
1215
{
1316
public:
@@ -20,22 +23,23 @@ class OMInterpreter
2023
private:
2124
void callDynamic(OMMethod *met);
2225
void popLastFrame();
23-
void loop() {
24-
jint result = 0;
25-
while (!result)
26-
{
27-
result = execute();
28-
}
26+
void loop()
27+
{
28+
jint result = EXEC_OK;
29+
while (!result)
30+
{
31+
result = execute();
32+
}
2933

30-
if (result == 2)
31-
{
32-
return;
33-
}
34-
else
35-
{
36-
logger.error("function exited with code {}", result);
37-
throw result;
38-
}
34+
if (result == EXEC_RETURN)
35+
{
36+
return;
37+
}
38+
else
39+
{
40+
logger.error("function exited with code {}", result);
41+
throw result;
42+
}
3943
}
4044

4145
log::OMLogger logger;

include/openminecraft/vm/pixeltower/v0/om_pixeltower_threads.hpp

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,10 @@ template <typename T> inline void stackPushAccessW(T data)
9595
}
9696
else
9797
{
98-
auto raw = (jint *)&data;
99-
stackPushAccess<jint>(data); // low bits
100-
stackPushAccess<jint>(data >> 32); // high bits
98+
auto raw = *(int64_t *)&data;
99+
100+
stackPushAccess<jint>(raw & 0xffffffff); // low bits
101+
stackPushAccess<jint>(raw >> 32); // high bits
101102
}
102103
}
103104

@@ -281,6 +282,73 @@ inline void accessFieldStatic(OMField *field)
281282
}
282283
}
283284

285+
template <typename Ttarget, typename Tvalue> inline void fetchFieldStaticI(OMField *field)
286+
{
287+
stackPushAccess<Tvalue>(*reinterpret_cast<Ttarget *>(
288+
static_cast<void *>(static_cast<uint8_t *>(field->klass->staticBlock) + field->offset)));
289+
}
290+
291+
template <typename Ttarget, typename Tvalue> inline void fetchFieldStaticW(OMField *field)
292+
{
293+
stackPushAccessW<Tvalue>(*reinterpret_cast<Ttarget *>(
294+
static_cast<void *>(static_cast<uint8_t *>(field->klass->staticBlock) + field->offset)));
295+
}
296+
297+
inline void fetchFieldStatic(OMField *field)
298+
{
299+
int p = 0;
300+
auto res = bytecode::descriptor::decodeType(field->desc, &p);
301+
if (res.type == util::Err)
302+
{
303+
throw err::OMValidationError{err::Instructions, "unknown field descriptor", field->desc};
304+
}
305+
306+
switch (hash_compile_time(res.unwrap().c_str()))
307+
{
308+
case "byte"_hash: {
309+
fetchFieldStaticI<jbyte, jint>(field);
310+
break;
311+
}
312+
case "short"_hash: {
313+
fetchFieldStaticI<jshort, jint>(field);
314+
break;
315+
}
316+
case "boolean"_hash: {
317+
fetchFieldStaticI<jboolean, jint>(field);
318+
break;
319+
}
320+
case "int"_hash: {
321+
fetchFieldStaticI<jint, jint>(field);
322+
break;
323+
}
324+
case "float"_hash: {
325+
fetchFieldStaticI<jfloat, jfloat>(field);
326+
break;
327+
}
328+
case "long"_hash: {
329+
fetchFieldStaticW<jlong, jlong>(field);
330+
break;
331+
}
332+
case "double"_hash: {
333+
fetchFieldStaticW<jdouble, jdouble>(field);
334+
break;
335+
}
336+
default: {
337+
auto h = currentThread.currentFrame->method->klass->heap;
338+
auto target = static_cast<void *>(static_cast<uint8_t *>(field->klass->staticBlock) + field->offset);
339+
if (h->ptrCompEnabled())
340+
{
341+
stackPushAccess<void *>(h->decompressPtr(*reinterpret_cast<uint32_t *>(target)));
342+
}
343+
else
344+
{
345+
stackPushAccess<void *>(*reinterpret_cast<void **>(target));
346+
}
347+
break;
348+
}
349+
}
350+
}
351+
284352
} // namespace openminecraft::vm::pixeltower::v0
285353

286354
#endif

src/boot/entrypoint.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ int boot(std::vector<std::string> args)
168168
if (strcmp(met->name, "main") == 0 && strcmp(met->desc, "([Ljava/lang/String;)V") == 0)
169169
{
170170
tower->boot(met);
171-
tower->mainLoop();
172171
break;
173172
}
174173
met = met->next;

src/vm/pixeltower/v0/om_pixeltower.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include "openminecraft/mem/om_mem_allocator.hpp"
44
#include "openminecraft/util/om_util_result.hpp"
55
#include "openminecraft/vm/classfile/om_class_file.hpp"
6-
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_base.hpp"
76
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_heap.hpp"
87
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_interpreter.hpp"
98
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_method.hpp"
@@ -34,10 +33,6 @@ void OMPixelTower::boot(OMMethod *method)
3433
interpreter->call(method);
3534
}
3635

37-
void OMPixelTower::mainLoop()
38-
{
39-
}
40-
4136
void OMPixelTower::init(std::string basePath)
4237
{
4338
std::vector<std::shared_ptr<std::istream>> target;

src/vm/pixeltower/v0/om_pixeltower_interpreter.cpp

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void OMInterpreter::call(OMMethod *met)
4848
{
4949
*localAccess<void *>(i) = nullptr;
5050
}
51-
loop();
51+
loop();
5252
return;
5353
}
5454

@@ -165,37 +165,37 @@ jint OMInterpreter::execute()
165165
case op_iconst_i(5): {
166166
stackPushAccess<jint>((jint)currentThread.pc[0] - (jint)op_iconst_i(0));
167167
currentThread.pc++;
168-
return 0;
168+
return EXEC_OK;
169169
}
170170
case op_lconst_l(0):
171171
case op_lconst_l(1): {
172172
stackPushAccessW<jlong>((jlong)currentThread.pc[0] - (jlong)op_lconst_l(0));
173173
currentThread.pc++;
174-
return 0;
174+
return EXEC_OK;
175175
}
176176
case op_iload_n(0):
177177
case op_iload_n(1):
178178
case op_iload_n(2):
179179
case op_iload_n(3): {
180180
stackPushAccess<jint>(localAccessValue<jint>(currentThread.pc[0] - op_iload_n(0)));
181181
currentThread.pc++;
182-
return 0;
182+
return EXEC_OK;
183183
}
184184
case op_lload_n(0):
185185
case op_lload_n(1):
186186
case op_lload_n(2):
187187
case op_lload_n(3): {
188188
stackPushAccessW<jlong>(localAccessValueW<jlong>(currentThread.pc[0] - op_lload_n(0)));
189189
currentThread.pc++;
190-
return 0;
190+
return EXEC_OK;
191191
}
192192
case op_aload_n(0):
193193
case op_aload_n(1):
194194
case op_aload_n(2):
195195
case op_aload_n(3): {
196196
stackPushAccess<void *>(localAccessValue<void *>(currentThread.pc[0] - op_aload_n(0)));
197197
currentThread.pc++;
198-
return 0;
198+
return EXEC_OK;
199199
}
200200
case op_istore_n(0):
201201
case op_istore_n(1):
@@ -204,7 +204,7 @@ jint OMInterpreter::execute()
204204
*localAccess<jint>(currentThread.pc[0] - op_istore_n(0)) = stackTopAccess<jint>();
205205
stackPop();
206206
currentThread.pc++;
207-
return 0;
207+
return EXEC_OK;
208208
}
209209
case op_astore_n(0):
210210
case op_astore_n(1):
@@ -213,22 +213,22 @@ jint OMInterpreter::execute()
213213
*localAccess<void *>(currentThread.pc[0] - op_astore_n(0)) = stackTopAccess<void *>();
214214
stackPop();
215215
currentThread.pc++;
216-
return 0;
216+
return EXEC_OK;
217217
}
218218
case op_pop: {
219219
stackPop();
220220
currentThread.pc++;
221-
return 0;
221+
return EXEC_OK;
222222
}
223223
case op_pop2: {
224224
stackPopW();
225225
currentThread.pc++;
226-
return 0;
226+
return EXEC_OK;
227227
}
228228
case op_dup: {
229229
stackPushAccess<void *>(stackTopAccess<void *>());
230230
currentThread.pc++;
231-
return 0;
231+
return EXEC_OK;
232232
}
233233
case op_ladd: {
234234
auto item2 = stackTopAccessW<jlong>();
@@ -237,14 +237,14 @@ jint OMInterpreter::execute()
237237
stackPopW();
238238
stackPushAccessW<jlong>(item2 + item);
239239
currentThread.pc++;
240-
return 0;
240+
return EXEC_OK;
241241
}
242242
case op_i2l: {
243243
auto v = stackTopAccess<jint>();
244244
stackPop();
245245
stackPushAccessW<jlong>(v);
246246
currentThread.pc++;
247-
return 0;
247+
return EXEC_OK;
248248
}
249249
case op_lcmp: {
250250
auto item2 = stackTopAccessW<jlong>();
@@ -264,7 +264,7 @@ jint OMInterpreter::execute()
264264
stackPushAccess<jint>(-1);
265265
}
266266
currentThread.pc++;
267-
return 0;
267+
return EXEC_OK;
268268
}
269269
case op_ifge: {
270270
auto i = stackTopAccess<jint>();
@@ -277,7 +277,7 @@ jint OMInterpreter::execute()
277277
{
278278
currentThread.pc += 3;
279279
}
280-
return 0;
280+
return EXEC_OK;
281281
}
282282
case op_if_acmpne: {
283283
auto item = stackTopAccess<void *>();
@@ -293,60 +293,67 @@ jint OMInterpreter::execute()
293293
currentThread.pc += 3;
294294
}
295295

296-
return 0;
296+
return EXEC_OK;
297297
}
298298
case op_ireturn: {
299299
auto ret = stackTopAccess<jint>();
300300
popLastFrame();
301301
stackPushAccess<jint>(ret);
302-
return 2;
302+
return EXEC_RETURN;
303303
}
304304
case op_lreturn: {
305305
auto ret = stackTopAccessW<jlong>();
306306
popLastFrame();
307307
stackPushAccessW<jlong>(ret);
308-
return 2;
308+
return EXEC_RETURN;
309309
}
310310
case op_return: {
311311
popLastFrame();
312-
return 2;
312+
return EXEC_RETURN;
313+
}
314+
case op_getstatic: {
315+
auto n = *static_cast<OMField **>(static_cast<void *>(
316+
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
317+
fetchFieldStatic(n);
318+
currentThread.pc += 3;
319+
return EXEC_OK;
313320
}
314321
case op_putstatic: {
315322
auto n = *static_cast<OMField **>(static_cast<void *>(
316323
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
317324
accessFieldStatic(n);
318325
currentThread.pc += 3;
319326

320-
return 0;
327+
return EXEC_OK;
321328
}
322329
case op_putfield: {
323330
auto n = *static_cast<OMField **>(static_cast<void *>(
324331
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
325332
accessField(n);
326333
currentThread.pc += 3;
327334

328-
return 0;
335+
return EXEC_OK;
329336
}
330337
case op_invokevirtual: {
331338
auto n = *static_cast<OMMethod **>(static_cast<void *>(
332339
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
333340
callDynamic(n);
334341

335-
return 0;
342+
return EXEC_OK;
336343
}
337344
case op_invokespecial: {
338345
auto n = *static_cast<OMMethod **>(static_cast<void *>(
339346
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
340347
call(n);
341348

342-
return 0;
349+
return EXEC_OK;
343350
}
344351
case op_new: {
345352
auto n = *static_cast<OMKlass **>(static_cast<void *>(
346353
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
347354
stackPushAccess<void *>(n->allocateInstance());
348355
currentThread.pc += 3;
349-
return 0;
356+
return EXEC_OK;
350357
}
351358
default: {
352359
endExec:
@@ -364,6 +371,6 @@ jint OMInterpreter::execute()
364371
break;
365372
}
366373
}
367-
return 1;
374+
return EXEC_FAIL;
368375
}
369376
} // namespace openminecraft::vm::pixeltower::v0

0 commit comments

Comments
 (0)