Source file src/cmd/compile/internal/ssa/_gen/rulegen.go

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // This program generates Go code that applies rewrite rules to a Value.
     6  // The generated code implements a function of type func (v *Value) bool
     7  // which reports whether if did something.
     8  // Ideas stolen from the Swift Java compiler:
     9  // https://bitsavers.org/pdf/dec/tech_reports/WRL-2000-2.pdf
    10  
    11  package main
    12  
    13  import (
    14  	"bufio"
    15  	"bytes"
    16  	"flag"
    17  	"fmt"
    18  	"go/ast"
    19  	"go/format"
    20  	"go/parser"
    21  	"go/printer"
    22  	"go/token"
    23  	"io"
    24  	"log"
    25  	"os"
    26  	"path"
    27  	"regexp"
    28  	"sort"
    29  	"strconv"
    30  	"strings"
    31  
    32  	"golang.org/x/tools/go/ast/astutil"
    33  )
    34  
    35  // rule syntax:
    36  //  sexpr [&& extra conditions] => [@block] sexpr
    37  //
    38  // sexpr are s-expressions (lisp-like parenthesized groupings)
    39  // sexpr ::= [variable:](opcode sexpr*)
    40  //         | variable
    41  //         | <type>
    42  //         | [auxint]
    43  //         | {aux}
    44  //
    45  // aux      ::= variable | {code}
    46  // type     ::= variable | {code}
    47  // variable ::= some token
    48  // opcode   ::= one of the opcodes from the *Ops.go files
    49  
    50  // special rules: trailing ellipsis "..." (in the outermost sexpr?) must match on both sides of a rule.
    51  //                trailing three underscore "___" in the outermost match sexpr indicate the presence of
    52  //                   extra ignored args that need not appear in the replacement
    53  //                if the right-hand side is in {}, then it is code used to generate the result.
    54  
    55  // extra conditions is just a chunk of Go that evaluates to a boolean. It may use
    56  // variables declared in the matching tsexpr. The variable "v" is predefined to be
    57  // the value matched by the entire rule.
    58  
    59  // If multiple rules match, the first one in file order is selected.
    60  
    61  var (
    62  	genLog  = flag.Bool("log", false, "generate code that logs; for debugging only")
    63  	addLine = flag.Bool("line", false, "add line number comment to generated rules; for debugging only")
    64  )
    65  
    66  type Rule struct {
    67  	Rule string
    68  	Loc  string // file name & line number
    69  }
    70  
    71  func (r Rule) String() string {
    72  	return fmt.Sprintf("rule %q at %s", r.Rule, r.Loc)
    73  }
    74  
    75  func normalizeSpaces(s string) string {
    76  	return strings.Join(strings.Fields(strings.TrimSpace(s)), " ")
    77  }
    78  
    79  // parse returns the matching part of the rule, additional conditions, and the result.
    80  func (r Rule) parse() (match, cond, result string) {
    81  	s := strings.Split(r.Rule, "=>")
    82  	match = normalizeSpaces(s[0])
    83  	result = normalizeSpaces(s[1])
    84  	cond = ""
    85  	if i := strings.Index(match, "&&"); i >= 0 {
    86  		cond = normalizeSpaces(match[i+2:])
    87  		match = normalizeSpaces(match[:i])
    88  	}
    89  	return match, cond, result
    90  }
    91  
    92  func genRules(arch arch)          { genRulesSuffix(arch, "") }
    93  func genSplitLoadRules(arch arch) { genRulesSuffix(arch, "splitload") }
    94  func genLateLowerRules(arch arch) { genRulesSuffix(arch, "latelower") }
    95  
    96  func genRulesSuffix(arch arch, suff string) {
    97  	var readers []NamedReader
    98  	// Open input file.
    99  	var text io.Reader
   100  	name := arch.name + suff + ".rules"
   101  	text, err := os.Open(name)
   102  	if err != nil {
   103  		if suff == "" {
   104  			// All architectures must have a plain rules file.
   105  			log.Fatalf("can't read rule file: %v", err)
   106  		}
   107  		// Some architectures have bonus rules files that others don't share. That's fine.
   108  		return
   109  	}
   110  	readers = append(readers, NamedReader{name, text})
   111  
   112  	// Check for file of SIMD rules to add
   113  	if suff == "" {
   114  		simdname := "simd" + arch.name + ".rules"
   115  		simdtext, err := os.Open(simdname)
   116  		if err == nil {
   117  			readers = append(readers, NamedReader{simdname, simdtext})
   118  		}
   119  	}
   120  
   121  	// oprules contains a list of rules for each block and opcode
   122  	blockrules := map[string][]Rule{}
   123  	oprules := map[string][]Rule{}
   124  
   125  	// read rule file
   126  	scanner := MultiScannerFromReaders(readers)
   127  	rule := ""
   128  	var lineno int
   129  	var ruleLineno int // line number of "=>"
   130  	for scanner.Scan() {
   131  		lineno = scanner.Line()
   132  		line := scanner.Text()
   133  		if i := strings.Index(line, "//"); i >= 0 {
   134  			// Remove comments. Note that this isn't string safe, so
   135  			// it will truncate lines with // inside strings. Oh well.
   136  			line = line[:i]
   137  		}
   138  		rule += " " + line
   139  		rule = strings.TrimSpace(rule)
   140  		if rule == "" {
   141  			continue
   142  		}
   143  		if !strings.Contains(rule, "=>") {
   144  			continue
   145  		}
   146  		if ruleLineno == 0 {
   147  			ruleLineno = lineno
   148  		}
   149  		if strings.HasSuffix(rule, "=>") {
   150  			continue // continue on the next line
   151  		}
   152  		if n := balance(rule); n > 0 {
   153  			continue // open parentheses remain, continue on the next line
   154  		} else if n < 0 {
   155  			break // continuing the line can't help, and it will only make errors worse
   156  		}
   157  
   158  		loc := fmt.Sprintf("%s:%d", scanner.Name(), ruleLineno)
   159  		for _, rule2 := range expandOr(rule) {
   160  			r := Rule{Rule: rule2, Loc: loc}
   161  			if rawop := strings.Split(rule2, " ")[0][1:]; isBlock(rawop, arch) {
   162  				blockrules[rawop] = append(blockrules[rawop], r)
   163  				continue
   164  			}
   165  			// Do fancier value op matching.
   166  			match, _, _ := r.parse()
   167  			op, oparch, _, _, _, _ := parseValue(match, arch, loc)
   168  			opname := fmt.Sprintf("Op%s%s", oparch, op.name)
   169  			oprules[opname] = append(oprules[opname], r)
   170  		}
   171  		rule = ""
   172  		ruleLineno = 0
   173  	}
   174  	if err := scanner.Err(); err != nil {
   175  		log.Fatalf("scanner failed: %v\n", err)
   176  	}
   177  	if balance(rule) != 0 {
   178  		log.Fatalf("%s:%d: unbalanced rule: %v\n", scanner.Name(), lineno, rule)
   179  	}
   180  
   181  	// Order all the ops.
   182  	var ops []string
   183  	for op := range oprules {
   184  		ops = append(ops, op)
   185  	}
   186  	sort.Strings(ops)
   187  
   188  	genFile := &File{Arch: arch, Suffix: suff}
   189  	// Main rewrite routine is a switch on v.Op.
   190  	fn := &Func{Kind: "Value", ArgLen: -1}
   191  
   192  	sw := &Switch{Expr: exprf("v.Op")}
   193  	for _, op := range ops {
   194  		eop, ok := parseEllipsisRules(oprules[op], arch)
   195  		if ok {
   196  			if strings.Contains(oprules[op][0].Rule, "=>") && opByName(arch, op).aux != opByName(arch, eop).aux {
   197  				panic(fmt.Sprintf("can't use ... for ops that have different aux types: %s and %s", op, eop))
   198  			}
   199  			swc := &Case{Expr: exprf("%s", op)}
   200  			swc.add(stmtf("v.Op = %s", eop))
   201  			swc.add(stmtf("return true"))
   202  			sw.add(swc)
   203  			continue
   204  		}
   205  
   206  		swc := &Case{Expr: exprf("%s", op)}
   207  		swc.add(stmtf("return rewriteValue%s%s_%s(v)", arch.name, suff, op))
   208  		sw.add(swc)
   209  	}
   210  	if len(sw.List) > 0 { // skip if empty
   211  		fn.add(sw)
   212  	}
   213  	fn.add(stmtf("return false"))
   214  	genFile.add(fn)
   215  
   216  	// Generate a routine per op. Note that we don't make one giant routine
   217  	// because it is too big for some compilers.
   218  	for _, op := range ops {
   219  		rules := oprules[op]
   220  		_, ok := parseEllipsisRules(oprules[op], arch)
   221  		if ok {
   222  			continue
   223  		}
   224  
   225  		// rr is kept between iterations, so that each rule can check
   226  		// that the previous rule wasn't unconditional.
   227  		var rr *RuleRewrite
   228  		fn := &Func{
   229  			Kind:   "Value",
   230  			Suffix: fmt.Sprintf("_%s", op),
   231  			ArgLen: opByName(arch, op).argLength,
   232  		}
   233  		fn.add(declReserved("b", "v.Block"))
   234  		fn.add(declReserved("config", "b.Func.Config"))
   235  		fn.add(declReserved("fe", "b.Func.fe"))
   236  		fn.add(declReserved("typ", "&b.Func.Config.Types"))
   237  		for _, rule := range rules {
   238  			if rr != nil && !rr.CanFail {
   239  				log.Fatalf("unconditional rule %s is followed by other rules", rr.Match)
   240  			}
   241  			rr = &RuleRewrite{Loc: rule.Loc}
   242  			rr.Match, rr.Cond, rr.Result = rule.parse()
   243  			pos, _ := genMatch(rr, arch, rr.Match, fn.ArgLen >= 0)
   244  			if pos == "" {
   245  				pos = "v.Pos"
   246  			}
   247  			if rr.Cond != "" {
   248  				rr.add(breakf("!(%s)", rr.Cond))
   249  			}
   250  			genResult(rr, arch, rr.Result, pos)
   251  			if *genLog {
   252  				rr.add(stmtf("logRule(%q)", rule.Loc))
   253  			}
   254  			fn.add(rr)
   255  		}
   256  		if rr.CanFail {
   257  			fn.add(stmtf("return false"))
   258  		}
   259  		genFile.add(fn)
   260  	}
   261  
   262  	// Generate block rewrite function. There are only a few block types
   263  	// so we can make this one function with a switch.
   264  	fn = &Func{Kind: "Block"}
   265  	fn.add(declReserved("config", "b.Func.Config"))
   266  	fn.add(declReserved("typ", "&b.Func.Config.Types"))
   267  
   268  	sw = &Switch{Expr: exprf("b.Kind")}
   269  	ops = ops[:0]
   270  	for op := range blockrules {
   271  		ops = append(ops, op)
   272  	}
   273  	sort.Strings(ops)
   274  	for _, op := range ops {
   275  		name, data := getBlockInfo(op, arch)
   276  		swc := &Case{Expr: exprf("%s", name)}
   277  		for _, rule := range blockrules[op] {
   278  			swc.add(genBlockRewrite(rule, arch, data))
   279  		}
   280  		sw.add(swc)
   281  	}
   282  	if len(sw.List) > 0 { // skip if empty
   283  		fn.add(sw)
   284  	}
   285  	fn.add(stmtf("return false"))
   286  	genFile.add(fn)
   287  
   288  	// Remove unused imports and variables.
   289  	buf := new(bytes.Buffer)
   290  	fprint(buf, genFile)
   291  	fset := token.NewFileSet()
   292  	file, err := parser.ParseFile(fset, "", buf, parser.ParseComments)
   293  	if err != nil {
   294  		filename := fmt.Sprintf("%s_broken.go", arch.name)
   295  		if err := os.WriteFile(filename, buf.Bytes(), 0644); err != nil {
   296  			log.Printf("failed to dump broken code to %s: %v", filename, err)
   297  		} else {
   298  			log.Printf("dumped broken code to %s", filename)
   299  		}
   300  		log.Fatalf("failed to parse generated code for arch %s: %v", arch.name, err)
   301  	}
   302  	tfile := fset.File(file.Pos())
   303  
   304  	// First, use unusedInspector to find the unused declarations by their
   305  	// start position.
   306  	u := unusedInspector{unused: make(map[token.Pos]bool)}
   307  	u.node(file)
   308  
   309  	// Then, delete said nodes via astutil.Apply.
   310  	pre := func(c *astutil.Cursor) bool {
   311  		node := c.Node()
   312  		if node == nil {
   313  			return true
   314  		}
   315  		if u.unused[node.Pos()] {
   316  			c.Delete()
   317  			// Unused imports and declarations use exactly
   318  			// one line. Prevent leaving an empty line.
   319  			tfile.MergeLine(tfile.Position(node.Pos()).Line)
   320  			return false
   321  		}
   322  		return true
   323  	}
   324  	post := func(c *astutil.Cursor) bool {
   325  		switch node := c.Node().(type) {
   326  		case *ast.GenDecl:
   327  			if len(node.Specs) == 0 {
   328  				// Don't leave a broken or empty GenDecl behind,
   329  				// such as "import ()".
   330  				c.Delete()
   331  			}
   332  		}
   333  		return true
   334  	}
   335  	file = astutil.Apply(file, pre, post).(*ast.File)
   336  
   337  	// Write the well-formatted source to file
   338  	f, err := os.Create(outFile("rewrite" + arch.name + suff + ".go"))
   339  	if err != nil {
   340  		log.Fatalf("can't write output: %v", err)
   341  	}
   342  	defer f.Close()
   343  	// gofmt result; use a buffered writer, as otherwise go/format spends
   344  	// far too much time in syscalls.
   345  	bw := bufio.NewWriter(f)
   346  	if err := format.Node(bw, fset, file); err != nil {
   347  		log.Fatalf("can't format output: %v", err)
   348  	}
   349  	if err := bw.Flush(); err != nil {
   350  		log.Fatalf("can't write output: %v", err)
   351  	}
   352  	if err := f.Close(); err != nil {
   353  		log.Fatalf("can't write output: %v", err)
   354  	}
   355  }
   356  
   357  // unusedInspector can be used to detect unused variables and imports in an
   358  // ast.Node via its node method. The result is available in the "unused" map.
   359  //
   360  // note that unusedInspector is lazy and best-effort; it only supports the node
   361  // types and patterns used by the rulegen program.
   362  type unusedInspector struct {
   363  	// scope is the current scope, which can never be nil when a declaration
   364  	// is encountered. That is, the unusedInspector.node entrypoint should
   365  	// generally be an entire file or block.
   366  	scope *scope
   367  
   368  	// unused is the resulting set of unused declared names, indexed by the
   369  	// starting position of the node that declared the name.
   370  	unused map[token.Pos]bool
   371  
   372  	// defining is the object currently being defined; this is useful so
   373  	// that if "foo := bar" is unused and removed, we can then detect if
   374  	// "bar" becomes unused as well.
   375  	defining *object
   376  }
   377  
   378  // scoped opens a new scope when called, and returns a function which closes
   379  // that same scope. When a scope is closed, unused variables are recorded.
   380  func (u *unusedInspector) scoped() func() {
   381  	outer := u.scope
   382  	u.scope = &scope{outer: outer, objects: map[string]*object{}}
   383  	return func() {
   384  		for anyUnused := true; anyUnused; {
   385  			anyUnused = false
   386  			for _, obj := range u.scope.objects {
   387  				if obj.numUses > 0 {
   388  					continue
   389  				}
   390  				u.unused[obj.pos] = true
   391  				for _, used := range obj.used {
   392  					if used.numUses--; used.numUses == 0 {
   393  						anyUnused = true
   394  					}
   395  				}
   396  				// We've decremented numUses for each of the
   397  				// objects in used. Zero this slice too, to keep
   398  				// everything consistent.
   399  				obj.used = nil
   400  			}
   401  		}
   402  		u.scope = outer
   403  	}
   404  }
   405  
   406  func (u *unusedInspector) exprs(list []ast.Expr) {
   407  	for _, x := range list {
   408  		u.node(x)
   409  	}
   410  }
   411  
   412  func (u *unusedInspector) node(node ast.Node) {
   413  	switch node := node.(type) {
   414  	case *ast.File:
   415  		defer u.scoped()()
   416  		for _, decl := range node.Decls {
   417  			u.node(decl)
   418  		}
   419  	case *ast.GenDecl:
   420  		for _, spec := range node.Specs {
   421  			u.node(spec)
   422  		}
   423  	case *ast.ImportSpec:
   424  		impPath, _ := strconv.Unquote(node.Path.Value)
   425  		name := path.Base(impPath)
   426  		u.scope.objects[name] = &object{
   427  			name: name,
   428  			pos:  node.Pos(),
   429  		}
   430  	case *ast.FuncDecl:
   431  		u.node(node.Type)
   432  		if node.Body != nil {
   433  			u.node(node.Body)
   434  		}
   435  	case *ast.FuncType:
   436  		if node.Params != nil {
   437  			u.node(node.Params)
   438  		}
   439  		if node.Results != nil {
   440  			u.node(node.Results)
   441  		}
   442  	case *ast.FieldList:
   443  		for _, field := range node.List {
   444  			u.node(field)
   445  		}
   446  	case *ast.Field:
   447  		u.node(node.Type)
   448  
   449  	// statements
   450  
   451  	case *ast.BlockStmt:
   452  		defer u.scoped()()
   453  		for _, stmt := range node.List {
   454  			u.node(stmt)
   455  		}
   456  	case *ast.DeclStmt:
   457  		u.node(node.Decl)
   458  	case *ast.IfStmt:
   459  		if node.Init != nil {
   460  			u.node(node.Init)
   461  		}
   462  		u.node(node.Cond)
   463  		u.node(node.Body)
   464  		if node.Else != nil {
   465  			u.node(node.Else)
   466  		}
   467  	case *ast.ForStmt:
   468  		if node.Init != nil {
   469  			u.node(node.Init)
   470  		}
   471  		if node.Cond != nil {
   472  			u.node(node.Cond)
   473  		}
   474  		if node.Post != nil {
   475  			u.node(node.Post)
   476  		}
   477  		u.node(node.Body)
   478  	case *ast.SwitchStmt:
   479  		if node.Init != nil {
   480  			u.node(node.Init)
   481  		}
   482  		if node.Tag != nil {
   483  			u.node(node.Tag)
   484  		}
   485  		u.node(node.Body)
   486  	case *ast.CaseClause:
   487  		u.exprs(node.List)
   488  		defer u.scoped()()
   489  		for _, stmt := range node.Body {
   490  			u.node(stmt)
   491  		}
   492  	case *ast.BranchStmt:
   493  	case *ast.ExprStmt:
   494  		u.node(node.X)
   495  	case *ast.AssignStmt:
   496  		if node.Tok != token.DEFINE {
   497  			u.exprs(node.Rhs)
   498  			u.exprs(node.Lhs)
   499  			break
   500  		}
   501  		lhs := node.Lhs
   502  		if len(lhs) == 2 && lhs[1].(*ast.Ident).Name == "_" {
   503  			lhs = lhs[:1]
   504  		}
   505  		if len(lhs) != 1 {
   506  			panic("no support for := with multiple names")
   507  		}
   508  
   509  		name := lhs[0].(*ast.Ident)
   510  		obj := &object{
   511  			name: name.Name,
   512  			pos:  name.NamePos,
   513  		}
   514  
   515  		old := u.defining
   516  		u.defining = obj
   517  		u.exprs(node.Rhs)
   518  		u.defining = old
   519  
   520  		u.scope.objects[name.Name] = obj
   521  	case *ast.ReturnStmt:
   522  		u.exprs(node.Results)
   523  	case *ast.IncDecStmt:
   524  		u.node(node.X)
   525  
   526  	// expressions
   527  
   528  	case *ast.CallExpr:
   529  		u.node(node.Fun)
   530  		u.exprs(node.Args)
   531  	case *ast.SelectorExpr:
   532  		u.node(node.X)
   533  	case *ast.UnaryExpr:
   534  		u.node(node.X)
   535  	case *ast.BinaryExpr:
   536  		u.node(node.X)
   537  		u.node(node.Y)
   538  	case *ast.StarExpr:
   539  		u.node(node.X)
   540  	case *ast.ParenExpr:
   541  		u.node(node.X)
   542  	case *ast.IndexExpr:
   543  		u.node(node.X)
   544  		u.node(node.Index)
   545  	case *ast.TypeAssertExpr:
   546  		u.node(node.X)
   547  		u.node(node.Type)
   548  	case *ast.Ident:
   549  		if obj := u.scope.Lookup(node.Name); obj != nil {
   550  			obj.numUses++
   551  			if u.defining != nil {
   552  				u.defining.used = append(u.defining.used, obj)
   553  			}
   554  		}
   555  	case *ast.BasicLit:
   556  	case *ast.CompositeLit:
   557  		for _, e := range node.Elts {
   558  			u.node(e)
   559  		}
   560  	case *ast.KeyValueExpr:
   561  		u.node(node.Key)
   562  		u.node(node.Value)
   563  	case *ast.ValueSpec:
   564  		u.exprs(node.Values)
   565  	default:
   566  		panic(fmt.Sprintf("unhandled node: %T", node))
   567  	}
   568  }
   569  
   570  // scope keeps track of a certain scope and its declared names, as well as the
   571  // outer (parent) scope.
   572  type scope struct {
   573  	outer   *scope             // can be nil, if this is the top-level scope
   574  	objects map[string]*object // indexed by each declared name
   575  }
   576  
   577  func (s *scope) Lookup(name string) *object {
   578  	if obj := s.objects[name]; obj != nil {
   579  		return obj
   580  	}
   581  	if s.outer == nil {
   582  		return nil
   583  	}
   584  	return s.outer.Lookup(name)
   585  }
   586  
   587  // object keeps track of a declared name, such as a variable or import.
   588  type object struct {
   589  	name string
   590  	pos  token.Pos // start position of the node declaring the object
   591  
   592  	numUses int       // number of times this object is used
   593  	used    []*object // objects that its declaration makes use of
   594  }
   595  
   596  func fprint(w io.Writer, n Node) {
   597  	switch n := n.(type) {
   598  	case *File:
   599  		file := n
   600  		seenRewrite := make(map[[3]string]string)
   601  		fmt.Fprintf(w, "// Code generated from _gen/%s%s.rules using 'go generate'; DO NOT EDIT.\n", n.Arch.name, n.Suffix)
   602  		fmt.Fprintf(w, "\npackage ssa\n")
   603  		for _, path := range append([]string{
   604  			"fmt",
   605  			"internal/buildcfg",
   606  			"math",
   607  			"math/bits",
   608  			"cmd/internal/obj",
   609  			"cmd/compile/internal/base",
   610  			"cmd/compile/internal/types",
   611  			"cmd/compile/internal/ir",
   612  		}, n.Arch.imports...) {
   613  			fmt.Fprintf(w, "import %q\n", path)
   614  		}
   615  		for _, f := range n.List {
   616  			f := f.(*Func)
   617  			fmt.Fprintf(w, "func rewrite%s%s%s%s(", f.Kind, n.Arch.name, n.Suffix, f.Suffix)
   618  			fmt.Fprintf(w, "%c *%s) bool {\n", strings.ToLower(f.Kind)[0], f.Kind)
   619  			if f.Kind == "Value" && f.ArgLen > 0 {
   620  				for i := f.ArgLen - 1; i >= 0; i-- {
   621  					fmt.Fprintf(w, "v_%d := v.Args[%d]\n", i, i)
   622  				}
   623  			}
   624  			for _, n := range f.List {
   625  				fprint(w, n)
   626  
   627  				if rr, ok := n.(*RuleRewrite); ok {
   628  					k := [3]string{
   629  						normalizeMatch(rr.Match, file.Arch),
   630  						normalizeWhitespace(rr.Cond),
   631  						normalizeWhitespace(rr.Result),
   632  					}
   633  					if prev, ok := seenRewrite[k]; ok {
   634  						log.Fatalf("duplicate rule %s, previously seen at %s\n", rr.Loc, prev)
   635  					}
   636  					seenRewrite[k] = rr.Loc
   637  				}
   638  			}
   639  			fmt.Fprintf(w, "}\n")
   640  		}
   641  	case *Switch:
   642  		fmt.Fprintf(w, "switch ")
   643  		fprint(w, n.Expr)
   644  		fmt.Fprintf(w, " {\n")
   645  		for _, n := range n.List {
   646  			fprint(w, n)
   647  		}
   648  		fmt.Fprintf(w, "}\n")
   649  	case *Case:
   650  		fmt.Fprintf(w, "case ")
   651  		fprint(w, n.Expr)
   652  		fmt.Fprintf(w, ":\n")
   653  		for _, n := range n.List {
   654  			fprint(w, n)
   655  		}
   656  	case *RuleRewrite:
   657  		if *addLine {
   658  			fmt.Fprintf(w, "// %s\n", n.Loc)
   659  		}
   660  		fmt.Fprintf(w, "// match: %s\n", n.Match)
   661  		if n.Cond != "" {
   662  			fmt.Fprintf(w, "// cond: %s\n", n.Cond)
   663  		}
   664  		fmt.Fprintf(w, "// result: %s\n", n.Result)
   665  		fmt.Fprintf(w, "for %s {\n", n.Check)
   666  		nCommutative := 0
   667  		for _, n := range n.List {
   668  			if b, ok := n.(*CondBreak); ok {
   669  				b.InsideCommuteLoop = nCommutative > 0
   670  			}
   671  			fprint(w, n)
   672  			if loop, ok := n.(StartCommuteLoop); ok {
   673  				if nCommutative != loop.Depth {
   674  					panic("mismatch commute loop depth")
   675  				}
   676  				nCommutative++
   677  			}
   678  		}
   679  		fmt.Fprintf(w, "return true\n")
   680  		for i := 0; i < nCommutative; i++ {
   681  			fmt.Fprintln(w, "}")
   682  		}
   683  		if n.CommuteDepth > 0 && n.CanFail {
   684  			fmt.Fprint(w, "break\n")
   685  		}
   686  		fmt.Fprintf(w, "}\n")
   687  	case *Declare:
   688  		fmt.Fprintf(w, "%s := ", n.Name)
   689  		fprint(w, n.Value)
   690  		fmt.Fprintln(w)
   691  	case *CondBreak:
   692  		fmt.Fprintf(w, "if ")
   693  		fprint(w, n.Cond)
   694  		fmt.Fprintf(w, " {\n")
   695  		if n.InsideCommuteLoop {
   696  			fmt.Fprintf(w, "continue")
   697  		} else {
   698  			fmt.Fprintf(w, "break")
   699  		}
   700  		fmt.Fprintf(w, "\n}\n")
   701  	case ast.Node:
   702  		printConfig.Fprint(w, emptyFset, n)
   703  		if _, ok := n.(ast.Stmt); ok {
   704  			fmt.Fprintln(w)
   705  		}
   706  	case StartCommuteLoop:
   707  		fmt.Fprintf(w, "for _i%[1]d := 0; _i%[1]d <= 1; _i%[1]d, %[2]s_0, %[2]s_1 = _i%[1]d + 1, %[2]s_1, %[2]s_0 {\n", n.Depth, n.V)
   708  	default:
   709  		log.Fatalf("cannot print %T", n)
   710  	}
   711  }
   712  
   713  var printConfig = printer.Config{
   714  	Mode: printer.RawFormat, // we use go/format later, so skip work here
   715  }
   716  
   717  var emptyFset = token.NewFileSet()
   718  
   719  // Node can be a Statement or an ast.Expr.
   720  type Node interface{}
   721  
   722  // Statement can be one of our high-level statement struct types, or an
   723  // ast.Stmt under some limited circumstances.
   724  type Statement interface{}
   725  
   726  // BodyBase is shared by all of our statement pseudo-node types which can
   727  // contain other statements.
   728  type BodyBase struct {
   729  	List    []Statement
   730  	CanFail bool
   731  }
   732  
   733  func (w *BodyBase) add(node Statement) {
   734  	var last Statement
   735  	if len(w.List) > 0 {
   736  		last = w.List[len(w.List)-1]
   737  	}
   738  	if node, ok := node.(*CondBreak); ok {
   739  		w.CanFail = true
   740  		if last, ok := last.(*CondBreak); ok {
   741  			// Add to the previous "if <cond> { break }" via a
   742  			// logical OR, which will save verbosity.
   743  			last.Cond = &ast.BinaryExpr{
   744  				Op: token.LOR,
   745  				X:  last.Cond,
   746  				Y:  node.Cond,
   747  			}
   748  			return
   749  		}
   750  	}
   751  
   752  	w.List = append(w.List, node)
   753  }
   754  
   755  // predeclared contains globally known tokens that should not be redefined.
   756  var predeclared = map[string]bool{
   757  	"nil":   true,
   758  	"false": true,
   759  	"true":  true,
   760  }
   761  
   762  // declared reports if the body contains a Declare with the given name.
   763  func (w *BodyBase) declared(name string) bool {
   764  	if predeclared[name] {
   765  		// Treat predeclared names as having already been declared.
   766  		// This lets us use nil to match an aux field or
   767  		// true and false to match an auxint field.
   768  		return true
   769  	}
   770  	for _, s := range w.List {
   771  		if decl, ok := s.(*Declare); ok && decl.Name == name {
   772  			return true
   773  		}
   774  	}
   775  	return false
   776  }
   777  
   778  // These types define some high-level statement struct types, which can be used
   779  // as a Statement. This allows us to keep some node structs simpler, and have
   780  // higher-level nodes such as an entire rule rewrite.
   781  //
   782  // Note that ast.Expr is always used as-is; we don't declare our own expression
   783  // nodes.
   784  type (
   785  	File struct {
   786  		BodyBase // []*Func
   787  		Arch     arch
   788  		Suffix   string
   789  	}
   790  	Func struct {
   791  		BodyBase
   792  		Kind   string // "Value" or "Block"
   793  		Suffix string
   794  		ArgLen int32 // if kind == "Value", number of args for this op
   795  	}
   796  	Switch struct {
   797  		BodyBase // []*Case
   798  		Expr     ast.Expr
   799  	}
   800  	Case struct {
   801  		BodyBase
   802  		Expr ast.Expr
   803  	}
   804  	RuleRewrite struct {
   805  		BodyBase
   806  		Match, Cond, Result string // top comments
   807  		Check               string // top-level boolean expression
   808  
   809  		Alloc        int    // for unique var names
   810  		Loc          string // file name & line number of the original rule
   811  		CommuteDepth int    // used to track depth of commute loops
   812  	}
   813  	Declare struct {
   814  		Name  string
   815  		Value ast.Expr
   816  	}
   817  	CondBreak struct {
   818  		Cond              ast.Expr
   819  		InsideCommuteLoop bool
   820  	}
   821  	StartCommuteLoop struct {
   822  		Depth int
   823  		V     string
   824  	}
   825  )
   826  
   827  // exprf parses a Go expression generated from fmt.Sprintf, panicking if an
   828  // error occurs.
   829  func exprf(format string, a ...interface{}) ast.Expr {
   830  	src := fmt.Sprintf(format, a...)
   831  	expr, err := parser.ParseExpr(src)
   832  	if err != nil {
   833  		log.Fatalf("expr parse error on %q: %v", src, err)
   834  	}
   835  	return expr
   836  }
   837  
   838  // stmtf parses a Go statement generated from fmt.Sprintf. This function is only
   839  // meant for simple statements that don't have a custom Statement node declared
   840  // in this package, such as ast.ReturnStmt or ast.ExprStmt.
   841  func stmtf(format string, a ...interface{}) Statement {
   842  	src := fmt.Sprintf(format, a...)
   843  	fsrc := "package p\nfunc _() {\n" + src + "\n}\n"
   844  	file, err := parser.ParseFile(token.NewFileSet(), "", fsrc, 0)
   845  	if err != nil {
   846  		log.Fatalf("stmt parse error on %q: %v", src, err)
   847  	}
   848  	return file.Decls[0].(*ast.FuncDecl).Body.List[0]
   849  }
   850  
   851  var reservedNames = map[string]bool{
   852  	"v":      true, // Values[i], etc
   853  	"b":      true, // v.Block
   854  	"config": true, // b.Func.Config
   855  	"fe":     true, // b.Func.fe
   856  	"typ":    true, // &b.Func.Config.Types
   857  }
   858  
   859  // declf constructs a simple "name := value" declaration,
   860  // using exprf for its value.
   861  //
   862  // name must not be one of reservedNames.
   863  // This helps prevent unintended shadowing and name clashes.
   864  // To declare a reserved name, use declReserved.
   865  func declf(loc, name, format string, a ...interface{}) *Declare {
   866  	if reservedNames[name] {
   867  		log.Fatalf("rule %s uses the reserved name %s", loc, name)
   868  	}
   869  	return &Declare{name, exprf(format, a...)}
   870  }
   871  
   872  // declReserved is like declf, but the name must be one of reservedNames.
   873  // Calls to declReserved should generally be static and top-level.
   874  func declReserved(name, value string) *Declare {
   875  	if !reservedNames[name] {
   876  		panic(fmt.Sprintf("declReserved call does not use a reserved name: %q", name))
   877  	}
   878  	return &Declare{name, exprf("%s", value)}
   879  }
   880  
   881  // breakf constructs a simple "if cond { break }" statement, using exprf for its
   882  // condition.
   883  func breakf(format string, a ...interface{}) *CondBreak {
   884  	return &CondBreak{Cond: exprf(format, a...)}
   885  }
   886  
   887  func genBlockRewrite(rule Rule, arch arch, data blockData) *RuleRewrite {
   888  	rr := &RuleRewrite{Loc: rule.Loc}
   889  	rr.Match, rr.Cond, rr.Result = rule.parse()
   890  	_, _, auxint, aux, s := extract(rr.Match) // remove parens, then split
   891  
   892  	// check match of control values
   893  	if len(s) < data.controls {
   894  		log.Fatalf("incorrect number of arguments in %s, got %v wanted at least %v", rule, len(s), data.controls)
   895  	}
   896  	controls := s[:data.controls]
   897  	pos := make([]string, data.controls)
   898  	for i, arg := range controls {
   899  		cname := fmt.Sprintf("b.Controls[%v]", i)
   900  		if strings.Contains(arg, "(") {
   901  			vname, expr := splitNameExpr(arg)
   902  			if vname == "" {
   903  				vname = fmt.Sprintf("v_%v", i)
   904  			}
   905  			rr.add(declf(rr.Loc, vname, "%s", cname))
   906  			p, op := genMatch0(rr, arch, expr, vname, nil, false) // TODO: pass non-nil cnt?
   907  			if op != "" {
   908  				check := fmt.Sprintf("%s.Op == %s", cname, op)
   909  				if rr.Check == "" {
   910  					rr.Check = check
   911  				} else {
   912  					rr.Check += " && " + check
   913  				}
   914  			}
   915  			if p == "" {
   916  				p = vname + ".Pos"
   917  			}
   918  			pos[i] = p
   919  		} else {
   920  			rr.add(declf(rr.Loc, arg, "%s", cname))
   921  			pos[i] = arg + ".Pos"
   922  		}
   923  	}
   924  	for _, e := range []struct {
   925  		name, field, dclType string
   926  	}{
   927  		{auxint, "AuxInt", data.auxIntType()},
   928  		{aux, "Aux", data.auxType()},
   929  	} {
   930  		if e.name == "" {
   931  			continue
   932  		}
   933  
   934  		if e.dclType == "" {
   935  			log.Fatalf("op %s has no declared type for %s", data.name, e.field)
   936  		}
   937  		if !token.IsIdentifier(e.name) || rr.declared(e.name) {
   938  			rr.add(breakf("%sTo%s(b.%s) != %s", unTitle(e.field), title(e.dclType), e.field, e.name))
   939  		} else {
   940  			rr.add(declf(rr.Loc, e.name, "%sTo%s(b.%s)", unTitle(e.field), title(e.dclType), e.field))
   941  		}
   942  	}
   943  	if rr.Cond != "" {
   944  		rr.add(breakf("!(%s)", rr.Cond))
   945  	}
   946  
   947  	// Rule matches. Generate result.
   948  	outop, _, auxint, aux, t := extract(rr.Result) // remove parens, then split
   949  	blockName, outdata := getBlockInfo(outop, arch)
   950  	if len(t) < outdata.controls {
   951  		log.Fatalf("incorrect number of output arguments in %s, got %v wanted at least %v", rule, len(s), outdata.controls)
   952  	}
   953  
   954  	// Check if newsuccs is the same set as succs.
   955  	succs := s[data.controls:]
   956  	newsuccs := t[outdata.controls:]
   957  	m := map[string]bool{}
   958  	for _, succ := range succs {
   959  		if m[succ] {
   960  			log.Fatalf("can't have a repeat successor name %s in %s", succ, rule)
   961  		}
   962  		m[succ] = true
   963  	}
   964  	for _, succ := range newsuccs {
   965  		if !m[succ] {
   966  			log.Fatalf("unknown successor %s in %s", succ, rule)
   967  		}
   968  		delete(m, succ)
   969  	}
   970  	if len(m) != 0 {
   971  		log.Fatalf("unmatched successors %v in %s", m, rule)
   972  	}
   973  
   974  	var genControls [2]string
   975  	for i, control := range t[:outdata.controls] {
   976  		// Select a source position for any new control values.
   977  		// TODO: does it always make sense to use the source position
   978  		// of the original control values or should we be using the
   979  		// block's source position in some cases?
   980  		newpos := "b.Pos" // default to block's source position
   981  		if i < len(pos) && pos[i] != "" {
   982  			// Use the previous control value's source position.
   983  			newpos = pos[i]
   984  		}
   985  
   986  		// Generate a new control value (or copy an existing value).
   987  		genControls[i] = genResult0(rr, arch, control, false, false, newpos, nil)
   988  	}
   989  	switch outdata.controls {
   990  	case 0:
   991  		rr.add(stmtf("b.Reset(%s)", blockName))
   992  	case 1:
   993  		rr.add(stmtf("b.resetWithControl(%s, %s)", blockName, genControls[0]))
   994  	case 2:
   995  		rr.add(stmtf("b.resetWithControl2(%s, %s, %s)", blockName, genControls[0], genControls[1]))
   996  	default:
   997  		log.Fatalf("too many controls: %d", outdata.controls)
   998  	}
   999  
  1000  	if auxint != "" {
  1001  		// Make sure auxint value has the right type.
  1002  		rr.add(stmtf("b.AuxInt = %sToAuxInt(%s)", unTitle(outdata.auxIntType()), auxint))
  1003  	}
  1004  	if aux != "" {
  1005  		// Make sure aux value has the right type.
  1006  		rr.add(stmtf("b.Aux = %sToAux(%s)", unTitle(outdata.auxType()), aux))
  1007  	}
  1008  
  1009  	succChanged := false
  1010  	for i := 0; i < len(succs); i++ {
  1011  		if succs[i] != newsuccs[i] {
  1012  			succChanged = true
  1013  		}
  1014  	}
  1015  	if succChanged {
  1016  		if len(succs) != 2 {
  1017  			log.Fatalf("changed successors, len!=2 in %s", rule)
  1018  		}
  1019  		if succs[0] != newsuccs[1] || succs[1] != newsuccs[0] {
  1020  			log.Fatalf("can only handle swapped successors in %s", rule)
  1021  		}
  1022  		rr.add(stmtf("b.swapSuccessors()"))
  1023  	}
  1024  
  1025  	if *genLog {
  1026  		rr.add(stmtf("logRule(%q)", rule.Loc))
  1027  	}
  1028  	return rr
  1029  }
  1030  
  1031  // genMatch returns the variable whose source position should be used for the
  1032  // result (or "" if no opinion), and a boolean that reports whether the match can fail.
  1033  func genMatch(rr *RuleRewrite, arch arch, match string, pregenTop bool) (pos, checkOp string) {
  1034  	cnt := varCount(rr)
  1035  	return genMatch0(rr, arch, match, "v", cnt, pregenTop)
  1036  }
  1037  
  1038  func genMatch0(rr *RuleRewrite, arch arch, match, v string, cnt map[string]int, pregenTop bool) (pos, checkOp string) {
  1039  	if match[0] != '(' || match[len(match)-1] != ')' {
  1040  		log.Fatalf("%s: non-compound expr in genMatch0: %q", rr.Loc, match)
  1041  	}
  1042  	op, oparch, typ, auxint, aux, args := parseValue(match, arch, rr.Loc)
  1043  
  1044  	checkOp = fmt.Sprintf("Op%s%s", oparch, op.name)
  1045  
  1046  	if op.faultOnNilArg0 || op.faultOnNilArg1 {
  1047  		// Prefer the position of an instruction which could fault.
  1048  		pos = v + ".Pos"
  1049  	}
  1050  
  1051  	// If the last argument is ___, it means "don't care about trailing arguments, really"
  1052  	// The likely/intended use is for rewrites that are too tricky to express in the existing pattern language
  1053  	// Do a length check early because long patterns fed short (ultimately not-matching) inputs will
  1054  	// do an indexing error in pattern-matching.
  1055  	if op.argLength == -1 {
  1056  		l := len(args)
  1057  		if l == 0 || args[l-1] != "___" {
  1058  			rr.add(breakf("len(%s.Args) != %d", v, l))
  1059  		} else if l > 1 && args[l-1] == "___" {
  1060  			rr.add(breakf("len(%s.Args) < %d", v, l-1))
  1061  		}
  1062  	}
  1063  
  1064  	for _, e := range []struct {
  1065  		name, field, dclType string
  1066  	}{
  1067  		{typ, "Type", "*types.Type"},
  1068  		{auxint, "AuxInt", op.auxIntType()},
  1069  		{aux, "Aux", op.auxType()},
  1070  	} {
  1071  		if e.name == "" {
  1072  			continue
  1073  		}
  1074  
  1075  		if e.dclType == "" {
  1076  			log.Fatalf("op %s has no declared type for %s", op.name, e.field)
  1077  		}
  1078  		if !token.IsIdentifier(e.name) || rr.declared(e.name) {
  1079  			switch e.field {
  1080  			case "Aux":
  1081  				rr.add(breakf("auxTo%s(%s.%s) != %s", title(e.dclType), v, e.field, e.name))
  1082  			case "AuxInt":
  1083  				rr.add(breakf("auxIntTo%s(%s.%s) != %s", title(e.dclType), v, e.field, e.name))
  1084  			case "Type":
  1085  				rr.add(breakf("%s.%s != %s", v, e.field, e.name))
  1086  			}
  1087  		} else {
  1088  			switch e.field {
  1089  			case "Aux":
  1090  				rr.add(declf(rr.Loc, e.name, "auxTo%s(%s.%s)", title(e.dclType), v, e.field))
  1091  			case "AuxInt":
  1092  				rr.add(declf(rr.Loc, e.name, "auxIntTo%s(%s.%s)", title(e.dclType), v, e.field))
  1093  			case "Type":
  1094  				rr.add(declf(rr.Loc, e.name, "%s.%s", v, e.field))
  1095  			}
  1096  		}
  1097  	}
  1098  
  1099  	commutative := op.commutative
  1100  	if commutative {
  1101  		if args[0] == args[1] {
  1102  			// When we have (Add x x), for any x,
  1103  			// even if there are other uses of x besides these two,
  1104  			// and even if x is not a variable,
  1105  			// we can skip the commutative match.
  1106  			commutative = false
  1107  		}
  1108  		if cnt[args[0]] == 1 && cnt[args[1]] == 1 {
  1109  			// When we have (Add x y) with no other uses
  1110  			// of x and y in the matching rule and condition,
  1111  			// then we can skip the commutative match (Add y x).
  1112  			commutative = false
  1113  		}
  1114  	}
  1115  
  1116  	if !pregenTop {
  1117  		// Access last argument first to minimize bounds checks.
  1118  		for n := len(args) - 1; n > 0; n-- {
  1119  			a := args[n]
  1120  			if a == "_" {
  1121  				continue
  1122  			}
  1123  			if !rr.declared(a) && token.IsIdentifier(a) && !(commutative && len(args) == 2) {
  1124  				rr.add(declf(rr.Loc, a, "%s.Args[%d]", v, n))
  1125  				// delete the last argument so it is not reprocessed
  1126  				args = args[:n]
  1127  			} else {
  1128  				rr.add(stmtf("_ = %s.Args[%d]", v, n))
  1129  			}
  1130  			break
  1131  		}
  1132  	}
  1133  	if commutative && !pregenTop {
  1134  		for i := 0; i <= 1; i++ {
  1135  			vname := fmt.Sprintf("%s_%d", v, i)
  1136  			rr.add(declf(rr.Loc, vname, "%s.Args[%d]", v, i))
  1137  		}
  1138  	}
  1139  	if commutative {
  1140  		rr.add(StartCommuteLoop{rr.CommuteDepth, v})
  1141  		rr.CommuteDepth++
  1142  	}
  1143  	for i, arg := range args {
  1144  		if arg == "_" {
  1145  			continue
  1146  		}
  1147  		var rhs string
  1148  		if (commutative && i < 2) || pregenTop {
  1149  			rhs = fmt.Sprintf("%s_%d", v, i)
  1150  		} else {
  1151  			rhs = fmt.Sprintf("%s.Args[%d]", v, i)
  1152  		}
  1153  		if !strings.Contains(arg, "(") {
  1154  			// leaf variable
  1155  			if rr.declared(arg) {
  1156  				// variable already has a definition. Check whether
  1157  				// the old definition and the new definition match.
  1158  				// For example, (add x x).  Equality is just pointer equality
  1159  				// on Values (so cse is important to do before lowering).
  1160  				rr.add(breakf("%s != %s", arg, rhs))
  1161  			} else {
  1162  				if arg != rhs {
  1163  					rr.add(declf(rr.Loc, arg, "%s", rhs))
  1164  				}
  1165  			}
  1166  			continue
  1167  		}
  1168  		// compound sexpr
  1169  		argname, expr := splitNameExpr(arg)
  1170  		if argname == "" {
  1171  			argname = fmt.Sprintf("%s_%d", v, i)
  1172  		}
  1173  		if argname == "b" {
  1174  			log.Fatalf("don't name args 'b', it is ambiguous with blocks")
  1175  		}
  1176  
  1177  		if argname != rhs {
  1178  			rr.add(declf(rr.Loc, argname, "%s", rhs))
  1179  		}
  1180  		bexpr := exprf("%s.Op != addLater", argname)
  1181  		rr.add(&CondBreak{Cond: bexpr})
  1182  		argPos, argCheckOp := genMatch0(rr, arch, expr, argname, cnt, false)
  1183  		bexpr.(*ast.BinaryExpr).Y.(*ast.Ident).Name = argCheckOp
  1184  
  1185  		if argPos != "" {
  1186  			// Keep the argument in preference to the parent, as the
  1187  			// argument is normally earlier in program flow.
  1188  			// Keep the argument in preference to an earlier argument,
  1189  			// as that prefers the memory argument which is also earlier
  1190  			// in the program flow.
  1191  			pos = argPos
  1192  		}
  1193  	}
  1194  
  1195  	return pos, checkOp
  1196  }
  1197  
  1198  func genResult(rr *RuleRewrite, arch arch, result, pos string) {
  1199  	move := result[0] == '@'
  1200  	if move {
  1201  		// parse @block directive
  1202  		s := strings.SplitN(result[1:], " ", 2)
  1203  		rr.add(stmtf("b = %s", s[0]))
  1204  		result = s[1]
  1205  	}
  1206  	if result[0] == '{' {
  1207  		// Arbitrary code used to make the result
  1208  		rr.add(stmtf("v.copyOf(%s)", result[1:len(result)-1]))
  1209  		return
  1210  	}
  1211  	cse := make(map[string]string)
  1212  	genResult0(rr, arch, result, true, move, pos, cse)
  1213  }
  1214  
  1215  func genResult0(rr *RuleRewrite, arch arch, result string, top, move bool, pos string, cse map[string]string) string {
  1216  	resname, expr := splitNameExpr(result)
  1217  	result = expr
  1218  	// TODO: when generating a constant result, use f.constVal to avoid
  1219  	// introducing copies just to clean them up again.
  1220  	if result[0] != '(' {
  1221  		// variable
  1222  		if top {
  1223  			// It in not safe in general to move a variable between blocks
  1224  			// (and particularly not a phi node).
  1225  			// Introduce a copy.
  1226  			rr.add(stmtf("v.copyOf(%s)", result))
  1227  		}
  1228  		return result
  1229  	}
  1230  
  1231  	w := normalizeWhitespace(result)
  1232  	if prev := cse[w]; prev != "" {
  1233  		return prev
  1234  	}
  1235  
  1236  	op, oparch, typ, auxint, aux, args := parseValue(result, arch, rr.Loc)
  1237  
  1238  	// Find the type of the variable.
  1239  	typeOverride := typ != ""
  1240  	if typ == "" && op.typ != "" {
  1241  		typ = typeName(op.typ)
  1242  	}
  1243  
  1244  	v := "v"
  1245  	if top && !move {
  1246  		rr.add(stmtf("v.reset(Op%s%s)", oparch, op.name))
  1247  		if typeOverride {
  1248  			rr.add(stmtf("v.Type = %s", typ))
  1249  		}
  1250  	} else {
  1251  		if typ == "" {
  1252  			log.Fatalf("sub-expression %s (op=Op%s%s) at %s must have a type", result, oparch, op.name, rr.Loc)
  1253  		}
  1254  		if resname == "" {
  1255  			v = fmt.Sprintf("v%d", rr.Alloc)
  1256  		} else {
  1257  			v = resname
  1258  		}
  1259  		rr.Alloc++
  1260  		rr.add(declf(rr.Loc, v, "b.NewValue0(%s, Op%s%s, %s)", pos, oparch, op.name, typ))
  1261  		if move && top {
  1262  			// Rewrite original into a copy
  1263  			rr.add(stmtf("v.copyOf(%s)", v))
  1264  		}
  1265  	}
  1266  
  1267  	if auxint != "" {
  1268  		// Make sure auxint value has the right type.
  1269  		rr.add(stmtf("%s.AuxInt = %sToAuxInt(%s)", v, unTitle(op.auxIntType()), auxint))
  1270  	}
  1271  	if aux != "" {
  1272  		// Make sure aux value has the right type.
  1273  		rr.add(stmtf("%s.Aux = %sToAux(%s)", v, unTitle(op.auxType()), aux))
  1274  	}
  1275  	all := new(strings.Builder)
  1276  	for i, arg := range args {
  1277  		x := genResult0(rr, arch, arg, false, move, pos, cse)
  1278  		if i > 0 {
  1279  			all.WriteString(", ")
  1280  		}
  1281  		all.WriteString(x)
  1282  	}
  1283  	switch len(args) {
  1284  	case 0:
  1285  	case 1:
  1286  		rr.add(stmtf("%s.AddArg(%s)", v, all.String()))
  1287  	case 2, 3, 4, 5, 6:
  1288  		rr.add(stmtf("%s.AddArg%d(%s)", v, len(args), all.String()))
  1289  	default:
  1290  		rr.add(stmtf("%s.AddArgs(%s)", v, all.String()))
  1291  	}
  1292  
  1293  	if cse != nil {
  1294  		cse[w] = v
  1295  	}
  1296  	return v
  1297  }
  1298  
  1299  func split(s string) []string {
  1300  	var r []string
  1301  
  1302  outer:
  1303  	for s != "" {
  1304  		d := 0               // depth of ({[<
  1305  		var open, close byte // opening and closing markers ({[< or )}]>
  1306  		nonsp := false       // found a non-space char so far
  1307  		for i := 0; i < len(s); i++ {
  1308  			switch {
  1309  			case d == 0 && s[i] == '(':
  1310  				open, close = '(', ')'
  1311  				d++
  1312  			case d == 0 && s[i] == '<':
  1313  				open, close = '<', '>'
  1314  				d++
  1315  			case d == 0 && s[i] == '[':
  1316  				open, close = '[', ']'
  1317  				d++
  1318  			case d == 0 && s[i] == '{':
  1319  				open, close = '{', '}'
  1320  				d++
  1321  			case d == 0 && (s[i] == ' ' || s[i] == '\t'):
  1322  				if nonsp {
  1323  					r = append(r, strings.TrimSpace(s[:i]))
  1324  					s = s[i:]
  1325  					continue outer
  1326  				}
  1327  			case d > 0 && s[i] == open:
  1328  				d++
  1329  			case d > 0 && s[i] == close:
  1330  				d--
  1331  			case s[i] == ':':
  1332  				// ignore spaces after colons
  1333  				nonsp = true
  1334  				for i+1 < len(s) && (s[i+1] == ' ' || s[i+1] == '\t') {
  1335  					i++
  1336  				}
  1337  			default:
  1338  				nonsp = true
  1339  			}
  1340  		}
  1341  		if d != 0 {
  1342  			log.Fatalf("imbalanced expression: %q", s)
  1343  		}
  1344  		if nonsp {
  1345  			r = append(r, strings.TrimSpace(s))
  1346  		}
  1347  		break
  1348  	}
  1349  	return r
  1350  }
  1351  
  1352  // isBlock reports whether this op is a block opcode.
  1353  func isBlock(name string, arch arch) bool {
  1354  	for _, b := range genericBlocks {
  1355  		if b.name == name {
  1356  			return true
  1357  		}
  1358  	}
  1359  	for _, b := range arch.blocks {
  1360  		if b.name == name {
  1361  			return true
  1362  		}
  1363  	}
  1364  	return false
  1365  }
  1366  
  1367  func extract(val string) (op, typ, auxint, aux string, args []string) {
  1368  	val = val[1 : len(val)-1] // remove ()
  1369  
  1370  	// Split val up into regions.
  1371  	// Split by spaces/tabs, except those contained in (), {}, [], or <> or after colon.
  1372  	s := split(val)
  1373  
  1374  	// Extract restrictions and args.
  1375  	op = s[0]
  1376  	for _, a := range s[1:] {
  1377  		switch a[0] {
  1378  		case '<':
  1379  			typ = a[1 : len(a)-1] // remove <>
  1380  		case '[':
  1381  			auxint = a[1 : len(a)-1] // remove []
  1382  		case '{':
  1383  			aux = a[1 : len(a)-1] // remove {}
  1384  		default:
  1385  			args = append(args, a)
  1386  		}
  1387  	}
  1388  	return
  1389  }
  1390  
  1391  // parseValue parses a parenthesized value from a rule.
  1392  // The value can be from the match or the result side.
  1393  // It returns the op and unparsed strings for typ, auxint, and aux restrictions and for all args.
  1394  // oparch is the architecture that op is located in, or "" for generic.
  1395  func parseValue(val string, arch arch, loc string) (op opData, oparch, typ, auxint, aux string, args []string) {
  1396  	// Resolve the op.
  1397  	var s string
  1398  	s, typ, auxint, aux, args = extract(val)
  1399  
  1400  	// match reports whether x is a good op to select.
  1401  	// If strict is true, rule generation might succeed.
  1402  	// If strict is false, rule generation has failed,
  1403  	// but we're trying to generate a useful error.
  1404  	// Doing strict=true then strict=false allows
  1405  	// precise op matching while retaining good error messages.
  1406  	match := func(x opData, strict bool, archname string) bool {
  1407  		if x.name != s {
  1408  			return false
  1409  		}
  1410  		if x.argLength != -1 && int(x.argLength) != len(args) && (len(args) != 1 || args[0] != "...") {
  1411  			if strict {
  1412  				return false
  1413  			}
  1414  			log.Printf("%s: op %s (%s) should have %d args, has %d", loc, s, archname, x.argLength, len(args))
  1415  		}
  1416  		return true
  1417  	}
  1418  
  1419  	for _, x := range genericOps {
  1420  		if match(x, true, "generic") {
  1421  			op = x
  1422  			break
  1423  		}
  1424  	}
  1425  	for _, x := range arch.ops {
  1426  		if arch.name != "generic" && match(x, true, arch.name) {
  1427  			if op.name != "" {
  1428  				log.Fatalf("%s: matches for op %s found in both generic and %s", loc, op.name, arch.name)
  1429  			}
  1430  			op = x
  1431  			oparch = arch.name
  1432  			break
  1433  		}
  1434  	}
  1435  
  1436  	if op.name == "" {
  1437  		// Failed to find the op.
  1438  		// Run through everything again with strict=false
  1439  		// to generate useful diagnostic messages before failing.
  1440  		for _, x := range genericOps {
  1441  			match(x, false, "generic")
  1442  		}
  1443  		for _, x := range arch.ops {
  1444  			match(x, false, arch.name)
  1445  		}
  1446  		log.Fatalf("%s: unknown op %s", loc, s)
  1447  	}
  1448  
  1449  	// Sanity check aux, auxint.
  1450  	if auxint != "" && !opHasAuxInt(op) {
  1451  		log.Fatalf("%s: op %s %s can't have auxint", loc, op.name, op.aux)
  1452  	}
  1453  	if aux != "" && !opHasAux(op) {
  1454  		log.Fatalf("%s: op %s %s can't have aux", loc, op.name, op.aux)
  1455  	}
  1456  	return
  1457  }
  1458  
  1459  func opHasAuxInt(op opData) bool {
  1460  	switch op.aux {
  1461  	case "Bool", "Int8", "Int16", "Int32", "Int64", "Int128", "UInt8", "Float32", "Float64",
  1462  		"SymOff", "CallOff", "SymValAndOff", "TypSize", "ARM64BitField", "FlagConstant", "CCop",
  1463  		"PanicBoundsC", "PanicBoundsCC", "ARM64ConditionalParams":
  1464  		return true
  1465  	}
  1466  	return false
  1467  }
  1468  
  1469  func opHasAux(op opData) bool {
  1470  	switch op.aux {
  1471  	case "String", "Sym", "SymOff", "Call", "CallOff", "SymValAndOff", "Typ", "TypSize",
  1472  		"S390XCCMask", "S390XRotateParams", "PanicBoundsC", "PanicBoundsCC":
  1473  		return true
  1474  	}
  1475  	return false
  1476  }
  1477  
  1478  // splitNameExpr splits s-expr arg, possibly prefixed by "name:",
  1479  // into name and the unprefixed expression.
  1480  // For example, "x:(Foo)" yields "x", "(Foo)",
  1481  // and "(Foo)" yields "", "(Foo)".
  1482  func splitNameExpr(arg string) (name, expr string) {
  1483  	colon := strings.Index(arg, ":")
  1484  	if colon < 0 {
  1485  		return "", arg
  1486  	}
  1487  	openparen := strings.Index(arg, "(")
  1488  	if openparen < 0 {
  1489  		log.Fatalf("splitNameExpr(%q): colon but no open parens", arg)
  1490  	}
  1491  	if colon > openparen {
  1492  		// colon is inside the parens, such as in "(Foo x:(Bar))".
  1493  		return "", arg
  1494  	}
  1495  	return arg[:colon], strings.TrimSpace(arg[colon+1:])
  1496  }
  1497  
  1498  func getBlockInfo(op string, arch arch) (name string, data blockData) {
  1499  	for _, b := range genericBlocks {
  1500  		if b.name == op {
  1501  			return "Block" + op, b
  1502  		}
  1503  	}
  1504  	for _, b := range arch.blocks {
  1505  		if b.name == op {
  1506  			return "Block" + arch.name + op, b
  1507  		}
  1508  	}
  1509  	log.Fatalf("could not find block data for %s", op)
  1510  	panic("unreachable")
  1511  }
  1512  
  1513  // typeName returns the string to use to generate a type.
  1514  func typeName(typ string) string {
  1515  	if typ[0] == '(' {
  1516  		ts := strings.Split(typ[1:len(typ)-1], ",")
  1517  		if len(ts) != 2 {
  1518  			log.Fatalf("Tuple expect 2 arguments")
  1519  		}
  1520  		return "types.NewTuple(" + typeName(ts[0]) + ", " + typeName(ts[1]) + ")"
  1521  	}
  1522  	switch typ {
  1523  	case "Flags", "Mem", "Void", "Int128":
  1524  		return "types.Type" + typ
  1525  	default:
  1526  		return "typ." + typ
  1527  	}
  1528  }
  1529  
  1530  // balance returns the number of unclosed '(' characters in s.
  1531  // If a ')' appears without a corresponding '(', balance returns -1.
  1532  func balance(s string) int {
  1533  	balance := 0
  1534  	for _, c := range s {
  1535  		switch c {
  1536  		case '(':
  1537  			balance++
  1538  		case ')':
  1539  			balance--
  1540  			if balance < 0 {
  1541  				// don't allow ")(" to return 0
  1542  				return -1
  1543  			}
  1544  		}
  1545  	}
  1546  	return balance
  1547  }
  1548  
  1549  // findAllOpcode is a function to find the opcode portion of s-expressions.
  1550  var findAllOpcode = regexp.MustCompile(`[(](\w+[|])+\w+[)]`).FindAllStringIndex
  1551  
  1552  // excludeFromExpansion reports whether the substring s[idx[0]:idx[1]] in a rule
  1553  // should be disregarded as a candidate for | expansion.
  1554  // It uses simple syntactic checks to see whether the substring
  1555  // is inside an AuxInt expression or inside the && conditions.
  1556  func excludeFromExpansion(s string, idx []int) bool {
  1557  	left := s[:idx[0]]
  1558  	if strings.LastIndexByte(left, '[') > strings.LastIndexByte(left, ']') {
  1559  		// Inside an AuxInt expression.
  1560  		return true
  1561  	}
  1562  	right := s[idx[1]:]
  1563  	if strings.Contains(left, "&&") && strings.Contains(right, "=>") {
  1564  		// Inside && conditions.
  1565  		return true
  1566  	}
  1567  	return false
  1568  }
  1569  
  1570  // expandOr converts a rule into multiple rules by expanding | ops.
  1571  func expandOr(r string) []string {
  1572  	// Find every occurrence of |-separated things.
  1573  	// They look like MOV(B|W|L|Q|SS|SD)load or MOV(Q|L)loadidx(1|8).
  1574  	// Generate rules selecting one case from each |-form.
  1575  
  1576  	// Count width of |-forms.  They must match.
  1577  	n := 1
  1578  	for _, idx := range findAllOpcode(r, -1) {
  1579  		if excludeFromExpansion(r, idx) {
  1580  			continue
  1581  		}
  1582  		s := r[idx[0]:idx[1]]
  1583  		c := strings.Count(s, "|") + 1
  1584  		if c == 1 {
  1585  			continue
  1586  		}
  1587  		if n > 1 && n != c {
  1588  			log.Fatalf("'|' count doesn't match in %s: both %d and %d\n", r, n, c)
  1589  		}
  1590  		n = c
  1591  	}
  1592  	if n == 1 {
  1593  		// No |-form in this rule.
  1594  		return []string{r}
  1595  	}
  1596  	// Build each new rule.
  1597  	res := make([]string, n)
  1598  	for i := 0; i < n; i++ {
  1599  		buf := new(strings.Builder)
  1600  		x := 0
  1601  		for _, idx := range findAllOpcode(r, -1) {
  1602  			if excludeFromExpansion(r, idx) {
  1603  				continue
  1604  			}
  1605  			buf.WriteString(r[x:idx[0]])              // write bytes we've skipped over so far
  1606  			s := r[idx[0]+1 : idx[1]-1]               // remove leading "(" and trailing ")"
  1607  			buf.WriteString(strings.Split(s, "|")[i]) // write the op component for this rule
  1608  			x = idx[1]                                // note that we've written more bytes
  1609  		}
  1610  		buf.WriteString(r[x:])
  1611  		res[i] = buf.String()
  1612  	}
  1613  	return res
  1614  }
  1615  
  1616  // varCount returns a map which counts the number of occurrences of
  1617  // Value variables in the s-expression rr.Match and the Go expression rr.Cond.
  1618  func varCount(rr *RuleRewrite) map[string]int {
  1619  	cnt := map[string]int{}
  1620  	varCount1(rr.Loc, rr.Match, cnt)
  1621  	if rr.Cond != "" {
  1622  		expr, err := parser.ParseExpr(rr.Cond)
  1623  		if err != nil {
  1624  			log.Fatalf("%s: failed to parse cond %q: %v", rr.Loc, rr.Cond, err)
  1625  		}
  1626  		ast.Inspect(expr, func(n ast.Node) bool {
  1627  			if id, ok := n.(*ast.Ident); ok {
  1628  				cnt[id.Name]++
  1629  			}
  1630  			return true
  1631  		})
  1632  	}
  1633  	return cnt
  1634  }
  1635  
  1636  func varCount1(loc, m string, cnt map[string]int) {
  1637  	if m[0] == '<' || m[0] == '[' || m[0] == '{' {
  1638  		return
  1639  	}
  1640  	if token.IsIdentifier(m) {
  1641  		cnt[m]++
  1642  		return
  1643  	}
  1644  	// Split up input.
  1645  	name, expr := splitNameExpr(m)
  1646  	if name != "" {
  1647  		cnt[name]++
  1648  	}
  1649  	if expr[0] != '(' || expr[len(expr)-1] != ')' {
  1650  		log.Fatalf("%s: non-compound expr in varCount1: %q", loc, expr)
  1651  	}
  1652  	s := split(expr[1 : len(expr)-1])
  1653  	for _, arg := range s[1:] {
  1654  		varCount1(loc, arg, cnt)
  1655  	}
  1656  }
  1657  
  1658  // normalizeWhitespace replaces 2+ whitespace sequences with a single space.
  1659  func normalizeWhitespace(x string) string {
  1660  	x = strings.Join(strings.Fields(x), " ")
  1661  	x = strings.ReplaceAll(x, "( ", "(")
  1662  	x = strings.ReplaceAll(x, " )", ")")
  1663  	x = strings.ReplaceAll(x, "[ ", "[")
  1664  	x = strings.ReplaceAll(x, " ]", "]")
  1665  	x = strings.ReplaceAll(x, ")=>", ") =>")
  1666  	return x
  1667  }
  1668  
  1669  // opIsCommutative reports whether op s is commutative.
  1670  func opIsCommutative(op string, arch arch) bool {
  1671  	for _, x := range genericOps {
  1672  		if op == x.name {
  1673  			if x.commutative {
  1674  				return true
  1675  			}
  1676  			break
  1677  		}
  1678  	}
  1679  	if arch.name != "generic" {
  1680  		for _, x := range arch.ops {
  1681  			if op == x.name {
  1682  				if x.commutative {
  1683  					return true
  1684  				}
  1685  				break
  1686  			}
  1687  		}
  1688  	}
  1689  	return false
  1690  }
  1691  
  1692  func normalizeMatch(m string, arch arch) string {
  1693  	if token.IsIdentifier(m) {
  1694  		return m
  1695  	}
  1696  	op, typ, auxint, aux, args := extract(m)
  1697  	if opIsCommutative(op, arch) {
  1698  		if args[1] < args[0] {
  1699  			args[0], args[1] = args[1], args[0]
  1700  		}
  1701  	}
  1702  	s := new(strings.Builder)
  1703  	fmt.Fprintf(s, "%s <%s> [%s] {%s}", op, typ, auxint, aux)
  1704  	for _, arg := range args {
  1705  		prefix, expr := splitNameExpr(arg)
  1706  		fmt.Fprint(s, " ", prefix, normalizeMatch(expr, arch))
  1707  	}
  1708  	return s.String()
  1709  }
  1710  
  1711  func parseEllipsisRules(rules []Rule, arch arch) (newop string, ok bool) {
  1712  	if len(rules) != 1 {
  1713  		for _, r := range rules {
  1714  			if strings.Contains(r.Rule, "...") {
  1715  				log.Fatalf("%s: found ellipsis in rule, but there are other rules with the same op", r.Loc)
  1716  			}
  1717  		}
  1718  		return "", false
  1719  	}
  1720  	rule := rules[0]
  1721  	match, cond, result := rule.parse()
  1722  	if cond != "" || !isEllipsisValue(match) || !isEllipsisValue(result) {
  1723  		if strings.Contains(rule.Rule, "...") {
  1724  			log.Fatalf("%s: found ellipsis in non-ellipsis rule", rule.Loc)
  1725  		}
  1726  		checkEllipsisRuleCandidate(rule, arch)
  1727  		return "", false
  1728  	}
  1729  	op, oparch, _, _, _, _ := parseValue(result, arch, rule.Loc)
  1730  	return fmt.Sprintf("Op%s%s", oparch, op.name), true
  1731  }
  1732  
  1733  // isEllipsisValue reports whether s is of the form (OpX ...).
  1734  func isEllipsisValue(s string) bool {
  1735  	if len(s) < 2 || s[0] != '(' || s[len(s)-1] != ')' {
  1736  		return false
  1737  	}
  1738  	c := split(s[1 : len(s)-1])
  1739  	if len(c) != 2 || c[1] != "..." {
  1740  		return false
  1741  	}
  1742  	return true
  1743  }
  1744  
  1745  func checkEllipsisRuleCandidate(rule Rule, arch arch) {
  1746  	match, cond, result := rule.parse()
  1747  	if cond != "" {
  1748  		return
  1749  	}
  1750  	op, _, _, auxint, aux, args := parseValue(match, arch, rule.Loc)
  1751  	var auxint2, aux2 string
  1752  	var args2 []string
  1753  	var usingCopy string
  1754  	var eop opData
  1755  	if result[0] != '(' {
  1756  		// Check for (Foo x) => x, which can be converted to (Foo ...) => (Copy ...).
  1757  		args2 = []string{result}
  1758  		usingCopy = " using Copy"
  1759  	} else {
  1760  		eop, _, _, auxint2, aux2, args2 = parseValue(result, arch, rule.Loc)
  1761  	}
  1762  	// Check that all restrictions in match are reproduced exactly in result.
  1763  	if aux != aux2 || auxint != auxint2 || len(args) != len(args2) {
  1764  		return
  1765  	}
  1766  	if strings.Contains(rule.Rule, "=>") && op.aux != eop.aux {
  1767  		return
  1768  	}
  1769  	for i := range args {
  1770  		if args[i] != args2[i] {
  1771  			return
  1772  		}
  1773  	}
  1774  	switch {
  1775  	case opHasAux(op) && aux == "" && aux2 == "":
  1776  		fmt.Printf("%s: rule silently zeros aux, either copy aux or explicitly zero\n", rule.Loc)
  1777  	case opHasAuxInt(op) && auxint == "" && auxint2 == "":
  1778  		fmt.Printf("%s: rule silently zeros auxint, either copy auxint or explicitly zero\n", rule.Loc)
  1779  	default:
  1780  		fmt.Printf("%s: possible ellipsis rule candidate%s: %q\n", rule.Loc, usingCopy, rule.Rule)
  1781  	}
  1782  }
  1783  
  1784  func opByName(arch arch, name string) opData {
  1785  	name = name[2:]
  1786  	for _, x := range genericOps {
  1787  		if name == x.name {
  1788  			return x
  1789  		}
  1790  	}
  1791  	if arch.name != "generic" {
  1792  		name = name[len(arch.name):]
  1793  		for _, x := range arch.ops {
  1794  			if name == x.name {
  1795  				return x
  1796  			}
  1797  		}
  1798  	}
  1799  	log.Fatalf("failed to find op named %s in arch %s", name, arch.name)
  1800  	panic("unreachable")
  1801  }
  1802  
  1803  // auxType returns the Go type that this operation should store in its aux field.
  1804  func (op opData) auxType() string {
  1805  	switch op.aux {
  1806  	case "String":
  1807  		return "string"
  1808  	case "Sym":
  1809  		// Note: a Sym can be an *obj.LSym, a *ir.Name, or nil.
  1810  		return "Sym"
  1811  	case "SymOff":
  1812  		return "Sym"
  1813  	case "Call":
  1814  		return "Call"
  1815  	case "CallOff":
  1816  		return "Call"
  1817  	case "SymValAndOff":
  1818  		return "Sym"
  1819  	case "Typ":
  1820  		return "*types.Type"
  1821  	case "TypSize":
  1822  		return "*types.Type"
  1823  	case "S390XCCMask":
  1824  		return "s390x.CCMask"
  1825  	case "S390XRotateParams":
  1826  		return "s390x.RotateParams"
  1827  	case "PanicBoundsC":
  1828  		return "PanicBoundsC"
  1829  	case "PanicBoundsCC":
  1830  		return "PanicBoundsCC"
  1831  	default:
  1832  		return "invalid"
  1833  	}
  1834  }
  1835  
  1836  // auxIntType returns the Go type that this operation should store in its auxInt field.
  1837  func (op opData) auxIntType() string {
  1838  	switch op.aux {
  1839  	case "Bool":
  1840  		return "bool"
  1841  	case "Int8":
  1842  		return "int8"
  1843  	case "Int16":
  1844  		return "int16"
  1845  	case "Int32":
  1846  		return "int32"
  1847  	case "Int64":
  1848  		return "int64"
  1849  	case "Int128":
  1850  		return "int128"
  1851  	case "UInt8":
  1852  		return "uint8"
  1853  	case "Float32":
  1854  		return "float32"
  1855  	case "Float64":
  1856  		return "float64"
  1857  	case "CallOff":
  1858  		return "int32"
  1859  	case "SymOff":
  1860  		return "int32"
  1861  	case "SymValAndOff":
  1862  		return "ValAndOff"
  1863  	case "TypSize":
  1864  		return "int64"
  1865  	case "CCop":
  1866  		return "Op"
  1867  	case "FlagConstant":
  1868  		return "flagConstant"
  1869  	case "ARM64BitField":
  1870  		return "arm64BitField"
  1871  	case "ARM64ConditionalParams":
  1872  		return "arm64ConditionalParams"
  1873  	case "PanicBoundsC", "PanicBoundsCC":
  1874  		return "int64"
  1875  	default:
  1876  		return "invalid"
  1877  	}
  1878  }
  1879  
  1880  // auxType returns the Go type that this block should store in its aux field.
  1881  func (b blockData) auxType() string {
  1882  	switch b.aux {
  1883  	case "Sym":
  1884  		return "Sym"
  1885  	case "S390XCCMask", "S390XCCMaskInt8", "S390XCCMaskUint8":
  1886  		return "s390x.CCMask"
  1887  	case "S390XRotateParams":
  1888  		return "s390x.RotateParams"
  1889  	default:
  1890  		return "invalid"
  1891  	}
  1892  }
  1893  
  1894  // auxIntType returns the Go type that this block should store in its auxInt field.
  1895  func (b blockData) auxIntType() string {
  1896  	switch b.aux {
  1897  	case "S390XCCMaskInt8":
  1898  		return "int8"
  1899  	case "S390XCCMaskUint8":
  1900  		return "uint8"
  1901  	case "Int64":
  1902  		return "int64"
  1903  	default:
  1904  		return "invalid"
  1905  	}
  1906  }
  1907  
  1908  func title(s string) string {
  1909  	if i := strings.Index(s, "."); i >= 0 {
  1910  		switch strings.ToLower(s[:i]) {
  1911  		case "s390x": // keep arch prefix for clarity
  1912  			s = s[:i] + s[i+1:]
  1913  		default:
  1914  			s = s[i+1:]
  1915  		}
  1916  	}
  1917  	return strings.Title(s)
  1918  }
  1919  
  1920  func unTitle(s string) string {
  1921  	if i := strings.Index(s, "."); i >= 0 {
  1922  		switch strings.ToLower(s[:i]) {
  1923  		case "s390x": // keep arch prefix for clarity
  1924  			s = s[:i] + s[i+1:]
  1925  		default:
  1926  			s = s[i+1:]
  1927  		}
  1928  	}
  1929  	return strings.ToLower(s[:1]) + s[1:]
  1930  }
  1931  

View as plain text