From f6fc0dd620a838be3c98acc798ba58d6cbb0bac2 Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Sun, 19 Feb 2017 23:40:24 -0500 Subject: [PATCH] cmd/compile: update signature of runtime.memclr* runtime.memclr* functions have signatures func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) func memclrHasPointers(ptr unsafe.Pointer, n uintptr) Update compiler's copy. Also teach gc/mkbuiltin.go to handle unsafe.Pointer. The import statement and its support is not really necessary, but just to make it look like real Go code. Fixes #19185. Change-Id: I251d02571fde2716d4727e31e04d56ec04b6f22a Reviewed-on: https://go-review.googlesource.com/37257 Run-TryBot: Cherry Zhang TryBot-Result: Gobot Gobot Reviewed-by: Austin Clements --- src/cmd/compile/internal/gc/builtin.go | 85 ++++++++++--------- .../compile/internal/gc/builtin/runtime.go | 6 +- src/cmd/compile/internal/gc/mkbuiltin.go | 11 +++ src/cmd/compile/internal/gc/range.go | 4 +- 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/cmd/compile/internal/gc/builtin.go b/src/cmd/compile/internal/gc/builtin.go index c43e3a0b6b..9f8aa2697b 100644 --- a/src/cmd/compile/internal/gc/builtin.go +++ b/src/cmd/compile/internal/gc/builtin.go @@ -98,37 +98,37 @@ var runtimeDecls = [...]struct { {"makeslice64", funcTag, 90}, {"growslice", funcTag, 91}, {"memmove", funcTag, 92}, - {"memclrNoHeapPointers", funcTag, 93}, - {"memclrHasPointers", funcTag, 93}, - {"memequal", funcTag, 94}, - {"memequal8", funcTag, 95}, - {"memequal16", funcTag, 95}, - {"memequal32", funcTag, 95}, - {"memequal64", funcTag, 95}, - {"memequal128", funcTag, 95}, - {"int64div", funcTag, 96}, - {"uint64div", funcTag, 97}, - {"int64mod", funcTag, 96}, - {"uint64mod", funcTag, 97}, - {"float64toint64", funcTag, 98}, - {"float64touint64", funcTag, 99}, - {"float64touint32", funcTag, 101}, - {"int64tofloat64", funcTag, 102}, - {"uint64tofloat64", funcTag, 103}, - {"uint32tofloat64", funcTag, 104}, - {"complex128div", funcTag, 105}, - {"racefuncenter", funcTag, 106}, + {"memclrNoHeapPointers", funcTag, 94}, + {"memclrHasPointers", funcTag, 94}, + {"memequal", funcTag, 95}, + {"memequal8", funcTag, 96}, + {"memequal16", funcTag, 96}, + {"memequal32", funcTag, 96}, + {"memequal64", funcTag, 96}, + {"memequal128", funcTag, 96}, + {"int64div", funcTag, 97}, + {"uint64div", funcTag, 98}, + {"int64mod", funcTag, 97}, + {"uint64mod", funcTag, 98}, + {"float64toint64", funcTag, 99}, + {"float64touint64", funcTag, 100}, + {"float64touint32", funcTag, 102}, + {"int64tofloat64", funcTag, 103}, + {"uint64tofloat64", funcTag, 104}, + {"uint32tofloat64", funcTag, 105}, + {"complex128div", funcTag, 106}, + {"racefuncenter", funcTag, 107}, {"racefuncexit", funcTag, 5}, - {"raceread", funcTag, 106}, - {"racewrite", funcTag, 106}, - {"racereadrange", funcTag, 107}, - {"racewriterange", funcTag, 107}, - {"msanread", funcTag, 107}, - {"msanwrite", funcTag, 107}, + {"raceread", funcTag, 107}, + {"racewrite", funcTag, 107}, + {"racereadrange", funcTag, 108}, + {"racewriterange", funcTag, 108}, + {"msanread", funcTag, 108}, + {"msanwrite", funcTag, 108}, } func runtimeTypes() []*Type { - var typs [108]*Type + var typs [109]*Type typs[0] = bytetype typs[1] = typPtr(typs[0]) typs[2] = Types[TANY] @@ -222,20 +222,21 @@ func runtimeTypes() []*Type { typs[90] = functype(nil, []*Node{anonfield(typs[1]), anonfield(typs[15]), anonfield(typs[15])}, []*Node{anonfield(typs[88])}) typs[91] = functype(nil, []*Node{anonfield(typs[1]), anonfield(typs[88]), anonfield(typs[32])}, []*Node{anonfield(typs[88])}) typs[92] = functype(nil, []*Node{anonfield(typs[3]), anonfield(typs[3]), anonfield(typs[49])}, nil) - typs[93] = functype(nil, []*Node{anonfield(typs[1]), anonfield(typs[49])}, nil) - typs[94] = functype(nil, []*Node{anonfield(typs[3]), anonfield(typs[3]), anonfield(typs[49])}, []*Node{anonfield(typs[11])}) - typs[95] = functype(nil, []*Node{anonfield(typs[3]), anonfield(typs[3])}, []*Node{anonfield(typs[11])}) - typs[96] = functype(nil, []*Node{anonfield(typs[15]), anonfield(typs[15])}, []*Node{anonfield(typs[15])}) - typs[97] = functype(nil, []*Node{anonfield(typs[17]), anonfield(typs[17])}, []*Node{anonfield(typs[17])}) - typs[98] = functype(nil, []*Node{anonfield(typs[13])}, []*Node{anonfield(typs[15])}) - typs[99] = functype(nil, []*Node{anonfield(typs[13])}, []*Node{anonfield(typs[17])}) - typs[100] = Types[TUINT32] - typs[101] = functype(nil, []*Node{anonfield(typs[13])}, []*Node{anonfield(typs[100])}) - typs[102] = functype(nil, []*Node{anonfield(typs[15])}, []*Node{anonfield(typs[13])}) - typs[103] = functype(nil, []*Node{anonfield(typs[17])}, []*Node{anonfield(typs[13])}) - typs[104] = functype(nil, []*Node{anonfield(typs[100])}, []*Node{anonfield(typs[13])}) - typs[105] = functype(nil, []*Node{anonfield(typs[19]), anonfield(typs[19])}, []*Node{anonfield(typs[19])}) - typs[106] = functype(nil, []*Node{anonfield(typs[49])}, nil) - typs[107] = functype(nil, []*Node{anonfield(typs[49]), anonfield(typs[49])}, nil) + typs[93] = Types[TUNSAFEPTR] + typs[94] = functype(nil, []*Node{anonfield(typs[93]), anonfield(typs[49])}, nil) + typs[95] = functype(nil, []*Node{anonfield(typs[3]), anonfield(typs[3]), anonfield(typs[49])}, []*Node{anonfield(typs[11])}) + typs[96] = functype(nil, []*Node{anonfield(typs[3]), anonfield(typs[3])}, []*Node{anonfield(typs[11])}) + typs[97] = functype(nil, []*Node{anonfield(typs[15]), anonfield(typs[15])}, []*Node{anonfield(typs[15])}) + typs[98] = functype(nil, []*Node{anonfield(typs[17]), anonfield(typs[17])}, []*Node{anonfield(typs[17])}) + typs[99] = functype(nil, []*Node{anonfield(typs[13])}, []*Node{anonfield(typs[15])}) + typs[100] = functype(nil, []*Node{anonfield(typs[13])}, []*Node{anonfield(typs[17])}) + typs[101] = Types[TUINT32] + typs[102] = functype(nil, []*Node{anonfield(typs[13])}, []*Node{anonfield(typs[101])}) + typs[103] = functype(nil, []*Node{anonfield(typs[15])}, []*Node{anonfield(typs[13])}) + typs[104] = functype(nil, []*Node{anonfield(typs[17])}, []*Node{anonfield(typs[13])}) + typs[105] = functype(nil, []*Node{anonfield(typs[101])}, []*Node{anonfield(typs[13])}) + typs[106] = functype(nil, []*Node{anonfield(typs[19]), anonfield(typs[19])}, []*Node{anonfield(typs[19])}) + typs[107] = functype(nil, []*Node{anonfield(typs[49])}, nil) + typs[108] = functype(nil, []*Node{anonfield(typs[49]), anonfield(typs[49])}, nil) return typs[:] } diff --git a/src/cmd/compile/internal/gc/builtin/runtime.go b/src/cmd/compile/internal/gc/builtin/runtime.go index a6aae0c468..fc55104ef8 100644 --- a/src/cmd/compile/internal/gc/builtin/runtime.go +++ b/src/cmd/compile/internal/gc/builtin/runtime.go @@ -12,6 +12,8 @@ package runtime // emitted by compiler, not referred to by go programs +import "unsafe" + func newobject(typ *byte) *any func panicindex() func panicslice() @@ -129,8 +131,8 @@ func makeslice(typ *byte, len int, cap int) (ary []any) func makeslice64(typ *byte, len int64, cap int64) (ary []any) func growslice(typ *byte, old []any, cap int) (ary []any) func memmove(to *any, frm *any, length uintptr) -func memclrNoHeapPointers(ptr *byte, length uintptr) -func memclrHasPointers(ptr *byte, length uintptr) +func memclrNoHeapPointers(ptr unsafe.Pointer, n uintptr) +func memclrHasPointers(ptr unsafe.Pointer, n uintptr) func memequal(x, y *any, size uintptr) bool func memequal8(x, y *any) bool diff --git a/src/cmd/compile/internal/gc/mkbuiltin.go b/src/cmd/compile/internal/gc/mkbuiltin.go index 995f5be1df..6d918f8d25 100644 --- a/src/cmd/compile/internal/gc/mkbuiltin.go +++ b/src/cmd/compile/internal/gc/mkbuiltin.go @@ -72,6 +72,12 @@ func mkbuiltin(w io.Writer, name string) { } fmt.Fprintf(w, "{%q, funcTag, %d},\n", decl.Name.Name, interner.intern(decl.Type)) case *ast.GenDecl: + if decl.Tok == token.IMPORT { + if len(decl.Specs) != 1 || decl.Specs[0].(*ast.ImportSpec).Path.Value != "\"unsafe\"" { + log.Fatal("runtime cannot import other package") + } + continue + } if decl.Tok != token.VAR { log.Fatal("unhandled declaration kind", decl.Tok) } @@ -137,6 +143,11 @@ func (i *typeInterner) mktype(t ast.Expr) string { return "runetype" } return fmt.Sprintf("Types[T%s]", strings.ToUpper(t.Name)) + case *ast.SelectorExpr: + if t.X.(*ast.Ident).Name != "unsafe" || t.Sel.Name != "Pointer" { + log.Fatalf("unhandled type: %#v", t) + } + return "Types[TUNSAFEPTR]" case *ast.ArrayType: if t.Len == nil { diff --git a/src/cmd/compile/internal/gc/range.go b/src/cmd/compile/internal/gc/range.go index d1ac5780a0..9c57b7606b 100644 --- a/src/cmd/compile/internal/gc/range.go +++ b/src/cmd/compile/internal/gc/range.go @@ -419,13 +419,13 @@ func memclrrange(n, v1, v2, a *Node) bool { n.Left = nod(ONE, nod(OLEN, a, nil), nodintconst(0)) // hp = &a[0] - hp := temp(ptrto(Types[TUINT8])) + hp := temp(Types[TUNSAFEPTR]) tmp := nod(OINDEX, a, nodintconst(0)) tmp.Bounded = true tmp = nod(OADDR, tmp, nil) tmp = nod(OCONVNOP, tmp, nil) - tmp.Type = ptrto(Types[TUINT8]) + tmp.Type = Types[TUNSAFEPTR] n.Nbody.Append(nod(OAS, hp, tmp)) // hn = len(a) * sizeof(elem(a)) -- 2.50.0