| 7 | # https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 |
| 15 | # This is a rough parser for x86-64 and aarch64 assembly designed to work with |
| 16 | # https://github.com/pointlander/peg. delocate.go has a go:generate line for |
| 17 | # rebuilding delocate.peg.go from this file. |
| 18 |
| 19 | # To regenerate delocate.peg.go: |
| 20 | # |
| 21 | # go install github.com/pointlander/peg@latest |
| 22 | # ~/go/bin/peg <path-to-this-file> |
| 23 | # |
| 24 | # this will generate delocate.peg.go next to delocate.peg. |
| 25 |
| 26 | package main |
| 27 |
| 28 | type Asm Peg {} |
| 29 |
| 30 | AsmFile <- Statement* !. |
| 31 | Statement <- WS? (Label / ((GlobalDirective / |
| 32 | LocationDirective / |
| 33 | SymbolDefiningDirective / |
| 34 | LabelContainingDirective / |
| 35 | Instruction / |
| 36 | Directive / |
| 37 | Comment / ) WS? ((Comment? '\n') / ';'))) |
| 38 | GlobalDirective <- (".global" / ".globl") WS SymbolName |
| 39 | Directive <- '.' DirectiveName (WS Args)? |
| 40 | DirectiveName <- [[A-Z0-9_]]+ |
| 41 | LocationDirective <- FileDirective / LocDirective |
| 42 | FileDirective <- ".file" WS [^#\n]+ |
| 43 | LocDirective <- ".loc" WS [^#/\n]+ |
| 44 | Args <- Arg ((WS? ',' WS?) Arg)* |
| 45 | Arg <- QuotedArg / [[0-9a-z%+\-*_@.$]]* |
| 46 | QuotedArg <- '"' QuotedText '"' |
| 47 | QuotedText <- (EscapedChar / [^"])* |
| 48 | SymbolDefiningDirective <- SymbolDefiningDirectiveName WS (LocalSymbol / SymbolName) WS? ',' WS? SymbolArg |
| 49 | SymbolDefiningDirectiveName <- ".equiv" / ".equ" / ".set" |
| 50 | LabelContainingDirective <- LabelContainingDirectiveName WS SymbolArgs |
| 51 | LabelContainingDirectiveName <- ".xword" / ".word" / ".hword" / ".long" / ".byte" / ".8byte" / ".4byte" / ".quad" / ".tc" / ".localentry" / ".size" / ".type" / ".uleb128" / ".sleb128" |
| 52 | SymbolArgs <- SymbolArg ((WS? ',' WS?) SymbolArg)* |
| 53 |
| 54 | SymbolArg <- SymbolExpr |
| 55 | SymbolExpr <- SymbolAtom (WS? SymbolOperator WS? SymbolExpr)? |
| 56 | SymbolAtom <- LocalLabelRef / Offset / SymbolType / LocalSymbol TCMarker? / SymbolName Offset / SymbolName TCMarker? / Dot / OpenParen WS? SymbolExpr WS? CloseParen |
| 57 | SymbolOperator <- '+' / '-' / '|' / '<<' / '>>' |
| 58 | OpenParen <- '(' |
| 59 | CloseParen <- ')' |
| 60 |
| 61 | SymbolType <- [@%] ('function' / 'object') |
| 62 | Dot <- '.' |
| 63 | TCMarker <- '[TC]' |
| 64 | EscapedChar <- '\\' . |
| 65 | WS <- [ \t]+ |
| 66 | Comment <- ("//" / '#') [^\n]* |
| 67 | Label <- (LocalSymbol / LocalLabel / SymbolName) ':' |
| 68 | SymbolName <- [[A-Z._]][[A-Z.0-9$_]]* |
| 69 | LocalSymbol <- '.L' [[A-Za-z.0-9$_]]+ |
| 70 | LocalLabel <- [0-9][0-9$]* |
| 71 | LocalLabelRef <- [0-9][0-9$]*[bf] |
| 72 | InstructionPrefix <- "notrack" |
| 73 | Instruction <- (InstructionPrefix WS)? InstructionName (WS InstructionArg ((WS? ',' WS?) InstructionArg)*)? |
| 74 | InstructionName <- [[A-Z]][[A-Z.0-9]]* [.+\-]? |
| 75 | InstructionArg <- IndirectionIndicator? (ARMConstantTweak / RegisterOrConstant / LocalLabelRef / TOCRefHigh / TOCRefLow / GOTLocation / GOTAddress / GOTSymbolOffset / MemoryRef) AVX512Token* |
| 76 | GOTLocation <- '$_GLOBAL_OFFSET_TABLE_-' LocalSymbol |
| 77 | GOTAddress <- '_GLOBAL_OFFSET_TABLE_(%rip)' |
| 78 | GOTSymbolOffset <- ('$' SymbolName '@GOT' 'OFF'?) / (":got:" SymbolName) |
| 79 | AVX512Token <- WS? '{' '%'? [0-9a-z]* '}' |
| 80 | TOCRefHigh <- '.TOC.-' ('0b' / ('.L' [a-zA-Z_0-9]+)) "@ha" |
| 81 | TOCRefLow <- '.TOC.-' ('0b' / ('.L' [a-zA-Z_0-9]+)) "@l" |
| 82 | IndirectionIndicator <- '*' |
| 83 | Float <- [0-9]+ '.' [0-9]* |
| 84 | RegisterOrConstant <- (('%'[[A-Z]][[A-Z0-9]]*) / |
| 85 | ('$'? ((Offset Offset) / Offset)) / |
| 86 | ('#' Float ) / |
| 87 | ('#' Offset ('*' [0-9]+ ('-' [0-9] [0-9]*)?)? ) / |
| 88 | ('#' '~'? '(' [0-9] WS? "<<" WS? [0-9] ')' ) / |
| 89 | ARMRegister) |
| 90 | ![fb:(+\-] |
| 91 | ARMConstantTweak <- ((([us] "xt" [xwhb]) / "lsl" / "lsr" / "ror" / "asr" / "msl") (WS '#' Offset)?) / |
| 92 | "mul vl" / # multiply offset by the hardware's vector length |
| 93 | "mul #" [0-9] |
| 94 | ARMRegister <- "sp" / |
| 95 | ([xwdqshb] [0-9] [0-9]?) / |
| 96 | "xzr" / "wzr" / "NZCV" / SVE2PredicateRegister / ARMVectorRegister / SVE2SpecialValue / |
| 97 | ('{' WS? ARMVectorRegister (',' WS? ARMVectorRegister)* WS? '}' ('[' [0-9] [0-9]? ']')? ) |
| 98 | ARMVectorRegister <- [pvz] [0-9] [0-9]? ![[0-9a-z_]] ('.' [0-9]* [bsdhq] ('[' [0-9] [0-9]? ']')? )? |
| 99 | SVE2PredicateRegister <- "p" [0-9] [0-9]? "/" [[mz]] |
| 100 | # https://developer.arm.com/documentation/ddi0596/2020-12/SVE-Instructions/INCD--INCH--INCW--vector---Increment-vector-by-multiple-of-predicate-constraint-element-count- |
| 101 | SVE2SpecialValue <- ("pow2" / ("vl" [12345678] ![0-9] ) / "vl16" / "vl32" / "vl64" / "vl128" / "vl256" / "mul3" / "mul4" / "all") ![[0-9a-z_]] |
| 102 | # Compilers only output a very limited number of expression forms. Rather than |
| 103 | # implement a full expression parser, this enumerate those forms plus a few |
| 104 | # that appear in our hand-written assembly. |
| 105 | MemoryRef <- (SymbolRef BaseIndexScale / |
| 106 | SymbolRef / |
| 107 | Low12BitsSymbolRef / |
| 108 | Offset* BaseIndexScale / |
| 109 | SegmentRegister Offset BaseIndexScale / |
| 110 | SegmentRegister BaseIndexScale / |
| 111 | SegmentRegister Offset / |
| 112 | ARMBaseIndexScale / |
| 113 | BaseIndexScale) |
| 114 | SymbolRef <- (Offset* '+')? (LocalSymbol / SymbolName) Offset* ('@' Section Offset*)? |
| 115 | Low12BitsSymbolRef <- ":lo12:" (LocalSymbol / SymbolName) Offset? |
| 116 | ARMBaseIndexScale <- '[' ARMRegister (',' WS? (('#' Offset (('*' [0-9]+) / ('*' '(' [0-9]+ Operator [0-9]+ ')') / (('+' [0-9]+)*))? ) / ARMGOTLow12 / Low12BitsSymbolRef / ARMRegister) (',' WS? ARMConstantTweak)?)? ']' ARMPostincrement? |
| 117 | ARMGOTLow12 <- ":got_lo12:" SymbolName |
| 118 | ARMPostincrement <- '!' |
| 119 | BaseIndexScale <- '(' RegisterOrConstant? WS? (',' WS? RegisterOrConstant WS? (',' [0-9]+)? )? ')' |
| 120 | Operator <- [+\-] |
| 121 | Offset <- '+'? '-'? (("0b" [01]+) / ("0x" [[0-9A-F]]+) / [0-9]+) |
| 122 | Section <- [[A-Z@]]+ |
| 123 | SegmentRegister <- '%' [c-gs] 's:' |
| 124 |