From 879a1ff1e494188a7aec8f094fac9bd71850d392 Mon Sep 17 00:00:00 2001 From: Giovanni Bajo Date: Thu, 1 Mar 2018 01:39:01 +0100 Subject: [PATCH] test: improve asmcheck syntax asmcheck comments now support a compact form of specifying multiple checks for each platform, using the following syntax: amd64:"SHL\t[$]4","SHR\t[$]4" Negative checks are also parsed using the following syntax: amd64:-"ROR" though they are still not working. Moreover, out-of-line comments have been implemented. This allows to specify asmchecks on comment-only lines, that will be matched on the first subsequent non-comment non-empty line. // amd64:"XOR" // arm:"EOR" x ^= 1 Change-Id: I110c7462fc6a5c70fd4af0d42f516016ae7f2760 Reviewed-on: https://go-review.googlesource.com/97816 Reviewed-by: Keith Randall --- test/codegen/rotate.go | 2 ++ test/run.go | 74 ++++++++++++++++++++++++++++++------------ 2 files changed, 56 insertions(+), 20 deletions(-) diff --git a/test/codegen/rotate.go b/test/codegen/rotate.go index f305a9cfc0..f2b587d202 100644 --- a/test/codegen/rotate.go +++ b/test/codegen/rotate.go @@ -21,5 +21,7 @@ func rot64(x uint64) uint64 { } func copysign(a, b float64) float64 { + // amd64:"SHLQ\t[$]1","SHRQ\t[$]1","SHRQ\t[$]63","SHLQ\t[$]63","ORQ" + // ppc64le:"FCPSGN" s390x:"CPSDR",-"MOVD" return math.Copysign(a, b) } diff --git a/test/run.go b/test/run.go index ae29ad2fa6..271a6f8014 100644 --- a/test/run.go +++ b/test/run.go @@ -1247,8 +1247,23 @@ func (t *test) wantedErrors(file, short string) (errs []wantedError) { return } +const ( + // Regexp to match a single opcode check: optionally begin with "-" (to indicate + // a negative check), followed by a string literal enclosed in "" or ``. For "", + // backslashes must be handled. + reMatchCheck = `-?(?:\x60[^\x60]*\x60|"(?:[^"\\]|\\.)*")` +) + var ( - rxAsmCheck = regexp.MustCompile(`//(?:\s+(\w+):((?:"(?:.+?)")|(?:` + "`" + `(?:.+?)` + "`" + `)))+`) + // Regexp to split a line in code and comment, trimming spaces + rxAsmComment = regexp.MustCompile(`^\s*(.*?)\s*(?:\/\/\s*(.+)\s*)?$`) + + // Regexp to extract an architecture check: architecture name, followed by semi-colon, + // followed by a comma-separated list of opcode checks. + rxAsmPlatform = regexp.MustCompile(`(\w+):(` + reMatchCheck + `(?:,` + reMatchCheck + `)*)`) + + // Regexp to extract a single opcoded check + rxAsmCheck = regexp.MustCompile(reMatchCheck) ) type wantedAsmOpcode struct { @@ -1262,33 +1277,52 @@ func (t *test) wantedAsmOpcodes(fn string) (map[string]map[string][]wantedAsmOpc ops := make(map[string]map[string][]wantedAsmOpcode) archs := make(map[string]bool) + comment := "" src, _ := ioutil.ReadFile(fn) for i, line := range strings.Split(string(src), "\n") { - matches := rxAsmCheck.FindStringSubmatch(line) - if len(matches) == 0 { + matches := rxAsmComment.FindStringSubmatch(line) + code, cmt := matches[1], matches[2] + + // Keep comments pending in the comment variable until + // we find a line that contains some code. + comment += " " + cmt + if code == "" { continue } + // Parse and extract any architecture check from comments, + // made by one architecture name and multiple checks. lnum := fn + ":" + strconv.Itoa(i+1) - for j := 1; j < len(matches); j += 2 { - rxsrc, err := strconv.Unquote(matches[j+1]) - if err != nil { - log.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err) - } - oprx, err := regexp.Compile(rxsrc) - if err != nil { - log.Fatalf("%s:%d: %v", t.goFileName(), i+1, err) - } - arch := matches[j] - if ops[arch] == nil { - ops[arch] = make(map[string][]wantedAsmOpcode) + for _, ac := range rxAsmPlatform.FindAllStringSubmatch(comment, -1) { + arch, allchecks := ac[1], ac[2] + + for _, m := range rxAsmCheck.FindAllString(allchecks, -1) { + negative := false + if m[0] == '-' { + negative = true + m = m[1:] + } + + rxsrc, err := strconv.Unquote(m) + if err != nil { + log.Fatalf("%s:%d: error unquoting string: %v", t.goFileName(), i+1, err) + } + oprx, err := regexp.Compile(rxsrc) + if err != nil { + log.Fatalf("%s:%d: %v", t.goFileName(), i+1, err) + } + if ops[arch] == nil { + ops[arch] = make(map[string][]wantedAsmOpcode) + } + archs[arch] = true + ops[arch][lnum] = append(ops[arch][lnum], wantedAsmOpcode{ + negative: negative, + line: i + 1, + opcode: oprx, + }) } - archs[arch] = true - ops[arch][lnum] = append(ops[arch][lnum], wantedAsmOpcode{ - line: i + 1, - opcode: oprx, - }) } + comment = "" } var sarchs []string -- 2.48.1