From f1e8cc4ef775b06e44905cc5d15c00efbac151a3 Mon Sep 17 00:00:00 2001 From: Peace-Maker Date: Thu, 9 Apr 2020 18:28:26 +0200 Subject: [PATCH] Add shl.c.reg opcode handling #19 That opcode can be generated when compiling plugins without the peephole optimizer `--opt-level=0`. --- src/main/java/lysis/builder/MethodParser.java | 8 +++++ .../instructions/LShiftLeftConstant.java | 31 +++++++++++++++++++ src/main/java/lysis/instructions/Opcode.java | 2 +- src/main/java/lysis/nodes/NodeBuilder.java | 11 +++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 src/main/java/lysis/instructions/LShiftLeftConstant.java diff --git a/src/main/java/lysis/builder/MethodParser.java b/src/main/java/lysis/builder/MethodParser.java index af072e1..480d238 100644 --- a/src/main/java/lysis/builder/MethodParser.java +++ b/src/main/java/lysis/builder/MethodParser.java @@ -48,6 +48,7 @@ import lysis.instructions.LPushReg; import lysis.instructions.LPushStackAddress; import lysis.instructions.LReturn; +import lysis.instructions.LShiftLeftConstant; import lysis.instructions.LStack; import lysis.instructions.LStackAddress; import lysis.instructions.LStackAdjust; @@ -298,6 +299,13 @@ private LInstruction readInstruction(SPOpcode op) throws Exception { case sshr: case xor: return new LBinary(op, Register.Pri, Register.Alt); + + case shl_c_pri: + case shl_c_alt: { + // Only generated without the peephole optimizer. + Register reg = (op == SPOpcode.shl_c_pri) ? Register.Pri : Register.Alt; + return new LShiftLeftConstant(readInt32(), reg); + } case not: case neg: diff --git a/src/main/java/lysis/instructions/LShiftLeftConstant.java b/src/main/java/lysis/instructions/LShiftLeftConstant.java new file mode 100644 index 0000000..5f885a7 --- /dev/null +++ b/src/main/java/lysis/instructions/LShiftLeftConstant.java @@ -0,0 +1,31 @@ +package lysis.instructions; + +import java.io.DataOutputStream; +import java.io.IOException; + +import lysis.lstructure.Register; + +public class LShiftLeftConstant extends LInstructionReg { + + private long val_; + + public LShiftLeftConstant(long val, Register reg) { + super(reg); + val_ = val; + } + + public long val() { + return val_; + } + + @Override + public Opcode op() { + return Opcode.ShiftLeftConstant; + } + + @Override + public void print(DataOutputStream tw) throws IOException { + tw.writeBytes("shl.c." + RegisterName(reg()) + " " + val()); + } + +} diff --git a/src/main/java/lysis/instructions/Opcode.java b/src/main/java/lysis/instructions/Opcode.java index 92e9ecd..6c9f028 100644 --- a/src/main/java/lysis/instructions/Opcode.java +++ b/src/main/java/lysis/instructions/Opcode.java @@ -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, PushGlobal, StoreGlobal, LoadGlobal, Call, EqualConstant, LoadIndex, Unary, StoreGlobalConstant, StoreLocalConstant, ZeroLocal, Heap, MemCopy, Switch, GenArray, StackAdjust, LoadCtrl, StoreCtrl + 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 } diff --git a/src/main/java/lysis/nodes/NodeBuilder.java b/src/main/java/lysis/nodes/NodeBuilder.java index 392926b..e66754d 100644 --- a/src/main/java/lysis/nodes/NodeBuilder.java +++ b/src/main/java/lysis/nodes/NodeBuilder.java @@ -41,6 +41,7 @@ import lysis.instructions.LPushLocal; import lysis.instructions.LPushReg; import lysis.instructions.LPushStackAddress; +import lysis.instructions.LShiftLeftConstant; import lysis.instructions.LStack; import lysis.instructions.LStackAddress; import lysis.instructions.LStackAdjust; @@ -506,6 +507,16 @@ public void traverse(NodeBlock block) throws Exception { } break; } + + case ShiftLeftConstant: { + LShiftLeftConstant ins = (LShiftLeftConstant) uins; + DConstant val = new DConstant(ins.val()); + DBinary node = new DBinary(SPOpcode.shl, block.stack().reg(ins.reg()), val); + block.stack().set(ins.reg(), node); + block.add(val); + block.add(node); + break; + } case PushGlobal: { LPushGlobal ins = (LPushGlobal) uins;