Debian Codegraph @

internal/api/function.go

raw symbols SCIP
1 package api
2
3 import (
4 "bytes"
5 "fmt"
6 "sort"
7 "strconv"
8 "strings"
9 "text/tabwriter"
10
11 "github.com/mmcloughlin/avo/internal/inst"
12 )
13
14 // Function represents a function that constructs some collection of
15 // instruction forms.
16 type Function struct {
17 Instruction inst.Instruction
18 Suffixes inst.Suffixes
19 inst.Forms
20 }
21
22 // Name returns the function name.
23 func (f *Function) Name() string {
24 return f.opcodesuffix("_")
25 }
26
27 // Opcode returns the full Go opcode of the instruction built by this function. Includes any suffixes.
28 func (f *Function) Opcode() string {
29 return f.opcodesuffix(".")
30 }
31
32 func (f *Function) opcodesuffix(sep string) string {
33 n := f.Instruction.Opcode
34 for _, suffix := range f.Suffixes {
35 n += sep
36 n += suffix.String()
37 }
38 return n
39 }
40
41 // HasSuffix reports whether the function has the provided suffix.
42 func (f *Function) HasSuffix(suffix inst.Suffix) bool {
43 for _, s := range f.Suffixes {
44 if s == suffix {
45 return true
46 }
47 }
48 return false
49 }
50
51 // Summary returns a summary of the instruction this function constructs.
52 func (f *Function) Summary() string {
53 summary := f.Instruction.Summary
54 if len(f.Suffixes) > 0 {
55 summary += " (" + strings.Join(f.Suffixes.Summaries(), ", ") + ")"
56 }
57 return summary
58 }
59
60 // Doc returns the function document comment as a list of lines.
61 func (f *Function) Doc() []string {
62 lines := []string{
63 fmt.Sprintf("%s: %s.", f.Name(), f.Summary()),
64 "",
65 "Forms:",
66 "",
67 }
68
69 // Write a table of instruction forms.
70 buf := bytes.NewBuffer(nil)
71 w := tabwriter.NewWriter(buf, 0, 0, 1, ' ', 0)
72 for _, form := range f.Forms {
73 row := f.Opcode() + "\t" + strings.Join(form.Signature(), "\t") + "\n"
74 fmt.Fprint(w, row)
75 }
76 w.Flush()
77
78 tbl := strings.TrimSpace(buf.String())
79 for _, line := range strings.Split(tbl, "\n") {
80 lines = append(lines, "\t"+line)
81 }
82
83 return lines
84 }
85
86 // Signature of the function. Derived from the instruction forms generated by this function.
87 func (f *Function) Signature() Signature {
88 // Handle the case of forms with multiple arities.
89 switch {
90 case f.IsVariadic():
91 return variadic{name: "ops"}
92 case f.IsNiladic():
93 return niladic{}
94 }
95
96 // Generate nice-looking variable names.
97 n := f.Arity()
98 ops := make([]string, n)
99 count := map[string]int{}
100 for j := 0; j < n; j++ {
101 // Collect unique lowercase bytes from first characters of operand types.
102 s := map[byte]bool{}
103 for _, form := range f.Forms {
104 c := form.Operands[j].Type[0]
105 if 'a' <= c && c <= 'z' {
106 s[c] = true
107 }
108 }
109
110 // Operand name is the sorted bytes.
111 var b []byte
112 for c := range s {
113 b = append(b, c)
114 }
115 sort.Slice(b, func(i, j int) bool { return b[i] < b[j] })
116 name := string(b)
117
118 // Append a counter if we've seen it already.
119 m := count[name]
120 count[name]++
121 if m > 0 {
122 name += strconv.Itoa(m)
123 }
124 ops[j] = name
125 }
126
127 return argslist(ops)
128 }
129
130 // InstructionFunctions builds the list of all functions for a given
131 // instruction.
132 func InstructionFunctions(i inst.Instruction) []*Function {
133 // One function for each possible suffix combination.
134 bysuffix := map[string]*Function{}
135 for _, f := range i.Forms {
136 for _, suffixes := range f.SupportedSuffixes() {
137 k := suffixes.String()
138 if _, ok := bysuffix[k]; !ok {
139 bysuffix[k] = &Function{
140 Instruction: i,
141 Suffixes: suffixes,
142 }
143 }
144 bysuffix[k].Forms = append(bysuffix[k].Forms, f)
145 }
146 }
147
148 // Convert to a sorted slice.
149 var fns []*Function
150 for _, fn := range bysuffix {
151 fns = append(fns, fn)
152 }
153
154 SortFunctions(fns)
155
156 return fns
157 }
158
159 // InstructionsFunctions builds all functions for a list of instructions.
160 func InstructionsFunctions(is []inst.Instruction) []*Function {
161 var all []*Function
162 for _, i := range is {
163 fns := InstructionFunctions(i)
164 all = append(all, fns...)
165 }
166
167 SortFunctions(all)
168
169 return all
170 }
171
172 // SortFunctions sorts a list of functions by name.
173 func SortFunctions(fns []*Function) {
174 sort.Slice(fns, func(i, j int) bool {
175 return fns[i].Name() < fns[j].Name()
176 })
177 }