Commit a5e21fa3 authored by Tom Zhao's avatar Tom Zhao
Browse files

xz1919: added more bitwise operator support for armv6

parent a69cb0be
......@@ -268,7 +268,7 @@ public class ARMInstructionGenerator extends InstructionGenerator<ARMInstruction
List<Instruction> insList = ARMArithmeticLogic.binopInstruction
.get(operator)
.binopAssemble(e1reg, e1reg, op2, operator);
.binopAssemble(e1reg, e2reg, op2, operator);
instructions.addAll(insList.stream().map(i -> (ARMInstruction) i).collect(Collectors.toList()));
if (operator == Binop.DIV || operator == Binop.MOD) {
checkAndAddRoutine(CHECK_DIVIDE_BY_ZERO, msgLabelGenerator, dataSegmentMessages);
......
......@@ -11,47 +11,60 @@ public class Operand2 extends Address {
private final ARMImmediate immed;
private final Operand2Operator operator;
private final Register Rm;
private Operand2(Register Rm, Operand2Operator operator, ARMImmediate immed) {
private final Register Rs;
private Operand2(Register Rm, Register Rs, Operand2Operator operator, ARMImmediate immed) {
this.immed = immed;
this.operator = operator;
this.Rm = Rm;
this.Rs = Rs;
}
public Operand2(Register Rm, Operand2Operator operator, int intVal) {
this.immed = new ARMImmediate(intVal, BitNum.CONST5);
this.operator = operator;
this.Rm = Rm;
this.Rs = null;
}
public Operand2(ARMImmediate immed) {
this(null, Operand2Operator.NONE, immed);
this(null, null, Operand2Operator.NONE, immed);
}
public Operand2(int intVal) {
this(null, Operand2Operator.NONE, new ARMImmediate(intVal, BitNum.CONST8));
this(null, null, Operand2Operator.NONE, new ARMImmediate(intVal, BitNum.CONST8));
}
public Operand2(Register Rm) {
this(Rm, Operand2Operator.NONE, null);
this(Rm, null, Operand2Operator.NONE, null);
}
public Operand2(Register Rm, Operand2Operator operator) {
this(Rm, operator, null);
this(Rm, null, operator, null);
}
public Operand2(Register Rm, Register Rs, Operand2Operator operator) {
this(Rm, Rs, operator, null);
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
if (Rm != null) {
res.append(Rm.toString());
}
if (Rm != null) {
res.append(Rm.toString());
}
if (Rs != null) {
res.append(", ").append(operator).append(" ").append(Rs);
} else {
if (operator != Operand2Operator.NONE) {
res.append(", " + operator.toString() + " ");
res.append(", " + operator + " ");
}
if (immed != null) {
res.append(immed.toString());
res.append(immed.toString());
}
}
return res.toString();
}
......
......@@ -12,6 +12,7 @@ import backend.arm.instructions.addressing.AddressingMode2.AddrMode2;
import backend.arm.instructions.addressing.ARMImmediate;
import backend.arm.instructions.addressing.ARMImmediate.BitNum;
import backend.arm.instructions.addressing.Operand2;
import backend.arm.instructions.addressing.Operand2.Operand2Operator;
import backend.common.address.Address;
import backend.common.arithmeticLogic.ArithmeticLogic;
import backend.common.arithmeticLogic.BinopAssemble;
......@@ -33,13 +34,16 @@ public abstract class ARMArithmeticLogic extends ArithmeticLogic implements ARMI
public static final BinopAssemble BasicBinopAsm = (rd, rn, op2, b) -> {
Map<Binop, ARMInstruction> m = Map.of(
Binop.PLUS, new Add(rd, rn, op2, Cond.S),
Binop.MINUS, new Sub(rd, rn, op2, Cond.S),
Binop.MUL, new SMull(rd, rn, op2),
Binop.AND, new And(rd, rn, op2),
Binop.OR, new Or(rd, rn, op2),
Binop.BITAND, new And(rd, rn, op2),
Binop.BITOR, new Or(rd, rn, op2)
Binop.PLUS, new Add(rd, rd, op2, Cond.S),
Binop.MINUS, new Sub(rd, rd, op2, Cond.S),
Binop.MUL, new SMull(rd, rd, op2),
Binop.AND, new And(rd, rd, op2),
Binop.OR, new Or(rd, rd, op2),
Binop.BITAND, new And(rd, rd, op2),
Binop.BITOR, new Or(rd, rd, op2),
Binop.BITXOR, new Eor(rd, rd, op2),
Binop.BITSHL, new Mov(rd, new Operand2(rd, rn, Operand2Operator.LSL)),
Binop.BITSHR, new Mov(rd, new Operand2(rd, rn, Operand2Operator.ASR))
);
return List.of(m.get(b));
};
......@@ -82,21 +86,24 @@ public abstract class ARMArithmeticLogic extends ArithmeticLogic implements ARMI
};
public static final Map<Binop, BinopAssemble> binopInstruction = Map.ofEntries(
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.PLUS, BasicBinopAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.MINUS, BasicBinopAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.MUL, BasicBinopAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.AND, BasicBinopAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.OR, BasicBinopAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.BITAND, BasicBinopAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.BITOR, BasicBinopAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.DIV, DivModAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.MOD, DivModAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.GREATER, CmpAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.GREATER_EQUAL, CmpAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.LESS, CmpAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.LESS_EQUAL, CmpAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.EQUAL, CmpAsm),
new AbstractMap.SimpleEntry<Binop, BinopAssemble>(Binop.INEQUAL, CmpAsm)
new AbstractMap.SimpleEntry<>(Binop.PLUS, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.MINUS, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.MUL, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.AND, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.OR, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.BITAND, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.BITOR, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.BITXOR, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.BITSHL, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.BITSHR, BasicBinopAsm),
new AbstractMap.SimpleEntry<>(Binop.DIV, DivModAsm),
new AbstractMap.SimpleEntry<>(Binop.MOD, DivModAsm),
new AbstractMap.SimpleEntry<>(Binop.GREATER, CmpAsm),
new AbstractMap.SimpleEntry<>(Binop.GREATER_EQUAL, CmpAsm),
new AbstractMap.SimpleEntry<>(Binop.LESS, CmpAsm),
new AbstractMap.SimpleEntry<>(Binop.LESS_EQUAL, CmpAsm),
new AbstractMap.SimpleEntry<>(Binop.EQUAL, CmpAsm),
new AbstractMap.SimpleEntry<>(Binop.INEQUAL, CmpAsm)
);
public static final UnopAssemble armUnopAsm = (rd, rn, op, notUsed) -> {
......@@ -105,27 +112,13 @@ public abstract class ARMArithmeticLogic extends ArithmeticLogic implements ARMI
Unop.CHR, List.of(),
Unop.MINUS, List.of(new Rsb(rd, rn, new Operand2(new ARMImmediate(0, BitNum.CONST8)))),
Unop.NOT, List.of(new Xor(rd, rn, new Operand2(new ARMImmediate(1, BitNum.CONST8)))),
Unop.ORD, List.of()
Unop.ORD, List.of(),
Unop.BITNOT, List.of(new Mov(rd, new Operand2(rn), ARMMovType.MVN), new And(rd, rd, new Operand2(rn)))
);
return m.get(op);
};
public static UnopAssemble ComplementAsm = (rd, rn, op, notUsed) -> {
List<Instruction> list = new ArrayList<>();
list.add(new Mov(rd, new Operand2(rn), ARMMovType.MVN));
return list;
};
public static final Map<Unop, UnopAssemble> unopInstruction = Map.ofEntries(
new AbstractMap.SimpleEntry<Unop, UnopAssemble>(Unop.LEN, armUnopAsm),
new AbstractMap.SimpleEntry<Unop, UnopAssemble>(Unop.MINUS, armUnopAsm),
new AbstractMap.SimpleEntry<Unop, UnopAssemble>(Unop.NOT, armUnopAsm),
new AbstractMap.SimpleEntry<Unop, UnopAssemble>(Unop.ORD, armUnopAsm),
new AbstractMap.SimpleEntry<Unop, UnopAssemble>(Unop.CHR, armUnopAsm),
new AbstractMap.SimpleEntry<Unop, UnopAssemble>(Unop.COMPLEMENT, ComplementAsm)
);
protected ARMArithmeticLogic(Register rd, Register rn, Address operand2) {
super(rd, rn, operand2);
}
......
package backend.arm.instructions.arithmeticLogic;
import backend.arm.instructions.addressing.AddressingMode2;
import backend.arm.instructions.addressing.Operand2;
import backend.common.address.Address;
import utils.backend.Cond;
......
package backend.arm.instructions.arithmeticLogic;
import backend.common.address.Address;
import utils.backend.register.Register;
public class Eor extends ARMArithmeticLogic {
/* ORR{cond}{S} <Rd>, <Rn>, <operand2> */
public Eor(Register rd, Register rn, Address operand2) {
super(rd, rn, operand2);
}
@Override
public String assemble() {
return "EOR " + rd + ", " + rn + ", " + addr;
}
}
......@@ -18,7 +18,7 @@ public class BinopNode extends ExprNode {
public enum Binop {
PLUS, MINUS, MUL, DIV, MOD, GREATER, GREATER_EQUAL, LESS, LESS_EQUAL, EQUAL,
INEQUAL, AND, OR, BITAND, BITOR, BITXOR
INEQUAL, AND, OR, BITAND, BITOR, BITXOR, BITSHL, BITSHR
}
private ExprNode expr1;
......@@ -39,6 +39,9 @@ public class BinopNode extends ExprNode {
break;
case BITOR:
case BITAND:
case BITXOR:
case BITSHL:
case BITSHR:
type = new BasicType(BasicTypeEnum.INT, arch);
break;
default:
......
......@@ -13,7 +13,7 @@ public class UnopNode extends ExprNode {
*/
public enum Unop {
NOT, MINUS, LEN, ORD, CHR, COMPLEMENT
NOT, MINUS, LEN, ORD, CHR, BITNOT
}
ExprNode expr;
......@@ -34,6 +34,9 @@ public class UnopNode extends ExprNode {
case CHR:
type = new BasicType(BasicTypeEnum.CHAR, arch);
break;
case BITNOT:
type = new BasicType(BasicTypeEnum.INT, arch);
break;
}
}
......
......@@ -5,7 +5,7 @@
begin
int a = 0xA12;
if a == 298 then
if a == 2578 then
println "Correct"
else
println "Wrong"
......
# bitwise shift left operation
# Output:
# 20
# Program:
begin
int x = 5;
println x << 2
end
\ No newline at end of file
# bitwise shift left operation
# Output:
# 4
# Program:
begin
int x = 17;
println x >> 2
end
\ No newline at end of file
# bitwise shift left operation
# Output:
# 11
# Program:
begin
println 2 ^ 9
end
\ No newline at end of file
# bitwise shift left operation
# Output:
# 12
# 5
# Program:
begin
int a = 5;
int b = 12;
a = a ^ b;
b = a ^ b;
a = a ^ b;
println a;
println b
end
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment