Browse Source

Initial import

tags/v0.1.1
maze 9 months ago
parent
commit
007e3bf1c5
18 changed files with 5471 additions and 21 deletions
  1. 5
    21
      .gitignore
  2. 449
    0
      assembler.go
  3. 82
    0
      cmd/bpfasm/main.go
  4. 4
    0
      doc.go
  5. 6
    0
      go.mod
  6. 4
    0
      go.sum
  7. 140
    0
      parse.go
  8. 205
    0
      parser/BPF.g4
  9. 183
    0
      parser/bpf_base_listener.go
  10. 116
    0
      parser/bpf_base_visitor.go
  11. 313
    0
      parser/bpf_lexer.go
  12. 171
    0
      parser/bpf_listener.go
  13. 3667
    0
      parser/bpf_parser.go
  14. 90
    0
      parser/bpf_visitor.go
  15. 4
    0
      testdata/example1.asm
  16. 4
    0
      testdata/example2.asm
  17. 17
    0
      testdata/example3.asm
  18. 11
    0
      testdata/example4.asm

+ 5
- 21
.gitignore View File

@@ -12,28 +12,12 @@
# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# ---> Java
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
# IntelliJ files
.idea/

# Antlr artifacts
parser/*.interp
parser/*.tokens

+ 449
- 0
assembler.go View File

@@ -0,0 +1,449 @@
//go:generate antlr -package parser -Dlanguage=Go -visitor parser/BPF.g4
package bpf

import (
"errors"
"fmt"
"log"

"github.com/antlr/antlr4/runtime/Go/antlr"
"golang.org/x/net/bpf"

"maze.io/x/bpf/parser"
)

// Assemble BPF instructions from source.
func Assemble(source string) ([]bpf.RawInstruction, error) {
var (
input = antlr.NewInputStream(source)
lexer = parser.NewBPFLexer(input)
stream = antlr.NewCommonTokenStream(lexer, 0)
parse = parser.NewBPFParser(stream)
)
parse.BuildParseTrees = true

asm := new(assembler)
result := asm.Visit(parse.Program()).([]interface{})

/*
tokens := stream.GetAllTokens()
log.Printf("%d tokens", len(tokens))
for _, token := range tokens {
log.Printf("token: %s", token)
}
*/
log.Printf("result: %+v", result)

if result[1] == nil {
return result[0].([]bpf.RawInstruction), nil
}
return nil, result[1].(error)
}

type assembler struct {
*antlr.BaseParseTreeVisitor
}

func (asm *assembler) Visit(tree antlr.ParseTree) interface{} {
return tree.Accept(asm)
}

func (asm *assembler) VisitChildren(node antlr.RuleNode) interface{} {
var results []interface{}
for _, child := range node.GetChildren() {
switch child := child.(type) {
case *antlr.ErrorNodeImpl:
results = append(results, fmt.Errorf("invalid token %q", child.GetText()))

default:
instruction := child.(antlr.ParseTree).Accept(asm)
if instruction != nil {
results = append(results, instruction)
}
}
}
return results
}

func (asm *assembler) VisitProgram(ctx *parser.ProgramContext) interface{} {
program := []interface{}{}
labelAddresses := map[string]uint32{}

results := asm.VisitChildren(ctx)
for _, result := range results.([]interface{}) {
switch result := result.(type) {
case string:
labelAddresses[result] = uint32(len(program))
//log.Printf("label %q at %d", result, labelAddresses[result])

case []interface{}:
for _, word := range result {
switch word := word.(type) {
case string:
labelAddresses[word] = uint32(len(program))
default:
program = append(program, word)
}
}

case error:
return []interface{}{nil, result}

default:
program = append(program, result)
}
}

resolveLabelAddress := func(i int, label string) (skip uint8, err error) {
if addr, ok := labelAddresses[label]; ok {
// log.Printf("label %q at %d -> %d", label, addr, addr-uint32(i)-1)
if addr < uint32(i) {
return 0, fmt.Errorf("instruction %d: jump to negative offset label %q", i, label)
}
return uint8(addr - uint32(i) - 1), nil
}
return 0, fmt.Errorf("instruction %d: jump to unresolved label %q", i, label)
}

var instructions []bpf.Instruction
for i, word := range program {
// log.Printf("program: %T: %+v", word, word)
switch word := word.(type) {
case bpf.Instruction:
instructions = append(instructions, word)

case conditionalJump:
switch skip := word.skipTrue.(type) {
case uint8:
word.Instruction.SkipTrue = skip
case string:
addr, err := resolveLabelAddress(i, skip)
if err != nil {
return []interface{}{nil, err}
}
word.Instruction.SkipFalse = addr
default:
return []interface{}{nil, fmt.Errorf("instruction %d: invalid jump true %T", i, skip)}
}
switch skip := word.skipFalse.(type) {
case uint8:
word.Instruction.SkipFalse = skip
case string:
addr, err := resolveLabelAddress(i, skip)
if err != nil {
return []interface{}{nil, err}
}
word.Instruction.SkipFalse = addr
}
instructions = append(instructions, word.Instruction)

case conditionalJumpX:
switch skip := word.skipTrue.(type) {
case uint8:
word.Instruction.SkipTrue = skip
case string:
addr, err := resolveLabelAddress(i, skip)
if err != nil {
return []interface{}{nil, err}
}
word.Instruction.SkipFalse = addr
}
switch skip := word.skipFalse.(type) {
case uint8:
word.Instruction.SkipFalse = skip
case string:
addr, err := resolveLabelAddress(i, skip)
if err != nil {
return []interface{}{nil, err}
}
word.Instruction.SkipFalse = addr
}
instructions = append(instructions, word.Instruction)

default:
return []interface{}{nil, fmt.Errorf("instruction %d: unknown type %T", i, word)}
}
}

raw, err := bpf.Assemble(instructions)
return []interface{}{raw, err}
}

func (asm *assembler) VisitLabelDefinition(ctx *parser.LabelDefinitionContext) interface{} {
return ctx.IDENTIFIER().GetText()
}

func (asm *assembler) VisitLabel(ctx *parser.LabelContext) interface{} {
return ctx.IDENTIFIER().GetText()
}

func (asm *assembler) VisitComment(ctx *parser.CommentContext) interface{} {
return asm.VisitChildren(ctx)
}

func (asm *assembler) VisitInstruction(ctx *parser.InstructionContext) interface{} {
switch {
case ctx.AluOperation() != nil:
return asm.Visit(ctx.AluOperation())

case ctx.JumpOperation() != nil:
return asm.Visit(ctx.JumpOperation())

case ctx.JumpConditionalOperation() != nil:
return asm.Visit(ctx.JumpConditionalOperation())

case ctx.LoadOperation() != nil:
return asm.Visit(ctx.LoadOperation())

case ctx.StoreOperation() != nil:
return asm.Visit(ctx.StoreOperation())

case ctx.SimpleOperation() != nil:
return asm.Visit(ctx.SimpleOperation())

case ctx.ReturnOperation() != nil:
return asm.Visit(ctx.ReturnOperation())

default:
return fmt.Errorf("unknown instruction: %#+v", ctx)
}
}

func (asm *assembler) VisitAluOperation(ctx *parser.AluOperationContext) interface{} {
var (
opcode = asm.Visit(ctx.AluOperator()).(bpf.ALUOp)
)
switch {
case ctx.LiteralNumber() != nil:
return bpf.ALUOpConstant{Op: opcode, Val: asm.Visit(ctx.LiteralNumber()).(uint32)}
case ctx.RegisterX() != nil:
return bpf.ALUOpX{Op: opcode}
default:
return nil
}
}

func (asm *assembler) VisitAluOperator(ctx *parser.AluOperatorContext) interface{} {
return parseAluOpcode(ctx)
}

func (asm *assembler) VisitJumpOperation(ctx *parser.JumpOperationContext) interface{} {
return asm.VisitChildren(ctx)
}

func (asm *assembler) VisitJumpOperator(ctx *parser.JumpOperatorContext) interface{} {
return asm.VisitChildren(ctx)
}

type conditionalJump struct {
Instruction bpf.JumpIf
skipTrue interface{}
skipFalse interface{}
}

type conditionalJumpX struct {
Instruction bpf.JumpIfX
skipTrue interface{}
skipFalse interface{}
}

func (asm *assembler) VisitJumpConditionalOperation(ctx *parser.JumpConditionalOperationContext) interface{} {
var (
test = asm.Visit(ctx.JumpConditionalOperator()).(bpf.JumpTest)
ifTrue = asm.Visit(ctx.IfTrue())
ifFalse interface{}
)
if ctx.IfFalse() != nil {
ifFalse = asm.Visit(ctx.IfFalse())
}
switch {
case ctx.LiteralNumber() != nil:
return conditionalJump{
Instruction: bpf.JumpIf{
Cond: test,
Val: asm.Visit(ctx.LiteralNumber()).(uint32),
},
skipTrue: ifTrue,
skipFalse: ifFalse,
}

case ctx.RegisterX() != nil:
return conditionalJumpX{
Instruction: bpf.JumpIfX{
Cond: test,
},
skipTrue: ifTrue,
skipFalse: ifFalse,
}

default:
return errors.New("invalid conditional jump")
}
}

func (asm *assembler) VisitJumpConditionalOperator(ctx *parser.JumpConditionalOperatorContext) interface{} {
return parseJumpCondition(ctx)
}

func (asm *assembler) VisitIfTrue(ctx *parser.IfTrueContext) interface{} {
switch {
case ctx.Label() != nil:
return asm.Visit(ctx.Label())
default:
return uint8(asm.Visit(ctx.Number()).(uint32))
}
}

func (asm *assembler) VisitIfFalse(ctx *parser.IfFalseContext) interface{} {
switch {
case ctx.Label() != nil:
return asm.Visit(ctx.Label())
default:
return uint8(asm.Visit(ctx.Number()).(uint32))
}
}

func (asm *assembler) VisitLoadOperation(ctx *parser.LoadOperationContext) interface{} {
target := asm.Visit(ctx.LoadOperator()).(dst)

switch {
case ctx.AbsoluteNumber() != nil:
var (
number = asm.Visit(ctx.AbsoluteNumber()).(uint32)
instruction = bpf.LoadConstant{Val: number}
)
switch target {
case regA:
instruction.Dst = bpf.RegA
case regX:
instruction.Dst = bpf.RegX
}
return instruction

case ctx.RegisterR() != nil:
var (
register = asm.Visit(ctx.RegisterR()).(int)
instruction = bpf.LoadScratch{N: register}
)
switch target {
case regA:
instruction.Dst = bpf.RegA
case regX:
instruction.Dst = bpf.RegX
}
return instruction

case ctx.IndirectX() != nil:
var (
number = asm.Visit(ctx.IndirectX()).(uint32)
instruction = bpf.LoadIndirect{Off: number}
)
switch target {
case regA, regX:
instruction.Size = 1
case reg2:
instruction.Size = 2
case reg4:
instruction.Size = 4
}
return instruction

case ctx.Extension() != nil:
return bpf.LoadExtension{Num: asm.Visit(ctx.Extension()).(bpf.Extension)}

default:
return nil
}
}

func (asm *assembler) VisitLoadOperator(ctx *parser.LoadOperatorContext) interface{} {
switch {
case ctx.LDA() != nil:
return regA
case ctx.LDX() != nil:
return regX
case ctx.LDB() != nil:
return reg2
case ctx.LDH() != nil:
return reg4
default:
return invalid
}
}

func (asm *assembler) VisitStoreOperation(ctx *parser.StoreOperationContext) interface{} {
return bpf.StoreScratch{
Src: asm.Visit(ctx.StoreOperator()).(bpf.Register),
N: asm.Visit(ctx.RegisterR()).(int),
}
}

func (asm *assembler) VisitStoreOperator(ctx *parser.StoreOperatorContext) interface{} {
switch {
case ctx.STA() != nil:
return bpf.RegA
case ctx.STX() != nil:
return bpf.RegX
default:
return 0
}
}

func (asm *assembler) VisitSimpleOperation(ctx *parser.SimpleOperationContext) interface{} {
switch {
case ctx.TAX() != nil:
return bpf.TAX{}
case ctx.TXA() != nil:
return bpf.TXA{}
case ctx.NEG() != nil:
return bpf.NegateA{}
default:
return nil
}

}

func (asm *assembler) VisitReturnOperation(ctx *parser.ReturnOperationContext) interface{} {
switch {
case ctx.RegisterA() != nil:
return bpf.RetA{}

case ctx.LiteralNumber() != nil:
return bpf.RetConstant{Val: asm.Visit(ctx.LiteralNumber()).(uint32)}

default:
return nil
}
}

func (asm *assembler) VisitNumber(ctx *parser.NumberContext) interface{} {
return parseNumber(ctx.NUMBER().GetText())
}

func (asm *assembler) VisitLiteralNumber(ctx *parser.LiteralNumberContext) interface{} {
return asm.Visit(ctx.Number())
}

func (asm *assembler) VisitIndirectX(ctx *parser.IndirectXContext) interface{} {
return asm.Visit(ctx.Number())
}

func (asm *assembler) VisitAbsoluteNumber(ctx *parser.AbsoluteNumberContext) interface{} {
return asm.Visit(ctx.Number())
}

func (asm *assembler) VisitRegisterA(ctx *parser.RegisterAContext) interface{} {
return bpf.RegA
}

func (asm *assembler) VisitRegisterX(ctx *parser.RegisterXContext) interface{} {
return bpf.RegX
}

func (asm *assembler) VisitRegisterR(ctx *parser.RegisterRContext) interface{} {
return int(parseNumber(ctx.NUMBER().GetText()))
}

func (asm *assembler) VisitExtension(ctx *parser.ExtensionContext) interface{} {
return parseExtension(ctx.IDENTIFIER().GetText())
}

+ 82
- 0
cmd/bpfasm/main.go View File

@@ -0,0 +1,82 @@
/*
Package bpfasm implements a basic BPF assembler.


Output modes

To show the parsed assembly, supply the `-asm` flag.

To show the raw opcodes, compatible with `bpf_asm -c`, supply the `-opc` flag.

By default, bpfasm will output to the input file name with the added ".bin"
suffix.
*/
package main

import (
"encoding/binary"
"flag"
"fmt"
"io"
"io/ioutil"
"os"

"maze.io/x/bpf"
)

func main() {
outASM := flag.Bool("asm", false, "output assembly")
outCode := flag.Bool("opc", false, "output opcodes")
outFile := flag.String("o", "", "output file (default: stdout)")
flag.Parse()

if flag.NArg() != 1 {
fmt.Fprintln(os.Stderr, "usage: bpfasm <source>")
os.Exit(1)
}

b, err := ioutil.ReadFile(flag.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, "error loading", flag.Arg(0)+":", err)
os.Exit(2)
}

raw, err := bpf.Assemble(string(b))
if err != nil {
fmt.Fprintln(os.Stderr, "error assembling", flag.Arg(0)+":", err)
os.Exit(2)
}

if !*outASM && !*outCode && *outFile == "" {
*outFile = flag.Arg(0) + ".bin"
fmt.Fprintln(os.Stderr, "writing to", *outFile)
}

var output io.WriteCloser
if *outFile != "" {
if output, err = os.Create(*outFile); err != nil {
fmt.Fprintln(os.Stderr, "error opening", *outFile+":", err)
os.Exit(2)
}
} else {
output = os.Stdout
}
defer output.Close()

if *outCode {
for _, instruction := range raw {
fmt.Fprintf(output, "{ %#02x, %2d, %2d, %#08x },\n", instruction.Op, instruction.Jt, instruction.Jf, instruction.K)
}
} else if *outASM {
for _, instruction := range raw {
fmt.Fprintln(output, instruction.Disassemble())
}
} else {
for _, instruction := range raw {
binary.Write(output, binary.BigEndian, instruction)
}
}

//assembled := unsafe.Pointer(&raw[0])

}

+ 4
- 0
doc.go View File

@@ -0,0 +1,4 @@
/*
Package bpf is an assembler for the Berkeley Packet Filter (bpf).
*/
package bpf

+ 6
- 0
go.mod View File

@@ -0,0 +1,6 @@
module maze.io/x/bpf

require (
github.com/antlr/antlr4 v0.0.0-20190223165740-dade65a895c2
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7
)

+ 4
- 0
go.sum View File

@@ -0,0 +1,4 @@
github.com/antlr/antlr4 v0.0.0-20190223165740-dade65a895c2 h1:Q1TGw0wvj6lqZQ4/CMfZykGQDnkslNcvuDID+AfNiQE=
github.com/antlr/antlr4 v0.0.0-20190223165740-dade65a895c2/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7 h1:C2F/nMkR/9sfUTpvR3QrjBuTdvMUC/cFajkphs1YLQo=
golang.org/x/net v0.0.0-20190227160552-c95aed5357e7/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

+ 140
- 0
parse.go View File

@@ -0,0 +1,140 @@
package bpf

import (
"log"
"math"
"strconv"
"strings"

"golang.org/x/net/bpf"
"maze.io/x/bpf/parser"
)

type dst uint8

const (
regA dst = iota
regX
reg2
reg4
invalid dst = 0xff
)

func parseNumber(text string) uint32 {
switch {
case strings.HasPrefix(text, "0x"):
value, err := strconv.ParseUint(text[2:], 16, 32)
if err != nil {
log.Fatalln(err)
}
return uint32(value)

case strings.HasPrefix(text, "0b"):
value, err := strconv.ParseUint(text[2:], 2, 32)
if err != nil {
log.Fatalln(err)
}
return uint32(value)

case strings.HasPrefix(text, "0o"):
value, err := strconv.ParseUint(text[1:], 8, 32)
if err != nil {
log.Fatalln(err)
}
return uint32(value)

default:
value, err := strconv.Atoi(text)
if err != nil {
log.Fatalln(err)
}
return uint32(value)
}
}

func parseAluOpcode(ctx *parser.AluOperatorContext) bpf.ALUOp {
switch {
case ctx.ADD() != nil:
return bpf.ALUOpAdd
case ctx.SUB() != nil:
return bpf.ALUOpSub
case ctx.MUL() != nil:
return bpf.ALUOpMul
case ctx.DIV() != nil:
return bpf.ALUOpDiv
case ctx.MOD() != nil:
return bpf.ALUOpMod
case ctx.AND() != nil:
return bpf.ALUOpAnd
case ctx.OR() != nil:
return bpf.ALUOpOr
case ctx.XOR() != nil:
return bpf.ALUOpXor
case ctx.LSH() != nil:
return bpf.ALUOpShiftLeft
case ctx.RSH() != nil:
return bpf.ALUOpShiftRight
default:
return bpf.ALUOp(0)
}
}

func parseJumpCondition(ctx *parser.JumpConditionalOperatorContext) bpf.JumpTest {
switch {
case ctx.JEQ() != nil:
return bpf.JumpEqual
case ctx.JNE() != nil:
return bpf.JumpNotEqual
case ctx.JGT() != nil:
return bpf.JumpGreaterThan
case ctx.JLT() != nil:
return bpf.JumpLessThan
case ctx.JGE() != nil:
return bpf.JumpGreaterOrEqual
case ctx.JLE() != nil:
return bpf.JumpLessOrEqual
case ctx.JSE() != nil:
return bpf.JumpBitsSet
default:
return math.MaxUint16
}
}

func parseExtension(ext string) bpf.Extension {
switch ext {
case "len": // skb->len
return bpf.ExtLen
case "proto": // skb->protocol
return bpf.ExtProto
case "type": // skb->pkt_type
return bpf.ExtType
case "poff": // Payload start offset
return bpf.ExtPayloadOffset
case "ifidx": // skb->dev->ifindex
return bpf.ExtInterfaceIndex
case "nla": // Netlink attribute of type X with offset A
return bpf.ExtNetlinkAttr
case "nlan": // Nested Netlink attribute of type X with offset A
return bpf.ExtNetlinkAttrNested
case "mark": // skb->mark
return bpf.ExtMark
case "queue": // skb->queue_mapping
return bpf.ExtQueue
case "hatype": // skb->dev->type
return bpf.ExtLinkLayerType
case "rxhash": // skb->hash
return bpf.ExtRXHash
case "cpu": // raw_smp_processor_id()
return bpf.ExtCPUID
case "vlan_tci": // skb_vlan_tag_get(skb)
return bpf.ExtVLANTag
case "vlan_avail": // skb_vlan_tag_present(skb)
return bpf.ExtVLANTagPresent
case "vlan_tpid": // skb->vlan_proto
return bpf.ExtVLANProto
case "rand": // prandom_u32()
return bpf.ExtRand
default:
return 0
}
}

+ 205
- 0
parser/BPF.g4 View File

@@ -0,0 +1,205 @@
grammar BPF;

program
: (labelDefinition | labelDefinition? instruction)+ EOF
;

labelDefinition
: IDENTIFIER ':'
;

label : IDENTIFIER ;

comment
: COMMENT
;

instruction
: aluOperation
| jumpOperation
| jumpConditionalOperation
| loadOperation
| storeOperation
| simpleOperation
| returnOperation
;

aluOperation
: aluOperator (literalNumber | registerX)
;

aluOperator
: ADD
| SUB
| MUL
| DIV
| MOD
| AND
| OR
| XOR
| LSH
| RSH
;

ADD : 'add' | 'ADD' ;
SUB : 'sub' | 'SUB' ;
MUL : 'mul' | 'MUL' ;
DIV : 'div' | 'DIV' ;
MOD : 'mod' | 'MOD' ;
AND : 'and' | 'AND' ;
OR : 'or' | 'OR' ;
XOR : 'xor' | 'XOR' ;
LSH : 'lsh' | 'LSH' ;
RSH : 'rsh' | 'RSH' ;

jumpOperation
: jumpOperator number
;

jumpOperator
: JA
;

jumpConditionalOperation
: jumpConditionalOperator (literalNumber | registerX) ',' (ifTrue) (',' (ifFalse))?
;

jumpConditionalOperator
: JEQ
| JNE
| JLT
| JLE
| JGT
| JGE
| JSE
;

ifTrue
: number
| label
;

ifFalse
: number
| label
;

JA : 'ja' | 'JA' ;
JEQ : 'jeq' | 'JEQ' ;
JNE : 'jne' | 'JNE' | 'jneq' | 'JNEQ' ;
JLT : 'jlt' | 'JLT' ;
JLE : 'jle' | 'JLE' ;
JGT : 'jgt' | 'JGT' ;
JGE : 'jge' | 'JGE' ;
JSE : 'jse' | 'JSE' | 'jset' | 'JSET' ;

loadOperation
: loadOperator (indirectX | absoluteNumber | registerR | extension)
;

loadOperator
: LDA
| LDX
| LDB
| LDH
;

LDA : 'lda' | 'LDA' | 'ld' | 'LD' ;
LDX : 'ldx' | 'LDX' ;
LDB : 'ldb' | 'LDB' ;
LDH : 'ldh' | 'LDH' ;

storeOperation
: storeOperator registerR
;

storeOperator
: STA
| STX
;

STA : 'sta' | 'STA' | 'st' | 'ST' ;
STX : 'stx' | 'STX' ;

simpleOperation
: TAX
| TXA
| NEG
;

TAX : 'tax' | 'TAX' ;
TXA : 'txa' | 'TXA' ;
NEG : 'neg' | 'NEG' ;

returnOperation
: RET (registerA | literalNumber)
;

RET : 'ret' | 'RET' ;


number
: NUMBER
;

literalNumber
: ('#' number)
;

indirectX
: ('[' 'x' '+' number ']')
;

absoluteNumber
: ('[' number ']')
;

registerA
: REGA
;

registerX
: REGX
;

registerR
: 'r' NUMBER
;

extension
: IDENTIFIER
;

/*
NUMBER
: '0b' [01]+
| '0x' [0-9a-fA-F]+
| [0-9]+
| '-'? [0-9]+
;
*/
NUMBER
: '0b' [01]+
| '0x' [0-9a-fA-F]+
| '-'? [0-9]+
;

COMMENT
: ';' ~ [\r\n]* -> skip
;

IDENTIFIER
: [._a-zA-Z]+
;

WHITESPACE
: [ \t\r\n]+ -> skip
;

REGA
: 'a' | 'A'
;

REGX
: 'x' | 'X'
;

+ 183
- 0
parser/bpf_base_listener.go View File

@@ -0,0 +1,183 @@
// Code generated from parser/BPF.g4 by ANTLR 4.7.2. DO NOT EDIT.

package parser // BPF
import "github.com/antlr/antlr4/runtime/Go/antlr"

// BaseBPFListener is a complete listener for a parse tree produced by BPFParser.
type BaseBPFListener struct{}

var _ BPFListener = &BaseBPFListener{}

// VisitTerminal is called when a terminal node is visited.
func (s *BaseBPFListener) VisitTerminal(node antlr.TerminalNode) {}

// VisitErrorNode is called when an error node is visited.
func (s *BaseBPFListener) VisitErrorNode(node antlr.ErrorNode) {}

// EnterEveryRule is called when any rule is entered.
func (s *BaseBPFListener) EnterEveryRule(ctx antlr.ParserRuleContext) {}

// ExitEveryRule is called when any rule is exited.
func (s *BaseBPFListener) ExitEveryRule(ctx antlr.ParserRuleContext) {}

// EnterProgram is called when production program is entered.
func (s *BaseBPFListener) EnterProgram(ctx *ProgramContext) {}

// ExitProgram is called when production program is exited.
func (s *BaseBPFListener) ExitProgram(ctx *ProgramContext) {}

// EnterLabelDefinition is called when production labelDefinition is entered.
func (s *BaseBPFListener) EnterLabelDefinition(ctx *LabelDefinitionContext) {}

// ExitLabelDefinition is called when production labelDefinition is exited.
func (s *BaseBPFListener) ExitLabelDefinition(ctx *LabelDefinitionContext) {}

// EnterLabel is called when production label is entered.
func (s *BaseBPFListener) EnterLabel(ctx *LabelContext) {}

// ExitLabel is called when production label is exited.
func (s *BaseBPFListener) ExitLabel(ctx *LabelContext) {}

// EnterComment is called when production comment is entered.
func (s *BaseBPFListener) EnterComment(ctx *CommentContext) {}

// ExitComment is called when production comment is exited.
func (s *BaseBPFListener) ExitComment(ctx *CommentContext) {}

// EnterInstruction is called when production instruction is entered.
func (s *BaseBPFListener) EnterInstruction(ctx *InstructionContext) {}

// ExitInstruction is called when production instruction is exited.
func (s *BaseBPFListener) ExitInstruction(ctx *InstructionContext) {}

// EnterAluOperation is called when production aluOperation is entered.
func (s *BaseBPFListener) EnterAluOperation(ctx *AluOperationContext) {}

// ExitAluOperation is called when production aluOperation is exited.
func (s *BaseBPFListener) ExitAluOperation(ctx *AluOperationContext) {}

// EnterAluOperator is called when production aluOperator is entered.
func (s *BaseBPFListener) EnterAluOperator(ctx *AluOperatorContext) {}

// ExitAluOperator is called when production aluOperator is exited.
func (s *BaseBPFListener) ExitAluOperator(ctx *AluOperatorContext) {}

// EnterJumpOperation is called when production jumpOperation is entered.
func (s *BaseBPFListener) EnterJumpOperation(ctx *JumpOperationContext) {}

// ExitJumpOperation is called when production jumpOperation is exited.
func (s *BaseBPFListener) ExitJumpOperation(ctx *JumpOperationContext) {}

// EnterJumpOperator is called when production jumpOperator is entered.
func (s *BaseBPFListener) EnterJumpOperator(ctx *JumpOperatorContext) {}

// ExitJumpOperator is called when production jumpOperator is exited.
func (s *BaseBPFListener) ExitJumpOperator(ctx *JumpOperatorContext) {}

// EnterJumpConditionalOperation is called when production jumpConditionalOperation is entered.
func (s *BaseBPFListener) EnterJumpConditionalOperation(ctx *JumpConditionalOperationContext) {}

// ExitJumpConditionalOperation is called when production jumpConditionalOperation is exited.
func (s *BaseBPFListener) ExitJumpConditionalOperation(ctx *JumpConditionalOperationContext) {}

// EnterJumpConditionalOperator is called when production jumpConditionalOperator is entered.
func (s *BaseBPFListener) EnterJumpConditionalOperator(ctx *JumpConditionalOperatorContext) {}

// ExitJumpConditionalOperator is called when production jumpConditionalOperator is exited.
func (s *BaseBPFListener) ExitJumpConditionalOperator(ctx *JumpConditionalOperatorContext) {}

// EnterIfTrue is called when production ifTrue is entered.
func (s *BaseBPFListener) EnterIfTrue(ctx *IfTrueContext) {}

// ExitIfTrue is called when production ifTrue is exited.
func (s *BaseBPFListener) ExitIfTrue(ctx *IfTrueContext) {}

// EnterIfFalse is called when production ifFalse is entered.
func (s *BaseBPFListener) EnterIfFalse(ctx *IfFalseContext) {}

// ExitIfFalse is called when production ifFalse is exited.
func (s *BaseBPFListener) ExitIfFalse(ctx *IfFalseContext) {}

// EnterLoadOperation is called when production loadOperation is entered.
func (s *BaseBPFListener) EnterLoadOperation(ctx *LoadOperationContext) {}

// ExitLoadOperation is called when production loadOperation is exited.
func (s *BaseBPFListener) ExitLoadOperation(ctx *LoadOperationContext) {}

// EnterLoadOperator is called when production loadOperator is entered.
func (s *BaseBPFListener) EnterLoadOperator(ctx *LoadOperatorContext) {}

// ExitLoadOperator is called when production loadOperator is exited.
func (s *BaseBPFListener) ExitLoadOperator(ctx *LoadOperatorContext) {}

// EnterStoreOperation is called when production storeOperation is entered.
func (s *BaseBPFListener) EnterStoreOperation(ctx *StoreOperationContext) {}

// ExitStoreOperation is called when production storeOperation is exited.
func (s *BaseBPFListener) ExitStoreOperation(ctx *StoreOperationContext) {}

// EnterStoreOperator is called when production storeOperator is entered.
func (s *BaseBPFListener) EnterStoreOperator(ctx *StoreOperatorContext) {}

// ExitStoreOperator is called when production storeOperator is exited.
func (s *BaseBPFListener) ExitStoreOperator(ctx *StoreOperatorContext) {}

// EnterSimpleOperation is called when production simpleOperation is entered.
func (s *BaseBPFListener) EnterSimpleOperation(ctx *SimpleOperationContext) {}

// ExitSimpleOperation is called when production simpleOperation is exited.
func (s *BaseBPFListener) ExitSimpleOperation(ctx *SimpleOperationContext) {}

// EnterReturnOperation is called when production returnOperation is entered.
func (s *BaseBPFListener) EnterReturnOperation(ctx *ReturnOperationContext) {}

// ExitReturnOperation is called when production returnOperation is exited.
func (s *BaseBPFListener) ExitReturnOperation(ctx *ReturnOperationContext) {}

// EnterNumber is called when production number is entered.
func (s *BaseBPFListener) EnterNumber(ctx *NumberContext) {}

// ExitNumber is called when production number is exited.
func (s *BaseBPFListener) ExitNumber(ctx *NumberContext) {}

// EnterLiteralNumber is called when production literalNumber is entered.
func (s *BaseBPFListener) EnterLiteralNumber(ctx *LiteralNumberContext) {}

// ExitLiteralNumber is called when production literalNumber is exited.
func (s *BaseBPFListener) ExitLiteralNumber(ctx *LiteralNumberContext) {}

// EnterIndirectX is called when production indirectX is entered.
func (s *BaseBPFListener) EnterIndirectX(ctx *IndirectXContext) {}

// ExitIndirectX is called when production indirectX is exited.
func (s *BaseBPFListener) ExitIndirectX(ctx *IndirectXContext) {}

// EnterAbsoluteNumber is called when production absoluteNumber is entered.
func (s *BaseBPFListener) EnterAbsoluteNumber(ctx *AbsoluteNumberContext) {}

// ExitAbsoluteNumber is called when production absoluteNumber is exited.
func (s *BaseBPFListener) ExitAbsoluteNumber(ctx *AbsoluteNumberContext) {}

// EnterRegisterA is called when production registerA is entered.
func (s *BaseBPFListener) EnterRegisterA(ctx *RegisterAContext) {}

// ExitRegisterA is called when production registerA is exited.
func (s *BaseBPFListener) ExitRegisterA(ctx *RegisterAContext) {}

// EnterRegisterX is called when production registerX is entered.
func (s *BaseBPFListener) EnterRegisterX(ctx *RegisterXContext) {}

// ExitRegisterX is called when production registerX is exited.
func (s *BaseBPFListener) ExitRegisterX(ctx *RegisterXContext) {}

// EnterRegisterR is called when production registerR is entered.
func (s *BaseBPFListener) EnterRegisterR(ctx *RegisterRContext) {}

// ExitRegisterR is called when production registerR is exited.
func (s *BaseBPFListener) ExitRegisterR(ctx *RegisterRContext) {}

// EnterExtension is called when production extension is entered.
func (s *BaseBPFListener) EnterExtension(ctx *ExtensionContext) {}

// ExitExtension is called when production extension is exited.
func (s *BaseBPFListener) ExitExtension(ctx *ExtensionContext) {}

+ 116
- 0
parser/bpf_base_visitor.go View File

@@ -0,0 +1,116 @@
// Code generated from parser/BPF.g4 by ANTLR 4.7.2. DO NOT EDIT.

package parser // BPF
import "github.com/antlr/antlr4/runtime/Go/antlr"

type BaseBPFVisitor struct {
*antlr.BaseParseTreeVisitor
}

func (v *BaseBPFVisitor) VisitProgram(ctx *ProgramContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitLabelDefinition(ctx *LabelDefinitionContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitLabel(ctx *LabelContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitComment(ctx *CommentContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitInstruction(ctx *InstructionContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitAluOperation(ctx *AluOperationContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitAluOperator(ctx *AluOperatorContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitJumpOperation(ctx *JumpOperationContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitJumpOperator(ctx *JumpOperatorContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitJumpConditionalOperation(ctx *JumpConditionalOperationContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitJumpConditionalOperator(ctx *JumpConditionalOperatorContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitIfTrue(ctx *IfTrueContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitIfFalse(ctx *IfFalseContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitLoadOperation(ctx *LoadOperationContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitLoadOperator(ctx *LoadOperatorContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitStoreOperation(ctx *StoreOperationContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitStoreOperator(ctx *StoreOperatorContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitSimpleOperation(ctx *SimpleOperationContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitReturnOperation(ctx *ReturnOperationContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitNumber(ctx *NumberContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitLiteralNumber(ctx *LiteralNumberContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitIndirectX(ctx *IndirectXContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitAbsoluteNumber(ctx *AbsoluteNumberContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitRegisterA(ctx *RegisterAContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitRegisterX(ctx *RegisterXContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitRegisterR(ctx *RegisterRContext) interface{} {
return v.VisitChildren(ctx)
}

func (v *BaseBPFVisitor) VisitExtension(ctx *ExtensionContext) interface{} {
return v.VisitChildren(ctx)
}

+ 313
- 0
parser/bpf_lexer.go View File

@@ -0,0 +1,313 @@
// Code generated from parser/BPF.g4 by ANTLR 4.7.2. DO NOT EDIT.

package parser

import (
"fmt"
"unicode"

"github.com/antlr/antlr4/runtime/Go/antlr"
)

// Suppress unused import error
var _ = fmt.Printf
var _ = unicode.IsLetter

var serializedLexerAtn = []uint16{
3, 24715, 42794, 33075, 47597, 16764, 15335, 30598, 22884, 2, 44, 398,
8, 1, 4, 2, 9, 2, 4, 3, 9, 3, 4, 4, 9, 4, 4, 5, 9, 5, 4, 6, 9, 6, 4, 7,
9, 7, 4, 8, 9, 8, 4, 9, 9, 9, 4, 10, 9, 10, 4, 11, 9, 11, 4, 12, 9, 12,
4, 13, 9, 13, 4, 14, 9, 14, 4, 15, 9, 15, 4, 16, 9, 16, 4, 17, 9, 17, 4,
18, 9, 18, 4, 19, 9, 19, 4, 20, 9, 20, 4, 21, 9, 21, 4, 22, 9, 22, 4, 23,
9, 23, 4, 24, 9, 24, 4, 25, 9, 25, 4, 26, 9, 26, 4, 27, 9, 27, 4, 28, 9,
28, 4, 29, 9, 29, 4, 30, 9, 30, 4, 31, 9, 31, 4, 32, 9, 32, 4, 33, 9, 33,
4, 34, 9, 34, 4, 35, 9, 35, 4, 36, 9, 36, 4, 37, 9, 37, 4, 38, 9, 38, 4,
39, 9, 39, 4, 40, 9, 40, 4, 41, 9, 41, 4, 42, 9, 42, 4, 43, 9, 43, 3, 2,
3, 2, 3, 3, 3, 3, 3, 4, 3, 4, 3, 5, 3, 5, 3, 6, 3, 6, 3, 7, 3, 7, 3, 8,
3, 8, 3, 9, 3, 9, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 3, 10, 5, 10, 110,
10, 10, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 3, 11, 5, 11, 118, 10, 11, 3,
12, 3, 12, 3, 12, 3, 12, 3, 12, 3, 12, 5, 12, 126, 10, 12, 3, 13, 3, 13,
3, 13, 3, 13, 3, 13, 3, 13, 5, 13, 134, 10, 13, 3, 14, 3, 14, 3, 14, 3,
14, 3, 14, 3, 14, 5, 14, 142, 10, 14, 3, 15, 3, 15, 3, 15, 3, 15, 3, 15,
3, 15, 5, 15, 150, 10, 15, 3, 16, 3, 16, 3, 16, 3, 16, 5, 16, 156, 10,
16, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 3, 17, 5, 17, 164, 10, 17, 3, 18,
3, 18, 3, 18, 3, 18, 3, 18, 3, 18, 5, 18, 172, 10, 18, 3, 19, 3, 19, 3,
19, 3, 19, 3, 19, 3, 19, 5, 19, 180, 10, 19, 3, 20, 3, 20, 3, 20, 3, 20,
5, 20, 186, 10, 20, 3, 21, 3, 21, 3, 21, 3, 21, 3, 21, 3, 21, 5, 21, 194,
10, 21, 3, 22, 3, 22, 3, 22, 3, 22, 3, 22, 3, 22, 3, 22, 3, 22, 3, 22,
3, 22, 3, 22, 3, 22, 3, 22, 3, 22, 5, 22, 210, 10, 22, 3, 23, 3, 23, 3,
23, 3, 23, 3, 23, 3, 23, 5, 23, 218, 10, 23, 3, 24, 3, 24, 3, 24, 3, 24,
3, 24, 3, 24, 5, 24, 226, 10, 24, 3, 25, 3, 25, 3, 25, 3, 25, 3, 25, 3,
25, 5, 25, 234, 10, 25, 3, 26, 3, 26, 3, 26, 3, 26, 3, 26, 3, 26, 5, 26,
242, 10, 26, 3, 27, 3, 27, 3, 27, 3, 27, 3, 27, 3, 27, 3, 27, 3, 27, 3,
27, 3, 27, 3, 27, 3, 27, 3, 27, 3, 27, 5, 27, 258, 10, 27, 3, 28, 3, 28,
3, 28, 3, 28, 3, 28, 3, 28, 3, 28, 3, 28, 3, 28, 3, 28, 5, 28, 270, 10,
28, 3, 29, 3, 29, 3, 29, 3, 29, 3, 29, 3, 29, 5, 29, 278, 10, 29, 3, 30,
3, 30, 3, 30, 3, 30, 3, 30, 3, 30, 5, 30, 286, 10, 30, 3, 31, 3, 31, 3,
31, 3, 31, 3, 31, 3, 31, 5, 31, 294, 10, 31, 3, 32, 3, 32, 3, 32, 3, 32,
3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 3, 32, 5, 32, 306, 10, 32, 3, 33, 3,
33, 3, 33, 3, 33, 3, 33, 3, 33, 5, 33, 314, 10, 33, 3, 34, 3, 34, 3, 34,
3, 34, 3, 34, 3, 34, 5, 34, 322, 10, 34, 3, 35, 3, 35, 3, 35, 3, 35, 3,
35, 3, 35, 5, 35, 330, 10, 35, 3, 36, 3, 36, 3, 36, 3, 36, 3, 36, 3, 36,
5, 36, 338, 10, 36, 3, 37, 3, 37, 3, 37, 3, 37, 3, 37, 3, 37, 5, 37, 346,
10, 37, 3, 38, 3, 38, 3, 38, 3, 38, 6, 38, 352, 10, 38, 13, 38, 14, 38,
353, 3, 38, 3, 38, 3, 38, 3, 38, 6, 38, 360, 10, 38, 13, 38, 14, 38, 361,
3, 38, 5, 38, 365, 10, 38, 3, 38, 6, 38, 368, 10, 38, 13, 38, 14, 38, 369,
5, 38, 372, 10, 38, 3, 39, 3, 39, 7, 39, 376, 10, 39, 12, 39, 14, 39, 379,
11, 39, 3, 39, 3, 39, 3, 40, 6, 40, 384, 10, 40, 13, 40, 14, 40, 385, 3,
41, 6, 41, 389, 10, 41, 13, 41, 14, 41, 390, 3, 41, 3, 41, 3, 42, 3, 42,
3, 43, 3, 43, 2, 2, 44, 3, 3, 5, 4, 7, 5, 9, 6, 11, 7, 13, 8, 15, 9, 17,
10, 19, 11, 21, 12, 23, 13, 25, 14, 27, 15, 29, 16, 31, 17, 33, 18, 35,
19, 37, 20, 39, 21, 41, 22, 43, 23, 45, 24, 47, 25, 49, 26, 51, 27, 53,
28, 55, 29, 57, 30, 59, 31, 61, 32, 63, 33, 65, 34, 67, 35, 69, 36, 71,
37, 73, 38, 75, 39, 77, 40, 79, 41, 81, 42, 83, 43, 85, 44, 3, 2, 10, 3,
2, 50, 51, 5, 2, 50, 59, 67, 72, 99, 104, 3, 2, 50, 59, 4, 2, 12, 12, 15,
15, 6, 2, 48, 48, 67, 92, 97, 97, 99, 124, 5, 2, 11, 12, 15, 15, 34, 34,
4, 2, 67, 67, 99, 99, 4, 2, 90, 90, 122, 122, 2, 442, 2, 3, 3, 2, 2, 2,
2, 5, 3, 2, 2, 2, 2, 7, 3, 2, 2, 2, 2, 9, 3, 2, 2, 2, 2, 11, 3, 2, 2, 2,
2, 13, 3, 2, 2, 2, 2, 15, 3, 2, 2, 2, 2, 17, 3, 2, 2, 2, 2, 19, 3, 2, 2,
2, 2, 21, 3, 2, 2, 2, 2, 23, 3, 2, 2, 2, 2, 25, 3, 2, 2, 2, 2, 27, 3, 2,
2, 2, 2, 29, 3, 2, 2, 2, 2, 31, 3, 2, 2, 2, 2, 33, 3, 2, 2, 2, 2, 35, 3,
2, 2, 2, 2, 37, 3, 2, 2, 2, 2, 39, 3, 2, 2, 2, 2, 41, 3, 2, 2, 2, 2, 43,
3, 2, 2, 2, 2, 45, 3, 2, 2, 2, 2, 47, 3, 2, 2, 2, 2, 49, 3, 2, 2, 2, 2,
51, 3, 2, 2, 2, 2, 53, 3, 2, 2, 2, 2, 55, 3, 2, 2, 2, 2, 57, 3, 2, 2, 2,
2, 59, 3, 2, 2, 2, 2, 61, 3, 2, 2, 2, 2, 63, 3, 2, 2, 2, 2, 65, 3, 2, 2,
2, 2, 67, 3, 2, 2, 2, 2, 69, 3, 2, 2, 2, 2, 71, 3, 2, 2, 2, 2, 73, 3, 2,
2, 2, 2, 75, 3, 2, 2, 2, 2, 77, 3, 2, 2, 2, 2, 79, 3, 2, 2, 2, 2, 81, 3,
2, 2, 2, 2, 83, 3, 2, 2, 2, 2, 85, 3, 2, 2, 2, 3, 87, 3, 2, 2, 2, 5, 89,
3, 2, 2, 2, 7, 91, 3, 2, 2, 2, 9, 93, 3, 2, 2, 2, 11, 95, 3, 2, 2, 2, 13,
97, 3, 2, 2, 2, 15, 99, 3, 2, 2, 2, 17, 101, 3, 2, 2, 2, 19, 109, 3, 2,
2, 2, 21, 117, 3, 2, 2, 2, 23, 125, 3, 2, 2, 2, 25, 133, 3, 2, 2, 2, 27,
141, 3, 2, 2, 2, 29, 149, 3, 2, 2, 2, 31, 155, 3, 2, 2, 2, 33, 163, 3,
2, 2, 2, 35, 171, 3, 2, 2, 2, 37, 179, 3, 2, 2, 2, 39, 185, 3, 2, 2, 2,
41, 193, 3, 2, 2, 2, 43, 209, 3, 2, 2, 2, 45, 217, 3, 2, 2, 2, 47, 225,
3, 2, 2, 2, 49, 233, 3, 2, 2, 2, 51, 241, 3, 2, 2, 2, 53, 257, 3, 2, 2,
2, 55, 269, 3, 2, 2, 2, 57, 277, 3, 2, 2, 2, 59, 285, 3, 2, 2, 2, 61, 293,
3, 2, 2, 2, 63, 305, 3, 2, 2, 2, 65, 313, 3, 2, 2, 2, 67, 321, 3, 2, 2,
2, 69, 329, 3, 2, 2, 2, 71, 337, 3, 2, 2, 2, 73, 345, 3, 2, 2, 2, 75, 371,
3, 2, 2, 2, 77, 373, 3, 2, 2, 2, 79, 383, 3, 2, 2, 2, 81, 388, 3, 2, 2,
2, 83, 394, 3, 2, 2, 2, 85, 396, 3, 2, 2, 2, 87, 88, 7, 60, 2, 2, 88, 4,
3, 2, 2, 2, 89, 90, 7, 46, 2, 2, 90, 6, 3, 2, 2, 2, 91, 92, 7, 37, 2, 2,
92, 8, 3, 2, 2, 2, 93, 94, 7, 93, 2, 2, 94, 10, 3, 2, 2, 2, 95, 96, 7,
122, 2, 2, 96, 12, 3, 2, 2, 2, 97, 98, 7, 45, 2, 2, 98, 14, 3, 2, 2, 2,
99, 100, 7, 95, 2, 2, 100, 16, 3, 2, 2, 2, 101, 102, 7, 116, 2, 2, 102,
18, 3, 2, 2, 2, 103, 104, 7, 99, 2, 2, 104, 105, 7, 102, 2, 2, 105, 110,
7, 102, 2, 2, 106, 107, 7, 67, 2, 2, 107, 108, 7, 70, 2, 2, 108, 110, 7,
70, 2, 2, 109, 103, 3, 2, 2, 2, 109, 106, 3, 2, 2, 2, 110, 20, 3, 2, 2,
2, 111, 112, 7, 117, 2, 2, 112, 113, 7, 119, 2, 2, 113, 118, 7, 100, 2,
2, 114, 115, 7, 85, 2, 2, 115, 116, 7, 87, 2, 2, 116, 118, 7, 68, 2, 2,
117, 111, 3, 2, 2, 2, 117, 114, 3, 2, 2, 2, 118, 22, 3, 2, 2, 2, 119, 120,
7, 111, 2, 2, 120, 121, 7, 119, 2, 2, 121, 126, 7, 110, 2, 2, 122, 123,
7, 79, 2, 2, 123, 124, 7, 87, 2, 2, 124, 126, 7, 78, 2, 2, 125, 119, 3,
2, 2, 2, 125, 122, 3, 2, 2, 2, 126, 24, 3, 2, 2, 2, 127, 128, 7, 102, 2,
2, 128, 129, 7, 107, 2, 2, 129, 134, 7, 120, 2, 2, 130, 131, 7, 70, 2,
2, 131, 132, 7, 75, 2, 2, 132, 134, 7, 88, 2, 2, 133, 127, 3, 2, 2, 2,
133, 130, 3, 2, 2, 2, 134, 26, 3, 2, 2, 2, 135, 136, 7, 111, 2, 2, 136,
137, 7, 113, 2, 2, 137, 142, 7, 102, 2, 2, 138, 139, 7, 79, 2, 2, 139,
140, 7, 81, 2, 2, 140, 142, 7, 70, 2, 2, 141, 135, 3, 2, 2, 2, 141, 138,
3, 2, 2, 2, 142, 28, 3, 2, 2, 2, 143, 144, 7, 99, 2, 2, 144, 145, 7, 112,
2, 2, 145, 150, 7, 102, 2, 2, 146, 147, 7, 67, 2, 2, 147, 148, 7, 80, 2,
2, 148, 150, 7, 70, 2, 2, 149, 143, 3, 2, 2, 2, 149, 146, 3, 2, 2, 2, 150,
30, 3, 2, 2, 2, 151, 152, 7, 113, 2, 2, 152, 156, 7, 116, 2, 2, 153, 154,
7, 81, 2, 2, 154, 156, 7, 84, 2, 2, 155, 151, 3, 2, 2, 2, 155, 153, 3,
2, 2, 2, 156, 32, 3, 2, 2, 2, 157, 158, 7, 122, 2, 2, 158, 159, 7, 113,
2, 2, 159, 164, 7, 116, 2, 2, 160, 161, 7, 90, 2, 2, 161, 162, 7, 81, 2,
2, 162, 164, 7, 84, 2, 2, 163, 157, 3, 2, 2, 2, 163, 160, 3, 2, 2, 2, 164,
34, 3, 2, 2, 2, 165, 166, 7, 110, 2, 2, 166, 167, 7, 117, 2, 2, 167, 172,
7, 106, 2, 2, 168, 169, 7, 78, 2, 2, 169, 170, 7, 85, 2, 2, 170, 172, 7,
74, 2, 2, 171, 165, 3, 2, 2, 2, 171, 168, 3, 2, 2, 2, 172, 36, 3, 2, 2,
2, 173, 174, 7, 116, 2, 2, 174, 175, 7, 117, 2, 2, 175, 180, 7, 106, 2,
2, 176, 177, 7, 84, 2, 2, 177, 178, 7, 85, 2, 2, 178, 180, 7, 74, 2, 2,
179, 173, 3, 2, 2, 2, 179, 176, 3, 2, 2, 2, 180, 38, 3, 2, 2, 2, 181, 182,
7, 108, 2, 2, 182, 186, 7, 99, 2, 2, 183, 184, 7, 76, 2, 2, 184, 186, 7,
67, 2, 2, 185, 181, 3, 2, 2, 2, 185, 183, 3, 2, 2, 2, 186, 40, 3, 2, 2,
2, 187, 188, 7, 108, 2, 2, 188, 189, 7, 103, 2, 2, 189, 194, 7, 115, 2,
2, 190, 191, 7, 76, 2, 2, 191, 192, 7, 71, 2, 2, 192, 194, 7, 83, 2, 2,
193, 187, 3, 2, 2, 2, 193, 190, 3, 2, 2, 2, 194, 42, 3, 2, 2, 2, 195, 196,
7, 108, 2, 2, 196, 197, 7, 112, 2, 2, 197, 210, 7, 103, 2, 2, 198, 199,
7, 76, 2, 2, 199, 200, 7, 80, 2, 2, 200, 210, 7, 71, 2, 2, 201, 202, 7,
108, 2, 2, 202, 203, 7, 112, 2, 2, 203, 204, 7, 103, 2, 2, 204, 210, 7,
115, 2, 2, 205, 206, 7, 76, 2, 2, 206, 207, 7, 80, 2, 2, 207, 208, 7, 71,
2, 2, 208, 210, 7, 83, 2, 2, 209, 195, 3, 2, 2, 2, 209, 198, 3, 2, 2, 2,
209, 201, 3, 2, 2, 2, 209, 205, 3, 2, 2, 2, 210, 44, 3, 2, 2, 2, 211, 212,
7, 108, 2, 2, 212, 213, 7, 110, 2, 2, 213, 218, 7, 118, 2, 2, 214, 215,
7, 76, 2, 2, 215, 216, 7, 78, 2, 2, 216, 218, 7, 86, 2, 2, 217, 211, 3,
2, 2, 2, 217, 214, 3, 2, 2, 2, 218, 46, 3, 2, 2, 2, 219, 220, 7, 108, 2,
2, 220, 221, 7, 110, 2, 2, 221, 226, 7, 103, 2, 2, 222, 223, 7, 76, 2,
2, 223, 224, 7, 78, 2, 2, 224, 226, 7, 71, 2, 2, 225, 219, 3, 2, 2, 2,
225, 222, 3, 2, 2, 2, 226, 48, 3, 2, 2, 2, 227, 228, 7, 108, 2, 2, 228,
229, 7, 105, 2, 2, 229, 234, 7, 118, 2, 2, 230, 231, 7, 76, 2, 2, 231,
232, 7, 73, 2, 2, 232, 234, 7, 86, 2, 2, 233, 227, 3, 2, 2, 2, 233, 230,
3, 2, 2, 2, 234, 50, 3, 2, 2, 2, 235, 236, 7, 108, 2, 2, 236, 237, 7, 105,
2, 2, 237, 242, 7, 103, 2, 2, 238, 239, 7, 76, 2, 2, 239, 240, 7, 73, 2,
2, 240, 242, 7, 71, 2, 2, 241, 235, 3, 2, 2, 2, 241, 238, 3, 2, 2, 2, 242,
52, 3, 2, 2, 2, 243, 244, 7, 108, 2, 2, 244, 245, 7, 117, 2, 2, 245, 258,
7, 103, 2, 2, 246, 247, 7, 76, 2, 2, 247, 248, 7, 85, 2, 2, 248, 258, 7,
71, 2, 2, 249, 250, 7, 108, 2, 2, 250, 251, 7, 117, 2, 2, 251, 252, 7,
103, 2, 2, 252, 258, 7, 118, 2, 2, 253, 254, 7, 76, 2, 2, 254, 255, 7,
85, 2, 2, 255, 256, 7, 71, 2, 2, 256, 258, 7, 86, 2, 2, 257, 243, 3, 2,
2, 2, 257, 246, 3, 2, 2, 2, 257, 249, 3, 2, 2, 2, 257, 253, 3, 2, 2, 2,
258, 54, 3, 2, 2, 2, 259, 260, 7, 110, 2, 2, 260, 261, 7, 102, 2, 2, 261,
270, 7, 99, 2, 2, 262, 263, 7, 78, 2, 2, 263, 264, 7, 70, 2, 2, 264, 270,
7, 67, 2, 2, 265, 266, 7, 110, 2, 2, 266, 270, 7, 102, 2, 2, 267, 268,
7, 78, 2, 2, 268, 270, 7, 70, 2, 2, 269, 259, 3, 2, 2, 2, 269, 262, 3,
2, 2, 2, 269, 265, 3, 2, 2, 2, 269, 267, 3, 2, 2, 2, 270, 56, 3, 2, 2,
2, 271, 272, 7, 110, 2, 2, 272, 273, 7, 102, 2, 2, 273, 278, 7, 122, 2,
2, 274, 275, 7, 78, 2, 2, 275, 276, 7, 70, 2, 2, 276, 278, 7, 90, 2, 2,
277, 271, 3, 2, 2, 2, 277, 274, 3, 2, 2, 2, 278, 58, 3, 2, 2, 2, 279, 280,
7, 110, 2, 2, 280, 281, 7, 102, 2, 2, 281, 286, 7, 100, 2, 2, 282, 283,
7, 78, 2, 2, 283, 284, 7, 70, 2, 2, 284, 286, 7, 68, 2, 2, 285, 279, 3,
2, 2, 2, 285, 282, 3, 2, 2, 2, 286, 60, 3, 2, 2, 2, 287, 288, 7, 110, 2,
2, 288, 289, 7, 102, 2, 2, 289, 294, 7, 106, 2, 2, 290, 291, 7, 78, 2,
2, 291, 292, 7, 70, 2, 2, 292, 294, 7, 74, 2, 2, 293, 287, 3, 2, 2, 2,
293, 290, 3, 2, 2, 2, 294, 62, 3, 2, 2, 2, 295, 296, 7, 117, 2, 2, 296,
297, 7, 118, 2, 2, 297, 306, 7, 99, 2, 2, 298, 299, 7, 85, 2, 2, 299, 300,
7, 86, 2, 2, 300, 306, 7, 67, 2, 2, 301, 302, 7, 117, 2, 2, 302, 306, 7,
118, 2, 2, 303, 304, 7, 85, 2, 2, 304, 306, 7, 86, 2, 2, 305, 295, 3, 2,
2, 2, 305, 298, 3, 2, 2, 2, 305, 301, 3, 2, 2, 2, 305, 303, 3, 2, 2, 2,
306, 64, 3, 2, 2, 2, 307, 308, 7, 117, 2, 2, 308, 309, 7, 118, 2, 2, 309,
314, 7, 122, 2, 2, 310, 311, 7, 85, 2, 2, 311, 312, 7, 86, 2, 2, 312, 314,
7, 90, 2, 2, 313, 307, 3, 2, 2, 2, 313, 310, 3, 2, 2, 2, 314, 66, 3, 2,
2, 2, 315, 316, 7, 118, 2, 2, 316, 317, 7, 99, 2, 2, 317, 322, 7, 122,
2, 2, 318, 319, 7, 86, 2, 2, 319, 320, 7, 67, 2, 2, 320, 322, 7, 90, 2,
2, 321, 315, 3, 2, 2, 2, 321, 318, 3, 2, 2, 2, 322, 68, 3, 2, 2, 2, 323,
324, 7, 118, 2, 2, 324, 325, 7, 122, 2, 2, 325, 330, 7, 99, 2, 2, 326,
327, 7, 86, 2, 2, 327, 328, 7, 90, 2, 2, 328, 330, 7, 67, 2, 2, 329, 323,
3, 2, 2, 2, 329, 326, 3, 2, 2, 2, 330, 70, 3, 2, 2, 2, 331, 332, 7, 112,
2, 2, 332, 333, 7, 103, 2, 2, 333, 338, 7, 105, 2, 2, 334, 335, 7, 80,
2, 2, 335, 336, 7, 71, 2, 2, 336, 338, 7, 73, 2, 2, 337, 331, 3, 2, 2,
2, 337, 334, 3, 2, 2, 2, 338, 72, 3, 2, 2, 2, 339, 340, 7, 116, 2, 2, 340,
341, 7, 103, 2, 2, 341, 346, 7, 118, 2, 2, 342, 343, 7, 84, 2, 2, 343,
344, 7, 71, 2, 2, 344, 346, 7, 86, 2, 2, 345, 339, 3, 2, 2, 2, 345, 342,
3, 2, 2, 2, 346, 74, 3, 2, 2, 2, 347, 348, 7, 50, 2, 2, 348, 349, 7, 100,
2, 2, 349, 351, 3, 2, 2, 2, 350, 352, 9, 2, 2, 2, 351, 350, 3, 2, 2, 2,
352, 353, 3, 2, 2, 2, 353, 351, 3, 2, 2, 2, 353, 354, 3, 2, 2, 2, 354,
372, 3, 2, 2, 2, 355, 356, 7, 50, 2, 2, 356, 357, 7, 122, 2, 2, 357, 359,
3, 2, 2, 2, 358, 360, 9, 3, 2, 2, 359, 358, 3, 2, 2, 2, 360, 361, 3, 2,
2, 2, 361, 359, 3, 2, 2, 2, 361, 362, 3, 2, 2, 2, 362, 372, 3, 2, 2, 2,
363, 365, 7, 47, 2, 2, 364, 363, 3, 2, 2, 2, 364, 365, 3, 2, 2, 2, 365,
367, 3, 2, 2, 2, 366, 368, 9, 4, 2, 2, 367, 366, 3, 2, 2, 2, 368, 369,
3, 2, 2, 2, 369, 367, 3, 2, 2, 2, 369, 370, 3, 2, 2, 2, 370, 372, 3, 2,
2, 2, 371, 347, 3, 2, 2, 2, 371, 355, 3, 2, 2, 2, 371, 364, 3, 2, 2, 2,
372, 76, 3, 2, 2, 2, 373, 377, 7, 61, 2, 2, 374, 376, 10, 5, 2, 2, 375,
374, 3, 2, 2, 2, 376, 379, 3, 2, 2, 2, 377, 375, 3, 2, 2, 2, 377, 378,
3, 2, 2, 2, 378, 380, 3, 2, 2, 2, 379, 377, 3, 2, 2, 2, 380, 381, 8, 39,
2, 2, 381, 78, 3, 2, 2, 2, 382, 384, 9, 6, 2, 2, 383, 382, 3, 2, 2, 2,
384, 385, 3, 2, 2, 2, 385, 383, 3, 2, 2, 2, 385, 386, 3, 2, 2, 2, 386,
80, 3, 2, 2, 2, 387, 389, 9, 7, 2, 2, 388, 387, 3, 2, 2, 2, 389, 390, 3,
2, 2, 2, 390, 388, 3, 2, 2, 2, 390, 391, 3, 2, 2, 2, 391, 392, 3, 2, 2,
2, 392, 393, 8, 41, 2, 2, 393, 82, 3, 2, 2, 2, 394, 395, 9, 8, 2, 2, 395,
84, 3, 2, 2, 2, 396, 397, 9, 9, 2, 2, 397, 86, 3, 2, 2, 2, 39, 2, 109,
117, 125, 133, 141, 149, 155, 163, 171, 179, 185, 193, 209, 217, 225, 233,
241, 257, 269, 277, 285, 293, 305, 313, 321, 329, 337, 345, 353, 361, 364,
369, 371, 377, 385, 390, 3, 8, 2, 2,
}

var lexerDeserializer = antlr.NewATNDeserializer(nil)
var lexerAtn = lexerDeserializer.DeserializeFromUInt16(serializedLexerAtn)

var lexerChannelNames = []string{
"DEFAULT_TOKEN_CHANNEL", "HIDDEN",
}

var lexerModeNames = []string{
"DEFAULT_MODE",
}

var lexerLiteralNames = []string{
"", "':'", "','", "'#'", "'['", "'x'", "'+'", "']'", "'r'",
}

var lexerSymbolicNames = []string{
"", "", "", "", "", "", "", "", "", "ADD", "SUB", "MUL", "DIV", "MOD",
"AND", "OR", "XOR", "LSH", "RSH", "JA", "JEQ", "JNE", "JLT", "JLE", "JGT",
"JGE", "JSE", "LDA", "LDX", "LDB", "LDH", "STA", "STX", "TAX", "TXA", "NEG",
"RET", "NUMBER", "COMMENT", "IDENTIFIER", "WHITESPACE", "REGA", "REGX",
}

var lexerRuleNames = []string{
"T__0", "T__1", "T__2", "T__3", "T__4", "T__5", "T__6", "T__7", "ADD",
"SUB", "MUL", "DIV", "MOD", "AND", "OR", "XOR", "LSH", "RSH", "JA", "JEQ",
"JNE", "JLT", "JLE", "JGT", "JGE", "JSE", "LDA", "LDX", "LDB", "LDH", "STA",
"STX", "TAX", "TXA", "NEG", "RET", "NUMBER", "COMMENT", "IDENTIFIER", "WHITESPACE",
"REGA", "REGX",
}

type BPFLexer struct {
*antlr.BaseLexer
channelNames []string
modeNames []string
// TODO: EOF string
}

var lexerDecisionToDFA = make([]*antlr.DFA, len(lexerAtn.DecisionToState))

func init() {
for index, ds := range lexerAtn.DecisionToState {
lexerDecisionToDFA[index] = antlr.NewDFA(ds, index)
}
}

func NewBPFLexer(input antlr.CharStream) *BPFLexer {

l := new(BPFLexer)

l.BaseLexer = antlr.NewBaseLexer(input)
l.Interpreter = antlr.NewLexerATNSimulator(l, lexerAtn, lexerDecisionToDFA, antlr.NewPredictionContextCache())

l.channelNames = lexerChannelNames
l.modeNames = lexerModeNames
l.RuleNames = lexerRuleNames
l.LiteralNames = lexerLiteralNames
l.SymbolicNames = lexerSymbolicNames
l.GrammarFileName = "BPF.g4"
// TODO: l.EOF = antlr.TokenEOF

return l
}

// BPFLexer tokens.
const (
BPFLexerT__0 = 1
BPFLexerT__1 = 2
BPFLexerT__2 = 3
BPFLexerT__3 = 4
BPFLexerT__4 = 5
BPFLexerT__5 = 6
BPFLexerT__6 = 7
BPFLexerT__7 = 8
BPFLexerADD = 9
BPFLexerSUB = 10
BPFLexerMUL = 11
BPFLexerDIV = 12
BPFLexerMOD = 13
BPFLexerAND = 14
BPFLexerOR = 15
BPFLexerXOR = 16
BPFLexerLSH = 17
BPFLexerRSH = 18
BPFLexerJA = 19
BPFLexerJEQ = 20
BPFLexerJNE = 21
BPFLexerJLT = 22
BPFLexerJLE = 23
BPFLexerJGT = 24
BPFLexerJGE = 25
BPFLexerJSE = 26
BPFLexerLDA = 27
BPFLexerLDX = 28
BPFLexerLDB = 29
BPFLexerLDH = 30
BPFLexerSTA = 31
BPFLexerSTX = 32
BPFLexerTAX = 33
BPFLexerTXA = 34
BPFLexerNEG = 35
BPFLexerRET = 36
BPFLexerNUMBER = 37
BPFLexerCOMMENT = 38
BPFLexerIDENTIFIER = 39
BPFLexerWHITESPACE = 40
BPFLexerREGA = 41
BPFLexerREGX = 42
)

+ 171
- 0
parser/bpf_listener.go View File

@@ -0,0 +1,171 @@
// Code generated from parser/BPF.g4 by ANTLR 4.7.2. DO NOT EDIT.

package parser // BPF
import "github.com/antlr/antlr4/runtime/Go/antlr"

// BPFListener is a complete listener for a parse tree produced by BPFParser.
type BPFListener interface {
antlr.ParseTreeListener

// EnterProgram is called when entering the program production.
EnterProgram(c *ProgramContext)

// EnterLabelDefinition is called when entering the labelDefinition production.
EnterLabelDefinition(c *LabelDefinitionContext)

// EnterLabel is called when entering the label production.
EnterLabel(c *LabelContext)

// EnterComment is called when entering the comment production.
EnterComment(c *CommentContext)

// EnterInstruction is called when entering the instruction production.
EnterInstruction(c *InstructionContext)

// EnterAluOperation is called when entering the aluOperation production.
EnterAluOperation(c *AluOperationContext)

// EnterAluOperator is called when entering the aluOperator production.
EnterAluOperator(c *AluOperatorContext)

// EnterJumpOperation is called when entering the jumpOperation production.
EnterJumpOperation(c *JumpOperationContext)

// EnterJumpOperator is called when entering the jumpOperator production.
EnterJumpOperator(c *JumpOperatorContext)

// EnterJumpConditionalOperation is called when entering the jumpConditionalOperation production.
EnterJumpConditionalOperation(c *JumpConditionalOperationContext)

// EnterJumpConditionalOperator is called when entering the jumpConditionalOperator production.
EnterJumpConditionalOperator(c *JumpConditionalOperatorContext)

// EnterIfTrue is called when entering the ifTrue production.
EnterIfTrue(c *IfTrueContext)

// EnterIfFalse is called when entering the ifFalse production.
EnterIfFalse(c *IfFalseContext)

// EnterLoadOperation is called when entering the loadOperation production.
EnterLoadOperation(c *LoadOperationContext)

// EnterLoadOperator is called when entering the loadOperator production.
EnterLoadOperator(c *LoadOperatorContext)

// EnterStoreOperation is called when entering the storeOperation production.
EnterStoreOperation(c *StoreOperationContext)

// EnterStoreOperator is called when entering the storeOperator production.
EnterStoreOperator(c *StoreOperatorContext)

// EnterSimpleOperation is called when entering the simpleOperation production.
EnterSimpleOperation(c *SimpleOperationContext)

// EnterReturnOperation is called when entering the returnOperation production.
EnterReturnOperation(c *ReturnOperationContext)

// EnterNumber is called when entering the number production.
EnterNumber(c *NumberContext)

// EnterLiteralNumber is called when entering the literalNumber production.
EnterLiteralNumber(c *LiteralNumberContext)

// EnterIndirectX is called when entering the indirectX production.
EnterIndirectX(c *IndirectXContext)

// EnterAbsoluteNumber is called when entering the absoluteNumber production.
EnterAbsoluteNumber(c *AbsoluteNumberContext)

// EnterRegisterA is called when entering the registerA production.
EnterRegisterA(c *RegisterAContext)

// EnterRegisterX is called when entering the registerX production.
EnterRegisterX(c *RegisterXContext)

// EnterRegisterR is called when entering the registerR production.
EnterRegisterR(c *RegisterRContext)

// EnterExtension is called when entering the extension production.
EnterExtension(c *ExtensionContext)

// ExitProgram is called when exiting the program production.
ExitProgram(c *ProgramContext)

// ExitLabelDefinition is called when exiting the labelDefinition production.
ExitLabelDefinition(c *LabelDefinitionContext)

// ExitLabel is called when exiting the label production.
ExitLabel(c *LabelContext)

// ExitComment is called when exiting the comment production.
ExitComment(c *CommentContext)

// ExitInstruction is called when exiting the instruction production.
ExitInstruction(c *InstructionContext)

// ExitAluOperation is called when exiting the aluOperation production.
ExitAluOperation(c *AluOperationContext)

// ExitAluOperator is called when exiting the aluOperator production.
ExitAluOperator(c *AluOperatorContext)

// ExitJumpOperation is called when exiting the jumpOperation production.
ExitJumpOperation(c *JumpOperationContext)

// ExitJumpOperator is called when exiting the jumpOperator production.
ExitJumpOperator(c *JumpOperatorContext)

// ExitJumpConditionalOperation is called when exiting the jumpConditionalOperation production.
ExitJumpConditionalOperation(c *JumpConditionalOperationContext)

// ExitJumpConditionalOperator is called when exiting the jumpConditionalOperator production.
ExitJumpConditionalOperator(c *JumpConditionalOperatorContext)

// ExitIfTrue is called when exiting the ifTrue production.
ExitIfTrue(c *IfTrueContext)

// ExitIfFalse is called when exiting the ifFalse production.
ExitIfFalse(c *IfFalseContext)

// ExitLoadOperation is called when exiting the loadOperation production.
ExitLoadOperation(c *LoadOperationContext)

// ExitLoadOperator is called when exiting the loadOperator production.
ExitLoadOperator(c *LoadOperatorContext)

// ExitStoreOperation is called when exiting the storeOperation production.
ExitStoreOperation(c *StoreOperationContext)

// ExitStoreOperator is called when exiting the storeOperator production.
ExitStoreOperator(c *StoreOperatorContext)

// ExitSimpleOperation is called when exiting the simpleOperation production.
ExitSimpleOperation(c *SimpleOperationContext)

// ExitReturnOperation is called when exiting the returnOperation production.
ExitReturnOperation(c *ReturnOperationContext)

// ExitNumber is called when exiting the number production.
ExitNumber(c *NumberContext)

// ExitLiteralNumber is called when exiting the literalNumber production.
ExitLiteralNumber(c *LiteralNumberContext)

// ExitIndirectX is called when exiting the indirectX production.
ExitIndirectX(c *IndirectXContext)

// ExitAbsoluteNumber is called when exiting the absoluteNumber production.
ExitAbsoluteNumber(c *AbsoluteNumberContext)

// ExitRegisterA is called when exiting the registerA production.
ExitRegisterA(c *RegisterAContext)

// ExitRegisterX is called when exiting the registerX production.
ExitRegisterX(c *RegisterXContext)

// ExitRegisterR is called when exiting the registerR production.
ExitRegisterR(c *RegisterRContext)

// ExitExtension is called when exiting the extension production.
ExitExtension(c *ExtensionContext)
}

+ 3667
- 0
parser/bpf_parser.go
File diff suppressed because it is too large
View File


+ 90
- 0
parser/bpf_visitor.go View File

@@ -0,0 +1,90 @@
// Code generated from parser/BPF.g4 by ANTLR 4.7.2. DO NOT EDIT.

package parser // BPF
import "github.com/antlr/antlr4/runtime/Go/antlr"

// A complete Visitor for a parse tree produced by BPFParser.
type BPFVisitor interface {
antlr.ParseTreeVisitor

// Visit a parse tree produced by BPFParser#program.
VisitProgram(ctx *ProgramContext) interface{}

// Visit a parse tree produced by BPFParser#labelDefinition.
VisitLabelDefinition(ctx *LabelDefinitionContext) interface{}

// Visit a parse tree produced by BPFParser#label.
VisitLabel(ctx *LabelContext) interface{}

// Visit a parse tree produced by BPFParser#comment.
VisitComment(ctx *CommentContext) interface{}

// Visit a parse tree produced by BPFParser#instruction.
VisitInstruction(ctx *InstructionContext) interface{}

// Visit a parse tree produced by BPFParser#aluOperation.
VisitAluOperation(ctx *AluOperationContext) interface{}

// Visit a parse tree produced by BPFParser#aluOperator.
VisitAluOperator(ctx *AluOperatorContext) interface{}

// Visit a parse tree produced by BPFParser#jumpOperation.
VisitJumpOperation(ctx *JumpOperationContext) interface{}

// Visit a parse tree produced by BPFParser#jumpOperator.
VisitJumpOperator(ctx *JumpOperatorContext) interface{}

// Visit a parse tree produced by BPFParser#jumpConditionalOperation.
VisitJumpConditionalOperation(ctx *JumpConditionalOperationContext) interface{}

// Visit a parse tree produced by BPFParser#jumpConditionalOperator.
VisitJumpConditionalOperator(ctx *JumpConditionalOperatorContext) interface{}

// Visit a parse tree produced by BPFParser#ifTrue.
VisitIfTrue(ctx *IfTrueContext) interface{}

// Visit a parse tree produced by BPFParser#ifFalse.
VisitIfFalse(ctx *IfFalseContext) interface{}

// Visit a parse tree produced by BPFParser#loadOperation.
VisitLoadOperation(ctx *LoadOperationContext) interface{}

// Visit a parse tree produced by BPFParser#loadOperator.
VisitLoadOperator(ctx *LoadOperatorContext) interface{}

// Visit a parse tree produced by BPFParser#storeOperation.
VisitStoreOperation(ctx *StoreOperationContext) interface{}

// Visit a parse tree produced by BPFParser#storeOperator.
VisitStoreOperator(ctx *StoreOperatorContext) interface{}

// Visit a parse tree produced by BPFParser#simpleOperation.
VisitSimpleOperation(ctx *SimpleOperationContext) interface{}

// Visit a parse tree produced by BPFParser#returnOperation.
VisitReturnOperation(ctx *ReturnOperationContext) interface{}

// Visit a parse tree produced by BPFParser#number.
VisitNumber(ctx *NumberContext) interface{}

// Visit a parse tree produced by BPFParser#literalNumber.
VisitLiteralNumber(ctx *LiteralNumberContext) interface{}

// Visit a parse tree produced by BPFParser#indirectX.
VisitIndirectX(ctx *IndirectXContext) interface{}

// Visit a parse tree produced by BPFParser#absoluteNumber.
VisitAbsoluteNumber(ctx *AbsoluteNumberContext) interface{}

// Visit a parse tree produced by BPFParser#registerA.
VisitRegisterA(ctx *RegisterAContext) interface{}

// Visit a parse tree produced by BPFParser#registerX.
VisitRegisterX(ctx *RegisterXContext) interface{}

// Visit a parse tree produced by BPFParser#registerR.
VisitRegisterR(ctx *RegisterRContext) interface{}

// Visit a parse tree produced by BPFParser#extension.
VisitExtension(ctx *ExtensionContext) interface{}
}

+ 4
- 0
testdata/example1.asm View File

@@ -0,0 +1,4 @@
ldh [12]
jneq #2054,1
ret #4096
ret #0

+ 4
- 0
testdata/example2.asm View File

@@ -0,0 +1,4 @@
ld rand
jlt #2,0
ret #4096
ret #0

+ 17
- 0
testdata/example3.asm View File

@@ -0,0 +1,17 @@
;SECCOMP filter example

ld [4] ; offsetof(struct seccomp_data, arch)
jne #0xc000003e, bad ; AUDIT_ARCH_X86_64
ld [0] ; offsetof(struct seccomp_data, nr)
jeq #15, good ; __NR_rt_sigreturn
jeq #231, good ; __NR_exit_group
jeq #60, good ; __NR_exit
jeq #0, good ; __NR_read
jeq #1, good ; __NR_write
jeq #5, good ; __NR_fstat
jeq #9, good ; __NR_mmap
jeq #14, good ; __NR_rt_sigprocmask
jeq #13, good ; __NR_rt_sigaction
jeq #35, good ; __NR_nanosleep
bad: ret #0 ; SECCOMP_RET_KILL_THREAD
good: ret #0x7fff0000 ; SECCOMP_RET_ALLOW

+ 11
- 0
testdata/example4.asm View File

@@ -0,0 +1,11 @@
; icmp random packet sampling, 1 in 4
ldh [12]
jne #0x800, drop
ldb [23]
jneq #1, drop
; get a random uint32 number
ld rand
mod #4
jneq #1, drop
ret #-1
drop: ret #0

Loading…
Cancel
Save