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

📄 runner.java

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

package jeex.tiny;

import java.util.List;
import java.io.*;
/** 
 * Executes a compiled program.
 */
public class Runner {
	public static void main(String args[]) {
		if (args.length < 1) {
			System.out.println("Usage: TinyRunner " + "filename");
		}
		Memory memory = new Memory(1024);
		//load program from the input file
		CodeArea codes = null;
		try {
			codes = Assembler.loadProgram(args[0]);
		} catch (IOException e){
			System.out.println("Read file faile.");
			System.exit(1);
		}
		ProcessUnit pu = new ProcessUnit(memory,codes);
		(new Thread(pu)).start();
	}
	/**
	 * Run a program.
	 * @param list - the instruction set container.
	 */
	static void run(List list) {
		Memory memory = new Memory(1024);
		CodeArea codes = Assembler.loadProgram(list);	
		ProcessUnit pu = new ProcessUnit(memory,codes);
		
		(new Thread(pu)).start();
		
	}
}
/** 
 * Excutation engine of instructions, simulate CPU of a computer.
 * ProcessUnit executes a instruction one by one.Instructions are fetched from CodeArea.
 * Data are fetched from the memory.When executing, a process exception maybe occure.
 */
class ProcessUnit implements Runnable {
	CodeArea codes;
	Memory memory; 
	boolean halt = false; // to indicate whether the program is over
	 
	int register[] = new int[8]; // there are eight general registers
		
	ProcessUnit(Memory memory,CodeArea codes) {
		this.memory = memory;
		this.codes = codes;
		reset();
	}
	
	void reset() {
		register[Code.GP] = 0;
		register[Code.MP] = memory.size() -1;
		register[Code.PC] = 0;
	}
			
	public void run() {
		try {
			while(!halt) {
				Instruction in = codes.get(register[Code.PC]);
				register[Code.PC] ++;
				in.process(this);		
			}
		} catch(ProcessException e) {
			System.out.println("ERROR");
		}
	}
	
}
/**
 * CodeArea is a set of instructions. The process unit 
 * takes a Instruction from a CodeArea one by one.
 */
class CodeArea {
	java.util.Vector v 
		= new java.util.Vector();
	/** 
	 * Return an instruction from the CodeArea called by the process unit.
	 * @param addr - address of the instruction returned.
	 */
	Instruction get(int addr) {
		if (!isValidAddress(addr)) 
			throw new ProcessException("illegal instruction address");
		return (Instruction)v.get(addr);
	}
	/** 
	 * Adds an instruction to the CodeArea.
	 * This method will invoke when building the CodeArea.
	 */
	void addInstr(Instruction in) {
		v.add(in);
	}
	/** 
	 * Prints out all instrutions in the CodeArea.
	 */
	void dump(PrintStream out) {
		for(int i = 0 ;i < v.size(); i ++) {
			out.println(v.get(i));
		}
	}
	/** 
	 * Returns how many instructions in the CodeArea.
	 */
	int count() {
		return v.size();
	}
	/** 
	 * Check weather an adress is valid.
	 * @param addr -- address to be valiated.
	 * @return true if valid.
	 */
	private boolean isValidAddress(int addr) {
		if (0 <= addr && addr < v.size()) 
			return true;
		return false;
	}
}
/**
 * Memory simulate RAM of computer.
 */
class Memory {
	private int m[];
	
	Memory(int size) {
		m = new int[size];
	}
	/**
	 * Read an integer from memory.
	 */
	synchronized int get(int addr) 
			throws ProcessException {
		assertLegalAddress(addr);
		return m[addr];
	}
	/**
	 * Write an integer from memory.
	 */
	synchronized void put(int addr,int value) 
			throws ProcessException {
		assertLegalAddress(addr);
		m[addr] = value;
	}
	/**
	 * Show the memory.
	 */
	void dump(PrintStream out) {
		out.println(m);
	}
	/**
	 * Returns the size of memory.
	 */
	int size() {
		return m.length;	
	}
	/**
	 * Show an value in memory in a specified address.
	 */
	void dump(PrintStream out,int addr) {
		out.println(m[addr]);
	}
	/**
	 * Make sure the specified address is in the scope of memory.
	 */
	private void assertLegalAddress(int addr) 
			throws ProcessException {
		if (addr<0 || addr> (m.length - 1)) 
			throw new ProcessException("illegal memory address");
	}
}


⌨️ 快捷键说明

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