}
JvmStack.java
package org.itstack.demo.jvm.rtda;
/**
http://www.itstack.org
create by fuzhengwei on 2019/4/26
虚拟机栈
*/
public class JvmStack {
private int maxSize;
private int size;
private Frame _top;
public JvmStack(int maxSize) {
this.maxSize = maxSize;
}
public void push(Frame frame) {
if (this.size > this.maxSize) {
throw new StackOverflowError();
}
if (this._top != null) {
frame.lower = this._top;
}
this._top = frame;
this.size++;
}
public Frame pop() {
if (this._top == null) {
throw new RuntimeException(“jvm stack is empty!”);
}
Frame top = this._top;
this._top = top.lower;
top.lower = null;
this.size–;
return top;
}
public Frame top(){
if (this._top == null){
throw new RuntimeException(“jvm stack is empty!”);
}
return this._top;
}
}
LocalVars.java
package org.itstack.demo.jvm.rtda;
/**
http://www.itstack.org
create by fuzhengwei on 2019/4/26
局部变量表
*/
public class LocalVars {
private Slot[] slots;
public LocalVars(int maxLocals) {
if (maxLocals > 0) {
slots = new Slot[maxLocals];
for (int i = 0; i < maxLocals; i++) {
slots[i] = new Slot();
}
}
}
public void setInt(int idx, int val) {
this.slots[idx].num = val;
}
publi 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 c int getInt(int idx) {
return slots[idx].num;
}
public void setFloat(int idx, float val) {
this.slots[idx].num = (Float.valueOf(val)).intValue();
}
public Float getFloat(int idx) {
int num = this.slots[idx].num;
return (float) num;
}
public void setLong(int idx, long val) {
this.slots[idx].num = (int) val;
this.slots[idx + 1].num = (int) (val >> 32);
}
public Long getLong(int idx) {
int low = this.slots[idx].num;
int high = this.slots[idx + 1].num;
return ((long) high << 32) | (long) low;
}
public void setDouble(int idx, double val) {
setLong(idx, (long) val);
}
public Double getDouble(int idx) {
return Double.valueOf(getLong(idx));
}
public void setRef(int idx, Object ref) {
slots[idx].ref = ref;
}
public Object getRef(int idx) {
return slots[idx].ref;
}
}
OperandStack.java
package org.itstack.demo.jvm.rtda;
/**
http://www.itstack.org
create by fuzhengwei on 2019/4/26
操作数栈
*/
public class OperandStack {
private int size = 0;
private Slot[] slots;
public OperandStack(int maxStack) {
if (maxStack > 0) {
slots = new Slot[maxStack];
for (int i = 0; i < maxStack; i++) {
slots[i] = new Slot();
}
}
}
public void pushInt(int val) {
slots[size].num = val;
size++;
}
public int popInt(){
size --;
return slots[size].num;
}
public void pushRef(Object ref){
slots[size].ref = ref;
size++;
}
public Object popRef(){
size --;
Object ref = slots[size].ref;
slots[size].ref = null;
return ref;
}
}
Slot.java
package org.itstack.demo.jvm.rtda;
/**
http://www.itstack.org
create by fuzhengwei on 2019/4/26
数据槽
*/
public class Slot {
int num;
Object ref;
}
Thread.java
package org.itstack.demo.jvm.rtda;
/**
http://www.itstack.org
create by fuzhengwei on 2019/4/26
线程
*/
public class Thread {
//Program Counter 寄存器
private int pc;
//虚拟机栈
private JvmStack stack;
public Thread(){
this.stack = new JvmStack(1024);
}
public int pc(){