Skip to content

Commit

Permalink
improve parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Aug 8, 2024
1 parent 293bea0 commit b0064ba
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
20 changes: 19 additions & 1 deletion ocaml/parser/common.ml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ and string_of_operand (op : operand_t) =
| Ops ps -> let pstrs = List.map string_of_operand ps in
"{ " ^ (String.concat ", " pstrs) ^ " }"

let string_of_cond cond =
let string_of_cond (cond : cond_t) =
match cond with
| Eq (op0, op1) -> string_of_operand op0 ^ " == " ^ string_of_operand op1
| Neq (op0, op1) -> string_of_operand op0 ^ " != " ^ string_of_operand op1
Expand All @@ -88,6 +88,7 @@ let string_of_cond cond =
let string_of_instr instr =
match instr with
| Nop -> "nop"
| Err -> "err:"
| Label z -> "L" ^ (Z.to_string z)
| Assign (l, t, op) -> string_of_operand l ^ " = (" ^
string_of_type t ^ ") " ^ string_of_operand op
Expand All @@ -97,6 +98,8 @@ let string_of_instr instr =
" - " ^ string_of_operand r1
| Mul (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0 ^
" * " ^ string_of_operand r1
| Div (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0 ^
" / " ^ string_of_operand r1
| Wmul (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0 ^
" w* " ^ string_of_operand r1
| And (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0 ^
Expand All @@ -105,6 +108,8 @@ let string_of_instr instr =
" | " ^ string_of_operand r1
| Xor (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0 ^
" ^ " ^ string_of_operand r1
| Eq (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0 ^
" == " ^ string_of_operand r1
| Neq (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0 ^
" != " ^ string_of_operand r1
| Rshift (l, r0, r1) -> string_of_operand l ^ " = " ^ string_of_operand r0
Expand Down Expand Up @@ -146,10 +151,23 @@ let string_of_instr instr =
| VecUnpackHi (l, r) -> string_of_operand l ^
" = [vec_unpack_hi_expr] " ^ string_of_operand r
| DeferredInit v -> string_of_operand v ^ " = DEFERRED_INIT"
| BitFieldRef (l, p0, p1, p2) -> string_of_operand l ^ " = BIT_FIELD_REF <" ^
string_of_operand p0 ^ ", " ^
string_of_operand p1 ^ ", " ^
string_of_operand p2 ^ ">"
| VCondMask (l, p0, p1, p2) -> string_of_operand l ^ " = VCOND_MASK (" ^
string_of_operand p0 ^ ", " ^
string_of_operand p1 ^ ", " ^
string_of_operand p2 ^ ")"
| ViewConvertExpr (l, t, r) -> string_of_operand l ^ " = VIEW_CONVERT_EXPR <" ^
string_of_type t ^ ">(" ^
string_of_operand r ^ ")"
| VecPermExpr (l, op0, op1, op2) -> string_of_operand l ^ " = VEC_PERM_EXPR <" ^
string_of_operand op0 ^ ", " ^
string_of_operand op1 ^ ", " ^
string_of_operand op2 ^ ">"
| StoreLanes (m, r) -> string_of_operand m ^ " = STORE_LANES (" ^
string_of_operand r ^ ")"

let string_of_func f =
let strings_of_instrs =
Expand Down
9 changes: 9 additions & 0 deletions ocaml/parser/gimpleLexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"short" , SHORT;
"struct" , STRUCT;
"const" , CONST;
"static" , STATIC;
"signed" , SIGNED;
"unsigned" , UNSIGNED;
"long" , LONG;
Expand Down Expand Up @@ -56,6 +57,7 @@ token = parse
| '}' { RBRACK }
| '<' { LANGLE }
| '>' { RANGLE }
| '.' { DOT }
| ',' { COMMA }
| ':' { COLON }
| ';' { SEMICOLON }
Expand All @@ -65,6 +67,7 @@ token = parse
| '+' { ADDOP }
| '-' { SUBOP }
| '*' { MULOP }
| '/' { DIVOP }
| "w*" { WMULOP }
| '&' { ANDOP }
| '|' { OROP }
Expand All @@ -82,7 +85,11 @@ token = parse
| "WIDEN_MULT_PLUS_EXPR" { WMADDOP }
| "WIDEN_MULT_MINUS_EXPR" { WMSUBOP }
| ".DEFERRED_INIT" { DEFERRED_INIT }
| "BIT_FIELD_REF" { BIT_FIELD_REF }
| ".VCOND_MASK" { VCOND_MASK }
| ".STORE_LANES" { STORE_LANES }
| "VIEW_CONVERT_EXPR" { VIEW_CONVERT_EXPR }
| "VEC_PERM_EXPR" { VEC_PERM_EXPR }
| "vec_unpack_lo_expr" { VEC_UNPACK_LO_EXPR }
| "vec_unpack_hi_expr" { VEC_UNPACK_HI_EXPR }
(* Types *)
Expand All @@ -107,7 +114,9 @@ token = parse
| "char * {ref-all}" { CHAR_REF_ALL }
| "CLOBBER(eos)" { CLOBBER_EOS }
| "CLOBBER(eol)" { CLOBBER_EOL }
| "CLOBBER" { CLOBBER }
| "tail call" { TAIL_CALL }
| "err:" { ERR_LABEL }
| identity as id { try
Hashtbl.find keywords id
with Not_found ->
Expand Down
41 changes: 33 additions & 8 deletions ocaml/parser/gimpleParser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@
%token <int> UINT SINT BYTE BOOLS UBOOLS

%token LPAREN RPAREN LSQUARE RSQUARE LBRACK RBRACK LANGLE RANGLE
%token COMMA SEMICOLON COLON DQUOTE
%token COMMA SEMICOLON COLON DQUOTE DOT
/* Operators */
%token ADDOP SUBOP MULOP WMULOP ANDOP OROP XOROP LSHIFT RSHIFT EQOP NEQOP
%token LEOP GEOP EEQOP
%token LEOP GEOP EEQOP DIVOP
%token WMADDOP WMSUBOP QUESTION RARROW DEFERRED_INIT VCOND_MASK
%token VEC_UNPACK_LO_EXPR VEC_UNPACK_HI_EXPR
%token VEC_UNPACK_LO_EXPR VEC_UNPACK_HI_EXPR VIEW_CONVERT_EXPR
%token STORE_LANES VEC_PERM_EXPR BIT_FIELD_REF

/* Types */
%token CONST VOID BOOL CHAR INT SHORT LONG SIGNED UNSIGNED VECTOR STRUCT
%token BOOLS UBOOLS
%token BOOLS UBOOLS STATIC
/* Others */
%token ATTRIBUTE ACCESS MEM EOF RETURN BB IF ELSE GOTO
%token REMOVING_BASIC_BLOCK CHAR_REF_ALL PERCENT LOCAL_COUNT
%token CLOBBER_EOS CLOBBER_EOL TAIL_CALL
%token CLOBBER_EOS CLOBBER_EOL CLOBBER TAIL_CALL ERR_LABEL

%start gimple
%type <Syntax.function_t list> gimple
Expand Down Expand Up @@ -81,6 +83,9 @@ var_decl:
typ ID SEMICOLON { { vty = $1; vname = $2 } }
| typ ID LSQUARE NUM RSQUARE SEMICOLON { { vty = Array ($4, $1);
vname = $2 } }
| STATIC typ ID LSQUARE NUM RSQUARE EQOP STRING SEMICOLON
{ { vty = Array ($5, $2);
vname = $3 } }
;

ground_typ:
Expand Down Expand Up @@ -111,6 +116,7 @@ ground_typ:

typ:
ground_typ { $1 }
| STRUCT MULOP { Pointer (Struct "") }
| typ MULOP { Pointer $1 }
| typ LSQUARE NUM RSQUARE { Array ($3, $1) }
;
Expand Down Expand Up @@ -140,30 +146,40 @@ op:
;

ops:
| op { [ $1 ] }
| op COMMA ops { $1::$3 }
| mem COMMA ops { $1::$3 }
| mem ID COMMA ops { $1::$4 }
| op { [$1] }
| mem { [$1] }
| mem ID { [$1] }
| { [] }
;

instr:
| op EQOP LBRACK ops RBRACK LBRACK CLOBBER_EOS RBRACK SEMICOLON
{ Nop }
| op EQOP LBRACK ops RBRACK LBRACK CLOBBER_EOL RBRACK SEMICOLON
{ Nop }
| op EQOP LBRACK ops RBRACK LBRACK CLOBBER RBRACK SEMICOLON
{ Nop }
| ERR_LABEL { Err }
| LANGLE BB NUM RANGLE LSQUARE LOCAL_COUNT COLON NUM RSQUARE COLON
{ Label $3 }
| op EQOP LPAREN typ RPAREN op SEMICOLON
{ Assign ($1, $4, $6) }
| op EQOP op SEMICOLON { Assign ($1, Void, $3) }
| op EQOP op ADDOP op SEMICOLON { Add ($1, $3, $5) }
| op EQOP op SUBOP op SEMICOLON { Sub ($1, $3, $5) }
| op EQOP op WMULOP op SEMICOLON { Wmul ($1, $3, $5) }
| op EQOP op MULOP op SEMICOLON { Mul ($1, $3, $5) }
| op EQOP op DIVOP op SEMICOLON { Div ($1, $3, $5) }
| op EQOP op ANDOP op SEMICOLON { And ($1, $3, $5) }
| op EQOP op OROP op SEMICOLON { Or ($1, $3, $5) }
| op EQOP op XOROP op SEMICOLON { Xor ($1, $3, $5) }
| op EQOP op EEQOP op SEMICOLON { Eq ($1, $3, $5) }
| op EQOP op NEQOP op SEMICOLON { Neq ($1, $3, $5) }
| op EQOP op RSHIFT op SEMICOLON { Rshift ($1, $3, $5) }
| op EQOP op LSHIFT op SEMICOLON { Lshift ($1, $3, $5) }
| LANGLE BB NUM RANGLE LSQUARE LOCAL_COUNT COLON NUM RSQUARE COLON
{ Label $3 }
| op EQOP op QUESTION op COLON op SEMICOLON
{ Ite ($1, $3, $5, $7) }
| op EQOP mem SEMICOLON { Load ($1, $3) }
Expand All @@ -186,8 +202,16 @@ instr:
| op EQOP DEFERRED_INIT LPAREN NUM COMMA NUM COMMA
ANDOP STRING LSQUARE NUM RSQUARE RPAREN SEMICOLON
{ DeferredInit ($1) }
| op EQOP BIT_FIELD_REF LANGLE op COMMA op COMMA op RANGLE SEMICOLON
{ BitFieldRef ($1, $5, $7, $9) }
| op EQOP VCOND_MASK LPAREN op COMMA op COMMA op RPAREN SEMICOLON
{ VCondMask ($1, $5, $7, $9) }
| op EQOP VIEW_CONVERT_EXPR LANGLE typ RANGLE LPAREN op RPAREN SEMICOLON
{ ViewConvertExpr ($1, $5, $8) }
| op EQOP VEC_PERM_EXPR LANGLE op COMMA op COMMA op RANGLE SEMICOLON
{ VecPermExpr ($1, $5, $7, $9) }
| mem EQOP STORE_LANES LPAREN op RPAREN SEMICOLON
{ StoreLanes ($1, $5) }
| IF LPAREN condition RPAREN
GOTO LANGLE BB NUM RANGLE SEMICOLON LSQUARE FLOAT PERCENT RSQUARE
ELSE
Expand All @@ -204,6 +228,7 @@ mem:
| MULOP loc { Deref (Mem (Void, $2)) }
| MEM LANGLE typ RANGLE LSQUARE loc RSQUARE
{ Mem ($3, $6) }
| ANDOP mem { Ref $2 }
;

loc:
Expand Down
8 changes: 7 additions & 1 deletion ocaml/parser/syntax.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ type cond_t = | Eq of operand_t * operand_t | Neq of operand_t * operand_t

type label_t = Z.t

type instr_t = Nop
type instr_t = Nop | Err
| Label of label_t
| Assign of operand_t * type_t * operand_t
| Add of operand_t * operand_t * operand_t
| Sub of operand_t * operand_t * operand_t
| Mul of operand_t * operand_t * operand_t
| Div of operand_t * operand_t * operand_t
| Wmul of operand_t * operand_t * operand_t
| And of operand_t * operand_t * operand_t
| Or of operand_t * operand_t * operand_t
| Xor of operand_t * operand_t * operand_t
| Eq of operand_t * operand_t * operand_t
| Neq of operand_t * operand_t * operand_t
| Rshift of operand_t * operand_t * operand_t
| Lshift of operand_t * operand_t * operand_t
Expand All @@ -57,7 +59,11 @@ type instr_t = Nop
| VecUnpackLo of operand_t * operand_t
| VecUnpackHi of operand_t * operand_t
| DeferredInit of operand_t
| BitFieldRef of operand_t * operand_t * operand_t * operand_t
| VCondMask of operand_t * operand_t *operand_t * operand_t
| ViewConvertExpr of operand_t * type_t * operand_t
| VecPermExpr of operand_t * operand_t * operand_t * operand_t
| StoreLanes of operand_t * operand_t

type function_t = { attr : attribute_t; fty : type_t; fname : string;
params : param_t list; vars : var_t list; instrs : instr_t list }
Expand Down
8 changes: 7 additions & 1 deletion ocaml/parser/syntax.mli
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,18 @@ type cond_t = | Eq of operand_t * operand_t | Neq of operand_t * operand_t

type label_t = Z.t

type instr_t = Nop
type instr_t = Nop | Err
| Label of label_t
| Assign of operand_t * type_t * operand_t
| Add of operand_t * operand_t * operand_t
| Sub of operand_t * operand_t * operand_t
| Mul of operand_t * operand_t * operand_t
| Div of operand_t * operand_t * operand_t
| Wmul of operand_t * operand_t * operand_t
| And of operand_t * operand_t * operand_t
| Or of operand_t * operand_t * operand_t
| Xor of operand_t * operand_t * operand_t
| Eq of operand_t * operand_t * operand_t
| Neq of operand_t * operand_t * operand_t
| Rshift of operand_t * operand_t * operand_t
| Lshift of operand_t * operand_t * operand_t
Expand All @@ -57,7 +59,11 @@ type instr_t = Nop
| VecUnpackLo of operand_t * operand_t
| VecUnpackHi of operand_t * operand_t
| DeferredInit of operand_t
| BitFieldRef of operand_t * operand_t * operand_t * operand_t
| VCondMask of operand_t * operand_t *operand_t * operand_t
| ViewConvertExpr of operand_t * type_t * operand_t
| VecPermExpr of operand_t * operand_t * operand_t * operand_t
| StoreLanes of operand_t * operand_t

type function_t = { attr : attribute_t; fty : type_t; fname : string;
params : param_t list; vars : var_t list; instrs : instr_t list }
Expand Down

0 comments on commit b0064ba

Please sign in to comment.