Changeset 1305


Ignore:
Timestamp:
Oct 6, 2011, 11:07:04 AM (8 years ago)
Author:
tranquil
Message:

added indexes to loop constructors. Branch does not compile atm

Location:
Deliverables/D2.2/8051-indexed-labels-branch/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D2.2/8051-indexed-labels-branch/src/clight/clight.mli

    r1291 r1305  
    130130type label = Label.t
    131131
     132type loop_index = CostLabel.index option
     133
    132134type statement =
    133135  | Sskip                                       (**r do nothing *)
     
    136138  | Ssequence of statement*statement            (**r sequence *)
    137139  | Sifthenelse of expr*statement*statement     (**r conditional *)
    138   | Swhile of expr*statement                    (**r [while] loop *)
    139   | Sdowhile of expr*statement                  (**r [do] loop *)
    140   | Sfor of statement*expr*statement*statement  (**r [for] loop *)
     140  | Swhile of loop_index*expr*statement                 (**r [while] loop *)
     141  | Sdowhile of loop_index*expr*statement                       (**r [do] loop *)
     142  | Sfor of loop_index*statement*expr*statement*statement       (**r [for] loop *)
    141143  | Sbreak                                      (**r [break] statement *)
    142144  | Scontinue                                   (**r [continue] statement *)
  • Deliverables/D2.2/8051-indexed-labels-branch/src/clight/clightAnnotator.ml

    r1291 r1305  
    182182  | Clight.Ecall (x, e1, e2) -> assert false (* Should not happen. *)
    183183
     184let loop_increment prefix depth body = match depth  with
     185        | None -> body
     186        | Some d ->
     187                let uint = Clight.Tint(Clight.I32, AST.Unsigned) in
     188                let id = Clight.Expr(Clight.Evar (CostLabel.make_id prefix d), uint) in
     189                let one = Clight.Expr(Clight.Econst_int 1, uint) in
     190                let add a b = Clight.Expr(Clight.Ebinop(Clight.Oadd, a, b), uint) in
     191                Clight.Ssequence(body, Clight.Sassign(id, add id one))
     192               
     193let loop_reset_index prefix depth loop = match depth with
     194        | None -> loop
     195        | Some d ->
     196    let uint = Clight.Tint(Clight.I32, AST.Unsigned) in
     197    let id = Clight.Expr(Clight.Evar (CostLabel.make_id prefix d), uint) in
     198    let zero = Clight.Expr(Clight.Econst_int 0, uint) in
     199    Clight.Ssequence(Clight.Sassign(id, zero), loop)
     200
    184201(* Instrument a statement. *)
    185202
    186 let rec instrument_body cost_mapping cost_incr stmt = match stmt with
     203let rec instrument_body l_ind cost_mapping cost_incr stmt = match stmt with
    187204  | Clight.Sskip | Clight.Sbreak | Clight.Scontinue | Clight.Sreturn None
    188205  | Clight.Sgoto _ ->
     
    200217    Clight.Scall (eopt', f', args')
    201218  | Clight.Ssequence (s1, s2) ->
    202     Clight.Ssequence (instrument_body cost_mapping cost_incr s1,
    203                       instrument_body cost_mapping cost_incr s2)
     219    Clight.Ssequence (instrument_body l_ind cost_mapping cost_incr s1,
     220                      instrument_body l_ind cost_mapping cost_incr s2)
    204221  | Clight.Sifthenelse (e, s1, s2) ->
    205222    let e' = instrument_expr cost_mapping cost_incr e in
    206     let s1' = instrument_body cost_mapping cost_incr s1 in
    207     let s2' = instrument_body cost_mapping cost_incr s2 in
     223    let s1' = instrument_body l_ind cost_mapping cost_incr s1 in
     224    let s2' = instrument_body l_ind cost_mapping cost_incr s2 in
    208225    Clight.Sifthenelse (e', s1', s2')
    209   | Clight.Swhile (e, s) ->
    210     let e' = instrument_expr cost_mapping cost_incr e in
    211     let s' = instrument_body cost_mapping cost_incr s in
    212     Clight.Swhile (e', s')
    213   | Clight.Sdowhile (e, s) ->
    214     let e' = instrument_expr cost_mapping cost_incr e in
    215     let s' = instrument_body cost_mapping cost_incr s in
    216     Clight.Sdowhile (e', s')
    217   | Clight.Sfor (s1, e, s2, s3) ->
    218     let s1' = instrument_body cost_mapping cost_incr s1 in
    219     let e' = instrument_expr cost_mapping cost_incr e in
    220     let s2' = instrument_body cost_mapping cost_incr s2 in
    221     let s3' = instrument_body cost_mapping cost_incr s3 in
    222     Clight.Sfor (s1', e', s2', s3')
     226  | Clight.Swhile (i, e, s) ->
     227    let e' = instrument_expr cost_mapping cost_incr e in
     228    let s' = instrument_body l_ind cost_mapping cost_incr s in
     229                let s' = loop_increment l_ind i s' in
     230    loop_reset_index l_ind i (Clight.Swhile (None, e', s'))
     231  | Clight.Sdowhile (i, e, s) ->
     232    let e' = instrument_expr cost_mapping cost_incr e in
     233    let s' = instrument_body l_ind cost_mapping cost_incr s in
     234    let s' = loop_increment l_ind i s' in
     235    loop_reset_index l_ind i (Clight.Sdowhile (None, e', s'))
     236  | Clight.Sfor (i, s1, e, s2, s3) ->
     237    let s1' = instrument_body l_ind cost_mapping cost_incr s1 in
     238    let e' = instrument_expr cost_mapping cost_incr e in
     239    let s2' = instrument_body l_ind cost_mapping cost_incr s2 in
     240    let s3' = instrument_body l_ind cost_mapping cost_incr s3 in
     241    let s3' = loop_increment l_ind i s3' in
     242                loop_reset_index l_ind i (Clight.Sfor (None, s1', e', s2', s3'))
    223243  | Clight.Sreturn (Some e) ->
    224244    let e' = instrument_expr cost_mapping cost_incr e in
     
    226246  | Clight.Sswitch (e, ls) ->
    227247    let e' = instrument_expr cost_mapping cost_incr e in
    228     let ls' = instrument_ls cost_mapping cost_incr ls in
     248    let ls' = instrument_ls l_ind cost_mapping cost_incr ls in
    229249    Clight.Sswitch (e', ls')
    230250  | Clight.Slabel (lbl, s) ->
    231     let s' = instrument_body cost_mapping cost_incr s in
     251    let s' = instrument_body l_ind cost_mapping cost_incr s in
    232252    Clight.Slabel (lbl, s')
    233253  | Clight.Scost (lbl, s) when CostLabel.Map.mem lbl cost_mapping ->
    234254    (* Keep the cost label in the code. *)
    235     let s' = instrument_body cost_mapping cost_incr s in
     255    let s' = instrument_body l_ind cost_mapping cost_incr s in
    236256    let incr = CostLabel.Map.find lbl cost_mapping in
    237257    let fun_typ = Clight.Tfunction ([int_typ], Clight.Tvoid) in
     
    240260    Clight.Scost (lbl, Clight.Ssequence (Clight.Scall (None, f, args), s'))
    241261  (*
    242     let s' = instrument_body cost_mapping cost_incr s in
     262    let s' = instrument_body l_ind cost_mapping cost_incr s in
    243263    let incr = CostLabel.Map.find lbl cost_mapping in
    244264    if incr = 0 then s'
     
    251271  | Clight.Scost (lbl, s) ->
    252272    (* Keep the cost label in the code and show the increment of 0. *)
    253     let s' = instrument_body cost_mapping cost_incr s in
     273    let s' = instrument_body l_ind cost_mapping cost_incr s in
    254274    let fun_typ = Clight.Tfunction ([int_typ], Clight.Tvoid) in
    255275    let f = Clight.Expr (Clight.Evar cost_incr, fun_typ) in
     
    257277    Clight.Scost (lbl, Clight.Ssequence (Clight.Scall (None, f, args), s'))
    258278  (*
    259     instrument_body cost_mapping cost_incr s
     279    instrument_body l_ind cost_mapping cost_incr s
    260280  *)
    261 and instrument_ls cost_mapping cost_incr = function
     281and instrument_ls l_ind cost_mapping cost_incr = function
    262282  | Clight.LSdefault s ->
    263     let s' = instrument_body cost_mapping cost_incr s in
     283    let s' = instrument_body l_ind cost_mapping cost_incr s in
    264284    Clight.LSdefault s'
    265285  | Clight.LScase (i, s, ls) ->
    266     let s' = instrument_body cost_mapping cost_incr s in
    267     let ls' = instrument_ls cost_mapping cost_incr ls in
     286    let s' = instrument_body l_ind cost_mapping cost_incr s in
     287    let ls' = instrument_ls l_ind cost_mapping cost_incr ls in
    268288    Clight.LScase (i, s', ls')
     289               
     290(* calculating the maximal depth of single-entry loops *)
     291(* (as already calculated during the labeling phase)   *)
     292
     293let rec max_loop_index =
     294        let f_expr _ _ = () in
     295  let f_stmt stmt _ sub_stmts_res =
     296                let curr_max = List.fold_left max 0 sub_stmts_res in
     297                match stmt with
     298                  | Clight.Swhile (Some x, _, _) | Clight.Sdowhile (Some x, _, _)
     299                  | Clight.Sfor (Some x, _, _, _, _) -> max x curr_max (* = curr_max+1 ? *)
     300                        | _ -> curr_max in
     301  ClightFold.statement2 f_expr f_stmt
     302       
     303let rec loop_indexes_defs prefix max_depth =
     304        if max_depth = 0 then [] else
     305  let uint = Clight.Tint(Clight.I32, AST.Unsigned) in
     306        let id = CostLabel.make_id prefix max_depth in
     307        (id, uint) :: loop_indexes_defs prefix (max_depth-1)
    269308
    270309(* Instrument a function. *)
    271310
    272 let instrument_funct cost_mapping cost_incr (id, def) =
     311let instrument_funct l_ind cost_mapping cost_incr (id, def) =
    273312  let def = match def with
    274313    | Clight.Internal def ->
    275         let body = instrument_body cost_mapping cost_incr def.Clight.fn_body in
     314        let max_depth = max_loop_index def.Clight.fn_body in
     315        let indexes_defs = loop_indexes_defs l_ind max_depth in
     316        let body = instrument_body l_ind cost_mapping cost_incr def.Clight.fn_body in
    276317        Clight.Internal { def with Clight.fn_body = body }
    277318    | Clight.External _ -> def
  • Deliverables/D2.2/8051-indexed-labels-branch/src/clight/clightFold.ml

    r818 r1305  
    116116  | Clight.Ssequence (stmt1, stmt2) -> ([], [stmt1 ; stmt2])
    117117  | Clight.Sifthenelse (e, stmt1, stmt2) -> ([e], [stmt1 ; stmt2])
    118   | Clight.Swhile (e, stmt) | Clight.Sdowhile (e, stmt) -> ([e], [stmt])
    119   | Clight.Sfor (stmt1, e, stmt2, stmt3) -> ([e], [stmt1 ; stmt2 ; stmt3])
     118  | Clight.Swhile (_, e, stmt) | Clight.Sdowhile (_, e, stmt) -> ([e], [stmt])
     119  | Clight.Sfor (_, stmt1, e, stmt2, stmt3) -> ([e], [stmt1 ; stmt2 ; stmt3])
    120120  | Clight.Sreturn (Some e) -> ([e], [])
    121121  | Clight.Sswitch (e, lbl_stmts) -> ([e], labeled_statements_subs lbl_stmts)
     
    145145    | Clight.Sifthenelse _, e :: _, stmt1 :: stmt2 :: _ ->
    146146      Clight.Sifthenelse (e, stmt1, stmt2)
    147     | Clight.Swhile _, e :: _, stmt :: _ ->
    148       Clight.Swhile (e, stmt)
    149     | Clight.Sdowhile _, e :: _, stmt :: _ ->
    150       Clight.Sdowhile (e, stmt)
    151     | Clight.Sfor _, e :: _, stmt1 :: stmt2 :: stmt3 :: _ ->
    152       Clight.Sfor (stmt1, e, stmt2, stmt3)
     147    | Clight.Swhile (i, _, _), e :: _, stmt :: _ ->
     148      Clight.Swhile (i, e, stmt)
     149    | Clight.Sdowhile (i, _, _), e :: _, stmt :: _ ->
     150      Clight.Sdowhile (i, e, stmt)
     151    | Clight.Sfor (i, _, _, _, _), e :: _, stmt1 :: stmt2 :: stmt3 :: _ ->
     152      Clight.Sfor (i, stmt1, e, stmt2, stmt3)
    153153    | Clight.Sreturn (Some _), e :: _, _ -> Clight.Sreturn (Some e)
    154154    | Clight.Sswitch (_, lbl_stmts), e :: _, _ ->
  • Deliverables/D2.2/8051-indexed-labels-branch/src/clight/clightFromC.ml

    r818 r1305  
    490490      Sifthenelse(convertExpr env e, convertStmt env s1, convertStmt env s2)
    491491  | C.Swhile(e, s1) ->
    492       Swhile(convertExpr env e, convertStmt env s1)
     492      Swhile(None, convertExpr env e, convertStmt env s1)
    493493  | C.Sdowhile(s1, e) ->
    494       Sdowhile(convertExpr env e, convertStmt env s1)
     494      Sdowhile(None, convertExpr env e, convertStmt env s1)
    495495  | C.Sfor(s1, e, s2, s3) ->
    496       Sfor(convertStmt env s1, convertExpr env e, convertStmt env s2,
     496      Sfor(None, convertStmt env s1, convertExpr env e, convertStmt env s2,
    497497           convertStmt env s3)
    498498  | C.Sbreak ->
  • Deliverables/D2.2/8051-indexed-labels-branch/src/clight/clightPrinter.ml

    r1291 r1305  
    218218
    219219let rec print_stmt p s =
     220        let print_loop_depth = function
     221    | None -> ()
     222    | Some x -> fprintf p "/* single entry loop depth: %d */@" x in
    220223  match s with
    221224  | Sskip ->
     
    243246              print_stmt s1
    244247              print_stmt s2
    245   | Swhile(e, s) ->
     248  | Swhile(i, e, s) ->
     249                  print_loop_depth i;
    246250      fprintf p "@[<v 2>while (%a) {@ %a@;<0 -2>}@]"
    247251              print_expr e
    248252              print_stmt s
    249   | Sdowhile(e, s) ->
     253  | Sdowhile(i, e, s) ->
     254      print_loop_depth i;
    250255      fprintf p "@[<v 2>do {@ %a@;<0 -2>} while(%a);@]"
    251256              print_stmt s
    252257              print_expr e
    253   | Sfor(s_init, e, s_iter, s_body) ->
     258  | Sfor(i, s_init, e, s_iter, s_body) ->
     259      print_loop_depth i;
    254260      fprintf p "@[<v 2>for (@[<hv 0>%a;@ %a;@ %a) {@]@ %a@;<0 -2>}@]"
    255261              print_stmt_for s_init
     
    471477  | Ssequence(s1, s2) -> collect_stmt s1; collect_stmt s2
    472478  | Sifthenelse(e, s1, s2) -> collect_expr e; collect_stmt s1; collect_stmt s2
    473   | Swhile(e, s) -> collect_expr e; collect_stmt s
    474   | Sdowhile(e, s) -> collect_stmt s; collect_expr e
    475   | Sfor(s_init, e, s_iter, s_body) ->
     479  | Swhile(_, e, s) -> collect_expr e; collect_stmt s
     480  | Sdowhile(_, e, s) -> collect_stmt s; collect_expr e
     481  | Sfor(_, s_init, e, s_iter, s_body) ->
    476482      collect_stmt s_init; collect_expr e;
    477483      collect_stmt s_iter; collect_stmt s_body
  • Deliverables/D2.2/8051-indexed-labels-branch/src/common/costLabel.ml

    r1297 r1305  
    55
    66module StringMap = Map.Make(String)
    7 
    8 module Index =
    9         struct
    10                 include StringTools
    11         end
    127
    138(** Simple expressions are for now affine maps of the form a*_+b *)
     
    1813
    1914type index = int
     15
     16let make_id prefix depth = prefix ^ string_of_int depth
    2017
    2118type indexing = sexpr array
  • Deliverables/D2.2/8051-indexed-labels-branch/src/common/costLabel.mli

    r1297 r1305  
    1616(* trying a nameless approach *)
    1717type index = int
     18
     19val make_id : string -> index -> string
    1820
    1921type indexing = sexpr array
Note: See TracChangeset for help on using the changeset viewer.