Skip to content

Commit

Permalink
add partial go1.16 support
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Mar 17, 2024
1 parent a624602 commit 0171046
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cmd/xgo/main.go
Expand Up @@ -309,8 +309,8 @@ func checkGoVersion(goroot string, noInstrument bool) (*goinfo.GoVersion, error)
}
if !noInstrument {
minor := goVersion.Minor
if goVersion.Major != 1 || (minor < 17 || minor > 22) {
return nil, fmt.Errorf("only supports go1.17.0 ~ go1.22.1, current: %s", goVersionStr)
if goVersion.Major != 1 || (minor < 16 || minor > 22) {
return nil, fmt.Errorf("only supports go1.16.0 ~ go1.22.1, current: %s", goVersionStr)
}
}
return goVersion, nil
Expand Down
95 changes: 74 additions & 21 deletions cmd/xgo/patch.go
Expand Up @@ -77,7 +77,12 @@ func patchRuntimeDef(goRoot string, goVersion *goinfo.GoVersion) error {
cmd := exec.Command(filepath.Join(goRoot, "bin", "go"), "run", "mkbuiltin.go")
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd.Dir = filepath.Join(goRoot, "src/cmd/compile/internal/typecheck")

dir := "src/cmd/compile/internal/typecheck"
if goVersion.Major == 1 && goVersion.Minor <= 16 {
dir = "src/cmd/compile/internal/gc"
}
cmd.Dir = filepath.Join(goRoot, dir)

cmd.Env = os.Environ()
cmd.Env, err = patchEnvWithGoroot(cmd.Env, goRoot)
Expand All @@ -96,8 +101,12 @@ func patchRuntimeDef(goRoot string, goVersion *goinfo.GoVersion) error {
func prepareRuntimeDefs(goRoot string, goVersion *goinfo.GoVersion) error {
runtimeDefFile := "src/cmd/compile/internal/typecheck/_builtin/runtime.go"
if goVersion.Major == 1 && goVersion.Minor <= 19 {
// in go1.19 and below, builtin has no _ prefix
runtimeDefFile = "src/cmd/compile/internal/typecheck/builtin/runtime.go"
if goVersion.Minor > 16 {
// in go1.19 and below, builtin has no _ prefix
runtimeDefFile = "src/cmd/compile/internal/typecheck/builtin/runtime.go"
} else {
runtimeDefFile = "src/cmd/compile/internal/gc/builtin/runtime.go"
}
}
fullFile := filepath.Join(goRoot, runtimeDefFile)

Expand Down Expand Up @@ -165,12 +174,15 @@ func patchCompilerNoder(goroot string, goVersion *goinfo.GoVersion) error {
var noderFiles string
if goVersion.Major == 1 {
minor := goVersion.Minor
if minor == 17 {
noderFiles = patch.NoderFiles_1_18
if minor == 16 {
file = "src/cmd/compile/internal/gc/noder.go"
noderFiles = patch.NoderFiles_1_17
} else if minor == 17 {
noderFiles = patch.NoderFiles_1_17
} else if minor == 18 {
noderFiles = patch.NoderFiles_1_18
noderFiles = patch.NoderFiles_1_17
} else if minor == 19 {
noderFiles = patch.NoderFiles_1_18
noderFiles = patch.NoderFiles_1_17
} else if minor == 20 {
noderFiles = patch.NoderFiles_1_20
} else if minor == 21 {
Expand All @@ -190,12 +202,23 @@ func patchCompilerNoder(goroot string, goVersion *goinfo.GoVersion) error {
`"io"`,
},
)
content = addContentAfter(content, "/*<begin file_autogen>*/", "/*<end file_autogen>*/", []string{
`func LoadPackage`,
`for _, p := range noders {`,
`base.Timer.AddEvent(int64(lines), "lines")`,
"\n",
},
var anchors []string
if goVersion.Major == 1 && goVersion.Minor <= 16 {
anchors = []string{
"func parseFiles(filenames []string)",
"for _, p := range noders {",
"localpkg.Height = myheight",
"\n",
}
} else {
anchors = []string{
`func LoadPackage`,
`for _, p := range noders {`,
`base.Timer.AddEvent(int64(lines), "lines")`,
"\n",
}
}
content = addContentAfter(content, "/*<begin file_autogen>*/", "/*<end file_autogen>*/", anchors,
noderFiles)
return content, nil
})
Expand Down Expand Up @@ -228,7 +251,10 @@ func poatchIRGenericGen(goroot string, goVersion *goinfo.GoVersion) error {

func patchGcMain(goroot string, goVersion *goinfo.GoVersion) error {
file := "src/cmd/compile/internal/gc/main.go"
go117AndUnder := goVersion.Major == 1 && goVersion.Minor <= 17
go116AndUnder := goVersion.Major == 1 && goVersion.Minor <= 16
go117 := goVersion.Major == 1 && goVersion.Minor == 17
go118 := goVersion.Major == 1 && goVersion.Minor == 18
go119 := goVersion.Major == 1 && goVersion.Minor == 19
go119AndUnder := goVersion.Major == 1 && goVersion.Minor <= 19
go120 := goVersion.Major == 1 && goVersion.Minor == 20
go121 := goVersion.Major == 1 && goVersion.Minor == 21
Expand All @@ -246,12 +272,24 @@ func patchGcMain(goroot string, goVersion *goinfo.GoVersion) error {
initRuntimeTypeCheckGo117 := `typecheck.InitRuntime()`

var beforePatchContent string
patchAnchors := []string{`noder.LoadPackage(flag.Args())`, `dwarfgen.RecordPackageName()`}
if !go117AndUnder {
patchAnchors = append(patchAnchors, `ssagen.InitConfig()`)
var patchAnchors []string

if go116AndUnder {
// go1.16 is pretty old
patchAnchors = []string{
"loadsys()",
"parseFiles(flag.Args())",
"finishUniverse()",
"recordPackageName()",
}
} else {
// go 1.17 needs to call typecheck.InitRuntime() before patch
beforePatchContent = initRuntimeTypeCheckGo117 + "\n"
patchAnchors = []string{`noder.LoadPackage(flag.Args())`, `dwarfgen.RecordPackageName()`}
if !go117 {
patchAnchors = append(patchAnchors, `ssagen.InitConfig()`)
} else {
// go 1.17 needs to call typecheck.InitRuntime() before patch
beforePatchContent = initRuntimeTypeCheckGo117 + "\n"
}
}
patchAnchors = append(patchAnchors, "\n")
content = addContentAfter(content,
Expand All @@ -263,7 +301,7 @@ func patchGcMain(goroot string, goVersion *goinfo.GoVersion) error {
}
`)

if go117AndUnder {
if go117 {
// go1.17 needs to adjust typecheck.InitRuntime before patch
content = replaceContentAfter(content,
"/*<begin patch_init_runtime_type>*/", "/*<end patch_init_runtime_type>*/",
Expand All @@ -279,7 +317,22 @@ func patchGcMain(goroot string, goVersion *goinfo.GoVersion) error {
// - 1. by not calling to inline.InlinePackage
// - 2. by override base.Flag.LowerL to 0
// prefer 1 because it is more focused
if go119AndUnder || go120 || go121 {
if go116AndUnder {
inlineGuard := `if Debug.l != 0 {`
inlineAnchors := []string{
`fninit(xtop)`,
`Curfn = nil`,
`// Phase 5: Inlining`,
`if Debug_typecheckinl != 0 {`,
"\n",
}
content = replaceContentAfter(content,
"/*<begin prevent_inline>*/", "/*<end prevent_inline>*/",
inlineAnchors,
inlineGuard,
` // NOTE: turn off inline if there is any rewrite
`+strings.TrimSuffix(inlineGuard, " {")+` && !xgo_record.HasRewritten() {`)
} else if go117 || go118 || go119 || go120 || go121 {
inlineCall := `inline.InlinePackage(profile)`
if go119AndUnder {
// go1.19 and under does not hae PGO
Expand Down
2 changes: 1 addition & 1 deletion cmd/xgo/patch/runtime_def.go
Expand Up @@ -11,7 +11,7 @@ func (md *moduledata) funcName(nameOff int32) string {
return gostringnocopy(&md.funcnametab[nameOff])
}`

const NoderFiles_1_18 = ` // auto gen
const NoderFiles_1_17 = ` // auto gen
if os.Getenv("XGO_COMPILER_ENABLE")=="true" {
files := make([]*syntax.File, 0, len(noders))
for _, n := range noders {
Expand Down

0 comments on commit 0171046

Please sign in to comment.