Skip to content

Commit

Permalink
Panic if trunc operands are not compatible
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
pwaller committed Feb 24, 2019
1 parent 10a64da commit 5709403
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions ir/inst_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ type InstTrunc struct {
// NewTrunc returns a new trunc instruction based on the given source value and
// target type.
func NewTrunc(from value.Value, to types.Type) *InstTrunc {
fromType := from.Type()
// Note: intentional alias, so can use it for checking
// the integer types within vectors.
toType := to

if fromVectorT, ok := fromType.(*types.VectorType); ok {
toVectorT, ok := toType.(*types.VectorType)
if !ok {
panic(fmt.Errorf("trunc operands are not compatible: from=%v; to=%v", fromVectorT, to))
}

if fromVectorT.Len != toVectorT.Len {
msg := "trunc vector operand length mismatch: from=%v; to=%v"
panic(fmt.Errorf(msg, from.Type(), to))
}

fromType = fromVectorT.ElemType
toType = toVectorT.ElemType
}

if fromIntT, ok := fromType.(*types.IntType); ok {
toIntT, ok := toType.(*types.IntType)
if !ok {
msg := "trunc operands are not compatible: from=%v; to=%T"
panic(fmt.Errorf(msg, fromIntT, to))
}
fromSize := fromIntT.BitSize
toSize := toIntT.BitSize
if fromSize < toSize {
msg := "invalid trunc operands: from.BitSize < to.BitSize (%v is smaller than %v)"
panic(fmt.Errorf(msg, from.Type(), to))
}
}

return &InstTrunc{From: from, To: to}
}

Expand Down

0 comments on commit 5709403

Please sign in to comment.