Debian Codegraph @

x86/optab.go

raw symbols SCIP
1 package x86
2
3 import (
4 "errors"
5
6 "github.com/mmcloughlin/avo/ir"
7 "github.com/mmcloughlin/avo/operand"
8 )
9
10 // build constructs an instruction object from a list of acceptable forms, and
11 // given input operands and suffixes.
12 func build(forms []form, suffixes sffxs, ops []operand.Op) (*ir.Instruction, error) {
13 for i := range forms {
14 f := &forms[i]
15 if f.match(suffixes, ops) {
16 return f.build(suffixes, ops), nil
17 }
18 }
19 return nil, errors.New("bad operands")
20 }
21
22 // form represents an instruction form.
23 type form struct {
24 Opcode opc
25 SuffixesClass sffxscls
26 Features feature
27 ISAs isas
28 Arity uint8
29 Operands oprnds
30 }
31
32 // feature is a flags enumeration type representing instruction properties.
33 type feature uint8
34
35 const (
36 featureTerminal feature = 1 << iota
37 featureBranch
38 featureConditionalBranch
39 featureCancellingInputs
40 )
41
42 // oprnds is a list of explicit and implicit operands of an instruction form.
43 // The size of the array is output by optab generator.
44 type oprnds [maxoperands]oprnd
45
46 // oprnd represents an explicit or implicit operand to an instruction form.
47 type oprnd struct {
48 Type uint8
49 Implicit bool
50 Action action
51 }
52
53 // action an instruction form applies to an operand.
54 type action uint8
55
56 const (
57 actionN action = iota
58 actionR
59 actionW
60 actionRW action = actionR | actionW
61 )
62
63 // Read reports if the action includes read.
64 func (a action) Read() bool { return (a & actionR) != 0 }
65
66 // Read reports if the action includes write.
67 func (a action) Write() bool { return (a & actionW) != 0 }
68
69 // match reports whether this form matches the given suffixes and operand
70 // list.
71 func (f *form) match(suffixes sffxs, ops []operand.Op) bool {
72 // Match suffix.
73 accept := f.SuffixesClass.SuffixesSet()
74 if !accept[suffixes] {
75 return false
76 }
77
78 // Match operands.
79 if len(ops) != int(f.Arity) {
80 return false
81 }
82
83 for i, op := range ops {
84 t := oprndtype(f.Operands[i].Type)
85 if !t.Match(op) {
86 return false
87 }
88 }
89
90 return true
91 }
92
93 // build the full instruction object for this form and the given suffixes and
94 // operands. Assumes the form already matches the inputs.
95 func (f *form) build(suffixes sffxs, ops []operand.Op) *ir.Instruction {
96 // Base instruction properties.
97 i := &ir.Instruction{
98 Opcode: f.Opcode.String(),
99 Suffixes: suffixes.Strings(),
100 Operands: ops,
101 IsTerminal: (f.Features & featureTerminal) != 0,
102 IsBranch: (f.Features & featureBranch) != 0,
103 IsConditional: (f.Features & featureConditionalBranch) != 0,
104 CancellingInputs: (f.Features & featureCancellingInputs) != 0,
105 ISA: f.ISAs.List(),
106 }
107
108 // Input/output operands.
109 for _, spec := range f.Operands {
110 if spec.Type == 0 {
111 break
112 }
113
114 var op operand.Op
115 if spec.Implicit {
116 op = implreg(spec.Type).Register()
117 } else {
118 op, ops = ops[0], ops[1:]
119 }
120
121 if spec.Action.Read() {
122 i.Inputs = append(i.Inputs, op)
123 }
124 if spec.Action.Write() {
125 i.Outputs = append(i.Outputs, op)
126 }
127 }
128
129 return i
130 }