@@ -23,6 +23,21 @@ OMInterpreter::~OMInterpreter()
2323
2424void 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
106121void 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 *)¤tThread.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