Skip to content

Panic if trunc operands are not compatible #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
51 changes: 51 additions & 0 deletions ir/inst_conversion_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package ir

import (
"fmt"
"testing"

"github.com/llir/llvm/ir/constant"
"github.com/llir/llvm/ir/types"
"github.com/pkg/errors"
)

func TestTypeCheckTrunc(t *testing.T) {
cases := []struct {
fromTyp, toTyp types.Type
panicMessage string // "OK" if not panic'ing.
}{
{types.I64, types.I1,
"OK"},
{types.NewVector(2, types.I32), types.NewVector(2, types.I1),
"OK"},

{types.I32, types.I64,
"invalid trunc operands: from.BitSize < to.BitSize (i32 is smaller than i64)"},
{types.NewVector(2, types.I32), types.I1,
"trunc operands are not compatible: from=<2 x i32>; to=i1"},
{types.NewVector(1, types.I32), types.NewVector(2, types.I1),
"trunc vector operand length mismatch: from=<1 x i32>; to=<2 x i1>"},
{types.NewVector(2, types.I32), types.NewVector(2, types.I64),
"invalid trunc operands: from.BitSize < to.BitSize (<2 x i32> is smaller than <2 x i64>)"},
}

errOK := errors.New("OK")

for _, c := range cases {
testName := fmt.Sprintf("%v to %v", c.fromTyp, c.toTyp)
t.Run(testName, func(t *testing.T) {
var panicErr error
zeroVal := constant.NewZeroInitializer(c.fromTyp)
func() {
defer func() { panicErr = recover().(error) }()
trunc := NewTrunc(zeroVal, c.toTyp)
trunc.String()
panic(errOK)
}()
got := panicErr.Error()
if got != c.panicMessage {
t.Errorf("expected %q, got %q", c.panicMessage, got)
}
})
}
}