Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representação do código objeto public class Instruction { public byte op; // op-code (0..15) public byte r; // register field (0..15) public byte n; //

Similar presentations


Presentation on theme: "Representação do código objeto public class Instruction { public byte op; // op-code (0..15) public byte r; // register field (0..15) public byte n; //"— Presentation transcript:

1 Representação do código objeto public class Instruction { public byte op; // op-code (0..15) public byte r; // register field (0..15) public byte n; // length field (..255) public short d; // operand field ( …

2 Representação do código objeto (2) public class Instruction { … public static final byte LOADop = 0, LOADAop = 1 LOADIop = 2, LOADLop = 3, STOREop = 4, STOREIop = 5, CALLop =6, CALLIop = 7, RETURNop = 8, PUSHop = 10, POPop = 11, JUMPop = 12, JUMPIop = 13, JUMPIFop = 14, HALTop = 15; …

3 Representação do código objeto (3) public class Instruction { … public static final byte CBr = 0, CTr = 1, PBr = 2, PTr = 3, SBr = 4, STr = 5, HBr = 6, HTr = 7, LBr = 8, L1r = 9, L2r = 10, L3r = 11, L4r = 12, L5r = 13, L6r = 14, CPr = 15; public Instruction (byte op, byte r, byte n, short d) {…}

4 Representação do programa objeto private Instruction[] code = new Instruction[1024]; private short nextInstrAddr = 0; private void emit (byte op, byte n, byte r, short d) { code[nextInstrAddr++] = new Instruction(op, n, r, d);

5 Geração de código – uso do padrão visitor public final class Encoder implements Visitor { … … public void encode (Program prog) { prog.visit(this, null); } }

6 Geração de código public Object visitProgram (Program prog, Object arg) { prog.C.visit(this, arg); emit(Instruction.HALTop,0,0,0); }

7 Geração de código (atribuição) public Object visitAssignCommand (AssignCommand com, Object arg) { com.E.visit(this, arg); encodeAssign(com.V); return null; }

8 Geração de código (call) public Object visitCallCommand (CallCommand com, Object arg) { com.E.visit(this, arg); short p = address of routine com.I; emit(Instruction.CALLop, Instruction.SBr, Instruction PBr, p); return null; }

9 Geração de código (sequencial) public Object visitSequentialCommand (SequentialCommand com, Object arg) { com.C1.visit(this, arg); com.C2.visit(this, arg); return null; }

10 Geração de código (Let) public Object visitLetCommand (LetCommand com, Object arg) { com.D.visit(this, arg); com.C.visit(this, arg); short s = espaço alocado por D; if (s > 0) emit (Instruction.POPop,0,0,s); return null; }

11 Geração de código (Inteiro) public Object visitIntegerExpression (IntegerExpression expr, Object arg) { short v = valuation(expr.IL.spelling); emit (Instruction.LOADLop,0,0,v); return null; }

12 Geração de código (Variável) public Object visitVNameExpression (VNameExpression expr, Object arg) { encodeFetch(expr.V); return null; }

13 Geração de código (Operação) public Object visitUnaryExpression (UnaryExpression expr, Object arg) { expr.E.visit(this, arg); short p = endereço da rotina expr.O; emit (Instruction.CALLop, Instruction.SBr, Instruction.PBr, p); return null; }

14 Backpatching public Object visitWhileCommand (whileCommand com, Object arg) { short j = nextInstrAddr; emit(Instruction.JUMPop,0, Instruction.CBr,0); short g = nextInstrAddr; com.C.visit(this,arg); short h = nextInstrAddr; patch(j,h); com.E.visit(this, arg); emit(Instruction.JUMPIFop,1,Instruction.CBr,g); return null; }

15 Declaração de Constantes e Variáveis Known Value x Unknown Value Known Address x Unknown Address Lifetime

16 Alocação estática public abstract class RuntimeEntity { public short size; …} public class KnownValue extends RuntimeEntity { public short value; …} public class UnknownValue extends RuntimeEntity { public short address; …}

17 Alocação estática (2) public class KnownAddress extends RuntimeEntity { public short address; …}


Download ppt "Representação do código objeto public class Instruction { public byte op; // op-code (0..15) public byte r; // register field (0..15) public byte n; //"

Similar presentations


Ads by Google