compiler: add support for complex add and sub

This is fairly trivial to add and follows the implementation of gc:
170b8b4b12/src/cmd/compile/internal/gc/ssa.go (L2179-L2192)
Этот коммит содержится в:
Ayke van Laethem 2019-05-04 22:16:45 +02:00 коммит произвёл Ron Evans
родитель 1113f9ec0c
коммит 638bc17eeb
3 изменённых файлов: 28 добавлений и 0 удалений

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

@ -1821,6 +1821,22 @@ func (c *Compiler) parseBinOp(op token.Token, typ types.Type, x, y llvm.Value, p
ieq := c.builder.CreateFCmp(llvm.FloatOEQ, i1, i2, "") ieq := c.builder.CreateFCmp(llvm.FloatOEQ, i1, i2, "")
neq := c.builder.CreateAnd(req, ieq, "") neq := c.builder.CreateAnd(req, ieq, "")
return c.builder.CreateNot(neq, ""), nil return c.builder.CreateNot(neq, ""), nil
case token.ADD, token.SUB:
var r, i llvm.Value
switch op {
case token.ADD:
r = c.builder.CreateFAdd(r1, r2, "")
i = c.builder.CreateFAdd(i1, i2, "")
case token.SUB:
r = c.builder.CreateFSub(r1, r2, "")
i = c.builder.CreateFSub(i1, i2, "")
default:
panic("unreachable")
}
cplx := llvm.Undef(c.ctx.StructType([]llvm.Type{r.Type(), i.Type()}, false))
cplx = c.builder.CreateInsertValue(cplx, r, 0, "")
cplx = c.builder.CreateInsertValue(cplx, i, 1, "")
return cplx, nil
default: default:
return llvm.Value{}, c.makeError(pos, "todo: binop on complex number: "+op.String()) return llvm.Value{}, c.makeError(pos, "todo: binop on complex number: "+op.String())
} }

8
testdata/float.go предоставленный
Просмотреть файл

@ -56,4 +56,12 @@ func main() {
// cast complex // cast complex
println(complex64(c128)) println(complex64(c128))
println(complex128(c64)) println(complex128(c64))
// binops on complex numbers
c64 = 5+2i
println("complex64 add: ", c64 + -3+8i)
println("complex64 sub: ", c64 - -3+8i)
c128 = -5+2i
println("complex128 add:", c128 + 2+6i)
println("complex128 sub:", c128 - 2+6i)
} }

4
testdata/float.txt предоставленный
Просмотреть файл

@ -23,3 +23,7 @@
(+2.000000e+000-2.000000e+000i) (+2.000000e+000-2.000000e+000i)
(+6.666667e-001-2.000000e+000i) (+6.666667e-001-2.000000e+000i)
(+6.666667e-001+1.200000e+000i) (+6.666667e-001+1.200000e+000i)
complex64 add: (+2.000000e+000+1.000000e+001i)
complex64 sub: (+8.000000e+000+1.000000e+001i)
complex128 add: (-3.000000e+000+8.000000e+000i)
complex128 sub: (-7.000000e+000+8.000000e+000i)