Commit caf3b4a7 authored by Sebastien Binet's avatar Sebastien Binet Committed by Rob Pike

Preliminary support for 'copy' builtin function in exp/eval

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/2157042
parent 18b02f6c
...@@ -1239,6 +1239,38 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e ...@@ -1239,6 +1239,38 @@ func (a *exprInfo) compileBuiltinCallExpr(b *block, ft *FuncType, as []*expr) *e
} }
return expr return expr
case copyType:
if !checkCount(2, 2) {
return nil
}
src := as[1]
dst := as[0]
if src.t != dst.t {
a.diag("arguments to built-in function 'copy' must have same type\nsrc: %s\ndst: %s\n", src.t, dst.t)
return nil
}
if _, ok := src.t.lit().(*SliceType); !ok {
a.diag("src argument to 'copy' must be a slice (got: %s)", src.t)
return nil
}
if _, ok := dst.t.lit().(*SliceType); !ok {
a.diag("dst argument to 'copy' must be a slice (got: %s)", dst.t)
return nil
}
expr := a.newExpr(IntType, "function call")
srcf := src.asSlice()
dstf := dst.asSlice()
expr.eval = func(t *Thread) int64 {
src, dst := srcf(t), dstf(t)
nelems := src.Len
if nelems > dst.Len {
nelems = dst.Len
}
dst.Base.Sub(0, nelems).Assign(t, src.Base.Sub(0, nelems))
return nelems
}
return expr
case lenType: case lenType:
if !checkCount(1, 1) { if !checkCount(1, 1) {
return nil return nil
......
...@@ -712,6 +712,7 @@ var ( ...@@ -712,6 +712,7 @@ var (
panicType = &FuncType{builtin: "panic"} panicType = &FuncType{builtin: "panic"}
printType = &FuncType{builtin: "print"} printType = &FuncType{builtin: "print"}
printlnType = &FuncType{builtin: "println"} printlnType = &FuncType{builtin: "println"}
copyType = &FuncType{builtin: "copy"}
) )
// Two function types are identical if they have the same number of // Two function types are identical if they have the same number of
...@@ -1249,6 +1250,7 @@ func init() { ...@@ -1249,6 +1250,7 @@ func init() {
universe.DefineConst("cap", universePos, capType, nil) universe.DefineConst("cap", universePos, capType, nil)
universe.DefineConst("close", universePos, closeType, nil) universe.DefineConst("close", universePos, closeType, nil)
universe.DefineConst("closed", universePos, closedType, nil) universe.DefineConst("closed", universePos, closedType, nil)
universe.DefineConst("copy", universePos, copyType, nil)
universe.DefineConst("len", universePos, lenType, nil) universe.DefineConst("len", universePos, lenType, nil)
universe.DefineConst("make", universePos, makeType, nil) universe.DefineConst("make", universePos, makeType, nil)
universe.DefineConst("new", universePos, newType, nil) universe.DefineConst("new", universePos, newType, nil)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment