@@ -169,6 +169,12 @@ Object *Object_create(Class *class) {
169
169
return obj ;
170
170
}
171
171
172
+ intern inline i16 read_immediate_i16 (Frame * f ) {
173
+ u8 branchbyte1 = * (f -> pc ++ );
174
+ u8 branchbyte2 = * (f -> pc ++ );
175
+ return (i16 )((branchbyte1 << 8 ) | branchbyte2 );
176
+ }
177
+
172
178
void execute (ClassLoader class_loader , Method * method ) {
173
179
assert (method );
174
180
@@ -227,60 +233,49 @@ void execute(ClassLoader class_loader, Method *method) {
227
233
// TODO: Macro ifCOND(op) ...
228
234
case ifeq :
229
235
{
230
- u8 branchbyte1 = * (f .pc ++ );
231
- u8 branchbyte2 = * (f .pc ++ );
236
+ int offset = read_immediate_i16 (& f );
232
237
233
238
f .sp -= 1 ;
234
239
u32 value = * f .sp ;
235
240
236
241
if (value == 0 ) { // Branch succeeds
237
- int offset = (i16 )((branchbyte1 << 8 ) | branchbyte2 );
238
242
f .pc += offset - 3 ;
239
243
}
240
244
} break ;
241
245
case ifne :
242
246
{
243
- u8 branchbyte1 = * (f .pc ++ );
244
- u8 branchbyte2 = * (f .pc ++ );
245
-
246
- f .sp -= 1 ;
247
- u32 value = * f .sp ;
247
+ int offset = read_immediate_i16 (& f );
248
+ u32 value = f_pop (& f );
248
249
249
250
if (value != 0 ) { // Branch succeeds
250
- int offset = (i16 )((branchbyte1 << 8 ) | branchbyte2 );
251
251
f .pc += offset - 3 ;
252
252
}
253
253
} break ;
254
254
case if_icmpgt :
255
255
{
256
- u8 branchbyte1 = * (f .pc ++ );
257
- u8 branchbyte2 = * (f .pc ++ );
256
+ int offset = read_immediate_i16 (& f );
258
257
259
- f . sp -= 1 ; u32 value2 = * f . sp ;
260
- f . sp -= 1 ; u32 value1 = * f . sp ;
258
+ u32 value2 = f_pop ( & f ) ;
259
+ u32 value1 = f_pop ( & f ) ;
261
260
262
261
if (value1 > value2 ) { // Branch succeeds
263
- int offset = (i16 ) ((branchbyte1 << 8 ) | branchbyte2 );
264
262
f .pc += offset - 3 ;
265
263
}
266
264
} break ;
267
265
case if_icmpne :
268
266
{
269
- u8 branchbyte1 = * (f .pc ++ );
270
- u8 branchbyte2 = * (f .pc ++ );
267
+ int offset = read_immediate_i16 (& f );
271
268
272
- f . sp -= 1 ; u32 value2 = * f . sp ;
273
- f . sp -= 1 ; u32 value1 = * f . sp ;
269
+ u32 value2 = f_pop ( & f ) ;
270
+ u32 value1 = f_pop ( & f ) ;
274
271
275
272
if (value1 != value2 ) { // Branch succeeds
276
- int offset = (i16 ) ((branchbyte1 << 8 ) | branchbyte2 );
277
273
f .pc += offset - 3 ;
278
274
}
279
275
} break ;
280
276
case ireturn :
281
277
{
282
- f .sp -= 1 ;
283
- u32 value = * f .sp ;
278
+ u32 value = f_pop (& f );
284
279
printf ("Returning int %d\n" , value );
285
280
if (sb_is_empty (frames )) {
286
281
goto exit ;
@@ -293,8 +288,7 @@ void execute(ClassLoader class_loader, Method *method) {
293
288
sb_pop (frames );
294
289
295
290
// push the return value
296
- * f .sp = value ;
297
- f .sp ++ ;
291
+ f_push (& f , value );
298
292
} break ;
299
293
case return_void :
300
294
{
@@ -312,9 +306,7 @@ void execute(ClassLoader class_loader, Method *method) {
312
306
} break ;
313
307
case getstatic :
314
308
{
315
- u8 indexbyte1 = * (f .pc ++ );
316
- u8 indexbyte2 = * (f .pc ++ );
317
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
309
+ u16 index = read_immediate_i16 (& f );
318
310
319
311
cp_info field_name ;
320
312
Class * class = load_class_and_field_name_from_field_ref (
@@ -332,9 +324,7 @@ void execute(ClassLoader class_loader, Method *method) {
332
324
} break ;
333
325
case putstatic :
334
326
{
335
- u8 indexbyte1 = * (f .pc ++ );
336
- u8 indexbyte2 = * (f .pc ++ );
337
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
327
+ u16 index = read_immediate_i16 (& f );
338
328
339
329
u32 value = f_pop (& f );
340
330
@@ -379,9 +369,7 @@ void execute(ClassLoader class_loader, Method *method) {
379
369
} break ;
380
370
case invokestatic :
381
371
{
382
- u8 indexbyte1 = * (f .pc ++ );
383
- u8 indexbyte2 = * (f .pc ++ );
384
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
372
+ u16 index = read_immediate_i16 (& f );
385
373
386
374
u16 name_and_type_index =
387
375
method -> constant_pool [index - 1 ].as .methodref_info .name_and_type_index ;
@@ -439,17 +427,13 @@ void execute(ClassLoader class_loader, Method *method) {
439
427
} break ;
440
428
case goto_instr :
441
429
{
442
- u8 branchbyte1 = * (f .pc ++ );
443
- u8 branchbyte2 = * (f .pc ++ );
430
+ int offset = read_immediate_i16 (& f );
444
431
445
- int offset = (i16 )((branchbyte1 << 8 ) | branchbyte2 );
446
432
f .pc += offset - 3 ;
447
433
} break ;
448
434
case getfield :
449
435
{
450
- u8 indexbyte1 = * (f .pc ++ );
451
- u8 indexbyte2 = * (f .pc ++ );
452
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
436
+ u16 index = read_immediate_i16 (& f );
453
437
454
438
Object * this_ptr = (Object * )f_pop (& f );
455
439
@@ -470,9 +454,7 @@ void execute(ClassLoader class_loader, Method *method) {
470
454
} break ;
471
455
case putfield :
472
456
{
473
- u8 indexbyte1 = * (f .pc ++ );
474
- u8 indexbyte2 = * (f .pc ++ );
475
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
457
+ u16 index = read_immediate_i16 (& f );
476
458
477
459
u64 value = f_pop (& f );
478
460
Object * this_ptr = (Object * )f_pop (& f );
@@ -498,9 +480,7 @@ void execute(ClassLoader class_loader, Method *method) {
498
480
{
499
481
// https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-6.html#jvms-6.5.invokevirtual
500
482
// TODO: copied from invokespecial => refactor
501
- u8 indexbyte1 = * (f .pc ++ );
502
- u8 indexbyte2 = * (f .pc ++ );
503
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
483
+ u16 index = read_immediate_i16 (& f );
504
484
505
485
Object * this_ptr = (Object * )f_pop (& f );
506
486
@@ -542,9 +522,7 @@ void execute(ClassLoader class_loader, Method *method) {
542
522
} break ;
543
523
case invokespecial :
544
524
{
545
- u8 indexbyte1 = * (f .pc ++ );
546
- u8 indexbyte2 = * (f .pc ++ );
547
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
525
+ u16 index = read_immediate_i16 (& f );
548
526
549
527
Object * this_ptr = (Object * )f_pop (& f );
550
528
@@ -586,9 +564,7 @@ void execute(ClassLoader class_loader, Method *method) {
586
564
} break ;
587
565
case new :
588
566
{
589
- u8 indexbyte1 = * (f .pc ++ );
590
- u8 indexbyte2 = * (f .pc ++ );
591
- u16 index = (indexbyte1 << 8 ) | indexbyte2 ;
567
+ u16 index = read_immediate_i16 (& f );
592
568
593
569
Class * class = load_class_from_constant_pool (class_loader , method -> constant_pool , index );
594
570
Object * obj = Object_create (class );
0 commit comments