cmd/compile: add //go:embed support

This commit contains the compiler support for //go:embed lines.
The go command passes to the compiler an "embed config"
that maps literal patterns like *.txt to the set of files to embed.
The compiler then lays out the content of those files as static data
in the form of an embed.Files or string or []byte in the final object file.

The test for this code is the end-to-end test hooking up the
embed, cmd/compile, and cmd/go changes, in the next CL.

For #41191.

Change-Id: I916e57f8cc65871dc0044c13d3f90c252a3fe1bf
Reviewed-on: https://go-review.googlesource.com/c/go/+/243944
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
This commit is contained in:
Russ Cox 2020-07-19 00:32:02 -04:00
parent 400581b8b0
commit 8bde9b320e
8 changed files with 682 additions and 48 deletions

View file

@ -401,7 +401,7 @@ type LSym struct {
P []byte
R []Reloc
Extra *interface{} // *FuncInfo if present
Extra *interface{} // *FuncInfo or *FileInfo, if present
Pkg string
PkgIdx int32
@ -454,6 +454,33 @@ func (s *LSym) Func() *FuncInfo {
return f
}
// A FileInfo contains extra fields for SDATA symbols backed by files.
// (If LSym.Extra is a *FileInfo, LSym.P == nil.)
type FileInfo struct {
Name string // name of file to read into object file
Size int64 // length of file
}
// NewFileInfo allocates and returns a FileInfo for LSym.
func (s *LSym) NewFileInfo() *FileInfo {
if s.Extra != nil {
log.Fatalf("invalid use of LSym - NewFileInfo with Extra of type %T", *s.Extra)
}
f := new(FileInfo)
s.Extra = new(interface{})
*s.Extra = f
return f
}
// File returns the *FileInfo associated with s, or else nil.
func (s *LSym) File() *FileInfo {
if s.Extra == nil {
return nil
}
f, _ := (*s.Extra).(*FileInfo)
return f
}
type InlMark struct {
// When unwinding from an instruction in an inlined body, mark
// where we should unwind to.