compiler: fix int casting to use the source signedness

Previously, when casting an integer to a bigger integer, the destination
signedness was used. This is problematic when casting a negative int16
to uint32, for example, because it would cause zero-extension.
Этот коммит содержится в:
Ayke van Laethem 2019-04-05 16:51:03 +02:00 коммит произвёл Ron Evans
родитель 81a1114ee5
коммит 3a76a49ddf
2 изменённых файлов: 2 добавлений и 2 удалений

Просмотреть файл

@ -2559,7 +2559,7 @@ func (c *Compiler) parseConvert(typeFrom, typeTo types.Type, value llvm.Value, p
// Conversion between two integers. // Conversion between two integers.
if sizeFrom > sizeTo { if sizeFrom > sizeTo {
return c.builder.CreateTrunc(value, llvmTypeTo, ""), nil return c.builder.CreateTrunc(value, llvmTypeTo, ""), nil
} else if typeTo.Info()&types.IsUnsigned != 0 { // if unsigned } else if typeFrom.Info()&types.IsUnsigned != 0 { // if unsigned
return c.builder.CreateZExt(value, llvmTypeTo, ""), nil return c.builder.CreateZExt(value, llvmTypeTo, ""), nil
} else { // if signed } else { // if signed
return c.builder.CreateSExt(value, llvmTypeTo, ""), nil return c.builder.CreateSExt(value, llvmTypeTo, ""), nil

Просмотреть файл

@ -39,7 +39,7 @@ func printuint16(n uint16) {
printuint32(uint32(n)) printuint32(uint32(n))
} }
func printint16(n uint16) { func printint16(n int16) {
printint32(int32(n)) printint32(int32(n))
} }