cil-formatparse.opp.exp 54.9 KB
Newer Older
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
File "cil-formatparse.mly", line 259, characters 14-21:
Warning: the token AND_AND is unused.
File "cil-formatparse.mly", line 274, characters 24-27:
Warning: the token ASM is unused.
File "cil-formatparse.mly", line 244, characters 45-49:
Warning: the token AUTO is unused.
File "cil-formatparse.mly", line 276, characters 7-21:
Warning: the token BLOCKATTRIBUTE is unused.
File "cil-formatparse.mly", line 275, characters 7-21:
Warning: the token BUILTIN_VA_ARG is unused.
File "cil-formatparse.mly", line 275, characters 22-37:
Warning: the token BUILTIN_VA_LIST is unused.
File "cil-formatparse.mly", line 267, characters 14-18:
Warning: the token CASE is unused.
File "cil-formatparse.mly", line 305, characters 48-52:
Warning: the token CAST is unused.
File "cil-formatparse.mly", line 264, characters 7-12:
Warning: the token COLON is unused.
File "cil-formatparse.mly", line 233, characters 16-24:
Warning: the token CST_CHAR is unused.
File "cil-formatparse.mly", line 235, characters 16-25:
Warning: the token CST_FLOAT is unused.
File "cil-formatparse.mly", line 236, characters 16-26:
Warning: the token CST_STRING is unused.
File "cil-formatparse.mly", line 237, characters 16-27:
Warning: the token CST_WSTRING is unused.
File "cil-formatparse.mly", line 277, characters 7-15:
Warning: the token DECLSPEC is unused.
File "cil-formatparse.mly", line 267, characters 19-26:
Warning: the token DEFAULT is unused.
File "cil-formatparse.mly", line 268, characters 13-15:
Warning: the token DO is unused.
File "cil-formatparse.mly", line 242, characters 7-11:
Warning: the token ENUM is unused.
File "cil-formatparse.mly", line 240, characters 7-10:
Warning: the token EOF is unused.
File "cil-formatparse.mly", line 244, characters 16-22:
Warning: the token EXTERN is unused.
File "cil-formatparse.mly", line 268, characters 16-19:
Warning: the token FOR is unused.
File "cil-formatparse.mly", line 274, characters 35-45:
Warning: the token FUNCTION__ is unused.
File "cil-formatparse.mly", line 266, characters 22-26:
Warning: the token GOTO is unused.
File "cil-formatparse.mly", line 274, characters 17-23:
Warning: the token INLINE is unused.
File "cil-formatparse.mly", line 241, characters 40-45:
Warning: the token INT32 is unused.
File "cil-formatparse.mly", line 241, characters 34-39:
Warning: the token INT64 is unused.
File "cil-formatparse.mly", line 274, characters 64-71:
Warning: the token LABEL__ is unused.
File "cil-formatparse.mly", line 261, characters 17-28:
Warning: the token MINUS_MINUS is unused.
File "cil-formatparse.mly", line 278, characters 16-21:
Warning: the token MSASM is unused.
File "cil-formatparse.mly", line 278, characters 22-28:
Warning: the token MSATTR is unused.
File "cil-formatparse.mly", line 238, characters 16-26:
Warning: the token NAMED_TYPE is unused.
File "cil-formatparse.mly", line 259, characters 22-31:
Warning: the token PIPE_PIPE is unused.
File "cil-formatparse.mly", line 261, characters 7-16:
Warning: the token PLUS_PLUS is unused.
File "cil-formatparse.mly", line 279, characters 7-13:
Warning: the token PRAGMA is unused.
File "cil-formatparse.mly", line 274, characters 46-63:
Warning: the token PRETTY_FUNCTION__ is unused.
File "cil-formatparse.mly", line 264, characters 38-43:
Warning: the token QUEST is unused.
File "cil-formatparse.mly", line 244, characters 50-58:
Warning: the token REGISTER is unused.
File "cil-formatparse.mly", line 243, characters 7-13:
Warning: the token SIGNED is unused.
File "cil-formatparse.mly", line 244, characters 23-29:
Warning: the token STATIC is unused.
File "cil-formatparse.mly", line 267, characters 7-13:
Warning: the token SWITCH is unused.
File "cil-formatparse.mly", line 269, characters 10-14:
Warning: the token THEN is unused.
File "cil-formatparse.mly", line 242, characters 19-26:
Warning: the token TYPEDEF is unused.
83 84
File "cil-formatparse.mly", line 338, characters 0-10:
Warning: symbol initialize is unreachable from any of the start symbol(s).
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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
%{
open Cil
open Pretty
module E = Errormsg

let parse_error msg : 'a =           (* sm: c++-mode highlight hack: -> ' <- *)
  E.hadErrors := true;
  E.parse_error
    msg
    (Parsing.symbol_start ()) (Parsing.symbol_end ())


let getArg (argname: string) (args: (string * formatArg) list) = 
  try 
    snd (List.find (fun (n, a) -> n = argname) args)
  with _ -> 
    E.s (error "Pattern string %s does not have argument with name %s\n"
           !Lexerhack.currentPattern argname)

let wrongArgType (which: string) (expected: string) (found: formatArg) = 
  E.s (bug "Expecting %s argument (%s) and found %a\n" 
         expected which d_formatarg found)

let doUnop (uo: unop) subexp = 
  ((fun args -> 
        let e = (fst subexp) args in
        UnOp(uo, e, typeOf e)),

   (fun e -> match e with 
     UnOp(uo', e', _) when uo  = uo' -> (snd subexp) e' 
   | _ -> None)) 

let buildPlus e1 e2 : exp = 
  let t1 = typeOf e1 in
  if isPointerType t1 then 
    BinOp(PlusPI, e1, e2, t1)
  else 
    BinOp(PlusA, e1, e2, t1)

let buildMinus e1 e2 : exp = 
  let t1 = typeOf e1 in
  let t2 = typeOf e2 in
  if isPointerType t1 then
    if isPointerType t2 then
      BinOp(MinusPP, e1, e2, intType)
    else
      BinOp(MinusPI, e1, e2, t1)
  else
    BinOp(MinusA, e1, e2, t1)

let doBinop bop e1t e2t = 
  ((fun args -> 
    let e1 = (fst e1t) args in 
    let e2 = (fst e2t) args in 
    let t1 = typeOf e1 in
    BinOp(bop, e1, e2, t1)),

   (fun e -> match e with 
     BinOp(bop', e1, e2, _) when bop' = bop -> begin
       match (snd e1t) e1, (snd e2t) e2 with
         Some m1, Some m2 -> Some (m1 @ m2)
       | _, _ -> None
     end
   | _ -> None))

(* Check the equivalence of two format lists *)
let rec checkSameFormat (fl1: formatArg list) (fl2: formatArg list) = 
  match fl1, fl2 with 
    [], [] -> true
  | h1::t1, h2::t2 -> begin
      let rec checkOffsetEq o1 o2 = 
        match o1, o2 with
          NoOffset, NoOffset -> true
        | Field(f1, o1'), Field(f2, o2') -> 
            f1.fname = f2.fname && checkOffsetEq o1' o2'
        | Index(e1, o1'), Index(e2, o2') -> 
            checkOffsetEq o1' o2' && checkExpEq e1 e2
        | _, _ -> false

      and checkExpEq e1 e2 = 
        match e1, e2 with 
          Const(CInt64(n1, _, _)), Const(CInt64(n2, _, _)) -> n1 = n2
        | Lval l1, Lval l2 -> checkLvalEq l1 l2
        | UnOp(uo1, e1, _), UnOp(uo2, e2, _) -> 
            uo1 = uo2 && checkExpEq e1 e2
        | BinOp(bo1, e11, e12, _), BinOp(bo2, e21, e22, _) -> 
            bo1 = bo2 && checkExpEq e11 e21 && checkExpEq e21 e22
        | AddrOf l1, AddrOf l2 -> checkLvalEq l1 l2
        | StartOf l1, StartOf l2 -> checkLvalEq l1 l2
        | SizeOf t1, SizeOf t2 -> typeSig t1 = typeSig t2
        | _, _ -> 
            ignore (E.warn "checkSameFormat for Fe"); false

      and checkLvalEq l1 l2 = 
        match l1, l2 with
          (Var v1, o1), (Var v2, o2) -> v1 == v2 && checkOffsetEq o1 o2
        | (Mem e1, o1), (Mem e2, o2) -> 
            checkOffsetEq o1 o2 && checkExpEq e1 e2
        | _, _ -> false
      in
      let hdeq = 
        match h1, h2 with 
          Fv v1, Fv v2 -> v1 == v2
        | Fd n1, Fd n2 -> n1 = n2
        | Fe e1, Fe e2 -> checkExpEq e1 e2
        | Fi i1, Fi i2 -> ignore (E.warn "checkSameFormat for Fi"); false
        | Ft t1, Ft t2 -> typeSig t1 = typeSig t2
        | Fl l1, Fl l2 -> checkLvalEq l1 l2
        | Fo o1, Fo o2 -> checkOffsetEq o1 o2
        | Fc c1, Fc c2 -> c1 == c2
        | _, _ -> false
      in
      hdeq || checkSameFormat t1 t2
  end
  | _, _ -> false

let matchBinopEq (bopeq: binop -> bool) lvt et = 
  (fun i -> match i with 
    Set (lv, BinOp(bop', Lval (lv'), e', _), l) when bopeq bop' -> begin
      match lvt lv, lvt lv', et e' with 
        Some m1, Some m1', Some m2 -> 
          (* Must check that m1 and m2 are the same *)
          if checkSameFormat m1 m1' then 
            Some (m1 @ m2)
          else
            None
      | _, _, _ -> None
     end
  | _ -> None)

let doBinopEq bop lvt et = 
  ((fun loc args -> 
    let l = (fst lvt) args in
    Set(l, BinOp(bop, (Lval l), (fst et) args, typeOfLval l), loc)),

   matchBinopEq (fun bop' -> bop = bop') (snd lvt) (snd et))


let getField (bt: typ) (fname: string) : fieldinfo = 
  match unrollType bt with 
    TComp(ci, _) -> begin
      try
        List.find (fun f -> fname = f.fname) ci.cfields
      with Not_found -> 
        E.s (bug "Cannot find field %s in %s\n" fname (compFullName ci))
    end
  | t -> E.s (bug "Trying to access field %s in non-struct\n" fname) 


let matchIntType (ik: ikind) (t:typ) : formatArg list option = 
  match unrollType t with 
    TInt(ik', _) when ik = ik' -> Some []
  | _ -> None

let matchFloatType (fk: fkind) (t:typ) : formatArg list option = 
  match unrollType t with 
    TFloat(fk', _) when fk = fk' -> Some []
  | _ -> None

let doAttr (id: string) 
           (aargs: (((string * formatArg) list -> attrparam list) * 
                    (attrparam list -> formatArg list option)) option)
    = 
  let t = match aargs with 
    Some t -> t
  | None -> (fun _ -> []), 
            (function [] -> Some [] | _ -> None)
  in
  ((fun args -> Attr (id, (fst t) args)), 
   
   (fun attrs -> 
     (* Find the attributes with the same ID *)
     List.fold_left 
       (fun acc a -> 
         match acc, a with 
           Some _, _ -> acc (* We found one already *)
         | None, Attr(id', args) when id = id' -> 
             (* Now match the arguments *)
             (snd t) args 
         | None, _ -> acc)
       None
       attrs))


type falist = formatArg list

type maybeInit = 
    NoInit
  | InitExp of exp
  | InitCall of lval * exp list

%}
%start expression
%start instr
%start lval
%start offset
%start stmt
%start stmt_list
%start typename
284 285 286 287
%token ALIGNOF
%token AND
%token AND_AND
%token AND_EQ
288
%token <string> ARG_A
289 290
%token <string> ARG_E
%token <string> ARG_F
291
%token <string> ARG_I
292 293
%token <string> ARG_P
%token <string> ARG_S
294
%token <string> ARG_b
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
%token <string> ARG_c
%token <string> ARG_d
%token <string> ARG_e
%token <string> ARG_eo
%token <string> ARG_f
%token <string> ARG_g
%token <string> ARG_i
%token <string> ARG_k
%token <string> ARG_l
%token <string> ARG_lo
%token <string> ARG_o
%token <string> ARG_p
%token <string> ARG_s
%token <string> ARG_t
%token <string> ARG_u
%token <string> ARG_v
%token <string> ARG_va
%token ARROW
%token ASM
%token ATTRIBUTE
%token AUTO
%token BLOCKATTRIBUTE
%token BREAK
%token BUILTIN_VA_ARG
%token BUILTIN_VA_LIST
%token CASE
%token CHAR
322
%token CIRC
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
%token CIRC_EQ
%token COLON
%token COMMA
%token CONST
%token CONTINUE
%token <string> CST_CHAR
%token <string> CST_FLOAT
%token <string> CST_INT
%token <string> CST_STRING
%token <string> CST_WSTRING
%token DECLSPEC
%token DEFAULT
%token DO
%token DOT
%token DOUBLE
%token ELLIPSIS
%token ELSE
%token ENUM
%token EOF
%token EQ
343
%token EQ_EQ
344 345 346 347 348 349 350 351 352
%token EXCLAM
%token EXCLAM_EQ
%token EXTERN
%token FLOAT
%token FOR
%token FUNCTION__
%token GOTO
%token <string> IDENT
%token IF
353
%token INF
354
%token INF_EQ
355
%token INF_INF
356 357 358 359 360 361 362 363 364 365
%token INF_INF_EQ
%token INLINE
%token INT
%token INT32
%token INT64
%token LABEL__
%token LBRACE
%token LBRACKET
%token LONG
%token LPAREN
366
%token MINUS
367 368 369 370 371
%token MINUS_EQ
%token MINUS_MINUS
%token <string> MSASM
%token <string> MSATTR
%token <string> NAMED_TYPE
372
%token PERCENT
373 374 375 376 377 378
%token PERCENT_EQ
%token PIPE
%token PIPE_EQ
%token PIPE_PIPE
%token PLUS
%token PLUS_EQ
379
%token PLUS_PLUS
380 381
%token PRAGMA
%token PRETTY_FUNCTION__
382
%token QUEST
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
%token RBRACE
%token RBRACKET
%token REGISTER
%token RESTRICT
%token RETURN
%token RPAREN
%token SEMICOLON
%token SHORT
%token SIGNED
%token SIZEOF
%token SLASH
%token SLASH_EQ
%token STAR
%token STAR_EQ
%token STATIC
%token STRUCT
%token SUP
%token SUP_EQ
%token SUP_SUP
%token SUP_SUP_EQ
%token SWITCH
%token THEN
%token TILDE
%token TYPEDEF
%token TYPEOF
%token UNION
%token UNSIGNED
%token VOID
%token VOLATILE
%token WHILE
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
%nonassoc IF
%nonassoc ELSE
%left COMMA
%nonassoc ARG_I ARG_d ARG_e ARG_g ARG_i ARG_l ARG_lo ARG_v
%right AND_EQ CIRC_EQ EQ INF_INF_EQ MINUS_EQ PERCENT_EQ PIPE_EQ PLUS_EQ SLASH_EQ STAR_EQ SUP_SUP_EQ
%right COLON
%left PIPE_PIPE
%left AND_AND
%left ARG_b
%left PIPE
%left CIRC
%left AND
%left EQ_EQ EXCLAM_EQ
%left INF INF_EQ SUP SUP_EQ
%left INF_INF SUP_SUP
%left MINUS PLUS
%left CONST PERCENT RESTRICT SLASH STAR VOLATILE
%right ADDROF ALIGNOF ARG_u CAST EXCLAM MINUS_MINUS PLUS_PLUS RPAREN SIZEOF TILDE
%left LBRACKET
%left ARROW DOT LBRACE LPAREN
%nonassoc CST_INT IDENT QUEST
434 435 436 437 438 439 440 441 442 443 444 445 446
%type <((string * Cil.formatArg) list -> Cil.exp) * (Cil.exp -> Cil.formatArg list option)> constant
%type <((string * Cil.formatArg) list -> Cil.exp) * (Cil.exp -> Cil.formatArg list option)> expression
%type <(Cil.location -> (string * Cil.formatArg) list -> Cil.instr) * (Cil.instr -> Cil.formatArg list option)> instr
%type <((string * Cil.formatArg) list -> Cil.lval) * (Cil.lval -> Cil.formatArg list option)> lval
%type <(Cil.typ -> (string * Cil.formatArg) list -> Cil.offset) * (Cil.offset -> Cil.formatArg list option)> offset
%type <((string * Cil.formatArg) list -> (string * Cil.typ * Cil.attributes) list option * bool) * ((string * Cil.typ * Cil.attributes) list option * bool -> Cil.formatArg list option)> parameters
%type <((string -> Cil.typ -> Cil.varinfo) -> Cil.location -> (string * Cil.formatArg) list -> Cil.stmt)> stmt
%type <((string -> Cil.typ -> Cil.varinfo) -> Cil.location -> (string * Cil.formatArg) list -> Cil.stmt list)> stmt_list
%type <(Cil.attributes -> (string * Cil.formatArg) list -> Cil.typ) * (Cil.typ -> Cil.formatArg list option)> type_spec
%type <((string * Cil.formatArg) list -> Cil.typ) * (Cil.typ -> Cil.formatArg list option)> typename
%%

expression:
447
  _1 = ARG_e
448 449
    {                       (  (* Count arguments eagerly *) 
                            let currentArg = _1 in 
450 451 452 453 454 455 456
                            ((fun args ->
                               match getArg currentArg args with 
                                   Fe e -> e
                                 | a -> wrongArgType currentArg 
                                            "expression" a),

                             (fun e -> Some [ Fe e ]))
457 458 459 460 461
                         )}
| _1 = constant
    {                   ( _1 )}
| _1 = lval %prec IDENT
    {                        ( ((fun args -> Lval ((fst _1) args)),
462 463

                             (fun e -> match e with 
464
                                Lval l -> (snd _1) l 
465
                              | _ -> None))
466 467 468
                         )}
| _1 = SIZEOF _2 = expression
    {          ( ((fun args -> SizeOfE ((fst _2) args)),
469 470

                           fun e -> match e with 
471
                             SizeOfE e' -> (snd _2) e'
472
                           | _ -> None)
473 474 475
                        )}
| _1 = SIZEOF _2 = LPAREN _3 = typename _4 = RPAREN
    {                        ( ((fun args -> SizeOf ((fst _3) args)),
476 477
                          
                           (fun e -> match e with 
478
                              SizeOf t -> (snd _3) t
479
                           |  _ -> None))
480 481 482
                        )}
| _1 = ALIGNOF _2 = expression
    {          ( ((fun args -> AlignOfE ((fst _2) args)),
483 484

                           (fun e -> match e with
485 486 487 488
                             AlignOfE e' -> (snd _2) e' | _ -> None))
                        )}
| _1 = ALIGNOF _2 = LPAREN _3 = typename _4 = RPAREN
    {          ( ((fun args -> AlignOf ((fst _3) args)),
489 490

                           (fun e -> match e with
491 492 493 494 495 496 497 498 499 500 501 502 503 504
                             AlignOf t' -> (snd _3) t' | _ -> None))
                        )}
| _1 = PLUS _2 = expression
    {          ( _2 )}
| _1 = MINUS _2 = expression
    {          ( doUnop Neg _2 )}
| _1 = EXCLAM _2 = expression
    {          ( doUnop LNot _2 )}
| _1 = TILDE _2 = expression
    {          ( doUnop BNot _2 )}
| _1 = argu _2 = expression %prec ARG_u
    {                        ( ((fun args -> 
                             let e = (fst _2) args in
                             UnOp((fst _1) args, e, typeOf e)),
505 506 507

                           (fun e -> match e with 
                             UnOp(uo, e', _) -> begin
508
                               match (snd _1) uo, (snd _2) e' with 
509 510 511 512
                                 Some m1, Some m2 -> Some (m1 @ m2)
                               | _ -> None
                             end
                           | _ -> None))
513 514 515 516
                        )}
| _1 = AND _2 = expression %prec ADDROF
    {          ( ((fun args -> 
                             match (fst _2) args with
517 518 519
                                Lval l -> mkAddrOf l
                              | _ -> E.s (bug "AddrOf applied to a non lval")),
                          (fun e -> match e with 
520 521 522 523 524 525 526 527
                            AddrOf l -> (snd _2) (Lval l)
                          | e -> (snd _2) (Lval (mkMem e NoOffset))))
                         )}
| _1 = LPAREN _2 = expression _3 = RPAREN
    {                        ( _2 )}
| _1 = expression _2 = PLUS _3 = expression
    {   ( ((fun args -> buildPlus ((fst _1) args) 
                                                  ((fst _3) args)), 
528 529
                          (fun e -> match e with 
                            BinOp((PlusPI|PlusA), e1, e2, _) -> begin
530
                              match (snd _1) e1, (snd _3) e2 with
531 532 533 534
                                Some m1, Some m2 -> Some (m1 @ m2)
                              | _, _ -> None
                            end
                          | _ -> None))
535 536 537 538
                        )}
| _1 = expression _2 = MINUS _3 = expression
    {                        ( ((fun args -> buildMinus ((fst _1) args)
                                                   ((fst _3) args)),
539 540 541 542

                           (fun e -> match e with 
                             BinOp((MinusPP|MinusPI|MinusA), e1, e2, _) -> 
                               begin
543
                                 match (snd _1) e1, (snd _3) e2 with
544 545 546 547
                                   Some m1, Some m2 -> Some (m1 @ m2)
                                 | _, _ -> None
                               end
                           | _ -> None))
548 549 550 551 552 553
                        )}
| _1 = expression _2 = argb _3 = expression %prec ARG_b
    {                        ( ((fun args -> 
                               let e1 = (fst _1) args in 
                               let bop = (fst _2) args in
                               let e2 = (fst _3) args in 
554 555 556 557 558
                               let t1 = typeOf e1 in
                               BinOp(bop, e1, e2, t1)),
                           
                           (fun e -> match e with 
                             BinOp(bop, e1, e2, _) -> begin
559
                               match (snd _1) e1,(snd _2) bop,(snd _3) e2 with
560 561 562 563 564
                                 Some m1, Some m2, Some m3 -> 
                                   Some (m1 @ m2 @ m3)
                               | _, _, _ -> None
                             end
                           | _ -> None))
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
                        )}
| _1 = expression _2 = STAR _3 = expression
    {   ( doBinop Mult _1 _3 )}
| _1 = expression _2 = SLASH _3 = expression
    {   ( doBinop Div _1 _3 )}
| _1 = expression _2 = PERCENT _3 = expression
    {   ( doBinop Mod _1 _3 )}
| _1 = expression _2 = INF_INF _3 = expression
    {   ( doBinop Shiftlt _1 _3 )}
| _1 = expression _2 = SUP_SUP _3 = expression
    {   ( doBinop Shiftrt _1 _3 )}
| _1 = expression _2 = AND _3 = expression
    {   ( doBinop BAnd _1 _3 )}
| _1 = expression _2 = PIPE _3 = expression
    {   ( doBinop BOr _1 _3 )}
| _1 = expression _2 = CIRC _3 = expression
    {   ( doBinop BXor _1 _3 )}
| _1 = expression _2 = EQ_EQ _3 = expression
    {   ( doBinop Eq _1 _3 )}
| _1 = expression _2 = EXCLAM_EQ _3 = expression
    {   ( doBinop Ne _1 _3 )}
| _1 = expression _2 = INF _3 = expression
    {   ( doBinop Lt _1 _3 )}
| _1 = expression _2 = SUP _3 = expression
    {   ( doBinop Gt _1 _3 )}
| _1 = expression _2 = INF_EQ _3 = expression
    {   ( doBinop Le _1 _3 )}
| _1 = expression _2 = SUP_EQ _3 = expression
    {   ( doBinop Ge _1 _3 )}
| _1 = LPAREN _2 = typename _3 = RPAREN _4 = expression
    {           ( ((fun args -> 
                              let t = (fst _2) args in
                              let e = (fst _4) args in
598 599 600 601 602 603 604 605
                              mkCast e t),
                            
                            (fun e -> 
                              let t', e' = 
                                match e with 
                                  CastE (t', e') -> t', e'
                                | _ -> typeOf e, e
                              in
606
                              match (snd _2) t', (snd _4 e') with
607 608
                                Some m1, Some m2 -> Some (m1 @ m2)
                              | _, _ -> None))
609
                         )}
610 611

argu:
612
  _1 = ARG_u
613
    {                       ( let currentArg = _1 in
614 615 616 617 618 619
                         ((fun args ->
                           match getArg currentArg args with
                             Fu uo -> uo
                           | a -> wrongArgType currentArg "unnop" a),

                          fun uo -> Some [ Fu uo ])
620
                       )}
621 622

argb:
623
  _1 = ARG_b
624
    {                       ( let currentArg = _1 in
625 626 627 628 629 630
                         ((fun args ->
                           match getArg currentArg args with
                             Fb bo -> bo
                           | a -> wrongArgType currentArg "binop" a),

                          fun bo -> Some [ Fb bo ])
631
                       )}
632 633

constant:
634
  _1 = ARG_d
635
    {                       ( let currentArg = _1 in
636 637 638 639 640 641 642 643 644
                           ((fun args ->
                             match getArg currentArg args with
                               Fd n -> integer n
                              | a -> wrongArgType currentArg "integer" a),

                            fun e -> match e with 
                              Const(CInt64(n, _, _)) -> 
                                Some [ Fd (Int64.to_int n) ]
                            | _ -> None) 
645 646 647
                         )}
| _1 = ARG_g
    {                      ( let currentArg = _1 in
648 649 650 651 652 653 654 655 656
                        ((fun args ->
                             match getArg currentArg args with
                               Fg s -> Const(CStr s)
                              | a -> wrongArgType currentArg "string" a),

                            fun e -> match e with 
                              Const(CStr s) ->
                                Some [ Fg s ]
                            | _ -> None) 
657 658 659
                         )}
| _1 = CST_INT
    {                         ( let n = parseInt _1 in
660 661 662 663 664 665
                           ((fun args -> n),

                            (fun e -> match e, n with 
                              Const(CInt64(e', _, _)), 
                              Const(CInt64(n', _, _)) when e' = n' -> Some []
                            | _ -> None))
666
                         )}
667 668

lval:
669
  _1 = ARG_l
670
    {                      ( let currentArg = _1 in
671 672 673 674 675 676 677
                           ((fun args -> 
                                match getArg currentArg args with 
                                  Fl l -> l
                                | Fv v -> Var v, NoOffset
                                | a -> wrongArgType currentArg "lval" a),

                            fun l -> Some [ Fl l ])
678 679 680 681 682
                         )}
| _1 = argv _2 = offset %prec ARG_v
    {                         ( ((fun args -> 
                              let v = (fst _1) args in
                               (Var v, (fst _2) v.vtype args)),
683 684 685

                            (fun l -> match l with 
                              Var vi, off -> begin
686
                                match (snd _1) vi, (snd _2) off with 
687 688 689 690
                                  Some m1, Some m2 -> Some (m1 @ m2)
                                | _ -> None
                              end
                            | _ -> None))
691 692 693
                         )}
| _1 = STAR _2 = expression
    {                         ( ((fun args -> mkMem ((fst _2) args) NoOffset),
694 695

                           (fun l -> match l with 
696
                              Mem e, NoOffset -> (snd _2) e
697
                           | _, _ -> None))
698 699 700 701
                         )}
| _1 = expression _2 = ARROW _3 = IDENT _4 = offset
    {             ( ((fun args -> 
                   let e = (fst _1) args in
702 703 704
                   let baset = 
                     match unrollTypeDeep (typeOf e) with 
                       TPtr (t, _) -> t
705
                     | _ -> E.s (bug "Expecting a pointer for field %s\n" _3)
706
                   in
707 708
                   let fi = getField baset _3 in
                   mkMem e (Field(fi, (fst _4) fi.ftype args))),
709 710

                (fun l -> match l with 
711 712
                   Mem e, Field(fi, off) when fi.fname = _3 -> begin
                     match (snd _1) e, (snd _4) off with 
713 714 715 716
                       Some m1, Some m2 -> Some (m1 @ m2)
                     | _, _ -> None
                   end
                | _, _ -> None))
717 718 719 720
             )}
| _1 = LPAREN _2 = STAR _3 = expression _4 = RPAREN _5 = offset
    {             ( ((fun args -> 
                 let e = (fst _3) args in
721 722 723 724 725
                 let baset = 
                   match unrollTypeDeep (typeOf e) with 
                     TPtr (t, _) -> t
                   | _ -> E.s (bug "Expecting a pointer\n")
                 in
726
                 mkMem e ((fst _5) baset args)),
727 728 729

                (fun l -> match l with 
                  Mem e, off -> begin
730
                    match (snd _3) e, (snd _5 off) with 
731 732 733 734
                      Some m1, Some m2 -> Some (m1 @ m2)
                    | _, _ -> None
                  end
                | _, _ -> None))
735
              )}
736 737

argv:
738
  _1 = ARG_v
739
    {                       ( let currentArg = _1 in
740 741 742 743 744 745
                         ((fun args ->
                           match getArg currentArg args with
                             Fv v -> v
                           | a -> wrongArgType currentArg "varinfo" a),

                          fun v -> Some [ Fv v ])
746 747 748
                       )}
| _1 = IDENT
    {                       ( let currentArg = _1 in
749 750 751 752 753 754
                         ((fun args -> 
                           match getArg currentArg args with 
                             Fv v -> v
                           | a -> wrongArgType currentArg "varinfo" a),
                         (fun v -> 
                             E.s (bug "identifiers (%s) are not supported for deconstruction" currentArg)))
755
                       )}
756 757

offset:
758
  _1 = ARG_o
759
    {                     ( let currentArg = _1 in
760 761 762 763 764 765
                            ((fun t args -> 
                                match getArg currentArg args with 
                                  Fo o -> o
                                | a -> wrongArgType currentArg "offset" a),

                              (fun off -> Some [ Fo off ]))
766
                          )}
767
| 
768
    {                          ( ((fun t args -> NoOffset),
769 770 771 772

                             (fun off -> match off with 
                                NoOffset -> Some []
                              | _ -> None))
773 774 775 776 777
                          )}
| _1 = DOT _2 = IDENT _3 = offset
    {                          ( ((fun t args -> 
                                let fi = getField t _2 in
                                Field (fi, (fst _3) fi.ftype args)),
778 779

                            (fun off -> match off with 
780 781
                               Field (fi, off') when fi.fname = _2 -> 
                                 (snd _3) off'
782
                            | _ -> None))
783 784 785
                          )}
| _1 = LBRACKET _2 = expression _3 = RBRACKET _4 = offset
    {                   ( ((fun t args -> 
786 787 788 789 790
                     let bt = 
                       match unrollType t with 
                         TArray(bt, _, _) -> bt 
                       | _ -> E.s (error "Formatcil: expecting an array for index")
                     in
791 792
                     let e = (fst _2) args in 
                     Index(e, (fst _4) bt args)),
793 794 795

                    (fun off -> match off with 
                      Index (e, off') -> begin
796
                        match (snd _2) e, (snd _4) off with 
797 798 799 800
                          Some m1, Some m2 -> Some (m1 @ m2)
                        | _, _ -> None
                      end
                    | _ -> None))
801
                    )}
802 803

typename:
804
  _1 = one_formal
805 806
    {                      ( ((fun args -> 
                            let (_, ft, _) = (fst _1) args in
807 808
                            ft),

809 810
                         (fun t -> (snd _1) ("", t, [])))
                      )}
811 812

one_formal:
813
  _1 = type_spec _2 = attributes _3 = decl
814 815 816 817
    {                   ( ((fun args -> 
                        let tal = (fst _2) args in
                        let ts = (fst _1) tal args in
                        let (fn, ft, _) = (fst _3) ts args in
818 819 820
                        (fn, ft, [])),

                      (fun (fn, ft, fa) -> 
821
                         match (snd _3) (fn, ft) with 
822
                           Some (restt, m3) -> begin
823 824
                             match (snd _1) restt, 
                                   (snd _2) (typeAttrs restt)with
825 826 827 828 829
                               Some m1, Some m2 -> 
                                 Some (m1 @ m2 @ m3)
                             | _, _ -> None
                           end
                         | _ -> None))
830 831 832
                   )}
| _1 = ARG_f
    {                   ( let currentArg = _1 in
833 834 835 836 837 838
                     ((fun args -> 
                         match getArg currentArg args with
                          Ff (fn, ft, fa) -> (fn, ft, fa)
                         | a  -> wrongArgType currentArg "formal" a),

                      (fun (fn, ft, fa) -> Some [ Ff (fn, ft, fa) ]))
839
                   )}
840 841

type_spec:
842
  _1 = ARG_t
843
    {                ( let currentArg = _1 in
844 845 846 847 848 849
                     ((fun al args -> 
                       match getArg currentArg args with 
                          Ft t -> typeAddAttributes al t
                       | a -> wrongArgType currentArg "type" a),
                      
                      (fun t -> Some [ Ft t ]))
850 851 852
                      )}
| _1 = VOID
    {                    ( ((fun al args -> TVoid al),
853 854 855

                       (fun t -> match unrollType t with 
                           TVoid _ -> Some []
856 857 858
                         | _ -> None)) )}
| _1 = ARG_k
    {                    ( let currentArg = _1 in
859 860 861 862 863 864 865 866
                      ((fun al args -> 
                        match getArg currentArg args with 
                          Fk ik -> TInt(ik, al)
                        | a -> wrongArgType currentArg "ikind" a),

                       (fun t -> match unrollType t with 
                         TInt(ik, _) -> Some [ Fk ik ]
                       | _ -> None))
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
                    )}
| _1 = CHAR
    {                    ( ((fun al args -> TInt(IChar, al)),
                       (matchIntType IChar)) )}
| _1 = UNSIGNED _2 = CHAR
    {                    ( ((fun al args -> TInt(IUChar, al)), 
                       matchIntType IUChar) )}
| _1 = SHORT
    {                    ( ((fun al args -> TInt(IShort, al)), 
                       matchIntType IShort) )}
| _1 = UNSIGNED _2 = SHORT
    {                    ( ((fun al args -> TInt(IUShort, al)), 
                       matchIntType IUShort) )}
| _1 = INT
    {                    ( ((fun al args -> TInt(IInt, al)), 
                       matchIntType IInt) )}
| _1 = UNSIGNED _2 = INT
    {                    ( ((fun al args -> TInt(IUInt, al)), matchIntType IUInt) )}
| _1 = LONG
    {                     ( ((fun al args -> TInt(ILong, al)), 
                        matchIntType ILong) )}
| _1 = UNSIGNED _2 = LONG
    {                     ( ((fun al args -> TInt(IULong, al)), 
                        matchIntType IULong) )}
| _1 = LONG _2 = LONG
    {                       ( ((fun al args -> TInt(ILongLong, al)), 
893 894
                          
                          matchIntType ILongLong)
895 896 897
                        )}
| _1 = UNSIGNED _2 = LONG _3 = LONG
    {                          ( ((fun al args -> TInt(IULongLong, al)),
898 899

                             matchIntType IULongLong)
900 901 902
                           )}
| _1 = FLOAT
    {                    ( ((fun al args -> TFloat(FFloat, al)),
903
                       matchFloatType FFloat) 
904 905 906 907 908 909
                    )}
| _1 = DOUBLE
    {                    ( ((fun al args -> TFloat(FDouble, al)),
                       matchFloatType FDouble) )}
| _1 = STRUCT _2 = ARG_c
    {                 ( let currentArg = _2 in
910 911 912 913 914 915 916 917
                      ((fun al args -> 
                         match getArg currentArg args with 
                           Fc ci -> TComp(ci, al)
                         | a -> wrongArgType currentArg "compinfo" a),

                        (fun t -> match unrollType t with 
                            TComp(ci, _) -> Some [ Fc ci ]
                          | _ -> None))
918 919 920
                    )}
| _1 = UNION _2 = ARG_c
    {                ( let currentArg = _2 in
921 922 923 924 925 926 927 928 929
                     ((fun al args -> 
                         match getArg currentArg args with 
                           Fc ci -> TComp(ci, al)
                         | a -> wrongArgType currentArg "compinfo" a),

                        (fun t -> match unrollType t with 
                            TComp(ci, _) -> Some [ Fc ci ]
                          | _ -> None))

930 931 932 933
                   )}
| _1 = TYPEOF _2 = LPAREN _3 = expression _4 = RPAREN
    {                   ( ((fun al args -> typeAddAttributes al 
                                        (typeOf ((fst _3) args))),
934 935
                    
                      (fun t -> E.s (bug "Cannot match typeof(e)\n")))
936
                   )}
937 938

decl:
939
  _1 = STAR _2 = attributes _3 = decl
940 941 942
    {                    ( ((fun ts args -> 
                         let al = (fst _2) args in
                         (fst _3) (TPtr(ts, al)) args),
943 944

                       (fun (fn, ft) -> 
945
                         match (snd _3) (fn, ft) with 
946
                           Some (TPtr(bt, al), m2) -> begin
947
                             match (snd _2) al with 
948 949 950 951
                               Some m1 -> Some (bt, m1 @ m2)
                             | _ -> None
                           end
                         | _ -> None))
952 953 954
                    )}
| _1 = direct_decl
    {                ( _1 )}
955 956

direct_decl:
957
  
958
    {                   ( ((fun ts args -> ("", ts, [])),
959 960 961 962

                      (* Match any name in this case *)
                      (fun (fn, ft) -> 
                         Some (unrollType ft, [])))
963 964 965
                   )}
| _1 = IDENT
    {                   ( ((fun ts args -> (_1, ts, [])),
966 967

                      (fun (fn, ft) -> 
968
                        if fn = "" || fn = _1 then 
969 970 971
                          Some (unrollType ft, []) 
                        else 
                          None))
972 973 974 975 976
                   )}
| _1 = LPAREN _2 = attributes _3 = decl _4 = RPAREN
    {                   ( ((fun ts args -> 
                          let al = (fst _2) args in
                          (fst _3) (typeAddAttributes al ts) args),
977 978

                      (fun (fn, ft) -> begin
979
                        match (snd _3) (fn, ft) with
980
                          Some (restt, m2) -> begin
981
                            match (snd _2) (typeAttrs restt) with 
982 983 984 985 986
                              Some m1 -> Some (restt, m1 @ m2)
                            | _ -> None
                          end
                        | _ -> None
                      end))
987 988 989 990
                   )}
| _1 = direct_decl _2 = LBRACKET _3 = exp_opt _4 = RBRACKET
    {                   ( ((fun ts args -> 
                        (fst _1) (TArray(ts, (fst _3) args, [])) args),
991 992

                     (fun (fn, ft) -> 
993
                       match (snd _1) (fn, ft) with 
994
                         Some (TArray(bt, lo, _), m1) -> begin
995
                           match (snd _3) lo with 
996 997 998 999
                             Some m2 -> Some (unrollType bt, m1 @ m2)
                           | _ -> None
                         end 
                       | _ -> None))
1000 1001 1002 1003 1004 1005
                   )}
| _1 = LPAREN _2 = attributes _3 = decl _4 = RPAREN _5 = LPAREN _6 = parameters _7 = RPAREN
    {                   ( ((fun ts args -> 
                        let al = (fst _2) args in
                        let pars, isva = (fst _6) args in
                        (fst _3) (TFun(ts, pars, isva, al)) args),
1006 1007

                      (fun (fn, ft) -> 
1008
                         match (snd _3) (fn, ft) with 
1009
                           Some (TFun(rt, args, isva, al), m1) -> begin
1010
                             match (snd _2) al, (snd _6) (args, isva) with 
1011 1012 1013 1014 1015
                               Some m2, Some m6 
                               -> Some (unrollType rt, m1 @ m2 @ m6)
                             | _ -> None
                           end
                         | _ -> None))
1016
                   )}
1017 1018

parameters:
1019
  
1020
    {                   ( ((fun args -> (None, false)),
1021 1022 1023 1024 1025 1026

                     (* Match any formals *)
                      (fun (pars, isva) -> 
                        match pars, isva with 
                          (_, false) -> Some [] 
                        | _ -> None))
1027 1028 1029
                   )}
| _1 = parameters_ne
    {                   ( ((fun args -> 
1030
                        let (pars : (string * typ * attributes) list), 
1031
                            (isva : bool) = (fst _1) args in
1032 1033 1034
                        (Some pars), isva),

                     (function 
1035
                         ((Some pars), isva) -> (snd _1) (pars, isva)
1036
                       |  _ -> None))
1037
                   )}
1038 1039

parameters_ne:
1040
  _1 = ELLIPSIS
1041
    {                   ( ((fun args -> ([], true)),
1042 1043 1044 1045

                      (function 
                          ([], true) -> Some [] 
                        | _ -> None))
1046 1047 1048
                   )}
| _1 = ARG_va
    {                   ( let currentArg = _1 in
1049 1050 1051 1052 1053 1054 1055 1056
                     ((fun args -> 
                       match getArg currentArg args with
                         Fva isva -> ([], isva)
                       | a -> wrongArgType currentArg "vararg" a),

                     (function 
                         ([], isva) -> Some [ Fva isva ] 
                       | _ -> None))
1057 1058 1059
                   )}
| _1 = ARG_F
    {                   ( let currentArg = _1 in
1060 1061 1062 1063 1064 1065 1066 1067
                     ((fun args -> 
                       match getArg currentArg args with
                        FF fl -> ( fl, false)
                       | a  -> wrongArgType currentArg "formals" a),

                      (function 
                          (pars, false) -> Some [ FF pars ] 
                        | _ -> None))
1068 1069 1070
                   )}
| _1 = one_formal
    {                   ( ((fun args -> ([(fst _1) args], false)),
1071 1072

                     (function 
1073
                         ([ f ], false) -> (snd _1) f 
1074
                       | _ -> None))
1075 1076 1077 1078 1079
                   )}
| _1 = one_formal _2 = COMMA _3 = parameters_ne
    {                   ( ((fun args -> 
                        let this = (fst _1) args in
                        let (rest, isva) = (fst _3) args in
1080 1081 1082 1083
                        (this :: rest, isva)),

                      (function 
                          ((f::rest, isva)) -> begin
1084
                            match (snd _1) f, (snd _3) (rest, isva) with 
1085 1086 1087 1088
                              Some m1, Some m2 -> Some (m1 @ m2)
                            | _, _ -> None
                          end
                        | _ -> None))
1089
                   )}
1090 1091

exp_opt:
1092
  
1093
    {                   ( ((fun args -> None),
1094
                      (* Match anything if the pattern does not have a len *)
1095 1096 1097
                      (fun _ -> Some [])) )}
| _1 = expression
    {                   ( ((fun args -> Some ((fst _1) args)),
1098 1099

                      (fun lo -> match lo with
1100
                        Some e -> (snd _1) e
1101
                      | _ -> None))
1102 1103 1104
                   )}
| _1 = ARG_eo
    {                   ( let currentArg = _1 in
1105 1106 1107 1108 1109 1110
                     ((fun args ->
                       match getArg currentArg args with
                         Feo lo -> lo
                       | a -> wrongArgType currentArg "exp_opt" a),

                      fun lo -> Some [ Feo lo ])
1111
                   )}
1112 1113

attributes:
1114
  
1115 1116 1117 1118
    {                  ( ((fun args -> []), 
                     (fun attrs -> Some [])) )}
| _1 = ARG_A
    {                  ( let currentArg = _1 in
1119 1120 1121 1122 1123 1124
                    ((fun args -> 
                        match getArg currentArg args with
                          FA al -> al
                        | a -> wrongArgType currentArg "attributes" a),

                     (fun al -> Some [ FA al ]))
1125 1126 1127 1128
                  )}
| _1 = attribute _2 = attributes
    {                  ( ((fun args ->
                       addAttribute ((fst _1) args) ((fst _2) args)),
1129 1130
                     (* Pass all the attributes down *)
                     (fun attrs ->
1131
                       match (snd _1) attrs, (snd _2) attrs with
1132 1133
                         Some m1, Some m2 -> Some (m1 @ m2)
                       | _, _ -> None))
1134
                  )}
1135 1136

attribute:
1137
  _1 = CONST
1138 1139 1140 1141 1142 1143 1144
    {                                        ( doAttr "const" None )}
| _1 = RESTRICT
    {                                        ( doAttr "restrict" None )}
| _1 = VOLATILE
    {                                        ( doAttr "volatile" None )}
| _1 = ATTRIBUTE _2 = LPAREN _3 = LPAREN _4 = attr _5 = RPAREN _6 = RPAREN
    {                                        ( _4 )}
1145 1146

attr:
1147
  _1 = IDENT
1148 1149 1150
    {                          ( doAttr _1 None )}
| _1 = IDENT _2 = LPAREN _3 = attr_args_ne _4 = RPAREN
    {                          ( doAttr _1 (Some _3) )}
1151 1152

attr_args_ne:
1153
  _1 = attr_arg
1154
    {                                 ( ((fun args -> [ (fst _1) args ]),
1155 1156

                                    (fun aargs -> match aargs with 
1157
                                      [ arg ] -> (snd _1) arg
1158
                                    | _ -> None))
1159 1160 1161 1162 1163
                                 )}
| _1 = attr_arg _2 = COMMA _3 = attr_args_ne
    {                                 ( ((fun args -> 
                                      let this = (fst _1) args in
                                      this :: ((fst _3) args)),
1164 1165 1166

                                    (fun aargs -> match aargs with 
                                      h :: rest -> begin
1167
                                        match (snd _1) h, (snd _3) rest with
1168 1169 1170 1171
                                          Some m1, Some m2 -> Some (m1 @ m2)
                                        | _, _ -> None
                                      end
                                    | _ -> None))
1172 1173 1174
                                  )}
| _1 = ARG_P
    {                        ( let currentArg = _1 in
1175 1176 1177 1178 1179 1180
                          ((fun args -> 
                            match getArg currentArg args with
                              FP al -> al
                            | a -> wrongArgType currentArg "attrparams" a),

                           (fun al -> Some [ FP al ]))
1181
                        )}
1182 1183

attr_arg:
1184
  _1 = IDENT
1185
    {                     ( ((fun args -> ACons(_1, [])),
1186 1187

                        (fun aarg -> match aarg with 
1188
                            ACons(id, []) when id = _1 -> Some []
1189
                        | _ -> None))
1190 1191 1192
                     )}
| _1 = IDENT _2 = LPAREN _3 = attr_args_ne _4 = RPAREN
    {                     ( ((fun args -> ACons(_1, (fst _3) args)),
1193 1194

                        (fun aarg -> match aarg with 
1195 1196
                            ACons(id, args) when id = _1 -> 
                              (snd _3) args
1197
                        | _ -> None))
1198 1199 1200
                     )}
| _1 = ARG_p
    {                     ( let currentArg = _1 in
1201 1202 1203 1204 1205 1206
                       ((fun args -> 
                          match getArg currentArg args with
                            Fp p -> p
                          | a -> wrongArgType currentArg "attrparam" a),

                        (fun ap -> Some [ Fp ap]))
1207
                     )}
1208 1209

instr:
1210
  _1 = ARG_i _2 = SEMICOLON
1211
    {                        ( let currentArg = _1 in
1212 1213 1214 1215 1216 1217
                          ((fun loc args -> 
                                match getArg currentArg args with 
                                  Fi i -> i
                                | a -> wrongArgType currentArg "instr" a),

                           (fun i -> Some [ Fi i]))
1218 1219 1220 1221
                        )}
| _1 = lval _2 = EQ _3 = expression _4 = SEMICOLON
    {   ( ((fun loc args -> 
                              Set((fst _1) args, (fst _3) args, loc)),
1222 1223 1224

                           (fun i -> match i with
                             Set (lv, e, l) -> begin
1225
                               match (snd _1) lv, (snd _3) e with
1226 1227 1228 1229
                                 Some m1, Some m2 -> Some (m1 @ m2)
                               | _, _ -> None
                             end
                           | _ -> None))
1230 1231 1232 1233 1234
                        )}
| _1 = lval _2 = PLUS_EQ _3 = expression _4 = SEMICOLON
    {   ( ((fun loc args -> 
                              let l = (fst _1) args in
                              Set(l, buildPlus (Lval l) ((fst _3) args), loc)),
1235 1236 1237

                           matchBinopEq 
                             (fun bop -> bop = PlusPI || bop = PlusA)
1238 1239 1240 1241 1242
                             (snd _1) (snd _3)) 
                        )}
| _1 = lval _2 = MINUS_EQ _3 = expression _4 = SEMICOLON
    {   ( ((fun loc args -> 
                              let l = (fst _1) args in
1243
                              Set(l, 
1244
                                  buildMinus (Lval l) ((fst _3) args), loc)),
1245 1246 1247 1248

                           matchBinopEq (fun bop -> bop = MinusA 
                                               || bop = MinusPP 
                                               || bop = MinusPI) 
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
                                      (snd _1)  (snd _3)) 
                        )}
| _1 = lval _2 = STAR_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq Mult _1 _3 )}
| _1 = lval _2 = SLASH_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq Div _1 _3 )}
| _1 = lval _2 = PERCENT_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq Mod _1 _3 )}
| _1 = lval _2 = AND_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq BAnd _1 _3 )}
| _1 = lval _2 = PIPE_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq BOr _1 _3 )}
| _1 = lval _2 = CIRC_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq BXor _1 _3 )}
| _1 = lval _2 = INF_INF_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq Shiftlt _1 _3 )}
| _1 = lval _2 = SUP_SUP_EQ _3 = expression _4 = SEMICOLON
    {   ( doBinopEq Shiftrt _1 _3 )}
| _1 = lval _2 = EQ _3 = lval _4 = LPAREN _5 = arguments _6 = RPAREN _7 = SEMICOLON
    {   ( ((fun loc args -> 
                              Call(Some ((fst _1) args), Lval ((fst _3) args), 
                                     (fst _5) args, loc)),
1271 1272 1273

                           (fun i -> match i with 
                             Call(Some l, Lval f, args, loc) -> begin
1274
                               match (snd _1) l, (snd _3) f, (snd _5) args with
1275 1276 1277 1278 1279
                                 Some m1, Some m2, Some m3 -> 
                                   Some (m1 @ m2 @ m3)
                               | _, _, _ -> None
                             end
                           | _ -> None))
1280 1281 1282 1283 1284
                        )}
| _1 = lval _2 = LPAREN _3 = arguments _4 = RPAREN _5 = SEMICOLON
    {   ( ((fun loc args -> 
                              Call(None, Lval ((fst _1) args), 
                                     (fst _3) args, loc)), 
1285 1286 1287

                           (fun i -> match i with 
                             Call(None, Lval f, args, loc) -> begin
1288
                               match (snd _1) f, (snd _3) args with
1289 1290 1291 1292
                                 Some m1, Some m2 -> Some (m1 @ m2)
                               | _, _ -> None
                             end
                           | _ -> None))
1293 1294 1295 1296 1297
                         )}
| _1 = arglo _2 = lval _3 = LPAREN _4 = arguments _5 = RPAREN _6 = SEMICOLON
    {       ( ((fun loc args -> 
                       Call((fst _1) args, Lval ((fst _2) args), 
                            (fst _4) args, loc)), 
1298 1299 1300
                        
                        (fun i -> match i with 
                          Call(lo, Lval f, args, loc) -> begin
1301
                            match (snd _1) lo, (snd _2) f, (snd _4) args with
1302 1303 1304 1305 1306
                              Some m1, Some m2, Some m3 -> 
                                Some (m1 @ m2 @ m3)
                            | _, _, _ -> None
                          end
                        | _ -> None))
1307
                     )}
1308 1309

arglo:
1310
  _1 = ARG_lo
1311
    {                         ( let currentArg = _1 in
1312 1313 1314 1315 1316 1317 1318 1319 1320
                           ((fun args -> 
                             let res = 
                               match getArg currentArg args with
                                 Flo x -> x
                               | a -> wrongArgType currentArg "lval option" a
                             in
                             res),

                            (fun lo -> Some [ Flo lo ]))
1321
                         )}
1322 1323

arguments:
1324
  
1325
    {                  ( ((fun args -> []), 
1326 1327 1328 1329

                     (fun actuals -> match actuals with 
                          [] -> Some []
                         | _ -> None))
1330 1331 1332
                  )}
| _1 = arguments_ne
    {                  ( _1 )}
1333 1334

arguments_ne:
1335
  _1 = expression
1336
    {                  ( ((fun args -> [ (fst _1) args ]),
1337 1338

                     (fun actuals -> match actuals with 
1339
                        [ h ] -> (snd _1) h
1340
                       | _ -> None))
1341 1342 1343
                  )}
| _1 = ARG_E
    {                  (  let currentArg = _1 in
1344 1345 1346 1347 1348 1349
                     ((fun args -> 
                         match getArg currentArg args with
                           FE el -> el
                          | a -> wrongArgType currentArg "arguments" a),

                      (fun actuals -> Some [ FE actuals ]))
1350 1351 1352
                  )}
| _1 = expression _2 = COMMA _3 = arguments_ne
    {                  ( ((fun args -> ((fst _1) args) :: ((fst _3) args)),
1353 1354 1355

                     (fun actuals -> match actuals with 
                         h :: rest -> begin
1356
                           match (snd _1) h, (snd _3) rest with 
1357 1358 1359 1360
                             Some m1, Some m2 -> Some (m1 @ m2)
                           | _, _ -> None
                         end
                       | _ -> None))
1361
                  )}
1362 1363

stmt:
1364
  _1 = IF _2 = LPAREN _3 = expression _4 = RPAREN _5 = stmt %prec IF
1365 1366 1367
    {                  ( (fun mkTemp loc args -> 
                         mkStmt (If((fst _3) args, 
                                    mkBlock [ _5 mkTemp loc args ],
1368
                                    mkBlock [], loc)))
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
                  )}
| _1 = IF _2 = LPAREN _3 = expression _4 = RPAREN _5 = stmt _6 = ELSE _7 = stmt
    {                  ( (fun mkTemp loc args -> 
                         mkStmt (If((fst _3) args, 
                                    mkBlock [ _5 mkTemp loc args ],
                                    mkBlock [ _7 mkTemp loc args], loc)))
                  )}
| _1 = RETURN _2 = exp_opt _3 = SEMICOLON
    {                  ( (fun mkTemp loc args -> 
                         mkStmt (Return((fst _2) args, loc))) 
                  )}
| _1 = BREAK _2 = SEMICOLON
    {                  ( (fun mkTemp loc args -> 
1382
                         mkStmt (Break loc))
1383 1384 1385
                  )}
| _1 = CONTINUE _2 = SEMICOLON
    {                  ( (fun mkTemp loc args -> 
1386
                         mkStmt (Continue loc))
1387 1388 1389 1390
                  )}
| _1 = LBRACE _2 = stmt_list _3 = RBRACE
    {                  ( (fun mkTemp loc args -> 
                         let stmts = _2 mkTemp loc args in
1391
                         mkStmt (Block (mkBlock (stmts))))
1392 1393 1394 1395
                  )}
| _1 = WHILE _2 = LPAREN _3 = expression _4 = RPAREN _5 = stmt
    {                  ( (fun mkTemp loc args -> 
                        let e = (fst _3) args in
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
                        let e = 
                          if isPointerType(typeOf e) then 
                            mkCast e !upointType
                          else e
                        in
                        mkStmt 
                          (Loop (mkBlock [ mkStmt 
                                             (If(e,
                                                 mkBlock [],
                                                 mkBlock [ mkStmt 
                                                             (Break loc) ],
                                                 loc));
1408
                                           _5 mkTemp loc args ],
1409
                                 loc, None, None)))
1410 1411 1412 1413 1414 1415 1416
                   )}
| _1 = instr_list
    {                  ( (fun mkTemp loc args -> 
                       mkStmt (Instr (_1 loc args)))
                  )}
| _1 = ARG_s
    {                  ( let currentArg = _1 in
1417 1418 1419
                    (fun mkTemp loc args -> 
                       match getArg currentArg args with
                         Fs s -> s
1420
                       | a -> wrongArgType currentArg "stmt" a) )}
1421 1422

stmt_list:
1423
  
1424 1425 1426
    {                 ( (fun mkTemp loc args -> []) )}
| _1 = ARG_S
    {                 ( let currentArg = _1 in
1427 1428 1429 1430
                   (fun mkTemp loc args -> 
                       match getArg currentArg args with 
                       | FS sl -> sl 
                       | a -> wrongArgType currentArg "stmts" a)
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
                 )}
| _1 = stmt _2 = stmt_list
    {                 ( (fun mkTemp loc args -> 
                      let this = _1 mkTemp loc args in
                      this :: (_2 mkTemp loc args))
                 )}
| _1 = type_spec _2 = attributes _3 = decl _4 = maybe_init _5 = SEMICOLON _6 = stmt_list
    {                ( (fun mkTemp loc args -> 
                     let tal = (fst _2) args in
                     let ts  = (fst _1) tal args in
                     let (n, t, _) = (fst _3) ts args in
                     let init = _4 args in
1443 1444 1445
                     (* Before we proceed we must create the variable *)
                     let v = mkTemp n t in
                     (* Now we parse the rest *)
1446
                     let rest = _6 mkTemp loc ((n, Fv v) :: args) in
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
                     (* Now we add the initialization instruction to the 
                      * front *)
                     match init with 
                       NoInit -> rest
                     | InitExp e -> 
                         mkStmtOneInstr (Set((Var v, NoOffset), e, loc)) 
                         :: rest
                     | InitCall (f, args) ->
                         mkStmtOneInstr (Call(Some (Var v, NoOffset), 
                                              Lval f, args, loc))
                         :: rest

                                                           )
1460
                 )}
1461 1462

instr_list:
1463
  _1 = instr %prec COMMA
1464 1465 1466
    {                 ( (fun loc args -> [ ((fst _1) loc args) ]) )}
| _1 = ARG_I
    {                 ( let currentArg = _1 in
1467 1468 1469 1470
                   (fun loc args -> 
                       match getArg currentArg args with 
                       | FI il -> il 
                       | a -> wrongArgType currentArg "instrs" a)
1471 1472 1473 1474 1475 1476
                 )}
| _1 = instr _2 = instr_list
    {                 ( (fun loc args -> 
                      let this = (fst _1) loc args in
                      this :: (_2 loc args))
                 )}
1477 1478

maybe_init:
1479
  
1480 1481 1482 1483 1484 1485
    {                                ( (fun args -> NoInit) )}
| _1 = EQ _2 = expression
    {                                ( (fun args -> InitExp ((fst _2) args)) )}
| _1 = EQ _2 = lval _3 = LPAREN _4 = arguments _5 = RPAREN
    {                                ( (fun args -> 
                                    InitCall((fst _2) args, (fst _4) args)) )}
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498

%%