Skip to content

Commit

Permalink
avoid copying locks when intercepting variables
Browse files Browse the repository at this point in the history
  • Loading branch information
xhd2015 committed Apr 14, 2024
1 parent 70a2950 commit cb69019
Show file tree
Hide file tree
Showing 12 changed files with 397 additions and 62 deletions.
1 change: 1 addition & 0 deletions cmd/xgo/patch_compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ var compilerFiles = []_FilePath{
type2ExprPatch.FilePath,
type2AssignmentsPatch.FilePath,
syntaxWalkPatch.FilePath,
synatxParserPatch.FilePath,
noderWriterPatch.FilePath,
noderExprPatch.FilePath,
syntaxPrinterPatch.FilePath,
Expand Down
66 changes: 63 additions & 3 deletions cmd/xgo/patch_compiler_ast_type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,51 @@ var syntaxWalkPatch = &FilePatch{
},
}

var synatxParserPatch = &FilePatch{
FilePath: _FilePath{"src", "cmd", "compile", "internal", "syntax", "parser.go"},
Patches: []*Patch{
// {
// // import
// Mark: "syntax_parser_import_xgo_syntax",
// InsertIndex: 2,
// Anchors: []string{
// `package syntax`,
// `import (`,
// "\n",
// },
// Content: `xgo_syntax "cmd/compile/internal/xgo_rewrite_internal/patch/syntax"`,
// },
{
// NOTE: dependency injection
Mark: "syntax_parser_record_comment_declare",
InsertIndex: 0,
InsertBefore: true,
Anchors: []string{
`func (p *parser) init(file *PosBase,`,
},
Content: `var RecordComments func(file *PosBase, line, col uint, comment string)`,
},
{
Mark: "syntax_parser_record_comments",
InsertIndex: 6,
Anchors: []string{
`func (p *parser) init(file *PosBase,`,
`p.scanner.init(`,
`func(line`,
`text`,
`:=`,
`commentText(msg)`,
"\n",
},
Content: `
if RecordComments != nil {
RecordComments(file,line,col,text)
}
`,
},
},
}

var noderWriterPatch = &FilePatch{
FilePath: _FilePath{"src", "cmd", "compile", "internal", "noder", "writer.go"},
Patches: []*Patch{
Expand Down Expand Up @@ -302,11 +347,26 @@ func patchCompilerAstTypeCheck(goroot string, goVersion *goinfo.GoVersion) error
if err != nil {
return err
}
if goVersion.Major == 1 && goVersion.Minor < 20 {
// var mock: comments
if false {
// this does not work, because comments are turned off
err = synatxParserPatch.Apply(goroot, goVersion)
if err != nil {
return err
}
}
if goVersion.Major > 1 || goVersion.Minor >= 20 {
// only go1.20 and above supports const mock
return nil
err := patchCompilerForConstTrap(goroot, goVersion)
if err != nil {
return err
}
}
err = type2ExprPatch.Apply(goroot, goVersion)
return nil
}

func patchCompilerForConstTrap(goroot string, goVersion *goinfo.GoVersion) error {
err := type2ExprPatch.Apply(goroot, goVersion)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/xgo/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import "fmt"

const VERSION = "1.0.24"
const REVISION = "a9cbbd937997b1473a5f70f0131927adcdbf3b79+1"
const NUMBER = 181
const REVISION = "70a2950e60bbb075af333afa7a702fc02b9d55ed+1"
const NUMBER = 182

func getRevision() string {
revSuffix := ""
Expand Down
65 changes: 60 additions & 5 deletions patch/pkgdata/pkgdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type PackageData struct {
Vars map[string]bool
Vars map[string]*VarInfo
Consts map[string]*ConstInfo
Funcs map[string]bool
}
Expand All @@ -20,6 +20,9 @@ type ConstInfo struct {
Type string // t=xx
Untyped bool // no 'e='(explicit) flag
}
type VarInfo struct {
Trap bool // has comment trap
}

var pkgDataMapping map[string]*PackageData

Expand Down Expand Up @@ -56,11 +59,11 @@ func WritePkgData(pkgPath string, pkgData *PackageData) error {
if err != nil {
return err
}
writeSection(w, "[var]", pkgData.Vars)
err = writeVarSection(w, "[var]", pkgData.Vars)
if err != nil {
return err
}
writeSection(w, "[func]", pkgData.Funcs)
err = writeSection(w, "[func]", pkgData.Funcs)
if err != nil {
return err
}
Expand Down Expand Up @@ -120,6 +123,7 @@ func writeConstSection(w io.Writer, section string, m map[string]*ConstInfo) err
}
return nil
}

func writeConst(w io.Writer, info *ConstInfo) error {
if !info.Untyped {
_, err := io.WriteString(w, " ")
Expand Down Expand Up @@ -149,6 +153,49 @@ func writeConst(w io.Writer, info *ConstInfo) error {
return nil
}

func writeVarSection(w io.Writer, section string, m map[string]*VarInfo) error {
if len(m) == 0 {
return nil
}
_, err := io.WriteString(w, section)
if err != nil {
return err
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
for k, v := range m {
_, err := io.WriteString(w, k)
if err != nil {
return err
}
err = writeVar(w, v)
if err != nil {
return err
}
_, err = io.WriteString(w, "\n")
if err != nil {
return err
}
}
return nil
}
func writeVar(w io.Writer, info *VarInfo) error {
if info.Trap {
_, err := io.WriteString(w, " ")
if err != nil {
return err
}
_, err = io.WriteString(w, "t")
if err != nil {
return err
}
}

return nil
}

func load(pkgPath string) (*PackageData, error) {
if xgo_ctxt.XgoCompilePkgDataDir == "" {
return nil, fmt.Errorf("XGO_COMPILE_PKG_DATA_DIR not set")
Expand Down Expand Up @@ -218,9 +265,17 @@ func parsePkgData(content string) (*PackageData, error) {
p.Funcs[name] = true
case Section_Var:
if p.Vars == nil {
p.Vars = make(map[string]bool, 1)
p.Vars = make(map[string]*VarInfo, 1)
}
varInfo := &VarInfo{}
kvs := strings.Split(extra, " ")
for _, v := range kvs {
if v == "t" {
varInfo.Trap = true
continue
}
}
p.Vars[name] = true
p.Vars[name] = varInfo
case Section_Const:
if p.Consts == nil {
p.Consts = make(map[string]*ConstInfo, 1)
Expand Down
51 changes: 47 additions & 4 deletions patch/syntax/syntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,13 @@ func (c DeclKind) IsVarOrConst() bool {
}

type DeclInfo struct {
FuncDecl *syntax.FuncDecl
VarDecl *syntax.VarDecl
ConstDecl *syntax.ConstDecl
FuncDecl *syntax.FuncDecl
VarDecl *syntax.VarDecl
ConstDecl *syntax.ConstDecl

// is this var decl follow a const __xgo_trap_xxx = 1?
FollowingTrapConst bool

Kind DeclKind
Name string
RecvTypeName string
Expand Down Expand Up @@ -464,7 +468,46 @@ func getFuncDecls(files []*syntax.File, varTrap bool) []*DeclInfo {
declFuncs = append(declFuncs, fnDecls...)
}
}
return declFuncs
// compute __xgo_trap_xxx
n := len(declFuncs)
if n == 0 {
return nil
}
j := n
for i := n - 1; i >= 0; i-- {
if i == 0 || !isTrapped(declFuncs, i) {
j--
declFuncs[j] = declFuncs[i]
continue
}
// a special comment
declFuncs[i].FollowingTrapConst = true
j--
declFuncs[j] = declFuncs[i]
// remove the comment by skipping next
i--
}
return declFuncs[j:]
}

func isTrapped(declFuncs []*DeclInfo, i int) bool {
fn := declFuncs[i]
if fn.Kind != Kind_Var {
return false
}
last := declFuncs[i-1]
if last.Kind != Kind_Const {
return false
}
const xgoTrapPrefix = "__xgo_trap_"
if !strings.HasPrefix(last.Name, xgoTrapPrefix) {
return false
}
subName := last.Name[len(xgoTrapPrefix):]
if !strings.EqualFold(fn.Name, subName) {
return false
}
return true
}

func filterFuncDecls(funcDecls []*DeclInfo, pkgPath string) []*DeclInfo {
Expand Down
Loading

0 comments on commit cb69019

Please sign in to comment.