Skip to content

Commit 5709403

Browse files
committed
Panic if trunc operands are not compatible
* Support vector and int typed arguments. * Add a table driven test for the different cases. I've tried my best to make the error messages informative and consistent with other error messages, but further improvements are welcomed - please also feel free to take ownership of the branch and tweak the messages if appropriate. Updates #65. Subsumes part of PR #69.
1 parent 10a64da commit 5709403

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

ir/inst_conversion.go

+34
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,40 @@ type InstTrunc struct {
3030
// NewTrunc returns a new trunc instruction based on the given source value and
3131
// target type.
3232
func NewTrunc(from value.Value, to types.Type) *InstTrunc {
33+
fromType := from.Type()
34+
// Note: intentional alias, so can use it for checking
35+
// the integer types within vectors.
36+
toType := to
37+
38+
if fromVectorT, ok := fromType.(*types.VectorType); ok {
39+
toVectorT, ok := toType.(*types.VectorType)
40+
if !ok {
41+
panic(fmt.Errorf("trunc operands are not compatible: from=%v; to=%v", fromVectorT, to))
42+
}
43+
44+
if fromVectorT.Len != toVectorT.Len {
45+
msg := "trunc vector operand length mismatch: from=%v; to=%v"
46+
panic(fmt.Errorf(msg, from.Type(), to))
47+
}
48+
49+
fromType = fromVectorT.ElemType
50+
toType = toVectorT.ElemType
51+
}
52+
53+
if fromIntT, ok := fromType.(*types.IntType); ok {
54+
toIntT, ok := toType.(*types.IntType)
55+
if !ok {
56+
msg := "trunc operands are not compatible: from=%v; to=%T"
57+
panic(fmt.Errorf(msg, fromIntT, to))
58+
}
59+
fromSize := fromIntT.BitSize
60+
toSize := toIntT.BitSize
61+
if fromSize < toSize {
62+
msg := "invalid trunc operands: from.BitSize < to.BitSize (%v is smaller than %v)"
63+
panic(fmt.Errorf(msg, from.Type(), to))
64+
}
65+
}
66+
3367
return &InstTrunc{From: from, To: to}
3468
}
3569

0 commit comments

Comments
 (0)