File: RISCVParser.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 (169 lines) | stat: -rw-r--r-- 4,545 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
159
160
161
162
163
164
165
166
167
168
169
%{
(****************************************************************************)
(*                           the diy toolsuite                              *)
(*                                                                          *)
(* Jade Alglave, University College London, UK.                             *)
(* Luc Maranget, INRIA Paris-Rocquencourt, France.                          *)
(*                                                                          *)
(* Copyright 2017-present Institut National de Recherche en Informatique et *)
(* en Automatique and the authors. All rights reserved.                     *)
(*                                                                          *)
(* 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=RISCVBase

(* We consider memory order and IO as the same*)
let tr_rw = function
  | "r" -> A.R
  | "ir" -> A.IR
  | "w" -> A.W
  | "ow" -> A.OW 
  | "rw"  -> A.RW
  | "iorw" -> A.IORW
  | _ -> raise Parsing.Parse_error
%}

%token EOF
%token <RISCVBase.reg> ARCH_REG
%token <string> SYMB_REG
%token <int> NUM
%token <string> NAME
%token <int> PROC

%token SEMI COMMA PIPE COLON LPAR RPAR

/* Instruction tokens */
%token <RISCVBase.opi> OPI
%token <RISCVBase.opiw> OPIW
%token LI LUI LA
%token <RISCVBase.op> OP
%token <RISCVBase.opw> OPW
%token J
%token <RISCVBase.cond> BCC
%token <RISCVBase.width * RISCVBase.signed * RISCVBase.mo > LOAD
%token <RISCVBase.width * RISCVBase.mo> STORE
%token <RISCVBase.width * RISCVBase.mo> LR
%token <RISCVBase.width * RISCVBase.mo> SC
%token <RISCVBase.opamo * RISCVBase.width * RISCVBase.mo> AMO
%token FENCE FENCEI FENCETSO AUIPC
%token <string> META
%token NOP RET MV
%token <RISCVBase.signed * RISCVBase.width> EXT
%type <MiscParser.proc list * (RISCVBase.parsedPseudo) list list> main
%type <RISCVBase.parsedPseudo list> instr_option_seq
%start main instr_option_seq

%%

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_seq :
  | is=separated_nonempty_list(SEMI,instr_option) EOF { is }

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  { MetaConst.Int $1 }
| META { MetaConst.Meta $1 }

addr:
| NUM LPAR reg RPAR
    { $1,$3 }
| LPAR reg RPAR
    { 0,$2 }

addr0:
| NUM LPAR reg RPAR
    { if $1 <> 0 then raise Parsing.Parse_error;
      $3 }
| LPAR reg RPAR
    { $2 }

instr:
| NOP
  { A.INop }
| RET
  { A.Ret }
/* OPs */
| LI reg COMMA k
  { A.OpI (A.ORI,$2,A.Ireg A.X0,$4) }
| LA reg COMMA NAME
  { A.OpA (A.LA,$2,$4) }
| LUI reg COMMA k
  { A.OpI2 (A.LUI,$2,$4) }
| OPI reg COMMA reg COMMA k
  { A.OpI ($1,$2,$4,$6) }
| OPIW reg COMMA reg COMMA k
  { A.OpIW ($1,$2,$4,$6) }
| OP reg COMMA reg COMMA reg
  { A.Op ($1,$2,$4,$6) }
| OPW reg COMMA reg COMMA reg
  { A.OpW ($1,$2,$4,$6) }
| J NAME
    { A.J $2 }
| BCC reg COMMA reg COMMA NAME
    { A.Bcc ($1,$2,$4,$6) }
| MV reg COMMA reg
    { A.OpI (A.ADDI, $2, $4, MetaConst.Int 0) }
| LOAD reg COMMA addr
    { let w,s,mo = $1 in
    let off,r = $4 in
    A.Load (w,s,mo,$2,off,r) }
| STORE reg COMMA addr
    {let w,mo = $1 in
     let off,r = $4 in
     A.Store (w,mo,$2,off,r) }
| LR reg COMMA addr0
    { let w,mo = $1 in
    A.LoadReserve (w,mo,$2,$4) }
| SC reg COMMA reg COMMA addr0
    { let w,mo = $1 in
    A.StoreConditional (w,mo,$2,$4,$6) }
| AUIPC reg COMMA k
    { A.AUIPC ($2, $4) }
| AMO reg COMMA reg COMMA addr0
    { let op,w,mo = $1 in
    A.Amo (op,w,mo,$2,$4,$6) }
| FENCEI
    { A.FenceIns A.FenceI }
| FENCETSO
    { A.FenceIns A.FenceTSO }
| FENCE
    { A.FenceIns (A.Fence (A.RW,A.RW)) }
| FENCE NAME COMMA NAME
    { A.FenceIns (A.Fence (tr_rw $2,tr_rw $4)) }
| EXT reg COMMA reg
    { let s,w = $1 in A.Ext (s,w,$2,$4) }