Skip to content

Commit

Permalink
Improved align.(pri|alt) read
Browse files Browse the repository at this point in the history
  • Loading branch information
ShadowsAdi committed Mar 10, 2024
1 parent 2504487 commit 7b31676
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
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
19 changes: 12 additions & 7 deletions src/main/java/lysis/builder/MethodParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -118,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 @@ -228,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.addr_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 Expand Up @@ -685,13 +697,6 @@ private LInstruction readInstruction(SPOpcode op) throws Exception {
pc_ += num - 8; // skip dbgname
return new LDebugBreak();
}

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

default:
throw new Exception("Unrecognized opcode: " + op);
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/lysis/instructions/LAlign.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

import lysis.lstructure.Register;

public class LAlign extends LInstructionReg {
private long offset_;
public class LAlign extends LInstructionRegStack {
private long number_;

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

@Override
Expand All @@ -24,7 +24,6 @@ public Opcode op() {

@Override
public void print(DataOutputStream tw) throws IOException {
tw.writeBytes("align." + RegisterName(reg()) + ", "
+ (reg() == Register.Pri ? RegisterName(Register.Alt) : RegisterName(Register.Pri)) + offset());
tw.writeBytes("align." + (reg() == Register.Pri ? RegisterName(Register.Pri) : RegisterName(Register.Alt)) + number());
}
}

0 comments on commit 7b31676

Please sign in to comment.