You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
356 lines
7.4 KiB
356 lines
7.4 KiB
3 years ago
|
/*
|
||
|
* @(#)Disassembler.java 2.1 2003/10/07
|
||
|
*
|
||
|
* Copyright (C) 1999, 2003 D.A. Watt and D.F. Brown
|
||
|
* Dept. of Computing Science, University of Glasgow, Glasgow G12 8QQ Scotland
|
||
|
* and School of Computer and Math Sciences, The Robert Gordon University,
|
||
|
* St. Andrew Street, Aberdeen AB25 1HG, Scotland.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This software is provided free for educational use only. It may
|
||
|
* not be used for commercial purposes without the prior written permission
|
||
|
* of the authors.
|
||
|
*/
|
||
|
|
||
3 years ago
|
package triangle.abstractMachine;
|
||
3 years ago
|
|
||
|
import java.io.DataInputStream;
|
||
|
import java.io.FileInputStream;
|
||
|
import java.io.FileNotFoundException;
|
||
|
import java.io.IOException;
|
||
|
|
||
|
/**
|
||
3 years ago
|
* Disassembles the TAM code in the given file, and displays the instructions on
|
||
|
* standard output.
|
||
3 years ago
|
*
|
||
|
* For example:
|
||
|
*
|
||
|
* <pre>
|
||
|
* java TAM.Disassembler obj.tam
|
||
|
* </pre>
|
||
|
*
|
||
|
* <p>
|
||
|
* Copyright 1991 David A. Watt, University of Glasgow<br>
|
||
|
* Copyright 1998 Deryck F. Brown, The Robert Gordon University<br>
|
||
|
* </p>
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
public class Disassembler {
|
||
|
|
||
3 years ago
|
static String objectName;
|
||
|
|
||
|
static int CT;
|
||
|
|
||
|
/**
|
||
|
* Writes the r-field of an instruction in the form "l<I>reg</I>r", where l and
|
||
|
* r are the bracket characters to use.
|
||
|
*
|
||
|
* @param leftbracket the character to print before the register.
|
||
|
* @param r the number of the register.
|
||
|
* @param rightbracket the character to print after the register.
|
||
|
*/
|
||
3 years ago
|
private static void writeR(char leftbracket, Register r, char rightbracket) {
|
||
3 years ago
|
|
||
|
System.out.print(leftbracket);
|
||
3 years ago
|
System.out.print(r.toString());
|
||
3 years ago
|
System.out.print(rightbracket);
|
||
|
}
|
||
|
|
||
3 years ago
|
private static void writeR(char leftBracket, int r, char rightBracket) {
|
||
|
var register = Register.values()[r];
|
||
|
writeR(leftBracket, register, rightBracket);
|
||
|
}
|
||
|
|
||
3 years ago
|
/**
|
||
|
* Writes a void n-field of an instruction.
|
||
|
*/
|
||
|
private static void blankN() {
|
||
|
System.out.print(" ");
|
||
|
}
|
||
|
|
||
|
// Writes the n-field of an instruction.
|
||
|
/**
|
||
|
* Writes the n-field of an instruction in the form "(n)".
|
||
|
*
|
||
|
* @param n the integer to write.
|
||
|
*/
|
||
|
private static void writeN(int n) {
|
||
|
System.out.print("(" + n + ") ");
|
||
|
if (n < 10)
|
||
|
System.out.print(" ");
|
||
|
else if (n < 100)
|
||
|
System.out.print(" ");
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Writes the d-field of an instruction.
|
||
|
*
|
||
|
* @param d the integer to write.
|
||
|
*/
|
||
|
private static void writeD(int d) {
|
||
|
System.out.print(d);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Writes the name of primitive routine with relative address d.
|
||
|
*
|
||
|
* @param d the displacment of the primitive routine.
|
||
|
*/
|
||
|
private static void writePrimitive(int d) {
|
||
3 years ago
|
var primitive = Primitive.values()[d];
|
||
|
switch (primitive) {
|
||
|
case ID:
|
||
3 years ago
|
System.out.print("id ");
|
||
|
break;
|
||
3 years ago
|
case NOT:
|
||
3 years ago
|
System.out.print("not ");
|
||
|
break;
|
||
3 years ago
|
case AND:
|
||
3 years ago
|
System.out.print("and ");
|
||
|
break;
|
||
3 years ago
|
case OR:
|
||
3 years ago
|
System.out.print("or ");
|
||
|
break;
|
||
3 years ago
|
case SUCC:
|
||
3 years ago
|
System.out.print("succ ");
|
||
|
break;
|
||
3 years ago
|
case PRED:
|
||
3 years ago
|
System.out.print("pred ");
|
||
|
break;
|
||
3 years ago
|
case NEG:
|
||
3 years ago
|
System.out.print("neg ");
|
||
|
break;
|
||
3 years ago
|
case ADD:
|
||
3 years ago
|
System.out.print("add ");
|
||
|
break;
|
||
3 years ago
|
case SUB:
|
||
3 years ago
|
System.out.print("sub ");
|
||
|
break;
|
||
3 years ago
|
case MULT:
|
||
3 years ago
|
System.out.print("mult ");
|
||
|
break;
|
||
3 years ago
|
case DIV:
|
||
3 years ago
|
System.out.print("div ");
|
||
|
break;
|
||
3 years ago
|
case MOD:
|
||
3 years ago
|
System.out.print("mod ");
|
||
|
break;
|
||
3 years ago
|
case LT:
|
||
3 years ago
|
System.out.print("lt ");
|
||
|
break;
|
||
3 years ago
|
case LE:
|
||
3 years ago
|
System.out.print("le ");
|
||
|
break;
|
||
3 years ago
|
case GE:
|
||
3 years ago
|
System.out.print("ge ");
|
||
|
break;
|
||
3 years ago
|
case GT:
|
||
3 years ago
|
System.out.print("gt ");
|
||
|
break;
|
||
3 years ago
|
case EQ:
|
||
3 years ago
|
System.out.print("eq ");
|
||
|
break;
|
||
3 years ago
|
case NE:
|
||
3 years ago
|
System.out.print("ne ");
|
||
|
break;
|
||
3 years ago
|
case EOL:
|
||
3 years ago
|
System.out.print("eol ");
|
||
|
break;
|
||
3 years ago
|
case EOF:
|
||
3 years ago
|
System.out.print("eof ");
|
||
|
break;
|
||
3 years ago
|
case GET:
|
||
3 years ago
|
System.out.print("get ");
|
||
|
break;
|
||
3 years ago
|
case PUT:
|
||
3 years ago
|
System.out.print("put ");
|
||
|
break;
|
||
3 years ago
|
case GETEOL:
|
||
3 years ago
|
System.out.print("geteol ");
|
||
|
break;
|
||
3 years ago
|
case PUTEOL:
|
||
3 years ago
|
System.out.print("puteol ");
|
||
|
break;
|
||
3 years ago
|
case GETINT:
|
||
3 years ago
|
System.out.print("getint ");
|
||
|
break;
|
||
3 years ago
|
case PUTINT:
|
||
3 years ago
|
System.out.print("putint ");
|
||
|
break;
|
||
3 years ago
|
case NEW:
|
||
3 years ago
|
System.out.print("new ");
|
||
|
break;
|
||
3 years ago
|
case DISPOSE:
|
||
3 years ago
|
System.out.print("dispose ");
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Writes the given instruction in assembly-code format.
|
||
|
*
|
||
|
* @param instr the instruction to display.
|
||
|
*/
|
||
|
private static void writeInstruction(Instruction instr) {
|
||
|
|
||
3 years ago
|
switch (instr.opCode) {
|
||
|
case LOAD:
|
||
3 years ago
|
System.out.print("LOAD ");
|
||
3 years ago
|
writeN(instr.length);
|
||
|
writeD(instr.operand);
|
||
|
writeR('[', instr.register, ']');
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case LOADA:
|
||
3 years ago
|
System.out.print("LOADA ");
|
||
|
blankN();
|
||
3 years ago
|
writeD(instr.operand);
|
||
|
writeR('[', instr.register, ']');
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case LOADI:
|
||
3 years ago
|
System.out.print("LOADI ");
|
||
3 years ago
|
writeN(instr.length);
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case LOADL:
|
||
3 years ago
|
System.out.print("LOADL ");
|
||
|
blankN();
|
||
3 years ago
|
writeD(instr.operand);
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case STORE:
|
||
3 years ago
|
System.out.print("STORE ");
|
||
3 years ago
|
writeN(instr.length);
|
||
|
writeD(instr.operand);
|
||
|
writeR('[', instr.register, ']');
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case STOREI:
|
||
3 years ago
|
System.out.print("STOREI");
|
||
3 years ago
|
writeN(instr.length);
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case CALL:
|
||
3 years ago
|
System.out.print("CALL ");
|
||
3 years ago
|
if (instr.register == Register.PB) {
|
||
3 years ago
|
blankN();
|
||
3 years ago
|
writePrimitive(instr.operand);
|
||
3 years ago
|
} else {
|
||
3 years ago
|
writeR('(', instr.length, ')');
|
||
3 years ago
|
System.out.print(" ");
|
||
3 years ago
|
writeD(instr.operand);
|
||
|
writeR('[', instr.register, ']');
|
||
3 years ago
|
}
|
||
|
break;
|
||
|
|
||
3 years ago
|
case CALLI:
|
||
3 years ago
|
System.out.print("CALLI ");
|
||
|
break;
|
||
|
|
||
3 years ago
|
case RETURN:
|
||
3 years ago
|
System.out.print("RETURN");
|
||
3 years ago
|
writeN(instr.length);
|
||
|
writeD(instr.operand);
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case PUSH:
|
||
3 years ago
|
System.out.print("PUSH ");
|
||
|
blankN();
|
||
3 years ago
|
writeD(instr.operand);
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case POP:
|
||
3 years ago
|
System.out.print("POP ");
|
||
3 years ago
|
writeN(instr.length);
|
||
|
writeD(instr.operand);
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case JUMP:
|
||
3 years ago
|
System.out.print("JUMP ");
|
||
|
blankN();
|
||
3 years ago
|
writeD(instr.operand);
|
||
|
writeR('[', instr.register, ']');
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case JUMPI:
|
||
3 years ago
|
System.out.print("JUMPI ");
|
||
|
break;
|
||
|
|
||
3 years ago
|
case JUMPIF:
|
||
3 years ago
|
System.out.print("JUMPIF");
|
||
3 years ago
|
writeN(instr.length);
|
||
|
writeD(instr.operand);
|
||
|
writeR('[', instr.register, ']');
|
||
3 years ago
|
break;
|
||
|
|
||
3 years ago
|
case HALT:
|
||
3 years ago
|
System.out.print("HALT ");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Writes all instructions of the program in code store.
|
||
|
*/
|
||
|
private static void disassembleProgram() {
|
||
|
for (int addr = Machine.CB; addr < CT; addr++) {
|
||
|
System.out.print(addr + ": ");
|
||
|
writeInstruction(Machine.code[addr]);
|
||
|
System.out.println();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// LOADING
|
||
|
|
||
|
/**
|
||
|
* Loads the TAM object program into code store from the named file.
|
||
|
*
|
||
|
* @param objectName the name of the file containing the program.
|
||
|
*/
|
||
|
static void loadObjectProgram(String objectName) {
|
||
|
|
||
|
FileInputStream objectFile = null;
|
||
|
DataInputStream objectStream = null;
|
||
|
|
||
|
int addr;
|
||
|
boolean finished = false;
|
||
|
|
||
|
try {
|
||
|
objectFile = new FileInputStream(objectName);
|
||
|
objectStream = new DataInputStream(objectFile);
|
||
|
|
||
|
addr = Machine.CB;
|
||
|
while (!finished) {
|
||
|
Machine.code[addr] = Instruction.read(objectStream);
|
||
|
if (Machine.code[addr] == null)
|
||
|
finished = true;
|
||
|
else
|
||
|
addr = addr + 1;
|
||
|
}
|
||
|
CT = addr;
|
||
|
objectFile.close();
|
||
|
} catch (FileNotFoundException s) {
|
||
|
CT = Machine.CB;
|
||
|
System.err.println("Error opening object file: " + s);
|
||
|
} catch (IOException s) {
|
||
|
CT = Machine.CB;
|
||
|
System.err.println("Error reading object file: " + s);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// DISASSEMBLE
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
System.out.println("********** TAM Disassembler (Sun Version 2.1) **********");
|
||
|
|
||
|
if (args.length == 1)
|
||
|
objectName = args[0];
|
||
|
else
|
||
|
objectName = "obj.tam";
|
||
|
|
||
|
loadObjectProgram(objectName);
|
||
|
disassembleProgram();
|
||
|
}
|
||
3 years ago
|
}
|