From 5709403f2027a1479f5b458ef5e69e4aa8ffc3a6 Mon Sep 17 00:00:00 2001 From: Peter Waller
Date: Sat, 23 Feb 2019 19:54:44 +0000 Subject: [PATCH] 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. --- ir/inst_conversion.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/ir/inst_conversion.go b/ir/inst_conversion.go index 5e6d78a7..3066385a 100644 --- a/ir/inst_conversion.go +++ b/ir/inst_conversion.go @@ -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} }