1
2
3
4
5
6
7
8
9
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
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
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
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
105 log.Fatalf("can't read rule file: %v", err)
106 }
107
108 return
109 }
110 readers = append(readers, NamedReader{name, text})
111
112
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
122 blockrules := map[string][]Rule{}
123 oprules := map[string][]Rule{}
124
125
126 scanner := MultiScannerFromReaders(readers)
127 rule := ""
128 var lineno int
129 var ruleLineno int
130 for scanner.Scan() {
131 lineno = scanner.Line()
132 line := scanner.Text()
133 if i := strings.Index(line, "//"); i >= 0 {
134
135
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
151 }
152 if n := balance(rule); n > 0 {
153 continue
154 } else if n < 0 {
155 break
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
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
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
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 {
211 fn.add(sw)
212 }
213 fn.add(stmtf("return false"))
214 genFile.add(fn)
215
216
217
218 for _, op := range ops {
219 rules := oprules[op]
220 _, ok := parseEllipsisRules(oprules[op], arch)
221 if ok {
222 continue
223 }
224
225
226
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
263
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 {
283 fn.add(sw)
284 }
285 fn.add(stmtf("return false"))
286 genFile.add(fn)
287
288
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
305
306 u := unusedInspector{unused: make(map[token.Pos]bool)}
307 u.node(file)
308
309
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
318
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
329
330 c.Delete()
331 }
332 }
333 return true
334 }
335 file = astutil.Apply(file, pre, post).(*ast.File)
336
337
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
344
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
358
359
360
361
362 type unusedInspector struct {
363
364
365
366 scope *scope
367
368
369
370 unused map[token.Pos]bool
371
372
373
374
375 defining *object
376 }
377
378
379
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
397
398
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
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
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
571
572 type scope struct {
573 outer *scope
574 objects map[string]*object
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
588 type object struct {
589 name string
590 pos token.Pos
591
592 numUses int
593 used []*object
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,
715 }
716
717 var emptyFset = token.NewFileSet()
718
719
720 type Node interface{}
721
722
723
724 type Statement interface{}
725
726
727
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
742
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
756 var predeclared = map[string]bool{
757 "nil": true,
758 "false": true,
759 "true": true,
760 }
761
762
763 func (w *BodyBase) declared(name string) bool {
764 if predeclared[name] {
765
766
767
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
779
780
781
782
783
784 type (
785 File struct {
786 BodyBase
787 Arch arch
788 Suffix string
789 }
790 Func struct {
791 BodyBase
792 Kind string
793 Suffix string
794 ArgLen int32
795 }
796 Switch struct {
797 BodyBase
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
807 Check string
808
809 Alloc int
810 Loc string
811 CommuteDepth int
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
828
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
839
840
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,
853 "b": true,
854 "config": true,
855 "fe": true,
856 "typ": true,
857 }
858
859
860
861
862
863
864
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
873
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
882
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)
891
892
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)
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
948 outop, _, auxint, aux, t := extract(rr.Result)
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
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
977
978
979
980 newpos := "b.Pos"
981 if i < len(pos) && pos[i] != "" {
982
983 newpos = pos[i]
984 }
985
986
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
1002 rr.add(stmtf("b.AuxInt = %sToAuxInt(%s)", unTitle(outdata.auxIntType()), auxint))
1003 }
1004 if aux != "" {
1005
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
1032
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
1048 pos = v + ".Pos"
1049 }
1050
1051
1052
1053
1054
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
1103
1104
1105
1106 commutative = false
1107 }
1108 if cnt[args[0]] == 1 && cnt[args[1]] == 1 {
1109
1110
1111
1112 commutative = false
1113 }
1114 }
1115
1116 if !pregenTop {
1117
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
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
1155 if rr.declared(arg) {
1156
1157
1158
1159
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
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
1187
1188
1189
1190
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
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
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
1219
1220 if result[0] != '(' {
1221
1222 if top {
1223
1224
1225
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
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
1263 rr.add(stmtf("v.copyOf(%s)", v))
1264 }
1265 }
1266
1267 if auxint != "" {
1268
1269 rr.add(stmtf("%s.AuxInt = %sToAuxInt(%s)", v, unTitle(op.auxIntType()), auxint))
1270 }
1271 if aux != "" {
1272
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
1305 var open, close byte
1306 nonsp := false
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
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
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]
1369
1370
1371
1372 s := split(val)
1373
1374
1375 op = s[0]
1376 for _, a := range s[1:] {
1377 switch a[0] {
1378 case '<':
1379 typ = a[1 : len(a)-1]
1380 case '[':
1381 auxint = a[1 : len(a)-1]
1382 case '{':
1383 aux = a[1 : len(a)-1]
1384 default:
1385 args = append(args, a)
1386 }
1387 }
1388 return
1389 }
1390
1391
1392
1393
1394
1395 func parseValue(val string, arch arch, loc string) (op opData, oparch, typ, auxint, aux string, args []string) {
1396
1397 var s string
1398 s, typ, auxint, aux, args = extract(val)
1399
1400
1401
1402
1403
1404
1405
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
1438
1439
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
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
1479
1480
1481
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
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
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
1531
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
1542 return -1
1543 }
1544 }
1545 }
1546 return balance
1547 }
1548
1549
1550 var findAllOpcode = regexp.MustCompile(`[(](\w+[|])+\w+[)]`).FindAllStringIndex
1551
1552
1553
1554
1555
1556 func excludeFromExpansion(s string, idx []int) bool {
1557 left := s[:idx[0]]
1558 if strings.LastIndexByte(left, '[') > strings.LastIndexByte(left, ']') {
1559
1560 return true
1561 }
1562 right := s[idx[1]:]
1563 if strings.Contains(left, "&&") && strings.Contains(right, "=>") {
1564
1565 return true
1566 }
1567 return false
1568 }
1569
1570
1571 func expandOr(r string) []string {
1572
1573
1574
1575
1576
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
1594 return []string{r}
1595 }
1596
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]])
1606 s := r[idx[0]+1 : idx[1]-1]
1607 buf.WriteString(strings.Split(s, "|")[i])
1608 x = idx[1]
1609 }
1610 buf.WriteString(r[x:])
1611 res[i] = buf.String()
1612 }
1613 return res
1614 }
1615
1616
1617
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
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
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
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
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
1757 args2 = []string{result}
1758 usingCopy = " using Copy"
1759 } else {
1760 eop, _, _, auxint2, aux2, args2 = parseValue(result, arch, rule.Loc)
1761 }
1762
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
1804 func (op opData) auxType() string {
1805 switch op.aux {
1806 case "String":
1807 return "string"
1808 case "Sym":
1809
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
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
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
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":
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":
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