Ignore:
Timestamp:
Aug 3, 2011, 4:17:41 PM (9 years ago)
Author:
ayache
Message:

Bug fix in Deliverables/D2.2/8051: cast simplification.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D2.2/8051/src/clight/clightCasts.ml

    r818 r1099  
    2424  | _ -> Clight.I32
    2525
    26 let cmp_intsize cmp size1 size2 =
    27   cmp (int_of_intsize size1) (int_of_intsize size2)
    28 
    29 let max_intsize size1 size2 =
    30   if (int_of_intsize size1) < (int_of_intsize size2) then size2 else size1
    31 
    32 let intsize_union size1 size2 =
    33   intsize_of_int ((int_of_intsize size1) + (int_of_intsize size2))
     26let op_intsize_no_cast op size1 size2 =
     27  op (int_of_intsize size1) (int_of_intsize size2)
     28
     29let cmp_intsize cmp size1 size2 = op_intsize_no_cast cmp size1 size2
     30
     31let op_intsize op size1 size2 =
     32  intsize_of_int (op_intsize_no_cast op size1 size2)
     33
     34let max_intsize size1 size2 = op_intsize max size1 size2
     35
     36let intsize_union size1 size2 = op_intsize (+) size1 size2
    3437
    3538let pow2 = MiscPottier.pow 2
     
    6265  Clight.Tint (size, sign)
    6366
    64 
    65 let type_of_expr (Clight.Expr (_, t)) = t
     67let le_int_type size1 sign1 size2 sign2 = match sign1, sign2 with
     68  | AST.Unsigned, AST.Signed -> cmp_intsize (<) size1 size2
     69  | AST.Signed, AST.Unsigned -> false
     70  | _ -> cmp_intsize (<=) size1 size2
    6671
    6772let int_type_union t1 t2 =
     
    7580  Clight.Tint (size, sign)
    7681
     82
     83(* C types *)
     84
     85let type_of_expr (Clight.Expr (_, t)) = t
     86
    7787let cast_if_needed t (Clight.Expr (ed, t') as e) = match t, ed with
    7888  | _ when t = t' -> e
     
    8292  | _ -> Clight.Expr (Clight.Ecast (t, e), t)
    8393
    84 let rec simplify_binop t binop
    85     (Clight.Expr (ed1, t1) as e1)
    86     (Clight.Expr (ed2, t2) as e2) =
    87   let e1' = simplify_expr e1 in
    88   let e2' = simplify_expr e2 in
    89   let make_int i t = Clight.Expr (Clight.Econst_int i, t) in
    90 
    91   let (e1', e2', t') = match t1, t2, ed1, ed2 with
    92 
    93     | Clight.Tint _, Clight.Tint _,
    94       Clight.Econst_int i1, Clight.Econst_int i2 ->
    95       let t1' = smallest_int_type i1 in
    96       let t2' = smallest_int_type i2 in
    97       let t' = int_type_union t1' t2' in
    98       (make_int i1 t', make_int i2 t', t')
    99 
    100     | Clight.Tint _, Clight.Tint _, _, Clight.Econst_int i2 ->
    101       let t' = type_of_expr e1' in
    102       let e2' = make_int i2 t' in
    103       (e1', e2', t')
    104 
    105     | Clight.Tint _, Clight.Tint _, Clight.Econst_int i1, _ ->
    106       let t' = type_of_expr e2' in
    107       let e1' = make_int i1 t' in
    108       (e1', e2', t')
    109 
    110     | Clight.Tint _, Clight.Tint _, _, _ ->
    111       let t' = int_type_union (type_of_expr e1') (type_of_expr e2') in
    112       (cast_if_needed t' e1', cast_if_needed t' e2', t')
    113 
    114     | _ -> (e1', e2', t)
    115 
    116   in
    117 
    118   Clight.Expr (Clight.Ebinop (binop, e1', e2'), t')
    119 
    120 and simplify_bool_op f_bool t e1 e2 =
     94let le_ctype t1 t2 = match t1, t2 with
     95  | Clight.Tint (size1, sign1), Clight.Tint (size2, sign2) ->
     96    le_int_type size1 sign1 size2 sign2
     97  | _ -> t1 = t2
     98
     99
     100(* Simplification *)
     101
     102let rec simplify_bool_op f_bool t e1 e2 =
    121103  let (e1', e2', t') = simplify_and_same_type t e1 e2 in
    122104  Clight.Expr (f_bool e1' e2', t')
     
    136118  | Clight.Evar _ -> e
    137119
    138   | Clight.Esizeof _ -> Clight.Expr (ed, Clight.Tint (Clight.I8, AST.Unsigned))
     120  | Clight.Esizeof _ ->
     121    let intsize = intsize_of_int (Driver.TargetArch.int_size * 8) in
     122    Clight.Expr (ed, Clight.Tint (intsize, AST.Unsigned))
    139123
    140124  | Clight.Econst_float _ -> error_float ()
     
    148132    Clight.Expr (Clight.Eaddrof e', t)
    149133
    150   | Clight.Eunop (unop, e) ->
    151     let e' = simplify_expr e in
    152     Clight.Expr (Clight.Eunop (unop, e'), type_of_expr e')
    153 
    154   | Clight.Ebinop (binop, e1, e2) ->
    155     simplify_binop t binop e1 e2
    156 
    157   | Clight.Ecast (Clight.Tint (Clight.I32, AST.Signed), e) -> simplify_expr e
     134  | Clight.Eunop _ -> e
     135
     136  | Clight.Ebinop _ -> e
     137
     138  (* [(t1) unop ((t2) e)], when [e] simplified has type [t1] and [t1] <= [t2],
     139     is simplified to [unop e] *)
     140  | Clight.Ecast
     141      (t1,
     142       Clight.Expr
     143         (Clight.Eunop (unop, Clight.Expr (Clight.Ecast (_, e'), _)), t2))
     144      when le_ctype t1 t2 ->
     145    let e' = simplify_expr e' in
     146    let t' = type_of_expr e' in
     147    if t' = t1 then Clight.Expr (Clight.Eunop (unop, e'), t') else e
     148
     149  (* [(t) ((t') e1 binop (t') e2)], when [e1] and [e2] simplified have type [t]
     150     and [t] <= [t'], is simplified to [e] *)
     151  | Clight.Ecast
     152      (t,
     153       Clight.Expr
     154         (Clight.Ebinop
     155            (binop,
     156             Clight.Expr (Clight.Ecast (_, e1), _),
     157             Clight.Expr (Clight.Ecast (_, e2), _)),
     158          t'))
     159      when le_ctype t t' ->
     160    let e1 = simplify_expr e1 in
     161    let t1 = type_of_expr e1 in
     162    let e2 = simplify_expr e2 in
     163    let t2 = type_of_expr e2 in
     164    if t1 = t && t2 = t then Clight.Expr (Clight.Ebinop (binop, e1, e2), t)
     165    else e
    158166
    159167  | Clight.Ecast (t', e) ->
Note: See TracChangeset for help on using the changeset viewer.