⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 instruction.java

📁 java语言开发的基于tiny语言的编译器
💻 JAVA
字号:

package jeex.tiny;
/** 
 * An instruction can be executed by a process unit.
 */
abstract class Instruction {
	String name;
	Instruction(String name) {
		this.name = name;
	}
	abstract void process(ProcessUnit pu);
}
/** 
 * Register-only instruction. Format: opcode r,s,t
 * r is the result register, s and t are operand register.
 */
abstract class ROInstruction extends Instruction {	
	int r,s,t;
	ROInstruction(String name,int r,int s,int t) {
		super(name);
		this.r = r;
		this.s = s;
		this.t = t;
	}
	
	public String toString() {
		return name +" "+ r + "," + s + "," + t;
	}
}

/**
 * Regiseter-memory instruction. Format r,d(s)
 * "r" is a register, "d" and "s" form a memory address "a".
 */
abstract class RMInstruction extends Instruction {	
	int r,d,s;
	RMInstruction(String name,int r,int d,int s) {
		super(name);
		this.r = r;
		this.d = d;
		this.s = s;
	}
	
	public String toString() {
		return name +" "+ r + "," + d + "(" + s + ")";
	}
	
	int addr(ProcessUnit pu) {
		return d + pu.register[s];
	}
}
/**
 * Load value in memory to register.
 */
class LDInstr extends RMInstruction {
	LDInstr(int r,int d,int s) {
		super("LD",r,d,s);
	}		
	void process(ProcessUnit pu) {
		pu.register[r] = pu.memory.get(addr(pu));
	}
}
/**
 * Load address directly into register.
 */
class LDAInstr extends RMInstruction {
	LDAInstr(int r,int d,int s) {
		super("LDA",r,d,s);
	}		
	void process(ProcessUnit pu) {
		pu.register[r] = addr(pu);
	}
}
/**
 * Load constant directly into register.
 */
class LDCInstr extends RMInstruction {
	LDCInstr(int r,int d) {
		super("LDC",r,d,0);
	}	
	void process(ProcessUnit pu) {
		pu.register[r] = d;
	}
	public String toString() {
		return name + " " + r + "," + d;
	}
}	
/**
 * Store value in register to memory.
 */
class STInstr extends RMInstruction {
	STInstr(int r,int d,int s) {
		super("ST",r,d,s);
	}		
	void process(ProcessUnit pu) {
		pu.memory.put(addr(pu),pu.register[r]);
	}
}
/**
 * Jump if value in register is <0.
 */
class JLTInstr extends RMInstruction {
	JLTInstr(int r,int d,int s) {
		super("JLT",r,d,s);
	}		
	void process(ProcessUnit pu) {
		if (pu.register[r] < 0) pu.register[Code.PC] = addr(pu);
	}
}
/**
 * Jump if value in register is <=0.
 */
class JLEInstr extends RMInstruction {
	JLEInstr(int r,int d,int s) {
		super("JLE",r,d,s);
	}
	void process(ProcessUnit pu) {
		if (pu.register[r] <= 0) pu.register[Code.PC] = addr(pu);
	}
}
/**
 * Jump if value in register is >=0.
 */
class JGEInstr extends RMInstruction {
	JGEInstr(int r,int d,int s) {
		super("JGE",r,d,s);
	}		
	void process(ProcessUnit pu) {
		if (pu.register[r] >=0 ) pu.register[Code.PC] = addr(pu);
	}
}
/**
 * Jump if value in register is >0.
 */
class JGTInstr extends RMInstruction {
	JGTInstr(int r,int d,int s) {
		super("JGT",r,d,s);
	}		
	void process(ProcessUnit pu) {
		if (pu.register[r] > 0) pu.register[Code.PC] = addr(pu);
	}
}
/**
 * Jump if value in register is =0.
 */
class JEQInstr extends RMInstruction {
	JEQInstr(int r,int d,int s) {
		super("JEQ",r,d,s);
	}		
	void process(ProcessUnit pu) {
		if (pu.register[r] == 0) pu.register[Code.PC] = addr(pu);
	}
}
/**
 * Jump if value in register is !=0.
 */
class JNEInstr extends RMInstruction {
	JNEInstr(int r,int d,int s) {
		super("JNE",r,d,s);
	}		
	void process(ProcessUnit pu) {
		if (pu.register[r] != 0) pu.register[Code.PC] = addr(pu);
	}
}
/**
 * Stop execution.
 */
class HALTInstr extends ROInstruction {
	HALTInstr() {
		super("HALT",0,0,0);
	}
	void process(ProcessUnit pu) {
		pu.halt = true;
	}
	public String toString() {
		return name;
	}
}
/**
 * Read value from the standard input to register.
 */
class INInstr extends ROInstruction {
	INInstr(int r) {
		super("IN",r,0,0);
	}
	void process(ProcessUnit pu) {
	}
	public String toString() {
		return name + " " + r; 
	}
}
/**
 * Write value from register to the standard output.
 */
class OUTInstr extends ROInstruction {
	OUTInstr(int r) {
		super("OUT",r,0,0);
	}
	void process(ProcessUnit pu) {
		System.out.println(pu.register[r]);
	}
	public String toString() {
		return name + " " + r;
	}
}
/**
 * Add operation.
 */
class ADDInstr extends ROInstruction {
	ADDInstr(int r,int s, int t) {
		super("ADD",r,s,t);
	}
	void process(ProcessUnit pu) {
		pu.register[r] = pu.register[s] + pu.register[t];
	}
}
/**
 * Subtraction operation.
 */
class SUBInstr extends ROInstruction {
	SUBInstr(int r,int s, int t) {
		super("SUB",r,s,t);
	}
	void process(ProcessUnit pu) {
		pu.register[r] = pu.register[s] - pu.register[t];
	}
}
/**
 * Multiplicatin operation.
 */
class MULInstr extends ROInstruction {
	MULInstr(int r,int s, int t) {
		super("MUL",r,s,t);
	}
	void process(ProcessUnit pu) {
		pu.register[r] = pu.register[s] * pu.register[t];
	}
}
/**
 * Division operation.
 */
class DIVInstr extends ROInstruction {
	DIVInstr(int r,int s, int t) {
		super("DIV",r,s,t);
	}
	void process(ProcessUnit pu) {
		pu.register[r] = pu.register[s] / pu.register[t];
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -