File: BPFParser.mly

package info (click to toggle)
herdtools7 7.58-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,732 kB
  • sloc: ml: 128,583; ansic: 3,827; makefile: 670; python: 407; sh: 212; awk: 14
file content (158 lines) | stat: -rw-r--r-- 4,219 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
%{
(****************************************************************************)
(*                           the diy toolsuite                              *)
(*                                                                          *)
(* Copyright (c) 2024 Puranjay Mohan <puranjay@kernel.org>                  *)
(*                                                                          *)
(*                                                                          *)
(* This software is governed by the CeCILL-B license under French law and   *)
(* abiding by the rules of distribution of free software. You can use,      *)
(* modify and/ or redistribute the software under the terms of the CeCILL-B *)
(* license as circulated by CEA, CNRS and INRIA at the following URL        *)
(* "http://www.cecill.info". We also give a copy in LICENSE.txt.            *)
(****************************************************************************)

module A=BPFBase

%}

%token EOF
%token <BPFBase.reg> ARCH_REG
%token <string> SYMB_REG
%token <int> NUM
%token <string> NAME
%token <int> PROC
%token <BPFBase.signed * BPFBase.width> SIZE

%token SEMI PIPE COLON LPAR RPAR MINUS EQUAL STAR PLUS
%token COMMA

/* Instruction tokens */
%token <BPFBase.op> ALU_OP
%token <BPFBase.op> AMOF
%token <BPFBase.width> AMOXCHGT
%token <BPFBase.width> AMOCMPXCHGT
%token LOCK
%token GOTO
%token IF
%token <BPFBase.cond> COND
%token LDAQ
%token STRL

%type <MiscParser.proc list * (BPFBase.pseudo) list list> main
%start  main

%%

main:
| semi_opt proc_list iol_list EOF { $2,$3 }

semi_opt:
| { () }
| SEMI { () }

proc_list:
| ps=separated_nonempty_list(PIPE,PROC) SEMI
  { List.map (fun p -> p,None,MiscParser.Main) ps }

iol_list :
|  instr_option_list SEMI
    {[$1]}
|  instr_option_list SEMI iol_list {$1::$3}

instr_option_list :
  | instr_option
      {[$1]}
  | instr_option PIPE instr_option_list
      {$1::$3}

instr_option :
|            { A.Nop }
| NAME COLON instr_option { A.Label ($1,$3) }
| instr      { A.Instruction $1}

reg:
| SYMB_REG { A.Symbolic_reg $1 }
| ARCH_REG { $1 }

k:
| NUM { $1 }
| MINUS NUM { -$2 }
| PLUS NUM { $2 }

instr:
/* ALU OPS */

/* Register operand */
| reg ALU_OP reg
  { A.OP ($2,$1,$3) }
/* Immediate operand */
| reg ALU_OP k
  { A.OPI ($2,$1,$3) }

/* LDX r0 = *(size *)(r1 + 0) */
| reg EQUAL STAR LPAR SIZE STAR RPAR LPAR reg k RPAR
  { let s,w = $5 in
    A.LOAD (w,s,$1,$9,$10) }

/* LDAQ rd = load_acquire ((u64/u32 *)(rs + offset16)) */
| reg EQUAL LDAQ LPAR LPAR SIZE STAR RPAR LPAR reg k RPAR RPAR
 { let _,w = $6 in
   A.LDAQ (w,$1,$10,$11) }

/* STX *(size *)(r1 + 0) = r2  */
| STAR LPAR SIZE STAR RPAR LPAR reg k RPAR EQUAL reg
  { let _,w = $3 in
    A.STORE (w,$7,$8,$11) }

/* ST *(size *)(r1 + 0) = imm  */
| STAR LPAR SIZE STAR RPAR LPAR reg k RPAR EQUAL k
  { let _,w = $3 in
    A.STOREI (w,$7,$8,$11) }

/* STRL store_release ((size *)(rd + offset16), rs) */
| STRL LPAR LPAR SIZE STAR RPAR LPAR reg k RPAR COMMA reg RPAR
  { let _,w = $4 in
    A.STRL (w,$8,$9,$12) }

/* MOV r0 = r1 */
| reg EQUAL reg
  { A.MOV($1, $3)}

/* MOV r0 = 10 */
| reg EQUAL k
  { A.MOVI($1, $3)}

/* atomic ops with fetch rs = atomic_fetch_or ((u64 *)(rd + offset16), rs)  */
| reg EQUAL AMOF LPAR LPAR SIZE STAR RPAR LPAR reg k RPAR COMMA reg RPAR
  { let op = $3 in
    let _,w = $6 in
   A.AMO(op, w, $10, $11, $14, A.SC, true) }

/* atomic exchange rs = xchg_64 (rd + offset16, rs) */
| reg EQUAL AMOXCHGT LPAR reg k COMMA reg RPAR
  { let sz = $3 in
   A.AMO(A.AMOXCHG, sz, $5, $6, $8, A.SC, true) }

/* atomic compare and exchange r0 = cmpxchg_64 (rd + offset16, r0, rs) */
| reg EQUAL AMOCMPXCHGT LPAR reg k COMMA reg COMMA reg RPAR
  { let sz = $3 in
   A.AMO(A.AMOCMPXCHG, sz, $5, $6, $10, A.SC, true) }

/* atomic operations without fetch lock *(u64 *)(rd + offset16) = rs  */
| LOCK STAR LPAR SIZE STAR RPAR LPAR reg k RPAR ALU_OP reg
  { let _,w = $4 in
      let op = $11 in
    A.AMO(op, w, $8, $9, $12, A.X, false) }

/* Unconditional jump to label */
| GOTO NAME
  { A.GOTO($2) }

/* Conditional jump to label */
| IF reg COND reg GOTO NAME
  { let c=$3 in
    A.JCOND(c, $2, $4, $6)  }
| IF reg COND k GOTO NAME
  { let c=$3 in
    A.JCONDI(c, $2, $4, $6)  }