Skip to content

Commit

Permalink
unix: move mksyscall regexp to package level variables
Browse files Browse the repository at this point in the history
Avoid compiling regular expressions for each loop iteration, rather
declare them as package level variables so they are only compiled once.

Change-Id: I5c29a44e986ef8d97326134e8fc8e983671e68d8
Reviewed-on: https://go-review.googlesource.com/c/sys/+/542955
Reviewed-by: Joel Sing <joel@sing.id.au>
Run-TryBot: Joel Sing <joel@sing.id.au>
Reviewed-by: Carlos Amedee <carlos@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
  • Loading branch information
joshuasing authored and 4a6f656c committed Jan 27, 2024
1 parent 5710a32 commit bef1bd8
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions unix/mksyscall.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ var (
libcPath = "libc.so"
)

var (
regexpComma = regexp.MustCompile(`\s*,\s*`)
regexpParamKV = regexp.MustCompile(`^(\S*) (\S*)$`)
regexpSys = regexp.MustCompile(`^\/\/sys\t`)
regexpSysNonblock = regexp.MustCompile(`^\/\/sysnb\t`)
regexpSysDeclaration = regexp.MustCompile(`^\/\/sys(nb)?\t(\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$`)
regexpPointer = regexp.MustCompile(`^\*`)
regexpSlice = regexp.MustCompile(`^\[](.*)`)
regexpDragonflyExtp = regexp.MustCompile(`^(?i)extp(read|write)`)
regexpSyscallName = regexp.MustCompile(`([a-z])([A-Z])`)
)

// cmdLine returns this programs's commandline arguments
func cmdLine() string {
return "go run mksyscall.go " + strings.Join(os.Args[1:], " ")
Expand Down Expand Up @@ -75,12 +87,12 @@ func parseParamList(list string) []string {
if list == "" {
return []string{}
}
return regexp.MustCompile(`\s*,\s*`).Split(list, -1)
return regexpComma.Split(list, -1)
}

// parseParam splits a parameter into name and type
func parseParam(p string) Param {
ps := regexp.MustCompile(`^(\S*) (\S*)$`).FindStringSubmatch(p)
ps := regexpParamKV.FindStringSubmatch(p)
if ps == nil {
fmt.Fprintf(os.Stderr, "malformed parameter: %s\n", p)
os.Exit(1)
Expand Down Expand Up @@ -138,15 +150,15 @@ func main() {
s := bufio.NewScanner(file)
for s.Scan() {
t := s.Text()
nonblock := regexp.MustCompile(`^\/\/sysnb\t`).FindStringSubmatch(t)
if regexp.MustCompile(`^\/\/sys\t`).FindStringSubmatch(t) == nil && nonblock == nil {
nonblock := regexpSysNonblock.FindStringSubmatch(t)
if regexpSys.FindStringSubmatch(t) == nil && nonblock == nil {
continue
}

// Line must be of the form
// func Open(path string, mode int, perm int) (fd int, errno error)
// Split into name, in params, out params.
f := regexp.MustCompile(`^\/\/sys(nb)?\t(\w+)\(([^()]*)\)\s*(?:\(([^()]+)\))?\s*(?:=\s*((?i)SYS_[A-Z0-9_]+))?$`).FindStringSubmatch(t)
f := regexpSysDeclaration.FindStringSubmatch(t)
if f == nil {
fmt.Fprintf(os.Stderr, "%s:%s\nmalformed //sys declaration\n", path, t)
os.Exit(1)
Expand Down Expand Up @@ -184,7 +196,7 @@ func main() {
n := 0
for _, param := range in {
p := parseParam(param)
if regexp.MustCompile(`^\*`).FindStringSubmatch(p.Type) != nil {
if regexpPointer.FindStringSubmatch(p.Type) != nil {
args = append(args, "uintptr(unsafe.Pointer("+p.Name+"))")
} else if p.Type == "string" && errvar != "" {
text += fmt.Sprintf("\tvar _p%d *byte\n", n)
Expand All @@ -198,7 +210,7 @@ func main() {
text += fmt.Sprintf("\t_p%d, _ = BytePtrFromString(%s)\n", n, p.Name)
args = append(args, fmt.Sprintf("uintptr(unsafe.Pointer(_p%d))", n))
n++
} else if regexp.MustCompile(`^\[\](.*)`).FindStringSubmatch(p.Type) != nil {
} else if regexpSlice.FindStringSubmatch(p.Type) != nil {
// Convert slice into pointer, length.
// Have to be careful not to take address of &a[0] if len == 0:
// pass dummy pointer in that case.
Expand All @@ -218,7 +230,7 @@ func main() {
args = append(args, fmt.Sprintf("uintptr(%s)", p.Name))
}
} else if p.Type == "int64" && *dragonfly {
if regexp.MustCompile(`^(?i)extp(read|write)`).FindStringSubmatch(funct) == nil {
if regexpDragonflyExtp.FindStringSubmatch(funct) == nil {
args = append(args, "0")
}
if endianness == "big-endian" {
Expand Down Expand Up @@ -278,7 +290,7 @@ func main() {
// System call number.
if sysname == "" {
sysname = "SYS_" + funct
sysname = regexp.MustCompile(`([a-z])([A-Z])`).ReplaceAllString(sysname, `${1}_$2`)
sysname = regexpSyscallName.ReplaceAllString(sysname, `${1}_$2`)
sysname = strings.ToUpper(sysname)
}

Expand Down

0 comments on commit bef1bd8

Please sign in to comment.