Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 1 | # Copyright (c) 2017, Google Inc. |
| 2 | # |
| 3 | # Permission to use, copy, modify, and/or distribute this software for any |
| 4 | # purpose with or without fee is hereby granted, provided that the above |
| 5 | # copyright notice and this permission notice appear in all copies. |
| 6 | # |
| 7 | # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 8 | # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 9 | # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 10 | # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 11 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
| 12 | # OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
| 13 | # CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
| 14 | |
| 15 | # This is a rough parser for x86-64 and ppc64le 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 | package main |
| 20 | |
| 21 | type Asm Peg {} |
| 22 | |
| 23 | AsmFile <- Statement* !. |
| 24 | Statement <- WS? (Label / ((GlobalDirective / |
| 25 | LocationDirective / |
| 26 | LabelContainingDirective / |
| 27 | Instruction / |
| 28 | Directive / |
| 29 | Comment / ) WS? ((Comment? '\n') / ';'))) |
| 30 | GlobalDirective <- (".global" / ".globl") WS SymbolName |
| 31 | Directive <- '.' DirectiveName (WS Args)? |
| 32 | DirectiveName <- [[A-Z0-9_]]+ |
| 33 | LocationDirective <- (".file" / ".loc") WS [^#\n]+ |
| 34 | Args <- Arg ((WS? ',' WS?) Arg)* |
Robert Sloan | 8542c08 | 2018-02-05 09:07:34 -0800 | [diff] [blame] | 35 | Arg <- QuotedArg / [[0-9a-z%+\-*_@.]]* |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 36 | QuotedArg <- '"' QuotedText '"' |
| 37 | QuotedText <- (EscapedChar / [^"])* |
| 38 | LabelContainingDirective <- LabelContainingDirectiveName WS SymbolArgs |
Srinivas Paladugu | dd42a61 | 2019-08-09 19:30:39 +0000 | [diff] [blame^] | 39 | LabelContainingDirectiveName <- ".long" / ".set" / ".8byte" / ".4byte" / ".quad" / ".tc" / ".localentry" / ".size" / ".type" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 40 | SymbolArgs <- SymbolArg ((WS? ',' WS?) SymbolArg)* |
| 41 | SymbolArg <- Offset / |
| 42 | SymbolType / |
| 43 | (Offset / LocalSymbol / SymbolName / Dot) WS? Operator WS? (Offset / LocalSymbol / SymbolName) / |
| 44 | LocalSymbol TCMarker? / |
| 45 | SymbolName Offset / |
| 46 | SymbolName TCMarker? |
| 47 | SymbolType <- '@function' / '@object' |
| 48 | Dot <- '.' |
| 49 | TCMarker <- '[TC]' |
| 50 | EscapedChar <- '\\' . |
| 51 | WS <- [ \t]+ |
| 52 | Comment <- '#' [^\n]* |
| 53 | Label <- (LocalSymbol / LocalLabel / SymbolName) ':' |
| 54 | SymbolName <- [[A-Z._]][[A-Z.0-9$_]]* |
| 55 | LocalSymbol <- '.L' [[A-Z.0-9$_]]+ |
| 56 | LocalLabel <- [0-9][0-9$]* |
| 57 | LocalLabelRef <- [0-9][0-9$]*[bf] |
Robert Sloan | 5bdaadb | 2018-10-30 16:00:26 -0700 | [diff] [blame] | 58 | Instruction <- InstructionName (WS InstructionArg ((WS? ',' WS?) InstructionArg)*)? |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 59 | InstructionName <- [[A-Z]][[A-Z0-9]]* [.+\-]? |
Robert Sloan | 5bdaadb | 2018-10-30 16:00:26 -0700 | [diff] [blame] | 60 | InstructionArg <- IndirectionIndicator? (RegisterOrConstant / LocalLabelRef / TOCRefHigh / TOCRefLow / MemoryRef) AVX512Token* |
| 61 | AVX512Token <- WS? '{' '%'? [0-9a-z]* '}' |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 62 | TOCRefHigh <- '.TOC.-' ('0b' / ('.L' [a-zA-Z_0-9]+)) "@ha" |
| 63 | TOCRefLow <- '.TOC.-' ('0b' / ('.L' [a-zA-Z_0-9]+)) "@l" |
| 64 | IndirectionIndicator <- '*' |
| 65 | RegisterOrConstant <- (('%'[[A-Z]][[A-Z0-9]]*) / ('$'? ((Offset Offset) / Offset))) ![fb:(+\-] |
| 66 | # Compilers only output a very limited number of expression forms. Rather than |
| 67 | # implement a full expression parser, this enumerate those forms plus a few |
| 68 | # that appear in our hand-written assembly. |
| 69 | MemoryRef <- (SymbolRef BaseIndexScale / |
| 70 | SymbolRef / |
| 71 | Offset* BaseIndexScale / |
| 72 | SegmentRegister Offset BaseIndexScale / |
| 73 | SegmentRegister BaseIndexScale / |
| 74 | SegmentRegister Offset / |
| 75 | BaseIndexScale) |
| 76 | SymbolRef <- (Offset* '+')? (LocalSymbol / SymbolName) Offset* ('@' Section Offset*)? |
| 77 | BaseIndexScale <- '(' RegisterOrConstant? WS? (',' WS? RegisterOrConstant WS? (',' [0-9]+)? )? ')' |
| 78 | Operator <- [+\-] |
| 79 | Offset <- '+'? '-'? (("0b" [01]+) / ("0x" [[0-9A-F]]+) / [0-9]+) |
| 80 | Section <- [[A-Z@]]+ |
| 81 | SegmentRegister <- '%' [c-gs] 's:' |