Archived
Switch to generic visitor pattern.
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import Triangle.ErrorReporter;
|
||||
import Triangle.AbstractMachine.Instruction;
|
||||
import Triangle.AbstractMachine.Machine;
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractMachine.Primitive;
|
||||
import Triangle.AbstractMachine.Register;
|
||||
|
||||
public class Emitter {
|
||||
|
||||
// OBJECT CODE
|
||||
|
||||
// Implementation notes:
|
||||
// Object code is generated directly into the TAM Code Store, starting at
|
||||
// CB.
|
||||
// The address of the next instruction is held in nextInstrAddr.
|
||||
|
||||
ErrorReporter errorReporter;
|
||||
|
||||
int nextInstrAddr;
|
||||
|
||||
public Emitter(ErrorReporter errorReporter) {
|
||||
this.errorReporter = errorReporter;
|
||||
nextInstrAddr = Machine.CB;
|
||||
}
|
||||
|
||||
public int getNextInstrAddr() {
|
||||
return nextInstrAddr;
|
||||
}
|
||||
|
||||
public int emit(OpCode op) {
|
||||
return emit(op, 0, Register.CB, 0);
|
||||
}
|
||||
|
||||
public int emit(OpCode op, int operand) {
|
||||
return emit(op, 0, Register.CB, operand);
|
||||
}
|
||||
|
||||
public int emit(OpCode op, int length, int operand) {
|
||||
return emit(op, length, Register.CB, operand);
|
||||
}
|
||||
|
||||
public int emit(OpCode op, Register staticRegister, Register register, int operand) {
|
||||
return emit(op, staticRegister.ordinal(), register, operand);
|
||||
}
|
||||
|
||||
public int emit(OpCode op, Register register, int operand) {
|
||||
return emit(op, 0, register, operand);
|
||||
}
|
||||
|
||||
public int emit(OpCode op, Register register) {
|
||||
return emit(op, 0, register, 0);
|
||||
}
|
||||
|
||||
public int emit(OpCode op, int length, Register register) {
|
||||
return emit(op, length, register, 0);
|
||||
}
|
||||
|
||||
public int emit(OpCode op, Register register, Primitive primitive) {
|
||||
return emit(op, 0, register, primitive.ordinal());
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends an instruction, with the given fields, to the object code.
|
||||
*
|
||||
* @param op the opcode
|
||||
* @param length the length field
|
||||
* @param register the register field
|
||||
* @param operand the operand field
|
||||
* @return the code address of the new instruction
|
||||
**/
|
||||
public int emit(OpCode op, int length, Register register, int operand) {
|
||||
|
||||
if (length > 255) {
|
||||
errorReporter.reportRestriction("length of operand can't exceed 255 words");
|
||||
length = 255; // to allow code generation to continue
|
||||
}
|
||||
|
||||
var nextInstr = new Instruction(op, register, length, operand);
|
||||
|
||||
var currentInstrAddr = nextInstrAddr;
|
||||
if (nextInstrAddr == Machine.PB) {
|
||||
errorReporter.reportRestriction("too many instructions for code segment");
|
||||
} else {
|
||||
Machine.code[nextInstrAddr++] = nextInstr;
|
||||
}
|
||||
return currentInstrAddr;
|
||||
|
||||
}
|
||||
|
||||
// Patches the d-field of the instruction at address addr with the next
|
||||
// instruction address.
|
||||
public void patch(int addr) {
|
||||
Machine.code[addr].setOperand(nextInstrAddr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the object program in the given object file.
|
||||
*
|
||||
* @param objectFile the object file
|
||||
*/
|
||||
public void saveObjectProgram(String objectFileName) {
|
||||
try (var objectFile = new FileOutputStream(objectFileName)) {
|
||||
var objectStream = new DataOutputStream(objectFile);
|
||||
for (var addr = Machine.CB; addr < nextInstrAddr; addr++) {
|
||||
Machine.code[addr].write(objectStream);
|
||||
}
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
System.err.println("Error opening object file: " + fnfe);
|
||||
} catch (IOException ioe) {
|
||||
System.err.println("Error writing object file: " + ioe);
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
+29
@@ -0,0 +1,29 @@
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractSyntaxTrees.Vnames.Vname;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public abstract class AddressableEntity extends RuntimeEntity implements FetchableEntity {
|
||||
|
||||
protected final ObjectAddress address;
|
||||
|
||||
protected AddressableEntity(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
protected AddressableEntity(int size, Frame frame) {
|
||||
this(size, frame.getLevel(), frame.getSize());
|
||||
}
|
||||
|
||||
public ObjectAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public abstract void encodeStore(Emitter emitter, Frame frame, int size, Vname vname);
|
||||
|
||||
public abstract void encodeFetchAddress(Emitter emitter, Frame frame, Vname vname);
|
||||
|
||||
public abstract void encodeFetch(Emitter emitter, Frame frame, int size, Vname vname);
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* @(#)EqualityRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractMachine.Primitive;
|
||||
import Triangle.AbstractMachine.Register;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public class EqualityRoutine extends RuntimeEntity implements RoutineEntity {
|
||||
|
||||
private final Primitive primitive;
|
||||
|
||||
public EqualityRoutine(int size, Primitive primitive) {
|
||||
super(size);
|
||||
this.primitive = primitive;
|
||||
}
|
||||
|
||||
public final Primitive getPrimitive() {
|
||||
return primitive;
|
||||
}
|
||||
|
||||
public void encodeCall(Emitter emitter, Frame frame) {
|
||||
emitter.emit(OpCode.LOADL, frame.getSize() / 2);
|
||||
emitter.emit(OpCode.CALL, Register.PB, primitive);
|
||||
}
|
||||
|
||||
public void encodeFetch(Emitter emitter, Frame frame) {
|
||||
emitter.emit(OpCode.LOADA, 0, Register.SB, 0);
|
||||
emitter.emit(OpCode.LOADA, Register.PB, primitive);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractSyntaxTrees.Vnames.Vname;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public interface FetchableEntity {
|
||||
|
||||
void encodeFetch(Emitter emitter, Frame frame, int size, Vname vname);
|
||||
|
||||
}
|
||||
+5
-7
@@ -12,20 +12,18 @@
|
||||
* of the authors.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
public class Field extends RuntimeEntity {
|
||||
|
||||
public Field() {
|
||||
super();
|
||||
fieldOffset = 0;
|
||||
}
|
||||
private final int fieldOffset;
|
||||
|
||||
public Field(int size, int fieldOffset) {
|
||||
super(size);
|
||||
this.fieldOffset = fieldOffset;
|
||||
}
|
||||
|
||||
public int fieldOffset;
|
||||
|
||||
public final int getFieldOffset() {
|
||||
return fieldOffset;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* @(#)KnownAddress.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractMachine.Primitive;
|
||||
import Triangle.AbstractMachine.Register;
|
||||
import Triangle.AbstractSyntaxTrees.Vnames.Vname;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public class KnownAddress extends AddressableEntity {
|
||||
|
||||
public KnownAddress(int size, int level, int displacement) {
|
||||
super(size, level, displacement);
|
||||
}
|
||||
|
||||
public KnownAddress(int size, Frame frame) {
|
||||
super(size, frame);
|
||||
}
|
||||
|
||||
public void encodeStore(Emitter emitter, Frame frame, int size, Vname vname) {
|
||||
if (vname.indexed) {
|
||||
emitter.emit(OpCode.LOADA, 0, frame.getDisplayRegister(address), address.getDisplacement() + vname.offset);
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
emitter.emit(OpCode.STOREI, size, 0);
|
||||
} else {
|
||||
emitter.emit(OpCode.STORE, size, frame.getDisplayRegister(address),
|
||||
address.getDisplacement() + vname.offset);
|
||||
}
|
||||
}
|
||||
|
||||
public void encodeFetch(Emitter emitter, Frame frame, int size, Vname vname) {
|
||||
if (vname.indexed) {
|
||||
emitter.emit(OpCode.LOADA, 0, frame.getDisplayRegister(address), address.getDisplacement() + vname.offset);
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
emitter.emit(OpCode.LOADI, size, 0);
|
||||
} else {
|
||||
emitter.emit(OpCode.LOAD, size, frame.getDisplayRegister(address),
|
||||
address.getDisplacement() + vname.offset);
|
||||
}
|
||||
}
|
||||
|
||||
public void encodeFetchAddress(Emitter emitter, Frame frame, Vname vname) {
|
||||
emitter.emit(OpCode.LOADA, 0, frame.getDisplayRegister(address), address.getDisplacement() + vname.offset);
|
||||
if (vname.indexed) {
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* @(#)KnownRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractMachine.Register;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public class KnownRoutine extends RuntimeEntity implements RoutineEntity {
|
||||
|
||||
private final ObjectAddress address;
|
||||
|
||||
public KnownRoutine(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public final ObjectAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void encodeCall(Emitter emitter, Frame frame) {
|
||||
emitter.emit(OpCode.CALL, frame.getDisplayRegister(address), Register.CB, address.getDisplacement());
|
||||
}
|
||||
|
||||
public void encodeFetch(Emitter emitter, Frame frame) {
|
||||
emitter.emit(OpCode.LOADA, frame.getDisplayRegister(address), 0);
|
||||
emitter.emit(OpCode.LOADA, Register.CB, address.getDisplacement());
|
||||
}
|
||||
|
||||
}
|
||||
+16
-8
@@ -12,20 +12,28 @@
|
||||
* of the authors.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
public class KnownValue extends RuntimeEntity {
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractSyntaxTrees.Vnames.Vname;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public KnownValue() {
|
||||
super();
|
||||
value = 0;
|
||||
}
|
||||
public class KnownValue extends RuntimeEntity implements FetchableEntity {
|
||||
|
||||
private final int value;
|
||||
|
||||
public KnownValue(int size, int value) {
|
||||
super(size);
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int value;
|
||||
public final int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
public void encodeFetch(Emitter emitter, Frame frame, int size, Vname vname) {
|
||||
// presumably offset = 0 and indexed = false
|
||||
emitter.emit(OpCode.LOADL, 0, value);
|
||||
}
|
||||
}
|
||||
+13
-4
@@ -12,15 +12,24 @@
|
||||
* of the authors.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
public final class ObjectAddress {
|
||||
public class ObjectAddress {
|
||||
|
||||
private final int level;
|
||||
|
||||
private final int displacement;
|
||||
|
||||
public ObjectAddress(int level, int displacement) {
|
||||
this.level = level;
|
||||
this.displacement = displacement;
|
||||
}
|
||||
|
||||
public int level, displacement;
|
||||
public final int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
}
|
||||
public int getDisplacement() {
|
||||
return displacement;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* @(#)PrimitiveRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractMachine.Primitive;
|
||||
import Triangle.AbstractMachine.Register;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public class PrimitiveRoutine extends RuntimeEntity implements RoutineEntity {
|
||||
|
||||
private final Primitive primitive;
|
||||
|
||||
public PrimitiveRoutine(int size, Primitive primitive) {
|
||||
super(size);
|
||||
this.primitive = primitive;
|
||||
}
|
||||
|
||||
public final Primitive getPrimitive() {
|
||||
return primitive;
|
||||
}
|
||||
|
||||
public void encodeCall(Emitter emitter, Frame frame) {
|
||||
if (primitive != Primitive.ID) {
|
||||
emitter.emit(OpCode.CALL, Register.PB, primitive);
|
||||
}
|
||||
}
|
||||
|
||||
public void encodeFetch(Emitter emitter, Frame frame) {
|
||||
emitter.emit(OpCode.LOADA, 0, Register.SB, 0);
|
||||
emitter.emit(OpCode.LOADA, Register.PB, primitive);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public interface RoutineEntity {
|
||||
|
||||
void encodeCall(Emitter emitter, Frame frame);
|
||||
|
||||
void encodeFetch(Emitter emitter, Frame frame);
|
||||
}
|
||||
+6
-9
@@ -12,22 +12,19 @@
|
||||
* of the authors.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
// Run-time object
|
||||
|
||||
public abstract class RuntimeEntity {
|
||||
|
||||
public final static int maxRoutineLevel = 7;
|
||||
private final int size;
|
||||
|
||||
public RuntimeEntity() {
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public RuntimeEntity(int size) {
|
||||
protected RuntimeEntity(int size) {
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public int size;
|
||||
|
||||
public final int getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -12,7 +12,7 @@
|
||||
* of the authors.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
public class TypeRepresentation extends RuntimeEntity {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* @(#)UnknownAddress.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractMachine.Machine;
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractMachine.Primitive;
|
||||
import Triangle.AbstractMachine.Register;
|
||||
import Triangle.AbstractSyntaxTrees.Vnames.Vname;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public class UnknownAddress extends AddressableEntity {
|
||||
|
||||
public UnknownAddress(int size, int level, int displacement) {
|
||||
super(size, level, displacement);
|
||||
}
|
||||
|
||||
public void encodeStore(Emitter emitter, Frame frame, int size, Vname vname) {
|
||||
|
||||
emitter.emit(OpCode.LOAD, Machine.addressSize, frame.getDisplayRegister(address), address.getDisplacement());
|
||||
if (vname.indexed) {
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
}
|
||||
|
||||
int offset = vname.offset;
|
||||
if (offset != 0) {
|
||||
emitter.emit(OpCode.LOADL, 0, offset);
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
}
|
||||
emitter.emit(OpCode.STOREI, size, 0);
|
||||
}
|
||||
|
||||
public void encodeFetch(Emitter emitter, Frame frame, int size, Vname vname) {
|
||||
emitter.emit(OpCode.LOAD, Machine.addressSize, frame.getDisplayRegister(address), address.getDisplacement());
|
||||
|
||||
if (vname.indexed) {
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
}
|
||||
|
||||
int offset = vname.offset;
|
||||
if (offset != 0) {
|
||||
emitter.emit(OpCode.LOADL, offset);
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
}
|
||||
emitter.emit(OpCode.LOADI, size);
|
||||
}
|
||||
|
||||
public void encodeFetchAddress(Emitter emitter, Frame frame, Vname vname) {
|
||||
|
||||
emitter.emit(OpCode.LOAD, Machine.addressSize, frame.getDisplayRegister(address), address.getDisplacement());
|
||||
if (vname.indexed) {
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
}
|
||||
|
||||
int offset = vname.offset;
|
||||
if (offset != 0) {
|
||||
emitter.emit(OpCode.LOADL, offset);
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* @(#)UnknownRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractMachine.Machine;
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public class UnknownRoutine extends RuntimeEntity implements RoutineEntity {
|
||||
|
||||
private final ObjectAddress address;
|
||||
|
||||
public UnknownRoutine(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public final ObjectAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void encodeCall(Emitter emitter, Frame frame) {
|
||||
emitter.emit(OpCode.LOAD, Machine.closureSize, frame.getDisplayRegister(address), address.getDisplacement());
|
||||
emitter.emit(OpCode.CALLI, 0);
|
||||
}
|
||||
|
||||
public void encodeFetch(Emitter emitter, Frame frame) {
|
||||
emitter.emit(OpCode.LOAD, Machine.closureSize, frame.getDisplayRegister(address), address.getDisplacement());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* @(#)UnknownValue.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator.Entities;
|
||||
|
||||
import Triangle.AbstractMachine.OpCode;
|
||||
import Triangle.AbstractMachine.Primitive;
|
||||
import Triangle.AbstractMachine.Register;
|
||||
import Triangle.AbstractSyntaxTrees.Vnames.Vname;
|
||||
import Triangle.CodeGenerator.Emitter;
|
||||
import Triangle.CodeGenerator.Frame;
|
||||
|
||||
public class UnknownValue extends RuntimeEntity implements FetchableEntity {
|
||||
|
||||
private final ObjectAddress address;
|
||||
|
||||
public UnknownValue(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public UnknownValue(int size, Frame frame) {
|
||||
this(size, frame.getLevel(), frame.getSize());
|
||||
}
|
||||
|
||||
public final ObjectAddress getAddress() {
|
||||
return address;
|
||||
}
|
||||
|
||||
public void encodeFetch(Emitter emitter, Frame frame, int size, Vname vname) {
|
||||
if (vname.indexed) {
|
||||
emitter.emit(OpCode.LOADA, 0, frame.getDisplayRegister(address), address.getDisplacement() + vname.offset);
|
||||
emitter.emit(OpCode.CALL, Register.PB, Primitive.ADD);
|
||||
emitter.emit(OpCode.LOADI, size, 0);
|
||||
} else {
|
||||
emitter.emit(OpCode.LOAD, size, frame.getDisplayRegister(address),
|
||||
address.getDisplacement() + vname.offset);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
/*
|
||||
* @(#)EqualityRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
public class EqualityRoutine extends RuntimeEntity {
|
||||
|
||||
public EqualityRoutine() {
|
||||
super();
|
||||
}
|
||||
|
||||
public EqualityRoutine(int size, int displacement) {
|
||||
super(size);
|
||||
this.displacement = displacement;
|
||||
}
|
||||
|
||||
public int displacement;
|
||||
|
||||
}
|
||||
@@ -14,33 +14,60 @@
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
import Triangle.AbstractMachine.Register;
|
||||
import Triangle.CodeGenerator.Entities.ObjectAddress;
|
||||
|
||||
public class Frame {
|
||||
|
||||
public Frame() {
|
||||
this.level = 0;
|
||||
this.size = 0;
|
||||
}
|
||||
public static final Frame Initial = new Frame(0, 0);
|
||||
|
||||
public Frame(int level, Integer size) {
|
||||
this.level = level;
|
||||
this.size = size.intValue();
|
||||
}
|
||||
private final int level;
|
||||
|
||||
public Frame(int level, int size) {
|
||||
private final int size;
|
||||
|
||||
private Frame(int level, int size) {
|
||||
this.level = level;
|
||||
this.size = size;
|
||||
}
|
||||
|
||||
public Frame(Frame frame, int sizeIncrement) {
|
||||
this.level = frame.level;
|
||||
this.size = frame.size + sizeIncrement;
|
||||
public final int getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public Frame(Frame frame, Integer sizeIncrement) {
|
||||
this.level = frame.level;
|
||||
this.size = frame.size + sizeIncrement.intValue();
|
||||
public final int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
protected int level;
|
||||
protected int size;
|
||||
public Frame expand(int increment) {
|
||||
return new Frame(level, size + increment);
|
||||
}
|
||||
|
||||
public Frame replace(int size) {
|
||||
return new Frame(level, size);
|
||||
}
|
||||
|
||||
public Frame push(int size) {
|
||||
return new Frame(level + 1, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the display register appropriate for object code at the current
|
||||
* static level to access a data object at the static level of the given
|
||||
* address.
|
||||
*
|
||||
* @param address the address of the data object
|
||||
* @return the display register required for static addressing
|
||||
*/
|
||||
public Register getDisplayRegister(ObjectAddress address) {
|
||||
if (address.getLevel() == 0) {
|
||||
return Register.SB;
|
||||
}
|
||||
|
||||
if (level - address.getLevel() <= 6) {
|
||||
return Register.values()[Register.LB.ordinal() + level - address.getLevel()]; // LB|L1|...|L6
|
||||
}
|
||||
|
||||
// _errorReporter.ReportRestriction("can't access data more than 6 levels out");
|
||||
return Register.L6; // to allow code generation to continue
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* @(#)KnownAddress.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
public class KnownAddress extends RuntimeEntity {
|
||||
|
||||
public KnownAddress() {
|
||||
super();
|
||||
address = null;
|
||||
}
|
||||
|
||||
public KnownAddress(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public ObjectAddress address;
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* @(#)KnownRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
public class KnownRoutine extends RuntimeEntity {
|
||||
|
||||
public KnownRoutine() {
|
||||
super();
|
||||
address = null;
|
||||
}
|
||||
|
||||
public KnownRoutine(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public ObjectAddress address;
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* @(#)PrimitiveRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
public class PrimitiveRoutine extends RuntimeEntity {
|
||||
|
||||
public PrimitiveRoutine() {
|
||||
super();
|
||||
displacement = 0;
|
||||
}
|
||||
|
||||
public PrimitiveRoutine(int size, int displacement) {
|
||||
super(size);
|
||||
this.displacement = displacement;
|
||||
}
|
||||
|
||||
public int displacement;
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* @(#)UnknownAddress.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
public class UnknownAddress extends RuntimeEntity {
|
||||
|
||||
public UnknownAddress() {
|
||||
super();
|
||||
address = null;
|
||||
}
|
||||
|
||||
public UnknownAddress(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public ObjectAddress address;
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* @(#)UnknownRoutine.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
public class UnknownRoutine extends RuntimeEntity {
|
||||
|
||||
public UnknownRoutine() {
|
||||
super();
|
||||
address = null;
|
||||
}
|
||||
|
||||
public UnknownRoutine(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public ObjectAddress address;
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* @(#)UnknownValue.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.
|
||||
*/
|
||||
|
||||
package Triangle.CodeGenerator;
|
||||
|
||||
public class UnknownValue extends RuntimeEntity {
|
||||
|
||||
public UnknownValue() {
|
||||
super();
|
||||
address = null;
|
||||
}
|
||||
|
||||
public UnknownValue(int size, int level, int displacement) {
|
||||
super(size);
|
||||
address = new ObjectAddress(level, displacement);
|
||||
}
|
||||
|
||||
public ObjectAddress address;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user