Skip to content

Commit 7674f64

Browse files
committed
VM (new pixeltower): <clinit>
1 parent a354488 commit 7674f64

9 files changed

Lines changed: 110 additions & 56 deletions

File tree

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ class OMPixelTower
1818
void init(std::string basePath);
1919
void load(std::string path);
2020
void init(std::vector<std::shared_ptr<std::istream>> &streams);
21-
void stackTest(OMMethod *method);
21+
void boot(OMMethod *method);
22+
void mainLoop();
2223
OMKlassLoader *loader;
2324
OMPixelTowerHeap *heap;
2425
OMPixelTowerHeap *metaspace;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ class OMInterpreter
1414
OMInterpreter(OMPixelTowerHeap *heap, OMPixelTower *tower);
1515
~OMInterpreter();
1616

17-
bool execute();
17+
jint execute();
18+
void call(OMMethod *met);
1819

1920
private:
20-
void call(OMMethod *met);
2121
void callDynamic(OMMethod *met);
2222
void popLastFrame();
2323

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "openminecraft/log/om_log_common.hpp"
55
#include "openminecraft/vm/classfile/om_class_file.hpp"
66
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_heap.hpp"
7+
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_interpreter.hpp"
78
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_klass.hpp"
89
#include <memory>
910
#include <vector>
@@ -12,7 +13,7 @@ namespace openminecraft::vm::pixeltower::v0
1213
class OMKlassLoader
1314
{
1415
public:
15-
OMKlassLoader(OMPixelTowerHeap *heap, OMPixelTowerHeap *metaspace);
16+
OMKlassLoader(OMPixelTowerHeap *heap, OMPixelTowerHeap *metaspace, OMInterpreter *interpreter);
1617
~OMKlassLoader();
1718

1819
void stagClass(std::shared_ptr<classfile::OMClassFile> file)
@@ -27,6 +28,7 @@ class OMKlassLoader
2728
private:
2829
OMPixelTowerHeap *metaspace;
2930
OMPixelTowerHeap *heap;
31+
OMInterpreter *interpreter;
3032
log::OMLogger logger;
3133
std::vector<OMKlass *> classes;
3234
std::vector<std::shared_ptr<classfile::OMClassFile>> files;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class OMPixelTowerThread
1616

1717
std::thread::id id;
1818
std::string name;
19-
uint8_t *pc;
20-
OMFrame *currentFrame;
21-
void *stack;
22-
void *stackPointer;
23-
void *stackEnd;
19+
uint8_t *pc = nullptr;
20+
OMFrame *currentFrame = nullptr;
21+
void *stack = nullptr;
22+
void *stackPointer = nullptr;
23+
void *stackEnd = nullptr;
2424
};
2525

2626
extern thread_local OMPixelTowerThread currentThread;

src/boot/entrypoint.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_heap.hpp"
4141
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_method.hpp"
4242
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_oop.hpp"
43+
#include "openminecraft/vm/pixeltower/v1/om_pixeltower_debugger.hpp"
4344
#include <SDL3/SDL.h>
4445
#include <boost/stacktrace.hpp>
4546
#include <fmt/format.h>
@@ -161,16 +162,21 @@ int boot(std::vector<std::string> args)
161162
tower->load("../Test.class");
162163
tower->loader->loadClass("openminecraft/Test");
163164
auto cls = tower->loader->fetchClass("openminecraft/Test");
165+
logger->debug("{}", (void *)cls);
166+
pixeltower::v1::OMDebugger deb;
167+
deb.debugStack();
168+
/*
164169
auto met = cls->methods;
165170
while (met != nullptr)
166171
{
167172
if (strcmp(met->name, "main") == 0 && strcmp(met->desc, "([Ljava/lang/String;)V") == 0)
168173
{
169-
tower->stackTest(met);
174+
tower->boot(met);
175+
tower->mainLoop();
170176
break;
171177
}
172178
met = met->next;
173-
}
179+
}*/
174180
tower->destroyCurrentThread();
175181
commandBuffer.clear();
176182

src/vm/pixeltower/v0/om_pixeltower.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include "openminecraft/util/om_util_result.hpp"
55
#include "openminecraft/vm/classfile/om_class_file.hpp"
66
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_base.hpp"
7-
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_frame.hpp"
87
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_heap.hpp"
98
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_interpreter.hpp"
109
#include "openminecraft/vm/pixeltower/v0/om_pixeltower_method.hpp"
@@ -19,8 +18,8 @@ OMPixelTower::OMPixelTower() : logger("OMPixelTower", this)
1918
{
2019
heap = new OMPixelTowerHeap(1ul * 1024 * 1024, 1ul * 1024 * 1024 * 1024);
2120
metaspace = new OMPixelTowerHeap(1ul * 1024 * 1024, 8ul * 1024 * 1024);
22-
loader = new OMKlassLoader(heap, metaspace);
2321
interpreter = new OMInterpreter(heap, this);
22+
loader = new OMKlassLoader(heap, metaspace, interpreter);
2423
}
2524
OMPixelTower::~OMPixelTower()
2625
{
@@ -30,22 +29,17 @@ OMPixelTower::~OMPixelTower()
3029
delete metaspace;
3130
}
3231

33-
void OMPixelTower::stackTest(OMMethod *method)
32+
void OMPixelTower::boot(OMMethod *method)
3433
{
35-
auto frame = (OMFrame *)((uint8_t *)currentThread.stackPointer - sizeof(OMFrame));
36-
currentThread.stackPointer = (jbyte *)currentThread.stackPointer - sizeof(OMFrame) -
37-
method->maxLocals * sizeof(void *); // allocate whole frame + locals
38-
stackPush();
39-
logger.debug("current sp: {}", currentThread.stackPointer);
40-
frame->returnAddr = (void *)0x33550336;
41-
frame->prev = nullptr;
42-
frame->method = method;
43-
44-
currentThread.currentFrame = frame;
45-
currentThread.pc = method->code;
34+
interpreter->call(method);
35+
}
4636

47-
while (interpreter->execute())
37+
void OMPixelTower::mainLoop()
38+
{
39+
jint result = 0;
40+
while (!result)
4841
{
42+
result = interpreter->execute();
4943
}
5044
}
5145

src/vm/pixeltower/v0/om_pixeltower_interpreter.cpp

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@ OMInterpreter::~OMInterpreter()
2323

2424
void OMInterpreter::call(OMMethod *met)
2525
{
26+
if (!currentThread.currentFrame)
27+
{
28+
auto frame = (OMFrame *)((uint8_t *)currentThread.stackPointer - sizeof(OMFrame));
29+
currentThread.stackPointer = (jbyte *)currentThread.stackPointer - sizeof(OMFrame) -
30+
met->maxLocals * sizeof(void *); // allocate whole frame + locals
31+
stackPush();
32+
frame->returnAddr = (void *)0x33550336;
33+
frame->prev = nullptr;
34+
frame->method = met;
35+
36+
currentThread.currentFrame = frame;
37+
currentThread.pc = met->code;
38+
return;
39+
}
40+
2641
auto frame = currentThread.currentFrame;
2742
auto nextframe = (OMFrame *)((uint8_t *)currentThread.stackPointer - sizeof(OMFrame) + sizeof(void *) +
2843
met->args * sizeof(void *));
@@ -105,13 +120,14 @@ void OMInterpreter::callDynamic(OMMethod *met)
105120

106121
void OMInterpreter::popLastFrame()
107122
{
123+
auto met = currentThread.currentFrame->method;
108124
currentThread.pc = (uint8_t *)currentThread.currentFrame->returnAddr;
109125
currentThread.stackPointer = (uint8_t *)currentThread.currentFrame + sizeof(OMFrame); // popped whole frame
110126
currentThread.currentFrame = currentThread.currentFrame->prev;
111127
stackPush();
112128
}
113129

114-
bool OMInterpreter::execute()
130+
jint OMInterpreter::execute()
115131
{
116132
auto frame = currentThread.currentFrame;
117133
switch (currentThread.pc[0])
@@ -125,61 +141,70 @@ bool OMInterpreter::execute()
125141
case op_iconst_i(5): {
126142
stackPushAccess<jint>((jint)currentThread.pc[0] - (jint)op_iconst_i(0));
127143
currentThread.pc++;
128-
return true;
144+
return 0;
129145
}
130146
case op_lconst_l(0):
131147
case op_lconst_l(1): {
132148
stackPushAccessW<jlong>((jlong)currentThread.pc[0] - (jlong)op_lconst_l(0));
133149
currentThread.pc++;
134-
return true;
150+
return 0;
135151
}
136152
case op_iload_n(0):
137153
case op_iload_n(1):
138154
case op_iload_n(2):
139155
case op_iload_n(3): {
140156
stackPushAccess<jint>(localAccessValue<jint>(currentThread.pc[0] - op_iload_n(0)));
141157
currentThread.pc++;
142-
return true;
158+
return 0;
143159
}
144160
case op_lload_n(0):
145161
case op_lload_n(1):
146162
case op_lload_n(2):
147163
case op_lload_n(3): {
148164
stackPushAccessW<jlong>(localAccessValueW<jlong>(currentThread.pc[0] - op_lload_n(0)));
149165
currentThread.pc++;
150-
return true;
166+
return 0;
151167
}
152168
case op_aload_n(0):
153169
case op_aload_n(1):
154170
case op_aload_n(2):
155171
case op_aload_n(3): {
156172
stackPushAccess<void *>(localAccessValue<void *>(currentThread.pc[0] - op_aload_n(0)));
157173
currentThread.pc++;
158-
return true;
174+
return 0;
175+
}
176+
case op_istore_n(0):
177+
case op_istore_n(1):
178+
case op_istore_n(2):
179+
case op_istore_n(3): {
180+
*localAccess<jint>(currentThread.pc[0] - op_istore_n(0)) = stackTopAccess<jint>();
181+
stackPop();
182+
currentThread.pc++;
183+
return 0;
159184
}
160185
case op_astore_n(0):
161186
case op_astore_n(1):
162187
case op_astore_n(2):
163188
case op_astore_n(3): {
164189
*localAccess<void *>(currentThread.pc[0] - op_astore_n(0)) = stackTopAccess<void *>();
165-
stackPop();
190+
stackPop();
166191
currentThread.pc++;
167-
return true;
168-
}
192+
return 0;
193+
}
169194
case op_pop: {
170195
stackPop();
171196
currentThread.pc++;
172-
return true;
197+
return 0;
173198
}
174199
case op_pop2: {
175200
stackPopW();
176201
currentThread.pc++;
177-
return true;
202+
return 0;
178203
}
179204
case op_dup: {
180205
stackPushAccess<void *>(stackTopAccess<void *>());
181206
currentThread.pc++;
182-
return true;
207+
return 0;
183208
}
184209
case op_ladd: {
185210
auto item2 = stackTopAccessW<jlong>();
@@ -188,14 +213,14 @@ bool OMInterpreter::execute()
188213
stackPopW();
189214
stackPushAccessW<jlong>(item2 + item);
190215
currentThread.pc++;
191-
return true;
216+
return 0;
192217
}
193218
case op_i2l: {
194219
auto v = stackTopAccess<jint>();
195220
stackPop();
196221
stackPushAccessW<jlong>(v);
197222
currentThread.pc++;
198-
return true;
223+
return 0;
199224
}
200225
case op_lcmp: {
201226
auto item2 = stackTopAccessW<jlong>();
@@ -215,7 +240,7 @@ bool OMInterpreter::execute()
215240
stackPushAccess<jint>(-1);
216241
}
217242
currentThread.pc++;
218-
return true;
243+
return 0;
219244
}
220245
case op_ifge: {
221246
auto i = stackTopAccess<jint>();
@@ -228,7 +253,7 @@ bool OMInterpreter::execute()
228253
{
229254
currentThread.pc += 3;
230255
}
231-
return true;
256+
return 0;
232257
}
233258
case op_if_acmpne: {
234259
auto item = stackTopAccess<void *>();
@@ -244,62 +269,61 @@ bool OMInterpreter::execute()
244269
currentThread.pc += 3;
245270
}
246271

247-
return true;
272+
return 0;
248273
}
249274
case op_ireturn: {
250275
auto ret = stackTopAccess<jint>();
251276
popLastFrame();
252277
stackPushAccess<jint>(ret);
253-
return true;
278+
return 0;
254279
}
255280
case op_lreturn: {
256281
auto ret = stackTopAccessW<jlong>();
257282
popLastFrame();
258283
stackPushAccessW<jlong>(ret);
259-
return true;
284+
return 0;
260285
}
261286
case op_return: {
287+
bool isClinit = strcmp("<clinit>", currentThread.currentFrame->method->name) == 0;
262288
popLastFrame();
263-
return true;
289+
return isClinit ? 2 : 0;
264290
}
265291
case op_invokevirtual: {
266292
auto n = *static_cast<OMMethod **>(static_cast<void *>(
267293
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
268294
callDynamic(n);
269295

270-
return true;
296+
return 0;
271297
}
272298
case op_invokespecial: {
273299
auto n = *static_cast<OMMethod **>(static_cast<void *>(
274300
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
275301
call(n);
276302

277-
return true;
303+
return 0;
278304
}
279305
case op_new: {
280306
auto n = *static_cast<OMKlass **>(static_cast<void *>(
281307
frame->method->klass->constantPool + binary::be16ToNative(*(uint16_t *)(currentThread.pc + 1))));
282308
stackPushAccess<void *>(n->allocateInstance());
283309
currentThread.pc += 3;
284-
return true;
310+
return 0;
285311
}
286312
default: {
287-
logger.debug("unknown command at {}", (void *)currentThread.pc);
313+
logger.debug("unknown operand at {}", (void *)currentThread.pc);
288314
logger.debug("thread {}", (void *)&currentThread.id);
289315
logger.debug("pc pointed at {} ({}.{}{} + {})", (void *)currentThread.pc,
290316
currentThread.currentFrame->method->klass->name, currentThread.currentFrame->method->name,
291317
currentThread.currentFrame->method->desc,
292318
reinterpret_cast<size_t>(
293319
reinterpret_cast<void *>(static_cast<uint8_t *>(currentThread.pc) -
294320
static_cast<uint8_t *>(currentThread.currentFrame->method->code))));
295-
logger.debug("sp pointed at {}", (void *)currentThread.stackPointer);
296-
logger.debug("method metadata at {}", (void *)frame);
297321

298322
debugger.debugStack();
299323

300324
break;
301325
}
302326
}
303-
return false;
327+
return 1;
304328
}
305329
} // namespace openminecraft::vm::pixeltower::v0

0 commit comments

Comments
 (0)