mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
If we want to break up package gc at all, we will need to move
the compiler IR it defines into a separate package that can be
imported by packages that gc itself imports. This CL does that.
It also removes the TINT8 etc aliases so that all code is clear
about which package things are coming from.
This CL is automatically generated by the script below.
See the comments in the script for details about the changes.
[git-generate]
cd src/cmd/compile/internal/gc
rf '
# These names were never fully qualified
# when the types package was added.
# Do it now, to avoid confusion about where they live.
inline -rm \
Txxx \
TINT8 \
TUINT8 \
TINT16 \
TUINT16 \
TINT32 \
TUINT32 \
TINT64 \
TUINT64 \
TINT \
TUINT \
TUINTPTR \
TCOMPLEX64 \
TCOMPLEX128 \
TFLOAT32 \
TFLOAT64 \
TBOOL \
TPTR \
TFUNC \
TSLICE \
TARRAY \
TSTRUCT \
TCHAN \
TMAP \
TINTER \
TFORW \
TANY \
TSTRING \
TUNSAFEPTR \
TIDEAL \
TNIL \
TBLANK \
TFUNCARGS \
TCHANARGS \
NTYPE \
BADWIDTH
# esc.go and escape.go do not need to be split.
# Append esc.go onto the end of escape.go.
mv esc.go escape.go
# Pull out the type format installation from func Main,
# so it can be carried into package ir.
mv Main:/Sconv.=/-0,/TypeLinkSym/-1 InstallTypeFormats
# Names that need to be exported for use by code left in gc.
mv Isconst IsConst
mv asNode AsNode
mv asNodes AsNodes
mv asTypesNode AsTypesNode
mv basicnames BasicTypeNames
mv builtinpkg BuiltinPkg
mv consttype ConstType
mv dumplist DumpList
mv fdumplist FDumpList
mv fmtMode FmtMode
mv goopnames OpNames
mv inspect Inspect
mv inspectList InspectList
mv localpkg LocalPkg
mv nblank BlankNode
mv numImport NumImport
mv opprec OpPrec
mv origSym OrigSym
mv stmtwithinit StmtWithInit
mv dump DumpAny
mv fdump FDumpAny
mv nod Nod
mv nodl NodAt
mv newname NewName
mv newnamel NewNameAt
mv assertRepresents AssertValidTypeForConst
mv represents ValidTypeForConst
mv nodlit NewLiteral
# Types and fields that need to be exported for use by gc.
mv nowritebarrierrecCallSym SymAndPos
mv SymAndPos.lineno SymAndPos.Pos
mv SymAndPos.target SymAndPos.Sym
mv Func.lsym Func.LSym
mv Func.setWBPos Func.SetWBPos
mv Func.numReturns Func.NumReturns
mv Func.numDefers Func.NumDefers
mv Func.nwbrCalls Func.NWBRCalls
# initLSym is an algorithm left behind in gc,
# not an operation on Func itself.
mv Func.initLSym initLSym
mv nodeQueue NodeQueue
mv NodeQueue.empty NodeQueue.Empty
mv NodeQueue.popLeft NodeQueue.PopLeft
mv NodeQueue.pushRight NodeQueue.PushRight
# Many methods on Node are actually algorithms that
# would apply to any node implementation.
# Those become plain functions.
mv Node.funcname FuncName
mv Node.isBlank IsBlank
mv Node.isGoConst isGoConst
mv Node.isNil IsNil
mv Node.isParamHeapCopy isParamHeapCopy
mv Node.isParamStackCopy isParamStackCopy
mv Node.isSimpleName isSimpleName
mv Node.mayBeShared MayBeShared
mv Node.pkgFuncName PkgFuncName
mv Node.backingArrayPtrLen backingArrayPtrLen
mv Node.isterminating isTermNode
mv Node.labeledControl labeledControl
mv Nodes.isterminating isTermNodes
mv Nodes.sigerr fmtSignature
mv Node.MethodName methodExprName
mv Node.MethodFunc methodExprFunc
mv Node.IsMethod IsMethod
# Every node will need to implement RawCopy;
# Copy and SepCopy algorithms will use it.
mv Node.rawcopy Node.RawCopy
mv Node.copy Copy
mv Node.sepcopy SepCopy
# Extract Node.Format method body into func FmtNode,
# but leave method wrapper behind.
mv Node.Format:0,$ FmtNode
# Formatting helpers that will apply to all node implementations.
mv Node.Line Line
mv Node.exprfmt exprFmt
mv Node.jconv jconvFmt
mv Node.modeString modeString
mv Node.nconv nconvFmt
mv Node.nodedump nodeDumpFmt
mv Node.nodefmt nodeFmt
mv Node.stmtfmt stmtFmt
# Constant support needed for code moving to ir.
mv okforconst OKForConst
mv vconv FmtConst
mv int64Val Int64Val
mv float64Val Float64Val
mv Node.ValueInterface ConstValue
# Organize code into files.
mv LocalPkg BuiltinPkg ir.go
mv NumImport InstallTypeFormats Line fmt.go
mv syntax.go Nod NodAt NewNameAt Class Pxxx PragmaFlag Nointerface SymAndPos \
AsNode AsTypesNode BlankNode OrigSym \
Node.SliceBounds Node.SetSliceBounds Op.IsSlice3 \
IsConst Node.Int64Val Node.CanInt64 Node.Uint64Val Node.BoolVal Node.StringVal \
Node.RawCopy SepCopy Copy \
IsNil IsBlank IsMethod \
Node.Typ Node.StorageClass node.go
mv ConstType ConstValue Int64Val Float64Val AssertValidTypeForConst ValidTypeForConst NewLiteral idealType OKForConst val.go
# Move files to new ir package.
mv bitset.go class_string.go dump.go fmt.go \
ir.go node.go op_string.go val.go \
sizeof_test.go cmd/compile/internal/ir
'
: # fix mkbuiltin.go to generate the changes made to builtin.go during rf
sed -i '' '
s/\[T/[types.T/g
s/\*Node/*ir.Node/g
/internal\/types/c \
fmt.Fprintln(&b, `import (`) \
fmt.Fprintln(&b, ` "cmd/compile/internal/ir"`) \
fmt.Fprintln(&b, ` "cmd/compile/internal/types"`) \
fmt.Fprintln(&b, `)`)
' mkbuiltin.go
gofmt -w mkbuiltin.go
: # update cmd/dist to add internal/ir
cd ../../../dist
sed -i '' '/compile.internal.gc/a\
"cmd/compile/internal/ir",
' buildtool.go
gofmt -w buildtool.go
: # update cmd/compile TestFormats
cd ../..
go install std cmd
cd cmd/compile
go test -u || go test # first one updates but fails; second passes
Change-Id: I5f7caf6b20629b51970279e81231a3574d5b51db
Reviewed-on: https://go-review.googlesource.com/c/go/+/273008
Trust: Russ Cox <rsc@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
195 lines
5.2 KiB
Go
195 lines
5.2 KiB
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gc
|
|
|
|
import (
|
|
"cmd/compile/internal/base"
|
|
"cmd/compile/internal/ir"
|
|
"cmd/compile/internal/syntax"
|
|
"cmd/internal/objabi"
|
|
"cmd/internal/src"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
func makePos(b *src.PosBase, line, col uint) src.XPos {
|
|
return base.Ctxt.PosTable.XPos(src.MakePos(b, line, col))
|
|
}
|
|
|
|
func isSpace(c rune) bool {
|
|
return c == ' ' || c == '\t' || c == '\n' || c == '\r'
|
|
}
|
|
|
|
func isQuoted(s string) bool {
|
|
return len(s) >= 2 && s[0] == '"' && s[len(s)-1] == '"'
|
|
}
|
|
|
|
const (
|
|
FuncPragmas = ir.Nointerface |
|
|
ir.Noescape |
|
|
ir.Norace |
|
|
ir.Nosplit |
|
|
ir.Noinline |
|
|
ir.NoCheckPtr |
|
|
ir.CgoUnsafeArgs |
|
|
ir.UintptrEscapes |
|
|
ir.Systemstack |
|
|
ir.Nowritebarrier |
|
|
ir.Nowritebarrierrec |
|
|
ir.Yeswritebarrierrec
|
|
|
|
TypePragmas = ir.NotInHeap
|
|
)
|
|
|
|
func pragmaFlag(verb string) ir.PragmaFlag {
|
|
switch verb {
|
|
case "go:build":
|
|
return ir.GoBuildPragma
|
|
case "go:nointerface":
|
|
if objabi.Fieldtrack_enabled != 0 {
|
|
return ir.Nointerface
|
|
}
|
|
case "go:noescape":
|
|
return ir.Noescape
|
|
case "go:norace":
|
|
return ir.Norace
|
|
case "go:nosplit":
|
|
return ir.Nosplit | ir.NoCheckPtr // implies NoCheckPtr (see #34972)
|
|
case "go:noinline":
|
|
return ir.Noinline
|
|
case "go:nocheckptr":
|
|
return ir.NoCheckPtr
|
|
case "go:systemstack":
|
|
return ir.Systemstack
|
|
case "go:nowritebarrier":
|
|
return ir.Nowritebarrier
|
|
case "go:nowritebarrierrec":
|
|
return ir.Nowritebarrierrec | ir.Nowritebarrier // implies Nowritebarrier
|
|
case "go:yeswritebarrierrec":
|
|
return ir.Yeswritebarrierrec
|
|
case "go:cgo_unsafe_args":
|
|
return ir.CgoUnsafeArgs | ir.NoCheckPtr // implies NoCheckPtr (see #34968)
|
|
case "go:uintptrescapes":
|
|
// For the next function declared in the file
|
|
// any uintptr arguments may be pointer values
|
|
// converted to uintptr. This directive
|
|
// ensures that the referenced allocated
|
|
// object, if any, is retained and not moved
|
|
// until the call completes, even though from
|
|
// the types alone it would appear that the
|
|
// object is no longer needed during the
|
|
// call. The conversion to uintptr must appear
|
|
// in the argument list.
|
|
// Used in syscall/dll_windows.go.
|
|
return ir.UintptrEscapes
|
|
case "go:notinheap":
|
|
return ir.NotInHeap
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// pragcgo is called concurrently if files are parsed concurrently.
|
|
func (p *noder) pragcgo(pos syntax.Pos, text string) {
|
|
f := pragmaFields(text)
|
|
|
|
verb := strings.TrimPrefix(f[0], "go:")
|
|
f[0] = verb
|
|
|
|
switch verb {
|
|
case "cgo_export_static", "cgo_export_dynamic":
|
|
switch {
|
|
case len(f) == 2 && !isQuoted(f[1]):
|
|
case len(f) == 3 && !isQuoted(f[1]) && !isQuoted(f[2]):
|
|
default:
|
|
p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf(`usage: //go:%s local [remote]`, verb)})
|
|
return
|
|
}
|
|
case "cgo_import_dynamic":
|
|
switch {
|
|
case len(f) == 2 && !isQuoted(f[1]):
|
|
case len(f) == 3 && !isQuoted(f[1]) && !isQuoted(f[2]):
|
|
case len(f) == 4 && !isQuoted(f[1]) && !isQuoted(f[2]) && isQuoted(f[3]):
|
|
f[3] = strings.Trim(f[3], `"`)
|
|
if objabi.GOOS == "aix" && f[3] != "" {
|
|
// On Aix, library pattern must be "lib.a/object.o"
|
|
// or "lib.a/libname.so.X"
|
|
n := strings.Split(f[3], "/")
|
|
if len(n) != 2 || !strings.HasSuffix(n[0], ".a") || (!strings.HasSuffix(n[1], ".o") && !strings.Contains(n[1], ".so.")) {
|
|
p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_dynamic local [remote ["lib.a/object.o"]]`})
|
|
return
|
|
}
|
|
}
|
|
default:
|
|
p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_dynamic local [remote ["library"]]`})
|
|
return
|
|
}
|
|
case "cgo_import_static":
|
|
switch {
|
|
case len(f) == 2 && !isQuoted(f[1]):
|
|
default:
|
|
p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_import_static local`})
|
|
return
|
|
}
|
|
case "cgo_dynamic_linker":
|
|
switch {
|
|
case len(f) == 2 && isQuoted(f[1]):
|
|
f[1] = strings.Trim(f[1], `"`)
|
|
default:
|
|
p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_dynamic_linker "path"`})
|
|
return
|
|
}
|
|
case "cgo_ldflag":
|
|
switch {
|
|
case len(f) == 2 && isQuoted(f[1]):
|
|
f[1] = strings.Trim(f[1], `"`)
|
|
default:
|
|
p.error(syntax.Error{Pos: pos, Msg: `usage: //go:cgo_ldflag "arg"`})
|
|
return
|
|
}
|
|
default:
|
|
return
|
|
}
|
|
p.pragcgobuf = append(p.pragcgobuf, f)
|
|
}
|
|
|
|
// pragmaFields is similar to strings.FieldsFunc(s, isSpace)
|
|
// but does not split when inside double quoted regions and always
|
|
// splits before the start and after the end of a double quoted region.
|
|
// pragmaFields does not recognize escaped quotes. If a quote in s is not
|
|
// closed the part after the opening quote will not be returned as a field.
|
|
func pragmaFields(s string) []string {
|
|
var a []string
|
|
inQuote := false
|
|
fieldStart := -1 // Set to -1 when looking for start of field.
|
|
for i, c := range s {
|
|
switch {
|
|
case c == '"':
|
|
if inQuote {
|
|
inQuote = false
|
|
a = append(a, s[fieldStart:i+1])
|
|
fieldStart = -1
|
|
} else {
|
|
inQuote = true
|
|
if fieldStart >= 0 {
|
|
a = append(a, s[fieldStart:i])
|
|
}
|
|
fieldStart = i
|
|
}
|
|
case !inQuote && isSpace(c):
|
|
if fieldStart >= 0 {
|
|
a = append(a, s[fieldStart:i])
|
|
fieldStart = -1
|
|
}
|
|
default:
|
|
if fieldStart == -1 {
|
|
fieldStart = i
|
|
}
|
|
}
|
|
}
|
|
if !inQuote && fieldStart >= 0 { // Last field might end at the end of the string.
|
|
a = append(a, s[fieldStart:])
|
|
}
|
|
return a
|
|
}
|