| 1 | package gen |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | |
| 8 | "github.com/mmcloughlin/avo/internal/api" |
| 9 | "github.com/mmcloughlin/avo/internal/inst" |
| 10 | ) |
| 11 | |
| 12 | // Enum is a generated enumeration type. This assists with mapping between the |
| 13 | // conceptual values of the enum, and it's materialization as Go code. |
| 14 | type Enum struct { |
| 15 | name string |
| 16 | doc []string |
| 17 | values []string |
| 18 | } |
| 19 | |
| 20 | // NewEnum initializes an empty enum type with the given name. |
| 21 | func NewEnum(name string) *Enum { |
| 22 | return &Enum{name: name} |
| 23 | } |
| 24 | |
| 25 | // Name returns the type name. |
| 26 | func (e *Enum) Name() string { return e.name } |
| 27 | |
| 28 | // Receiver returns the receiver variable name. |
| 29 | func (e *Enum) Receiver() string { |
| 30 | return strings.ToLower(e.name[:1]) |
| 31 | } |
| 32 | |
| 33 | // SetDoc sets type documentation, as a list of lines. |
| 34 | func (e *Enum) SetDoc(doc ...string) { |
| 35 | e.doc = doc |
| 36 | } |
| 37 | |
| 38 | // Doc returns the type documentation. |
| 39 | func (e *Enum) Doc() []string { return e.doc } |
| 40 | |
| 41 | // AddValue adds a named enumerator. |
| 42 | func (e *Enum) AddValue(value string) { |
| 43 | e.values = append(e.values, value) |
| 44 | } |
| 45 | |
| 46 | // Values returns all enumerators. |
| 47 | func (e *Enum) Values() []string { return e.values } |
| 48 | |
| 49 | // None returns the name of the "unset" constant of this enumeration. |
| 50 | func (e *Enum) None() string { |
| 51 | return e.ConstName("None") |
| 52 | } |
| 53 | |
| 54 | // ConstName returns the constant name that refers to the given enumerator |
| 55 | // value. |
| 56 | func (e *Enum) ConstName(value string) string { |
| 57 | return e.name + value |
| 58 | } |
| 59 | |
| 60 | // ConstNames returns the constant names for all enumerator values. |
| 61 | func (e *Enum) ConstNames() []string { |
| 62 | var consts []string |
| 63 | for _, v := range e.values { |
| 64 | consts = append(consts, e.ConstName(v)) |
| 65 | } |
| 66 | return consts |
| 67 | } |
| 68 | |
| 69 | // MaxName returns the name of the constant that represents the maximum |
| 70 | // enumerator. This value is placed at the very end of the enum, so all values |
| 71 | // will be between the None and Max enumerators. |
| 72 | func (e *Enum) MaxName() string { |
| 73 | return strings.ToLower(e.ConstName("max")) |
| 74 | } |
| 75 | |
| 76 | // Max returns the value of the maximum enumerator. |
| 77 | func (e *Enum) Max() int { |
| 78 | return len(e.values) |
| 79 | } |
| 80 | |
| 81 | // UnderlyingType returns the underlying unsigned integer type used for the |
| 82 | // enumeration. This will be the smallest type that can represent all the |
| 83 | // values. |
| 84 | func (e *Enum) UnderlyingType() string { |
| 85 | b := uint(8) |
| 86 | for ; b < 64 && e.Max() > ((1<<b)-1); b <<= 1 { |
| 87 | } |
| 88 | return fmt.Sprintf("uint%d", b) |
| 89 | } |
| 90 | |
| 91 | // Table represents all the types required to represent the instruction |
| 92 | // operation table (optab). |
| 93 | type Table struct { |
| 94 | instructions []inst.Instruction |
| 95 | |
| 96 | operandType *Enum |
| 97 | implicitRegister *Enum |
| 98 | suffix *Enum |
| 99 | suffixesClass *Enum |
| 100 | isas *Enum |
| 101 | opcode *Enum |
| 102 | } |
| 103 | |
| 104 | // NewTable builds optab types to represent the given instructions. |
| 105 | func NewTable(is []inst.Instruction) *Table { |
| 106 | t := &Table{instructions: is} |
| 107 | t.init() |
| 108 | return t |
| 109 | } |
| 110 | |
| 111 | func (t *Table) init() { |
| 112 | // Operand type. |
| 113 | t.operandType = NewEnum("oprndtype") |
| 114 | types := inst.OperandTypes(t.instructions) |
| 115 | for _, typ := range types { |
| 116 | t.operandType.AddValue(api.OperandTypeIdentifier(typ)) |
| 117 | } |
| 118 | |
| 119 | // Implicit register. |
| 120 | registers := inst.ImplicitRegisters(t.instructions) |
| 121 | t.implicitRegister = NewEnum("implreg") |
| 122 | for _, r := range registers { |
| 123 | t.implicitRegister.AddValue(api.ImplicitRegisterIdentifier(r)) |
| 124 | } |
| 125 | |
| 126 | // Suffix. |
| 127 | t.suffix = NewEnum("sffx") |
| 128 | for _, s := range inst.UniqueSuffixes(t.instructions) { |
| 129 | t.suffix.AddValue(s.String()) |
| 130 | } |
| 131 | |
| 132 | // Suffixes class. |
| 133 | t.suffixesClass = NewEnum("sffxscls") |
| 134 | |
| 135 | classes := inst.SuffixesClasses(t.instructions) |
| 136 | keys := make([]string, 0, len(classes)) |
| 137 | for key := range classes { |
| 138 | keys = append(keys, key) |
| 139 | } |
| 140 | sort.Strings(keys) |
| 141 | |
| 142 | for _, key := range keys { |
| 143 | t.suffixesClass.AddValue(api.SuffixesClassIdentifier(key)) |
| 144 | } |
| 145 | |
| 146 | // ISAs. |
| 147 | t.isas = NewEnum("isas") |
| 148 | for _, isas := range inst.ISACombinations(t.instructions) { |
| 149 | t.isas.AddValue(api.ISAsIdentifier(isas)) |
| 150 | } |
| 151 | |
| 152 | // Opcodes. |
| 153 | t.opcode = NewEnum("opc") |
| 154 | for _, i := range t.instructions { |
| 155 | t.opcode.AddValue(i.Opcode) |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // OperandType returns the enumeration representing all possible operand types. |
| 160 | func (t *Table) OperandType() *Enum { return t.operandType } |
| 161 | |
| 162 | // OperandTypeConst returns the constant name for the given operand type. |
| 163 | func (t *Table) OperandTypeConst(typ string) string { |
| 164 | return t.operandType.ConstName(api.OperandTypeIdentifier(typ)) |
| 165 | } |
| 166 | |
| 167 | // ImplicitRegister returns the enumeration representing all possible operand |
| 168 | // types. |
| 169 | func (t *Table) ImplicitRegister() *Enum { return t.implicitRegister } |
| 170 | |
| 171 | // ImplicitRegisterConst returns the constant name for the given register. |
| 172 | func (t *Table) ImplicitRegisterConst(r string) string { |
| 173 | return t.implicitRegister.ConstName(api.ImplicitRegisterIdentifier(r)) |
| 174 | } |
| 175 | |
| 176 | // Suffix returns the enumeration representing instruction suffixes. |
| 177 | func (t *Table) Suffix() *Enum { return t.suffix } |
| 178 | |
| 179 | // SuffixConst returns the constant name for the given instruction suffix. |
| 180 | func (t *Table) SuffixConst(s inst.Suffix) string { return t.suffix.ConstName(s.String()) } |
| 181 | |
| 182 | // SuffixesTypeName returns the name of the array type for a list of suffixes. |
| 183 | func (t *Table) SuffixesTypeName() string { |
| 184 | return t.Suffix().Name() + "s" |
| 185 | } |
| 186 | |
| 187 | // SuffixesConst returns the constant for a list of suffixes. Suffixes is a |
| 188 | // generated array type, so the list is a value not slice type. |
| 189 | func (t *Table) SuffixesConst(suffixes inst.Suffixes) string { |
| 190 | return t.SuffixesTypeName() + t.SuffixesList(suffixes) |
| 191 | } |
| 192 | |
| 193 | // SuffixesList returns the constant literal for a list of suffixes, type name |
| 194 | // not included. Use SuffxesConst if the type is required. |
| 195 | func (t *Table) SuffixesList(suffixes inst.Suffixes) string { |
| 196 | var parts []string |
| 197 | for _, suffix := range suffixes { |
| 198 | parts = append(parts, t.SuffixConst(suffix)) |
| 199 | } |
| 200 | return "{" + strings.Join(parts, ", ") + "}" |
| 201 | } |
| 202 | |
| 203 | // SuffixesClass returns the enumeration representing all suffixes classes. |
| 204 | func (t *Table) SuffixesClass() *Enum { return t.suffixesClass } |
| 205 | |
| 206 | // SuffixesClassConst returns the constant name for a given suffixes class. The |
| 207 | // key is as returned from inst.SuffixesClasses() function. |
| 208 | func (t *Table) SuffixesClassConst(key string) string { |
| 209 | ident := api.SuffixesClassIdentifier(key) |
| 210 | if ident == "" { |
| 211 | return t.suffixesClass.None() |
| 212 | } |
| 213 | return t.suffixesClass.ConstName(ident) |
| 214 | } |
| 215 | |
| 216 | // ISAs returns the enumeration for all possible ISA combinations. |
| 217 | func (t *Table) ISAs() *Enum { return t.isas } |
| 218 | |
| 219 | // ISAsConst returns the constant name for the given ISA combination. |
| 220 | func (t *Table) ISAsConst(isas []string) string { |
| 221 | return t.isas.ConstName(api.ISAsIdentifier(isas)) |
| 222 | } |
| 223 | |
| 224 | // Opcode returns the opcode enumeration. |
| 225 | func (t *Table) Opcode() *Enum { return t.opcode } |
| 226 | |
| 227 | // OpcodeConst returns the constant name for the given opcode. |
| 228 | func (t *Table) OpcodeConst(opcode string) string { |
| 229 | return t.opcode.ConstName(opcode) |
| 230 | } |
| 231 | |
| 232 | // Features returns code for the features constant describing the features of |
| 233 | // the given instruction form. |
| 234 | func Features(i inst.Instruction, f inst.Form) string { |
| 235 | var enabled []string |
| 236 | for _, feature := range []struct { |
| 237 | Name string |
| 238 | Enabled bool |
| 239 | }{ |
| 240 | {"Terminal", i.IsTerminal()}, |
| 241 | {"Branch", i.IsBranch()}, |
| 242 | {"ConditionalBranch", i.IsConditionalBranch()}, |
| 243 | {"CancellingInputs", f.CancellingInputs}, |
| 244 | } { |
| 245 | if feature.Enabled { |
| 246 | enabled = append(enabled, "feature"+feature.Name) |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | if len(enabled) == 0 { |
| 251 | return "0" |
| 252 | } |
| 253 | return strings.Join(enabled, "|") |
| 254 | } |
| 255 | |
| 256 | // Action returns code representing the instruction action. |
| 257 | func Action(a inst.Action) string { |
| 258 | c := strings.ToUpper(a.String()) |
| 259 | if c == "" { |
| 260 | c = "N" |
| 261 | } |
| 262 | return "action" + c |
| 263 | } |