Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for align_(pri|alt) OPcodes #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/lysis/BitConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ public static int ToInt32(byte[] bytes, int offset) {
return result;
}

public static int ToInt32LittleEndian(byte[] bytes, int offset) {
int result = (int) bytes[offset + 3] & 0xff;
result |= ((int) bytes[offset + 2] & 0xff) << 8;
result |= ((int) bytes[offset + 1] & 0xff) << 16;
result |= ((int) bytes[offset] & 0xff) << 24;
return result;
}


public static long ToUInt32(byte[] bytes, int offset) {
long result = (int) bytes[offset] & 0xff;
result |= ((int) bytes[offset + 1] & 0xff) << 8;
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/lysis/builder/MethodParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import lysis.instructions.LZeroGlobal;
import lysis.instructions.LZeroLocal;
import lysis.instructions.SwitchCase;
import lysis.instructions.LAlign;
import lysis.lstructure.Function;
import lysis.lstructure.LBlock;
import lysis.lstructure.LGraph;
Expand Down Expand Up @@ -117,6 +118,11 @@ private long readUInt32() {
return BitConverter.ToUInt32(file_.code().bytes(), (int) pc_ - 4);
}

private int readInt32LittleEndian() {
pc_ += 4;
return BitConverter.ToInt32LittleEndian(file_.code().bytes(), (int) pc_ - 4);
}

private SPOpcode readOp() {
return SPOpcode.values()[(int) readUInt32()];
}
Expand Down Expand Up @@ -227,6 +233,13 @@ private LInstruction readInstruction(SPOpcode op) throws Exception {
case idxaddr_b:
return new LIndexAddress(readInt32());

case align_pri:
case align_alt:
{
Register reg = (op == SPOpcode.align_pri) ? Register.Pri : Register.Alt;
return new LAlign(trackGlobal(readInt32LittleEndian()), reg);
}

case move_pri:
case move_alt: {
Register reg = (op == SPOpcode.move_pri) ? Register.Pri : Register.Alt;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/lysis/builder/SourceBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import lysis.nodes.NodeBlock;
import lysis.nodes.NodeList;
import lysis.nodes.NodeType;
import lysis.nodes.types.DAlign;
import lysis.nodes.types.DArrayRef;
import lysis.nodes.types.DBinary;
import lysis.nodes.types.DBoolean;
Expand Down Expand Up @@ -410,6 +411,10 @@ private String buildLoadStoreRef(DNode node) throws Exception {
case Unary: {
return buildUnary((DUnary) node) + "/* ERROR unknown load Unary */";
}

case Align: {
return buildAlign((DAlign) node);
}

default:
throw new Exception("unknown load " + node.type());
Expand All @@ -419,6 +424,10 @@ private String buildLoadStoreRef(DNode node) throws Exception {
private String buildLoad(DLoad load) throws Exception {
return buildLoadStoreRef(load.getOperand(0));
}

private String buildAlign(DAlign alg) throws Exception {
return buildLoadStoreRef(alg.getOperand(0));
}

private String buildSysReq(DSysReq sysreq) throws Exception {
String args = "";
Expand Down Expand Up @@ -813,6 +822,9 @@ private void writeStatement(DNode node) throws Exception {
case Label:
writeLabel((DLabel) node);
break;

case Align:
break;

default:
throw new Exception("unknown op (" + node.type() + ")");
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/lysis/instructions/LAlign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lysis.instructions;

import java.io.DataOutputStream;
import java.io.IOException;

import lysis.lstructure.Register;

public class LAlign extends LInstructionRegStack {

private long number_;

public long number() {
return number_;
}
public LAlign(long number, Register reg) {
super(reg, number);
number_ = number;
}

@Override
public Opcode op() {
return Opcode.Align;
}

@Override
public void print(DataOutputStream tw) throws IOException {
tw.writeBytes("align." + (reg() == Register.Pri ? RegisterName(Register.Pri) : RegisterName(Register.Alt)) + number());
}
}
2 changes: 1 addition & 1 deletion src/main/java/lysis/instructions/Opcode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package lysis.instructions;

public enum Opcode {
LoadLocal, StoreLocal, LoadLocalRef, StoreLocalRef, Load, Constant, StackAddress, Store, IndexAddress, Move, PushReg, PushConstant, Pop, Stack, Return, Jump, JumpCondition, AddConstant, MulConstant, ZeroGlobal, IncGlobal, DecGlobal, IncLocal, DecLocal, IncI, IncReg, DecI, DecReg, Fill, Bounds, SysReq, Swap, PushStackAddress, DebugBreak, Goto, PushLocal, Exchange, Binary, ShiftLeftConstant, PushGlobal, StoreGlobal, LoadGlobal, Call, EqualConstant, LoadIndex, Unary, StoreGlobalConstant, StoreLocalConstant, ZeroLocal, Heap, MemCopy, Switch, GenArray, StackAdjust, LoadCtrl, StoreCtrl, InitArray
LoadLocal, StoreLocal, LoadLocalRef, StoreLocalRef, Load, Constant, StackAddress, Store, IndexAddress, Move, PushReg, PushConstant, Pop, Stack, Return, Jump, JumpCondition, AddConstant, MulConstant, ZeroGlobal, IncGlobal, DecGlobal, IncLocal, DecLocal, IncI, IncReg, DecI, DecReg, Fill, Bounds, SysReq, Swap, PushStackAddress, DebugBreak, Goto, PushLocal, Exchange, Binary, ShiftLeftConstant, PushGlobal, StoreGlobal, LoadGlobal, Call, EqualConstant, LoadIndex, Unary, StoreGlobalConstant, StoreLocalConstant, ZeroLocal, Heap, MemCopy, Switch, GenArray, StackAdjust, LoadCtrl, StoreCtrl, InitArray, Align
}
11 changes: 11 additions & 0 deletions src/main/java/lysis/nodes/NodeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@
import lysis.instructions.LUnary;
import lysis.instructions.LZeroGlobal;
import lysis.instructions.LZeroLocal;
import lysis.instructions.LAlign;
import lysis.instructions.Opcode;
import lysis.lstructure.Function;
import lysis.lstructure.LBlock;
import lysis.lstructure.LGraph;
import lysis.lstructure.Register;
import lysis.lstructure.Scope;
import lysis.lstructure.Variable;
import lysis.nodes.types.DAlign;
import lysis.nodes.types.DArrayRef;
import lysis.nodes.types.DBinary;
import lysis.nodes.types.DBoundsCheck;
Expand Down Expand Up @@ -386,6 +388,15 @@ public void traverse(NodeBlock block) throws Exception {
block.add(node);
break;
}

case Align: {
LAlign ins = (LAlign) uins;
DAlign node = new DAlign((ins.reg() == Register.Pri) ? block.stack().pri() : block.stack().alt(), ins.number());

block.stack().set((ins.reg() == Register.Pri) ? Register.Pri : Register.Alt, node);
block.add(node);
break;
}

case Move: {
LMove ins = (LMove) uins;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/lysis/nodes/NodeRewriter.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lysis.nodes;

import lysis.nodes.types.DAlign;
import lysis.nodes.types.DArrayRef;
import lysis.nodes.types.DBinary;
import lysis.nodes.types.DBoundsCheck;
Expand Down Expand Up @@ -291,6 +292,10 @@ public void visit(DCall call) throws Exception {
throw new Exception("unknown spop");
}
}

@Override
public void visit(DAlign check) {
}

private void rewriteBlock(NodeBlock block) throws Exception {
current_ = block;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/lysis/nodes/NodeType.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package lysis.nodes;

public enum NodeType {
Sentinel, Constant, DeclareLocal, DeclareStatic, LocalRef, Jump, JumpCondition, SysReq, Binary, BoundsCheck, ArrayRef, Store, Load, Return, Global, String, Boolean, Float, Function, Character, Call, TempName, Phi, Unary, IncDec, Heap, MemCopy, InlineArray, Switch, GenArray, Label
Sentinel, Constant, DeclareLocal, DeclareStatic, LocalRef, Jump, JumpCondition, SysReq, Binary, BoundsCheck, ArrayRef, Store, Load, Return, Global, String, Boolean, Float, Function, Character, Call, TempName, Phi, Unary, IncDec, Heap, MemCopy, InlineArray, Switch, GenArray, Label, Align
}
4 changes: 4 additions & 0 deletions src/main/java/lysis/nodes/NodeVisitor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package lysis.nodes;

import lysis.nodes.types.DAlign;
import lysis.nodes.types.DArrayRef;
import lysis.nodes.types.DBinary;
import lysis.nodes.types.DBoolean;
Expand Down Expand Up @@ -117,4 +118,7 @@ public void visit(DGenArray genarray) throws Exception {

public void visit(DLabel gototarget) {
}

public void visit(DAlign number) {
}
}
33 changes: 33 additions & 0 deletions src/main/java/lysis/nodes/types/DAlign.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package lysis.nodes.types;

import lysis.nodes.NodeType;
import lysis.nodes.NodeVisitor;

public class DAlign extends DUnaryNode {

private long amount_;

public DAlign(DNode index, long amount) throws Exception {
super(index);
amount_ = amount;
}

public long amount() {
return amount_;
}

@Override
public NodeType type() {
return NodeType.Align;
}

@Override
public void accept(NodeVisitor visitor) {
visitor.visit(this);
}

@Override
public boolean guard() {
return true;
}
}
4 changes: 4 additions & 0 deletions src/main/java/lysis/types/BackwardTypePropagation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import lysis.nodes.NodeList;
import lysis.nodes.NodeType;
import lysis.nodes.NodeVisitor;
import lysis.nodes.types.DAlign;
import lysis.nodes.types.DArrayRef;
import lysis.nodes.types.DBinary;
import lysis.nodes.types.DBoolean;
Expand Down Expand Up @@ -401,4 +402,7 @@ public void visit(DGenArray genarray) throws Exception {
genarray.getOperand(i).accept(this);
}
}

public void visit(DAlign check) {
}
}
5 changes: 5 additions & 0 deletions src/main/java/lysis/types/ForwardTypePropagation.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lysis.nodes.NodeList;
import lysis.nodes.NodeType;
import lysis.nodes.NodeVisitor;
import lysis.nodes.types.DAlign;
import lysis.nodes.types.DArrayRef;
import lysis.nodes.types.DBinary;
import lysis.nodes.types.DBoundsCheck;
Expand Down Expand Up @@ -293,4 +294,8 @@ public void visit(DGenArray genarray) throws Exception {
genarray.addType(new TypeUnit(tu));
}
}

@Override
public void visit(DAlign node) {
}
}
Loading