Repository: tetratelabs/wazero
Pull request #669 (2022-06-30 01:47:48)

asm: backfills unit tests and fixes node formatting


Patch:
diff --git a/internal/asm/amd64/impl.go b/internal/asm/amd64/impl.go
index c6219af3..c13f8a6f 100644
--- a/internal/asm/amd64/impl.go
+++ b/internal/asm/amd64/impl.go
@@ -129,17 +129,17 @@ func (n *nodeImpl) String() (ret string) {
 		ret = fmt.Sprintf("%s %s, 0x%x", instName, RegisterName(n.srcReg), n.dstConst)
 	case operandTypesMemoryToRegister:
 		if n.srcMemIndex != asm.NilRegister {
-			ret = fmt.Sprintf("%s [%s + %d + %s*0x%x], %s", instName,
+			ret = fmt.Sprintf("%s [%s + %#x + %s*%#x], %s", instName,
 				RegisterName(n.srcReg), n.srcConst, RegisterName(n.srcMemIndex), n.srcMemScale, RegisterName(n.dstReg))
 		} else {
 			ret = fmt.Sprintf("%s [%s + 0x%x], %s", instName, RegisterName(n.srcReg), n.srcConst, RegisterName(n.dstReg))
 		}
 	case operandTypesMemoryToConst:
 		if n.srcMemIndex != asm.NilRegister {
-			ret = fmt.Sprintf("%s [%s + %d + %s*0x%x], 0x%x", instName,
+			ret = fmt.Sprintf("%s [%s + %#x + %s*0x%x], 0x%x", instName,
 				RegisterName(n.srcReg), n.srcConst, RegisterName(n.srcMemIndex), n.srcMemScale, n.dstConst)
 		} else {
-			ret = fmt.Sprintf("%s [%s + 0x%x], 0x%x", instName, RegisterName(n.srcReg), n.srcConst, n.dstConst)
+			ret = fmt.Sprintf("%s [%s + %#x], 0x%x", instName, RegisterName(n.srcReg), n.srcConst, n.dstConst)
 		}
 	case operandTypesConstToMemory:
 		if n.dstMemIndex != asm.NilRegister {
@@ -150,6 +150,10 @@ func (n *nodeImpl) String() (ret string) {
 		}
 	case operandTypesConstToRegister:
 		ret = fmt.Sprintf("%s 0x%x, %s", instName, n.srcConst, RegisterName(n.dstReg))
+	case operandTypesStaticConstToRegister:
+		ret = fmt.Sprintf("%s $%#x, %s", instName, n.staticConst.Raw, RegisterName(n.dstReg))
+	case operandTypesRegisterToStaticConst:
+		ret = fmt.Sprintf("%s %s, $%#x", instName, RegisterName(n.srcReg), n.staticConst.Raw)
 	}
 	return
 }
diff --git a/internal/asm/arm64/impl.go b/internal/asm/arm64/impl.go
index 2dea0883..89be1e0e 100644
--- a/internal/asm/arm64/impl.go
+++ b/internal/asm/arm64/impl.go
@@ -101,16 +101,17 @@ func (n *nodeImpl) String() (ret string) {
 	case operandTypesRegisterToVectorRegister:
 		ret = fmt.Sprintf("%s %s, %s.%s[%d]", instName, RegisterName(n.srcReg), RegisterName(n.dstReg), n.vectorArrangement, n.dstVectorIndex)
 	case operandTypesVectorRegisterToRegister:
-		ret = fmt.Sprintf("%s %s.%s[%d], %s.%s[%d]", instName, RegisterName(n.srcReg), n.vectorArrangement, n.srcVectorIndex,
-			RegisterName(n.dstReg), n.vectorArrangement, n.dstVectorIndex)
+		ret = fmt.Sprintf("%s %s.%s[%d], %s", instName, RegisterName(n.srcReg), n.vectorArrangement, n.srcVectorIndex, RegisterName(n.dstReg))
 	case operandTypesVectorRegisterToMemory:
 		ret = fmt.Sprintf("%s %s.%s, [%s]", instName, RegisterName(n.srcReg), n.vectorArrangement, RegisterName(n.dstReg))
 	case operandTypesMemoryToVectorRegister:
 		ret = fmt.Sprintf("%s [%s], %s.%s", instName, RegisterName(n.srcReg), RegisterName(n.dstReg), n.vectorArrangement)
 	case operandTypesVectorRegisterToVectorRegister:
-		ret = fmt.Sprintf("%s %s.%[2]s, %s.%[2]s", instName, RegisterName(n.srcReg), RegisterName(n.dstReg), n.vectorArrangement)
+		ret = fmt.Sprintf("%s %[2]s.%[4]s, %[3]s.%[4]s", instName, RegisterName(n.srcReg), RegisterName(n.dstReg), n.vectorArrangement)
 	case operandTypesStaticConstToVectorRegister:
-		ret = fmt.Sprintf("%s $%v %s.%s", instName, n.staticConst, RegisterName(n.dstReg), n.vectorArrangement)
+		ret = fmt.Sprintf("%s $%#x %s.%s", instName, n.staticConst.Raw, RegisterName(n.dstReg), n.vectorArrangement)
+	case operandTypesTwoVectorRegistersToVectorRegister:
+		ret = fmt.Sprintf("%s (%s.%[5]s, %[3]s.%[5]s), %[4]s.%[5]s", instName, RegisterName(n.srcReg), RegisterName(n.srcReg2), RegisterName(n.dstReg), n.vectorArrangement)
 	}
 	return
 }


Test patch:
diff --git a/internal/asm/amd64/impl_1_test.go b/internal/asm/amd64/impl_1_test.go
index ce36e02f..14788778 100644
--- a/internal/asm/amd64/impl_1_test.go
+++ b/internal/asm/amd64/impl_1_test.go
@@ -137,22 +137,38 @@ func TestNodeImpl_String(t *testing.T) {
 			in: &nodeImpl{instruction: MOVL, types: operandTypesMemoryToRegister,
 				srcReg: RegDX, srcConst: 1, srcMemIndex: RegR12, srcMemScale: 2,
 				dstReg: RegAX},
-			exp: "MOVL [DX + 1 + R12*0x2], AX",
+			exp: "MOVL [DX + 0x1 + R12*0x2], AX",
 		},
 		{
 			in: &nodeImpl{instruction: CMPQ, types: operandTypesMemoryToConst,
 				srcReg: RegDX, srcConst: 1, srcMemIndex: RegR12, srcMemScale: 2,
 				dstConst: 123},
-			exp: "CMPQ [DX + 1 + R12*0x2], 0x7b",
+			exp: "CMPQ [DX + 0x1 + R12*0x2], 0x7b",
+		},
+		{
+			in:  &nodeImpl{instruction: CMPQ, types: operandTypesMemoryToConst, srcReg: RegDX, srcConst: 1, dstConst: 123},
+			exp: "CMPQ [DX + 0x1], 0x7b",
 		},
 		{
 			in:  &nodeImpl{instruction: MOVQ, types: operandTypesConstToMemory, srcConst: 123, dstReg: RegAX, dstConst: 100, dstMemScale: 8, dstMemIndex: RegR11},
 			exp: "MOVQ 0x7b, [AX + 0x64 + R11*0x8]",
 		},
+		{
+			in:  &nodeImpl{instruction: MOVQ, types: operandTypesConstToMemory, srcConst: 123, dstReg: RegAX, dstConst: 100},
+			exp: "MOVQ 0x7b, [AX + 0x64]",
+		},
 		{
 			in:  &nodeImpl{instruction: MOVQ, types: operandTypesConstToRegister, srcConst: 123, dstReg: RegAX},
 			exp: "MOVQ 0x7b, AX",
 		},
+		{
+			in:  &nodeImpl{instruction: LEAQ, types: operandTypesStaticConstToRegister, staticConst: &asm.StaticConst{Raw: []byte{0xff, 0x01, 0x2, 0xff}}, dstReg: RegAX},
+			exp: "LEAQ $0xff0102ff, AX",
+		},
+		{
+			in:  &nodeImpl{instruction: CMPQ, types: operandTypesRegisterToStaticConst, staticConst: &asm.StaticConst{Raw: []byte{0xff, 0x01, 0x2, 0xff}}, srcReg: RegAX},
+			exp: "CMPQ AX, $0xff0102ff",
+		},
 	}
 
 	for _, tt := range tests {
diff --git a/internal/asm/arm64/impl_1_test.go b/internal/asm/arm64/impl_1_test.go
index e24eb4f6..d2ecba85 100644
--- a/internal/asm/arm64/impl_1_test.go
+++ b/internal/asm/arm64/impl_1_test.go
@@ -108,7 +108,28 @@ func TestNodeImpl_String(t *testing.T) {
 		{
 			in: &nodeImpl{instruction: VCNT, types: operandTypesVectorRegisterToVectorRegister,
 				srcReg: RegV3, dstReg: RegV29, vectorArrangement: VectorArrangement2D, srcVectorIndex: 1},
-			exp: "VCNT V3.V3, V29.V3",
+			exp: "VCNT V3.2D, V29.2D",
+		},
+		{
+			in: &nodeImpl{instruction: VCNT, types: operandTypesVectorRegisterToVectorRegister,
+				srcReg: RegV3, dstReg: RegV29, vectorArrangement: VectorArrangement2D, srcVectorIndex: 1},
+			exp: "VCNT V3.2D, V29.2D",
+		},
+		{
+			in: &nodeImpl{instruction: UMOV, types: operandTypesVectorRegisterToRegister,
+				srcReg: RegV31, dstReg: RegR8, vectorArrangement: VectorArrangementS, srcVectorIndex: 1},
+			exp: "UMOV V31.S[1], R8",
+		},
+		{
+			in: &nodeImpl{instruction: UMOV, types: operandTypesTwoVectorRegistersToVectorRegister,
+				srcReg: RegV31, srcReg2: RegV1, dstReg: RegV8, vectorArrangement: VectorArrangementS, srcVectorIndex: 1},
+			exp: "UMOV (V31.S, V1.S), V8.S",
+		},
+		{
+			in: &nodeImpl{instruction: VORR, types: operandTypesStaticConstToVectorRegister,
+				staticConst: &asm.StaticConst{Raw: []byte{1, 2, 3, 4}},
+				dstReg:      RegV8, vectorArrangement: VectorArrangement16B, srcVectorIndex: 1},
+			exp: "VORR $0x01020304 V8.16B",
 		},
 	}
 
@@ -244,6 +265,19 @@ func Test_CompileMemoryWithRegisterOffsetToRegister(t *testing.T) {
 	require.Equal(t, operandTypeRegister, actualNode.types.dst)
 }
 
+func Test_CompileMemoryWithRegisterOffsetToVectorRegister(t *testing.T) {
+	a := NewAssembler(RegR10)
+	a.CompileMemoryWithRegisterOffsetToVectorRegister(MOVD, RegR27, RegR10, RegV31, VectorArrangementS)
+	actualNode := a.Current
+	require.Equal(t, MOVD, actualNode.instruction)
+	require.Equal(t, RegR27, actualNode.srcReg)
+	require.Equal(t, RegR10, actualNode.srcReg2)
+	require.Equal(t, RegV31, actualNode.dstReg)
+	require.Equal(t, VectorArrangementS, actualNode.vectorArrangement)
+	require.Equal(t, operandTypeMemory, actualNode.types.src)
+	require.Equal(t, operandTypeVectorRegister, actualNode.types.dst)
+}
+
 func Test_CompileRegisterToMemoryWithRegisterOffset(t *testing.T) {
 	a := NewAssembler(RegR10)
 	a.CompileRegisterToMemoryWithRegisterOffset(MOVD, RegR27, RegR10, RegR0)
@@ -256,6 +290,19 @@ func Test_CompileRegisterToMemoryWithRegisterOffset(t *testing.T) {
 	require.Equal(t, operandTypeMemory, actualNode.types.dst)
 }
 
+func Test_CompileVectorRegisterToMemoryWithRegisterOffset(t *testing.T) {
+	a := NewAssembler(RegR10)
+	a.CompileVectorRegisterToMemoryWithRegisterOffset(MOVD, RegV31, RegR10, RegR0, VectorArrangement2D)
+	actualNode := a.Current
+	require.Equal(t, MOVD, actualNode.instruction)
+	require.Equal(t, RegV31, actualNode.srcReg)
+	require.Equal(t, RegR10, actualNode.dstReg)
+	require.Equal(t, RegR0, actualNode.dstReg2)
+	require.Equal(t, VectorArrangement2D, actualNode.vectorArrangement)
+	require.Equal(t, operandTypeVectorRegister, actualNode.types.src)
+	require.Equal(t, operandTypeMemory, actualNode.types.dst)
+}
+
 func Test_CompileTwoRegistersToRegister(t *testing.T) {
 	a := NewAssembler(RegR10)
 	a.CompileTwoRegistersToRegister(MOVD, RegR27, RegR10, RegR0)
@@ -393,6 +440,19 @@ func Test_CompileVectorRegisterToVectorRegister(t *testing.T) {
 	require.Equal(t, VectorIndex(2), actualNode.dstVectorIndex)
 }
 
+func Test_CompileVectorRegisterToVectorRegisterWithConst(t *testing.T) {
+	a := NewAssembler(RegR10)
+	a.CompileVectorRegisterToVectorRegisterWithConst(VMOV, RegV3, RegV10, VectorArrangement1D, 1234)
+	actualNode := a.Current
+	require.Equal(t, VMOV, actualNode.instruction)
+	require.Equal(t, RegV3, actualNode.srcReg)
+	require.Equal(t, RegV10, actualNode.dstReg)
+	require.Equal(t, operandTypeVectorRegister, actualNode.types.src)
+	require.Equal(t, operandTypeVectorRegister, actualNode.types.dst)
+	require.Equal(t, VectorArrangement1D, actualNode.vectorArrangement)
+	require.Equal(t, int64(1234), actualNode.srcConst)
+}
+
 func Test_CompileTwoVectorRegistersToVectorRegister(t *testing.T) {
 	a := NewAssembler(RegR10)
 	a.CompileTwoVectorRegistersToVectorRegister(VMOV, RegV3, RegV15, RegV10, VectorArrangement1D)
@@ -406,6 +466,45 @@ func Test_CompileTwoVectorRegistersToVectorRegister(t *testing.T) {
 	require.Equal(t, VectorArrangement1D, actualNode.vectorArrangement)
 }
 
+func Test_CompileTwoVectorRegistersToVectorRegisterWithConst(t *testing.T) {
+	a := NewAssembler(RegR10)
+	a.CompileTwoVectorRegistersToVectorRegisterWithConst(VMOV, RegV3, RegV15, RegV10, VectorArrangement1D, 1234)
+	actualNode := a.Current
+	require.Equal(t, VMOV, actualNode.instruction)
+	require.Equal(t, RegV3, actualNode.srcReg)
+	require.Equal(t, RegV15, actualNode.srcReg2)
+	require.Equal(t, int64(1234), actualNode.srcConst)
+	require.Equal(t, RegV10, actualNode.dstReg)
+	require.Equal(t, operandTypeTwoVectorRegisters, actualNode.types.src)
+	require.Equal(t, operandTypeVectorRegister, actualNode.types.dst)
+	require.Equal(t, VectorArrangement1D, actualNode.vectorArrangement)
+}
+
+func Test_CompileStaticConstToVectorRegister(t *testing.T) {
+	s := asm.NewStaticConst([]byte{1, 2, 3, 4})
+	a := NewAssembler(RegR10)
+	a.CompileStaticConstToVectorRegister(VMOV, s, RegV3, VectorArrangement2D)
+	actualNode := a.Current
+	require.Equal(t, VMOV, actualNode.instruction)
+	require.Equal(t, s, actualNode.staticConst)
+	require.Equal(t, RegV3, actualNode.dstReg)
+	require.Equal(t, operandTypeStaticConst, actualNode.types.src)
+	require.Equal(t, operandTypeVectorRegister, actualNode.types.dst)
+	require.Equal(t, VectorArrangement2D, actualNode.vectorArrangement)
+}
+
+func Test_CompileStaticConstToRegister(t *testing.T) {
+	s := asm.NewStaticConst([]byte{1, 2, 3, 4})
+	a := NewAssembler(RegR10)
+	a.CompileStaticConstToRegister(ADR, s, RegR27)
+	actualNode := a.Current
+	require.Equal(t, ADR, actualNode.instruction)
+	require.Equal(t, s, actualNode.staticConst)
+	require.Equal(t, RegR27, actualNode.dstReg)
+	require.Equal(t, operandTypeMemory, actualNode.types.src)
+	require.Equal(t, operandTypeRegister, actualNode.types.dst)
+}
+
 func Test_checkRegisterToRegisterType(t *testing.T) {
 	tests := []struct {
 		src, dst                     asm.Register