| 1 | package api |
| 2 | |
| 3 | import ( |
| 4 | "path" |
| 5 | "sort" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | const ( |
| 10 | // Package is the avo package import path. |
| 11 | Package = "github.com/mmcloughlin/avo" |
| 12 | |
| 13 | // IRPackage is the package that defines intermediate representation types. |
| 14 | IRPackage = "ir" |
| 15 | |
| 16 | // OperandPackage is the package for operand types. |
| 17 | OperandPackage = "operand" |
| 18 | |
| 19 | // OperandType is the type used for operands. |
| 20 | OperandType = OperandPackage + ".Op" |
| 21 | |
| 22 | // RegisterPackage is the name of the package containing register types. |
| 23 | RegisterPackage = "reg" |
| 24 | |
| 25 | // RegisterType is the type used for registers. |
| 26 | RegisterType = RegisterPackage + ".Register" |
| 27 | ) |
| 28 | |
| 29 | // ImportPath returns the full import path for an avo subpackage. |
| 30 | func ImportPath(pkg string) string { |
| 31 | return path.Join(Package, pkg) |
| 32 | } |
| 33 | |
| 34 | // ImplicitRegisterIdentifier maps an implicit register name to a string |
| 35 | // suitable for a related Go identifier. |
| 36 | func ImplicitRegisterIdentifier(r string) string { |
| 37 | r = strings.Replace(r, "mm", "", 1) // handles the "xmm0" type |
| 38 | return strings.ToUpper(r) |
| 39 | } |
| 40 | |
| 41 | // ImplicitRegister returns avo syntax for the given implicit register type (from Opcodes XML). |
| 42 | func ImplicitRegister(r string) string { |
| 43 | return RegisterPackage + "." + ImplicitRegisterIdentifier(r) |
| 44 | } |
| 45 | |
| 46 | // OperandTypeIdentifier maps an operand type to a string suitable for a related |
| 47 | // Go identifier. |
| 48 | func OperandTypeIdentifier(t string) string { |
| 49 | return strings.ToUpper(strings.ReplaceAll(t, "/", "")) |
| 50 | } |
| 51 | |
| 52 | // CheckerName returns the name of the function that checks an operand of type t. |
| 53 | func CheckerName(t string) string { |
| 54 | return "operand.Is" + OperandTypeIdentifier(t) |
| 55 | } |
| 56 | |
| 57 | // ISAsIdentifier returns a string representation of an ISA list that suitable |
| 58 | // for use in a Go identifier. |
| 59 | func ISAsIdentifier(isas []string) string { |
| 60 | if len(isas) == 0 { |
| 61 | return "Base" |
| 62 | } |
| 63 | sorted := append([]string(nil), isas...) |
| 64 | sort.Strings(sorted) |
| 65 | ident := strings.Join(sorted, "_") |
| 66 | ident = strings.ReplaceAll(ident, ".", "") // SSE4.1 |
| 67 | ident = strings.ReplaceAll(ident, "+", "") // MMX+ |
| 68 | return ident |
| 69 | } |
| 70 | |
| 71 | // SuffixesClassIdentifier returns a string representation of a suffix class |
| 72 | // that's suitable for use in a Go identifier. |
| 73 | func SuffixesClassIdentifier(c string) string { |
| 74 | return strings.ToUpper(c) |
| 75 | } |