mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-09 13:33:39 +00:00
Implement shifting with a differently-sized integer
This commit is contained in:
+19
-7
@@ -1530,13 +1530,25 @@ func (c *Compiler) parseBinOp(frame *Frame, binop *ssa.BinOp) (llvm.Value, error
|
|||||||
return c.builder.CreateOr(x, y, ""), nil
|
return c.builder.CreateOr(x, y, ""), nil
|
||||||
case token.XOR: // ^
|
case token.XOR: // ^
|
||||||
return c.builder.CreateXor(x, y, ""), nil
|
return c.builder.CreateXor(x, y, ""), nil
|
||||||
case token.SHL: // <<
|
case token.SHL, token.SHR:
|
||||||
return c.builder.CreateShl(x, y, ""), nil
|
sizeX := c.targetData.TypeAllocSize(x.Type())
|
||||||
case token.SHR: // >>
|
sizeY := c.targetData.TypeAllocSize(y.Type())
|
||||||
if signed {
|
if sizeX > sizeY {
|
||||||
return c.builder.CreateAShr(x, y, ""), nil
|
// x and y must have equal sizes, make Y bigger in this case.
|
||||||
} else {
|
// y is unsigned, this has been checked by the Go type checker.
|
||||||
return c.builder.CreateLShr(x, y, ""), nil
|
y = c.builder.CreateZExt(y, x.Type(), "")
|
||||||
|
}
|
||||||
|
switch binop.Op {
|
||||||
|
case token.SHL: // <<
|
||||||
|
return c.builder.CreateShl(x, y, ""), nil
|
||||||
|
case token.SHR: // >>
|
||||||
|
if signed {
|
||||||
|
return c.builder.CreateAShr(x, y, ""), nil
|
||||||
|
} else {
|
||||||
|
return c.builder.CreateLShr(x, y, ""), nil
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
case token.AND_NOT: // &^
|
case token.AND_NOT: // &^
|
||||||
// Go specific. Calculate "and not" with x & (~y)
|
// Go specific. Calculate "and not" with x & (~y)
|
||||||
|
|||||||
Reference in New Issue
Block a user