Changeset 621 for Deliverables/D2.2


Ignore:
Timestamp:
Mar 2, 2011, 5:53:31 PM (9 years ago)
Author:
ayache
Message:

Bug fix in cost computation in D2.2.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D2.2/8051/src/ASM/ASMCosts.ml

    r619 r621  
    3636
    3737
    38 (* TODO: do not consider the very first instruction as ending the block since it
    39    contains the cost label whose cost we are trying to compute! *)
    40 let block_cost mem costs =
    41   let rec aux oldpc =
    42     if BitVectors.WordMap.mem oldpc costs then 0
    43     else
    44       let inst,pc,inst_cost = ASMInterpret.fetch mem oldpc in
    45       let cost = match inst_nature oldpc inst with
    46         | Return -> 0
    47         | Goto pc -> aux pc
    48         | Branch pc2 ->
    49           let pc1 =
    50             snd (BitVectors.half_add pc (BitVectors.vect_of_int 1 `Sixteen)) in
    51           let cost1 = aux pc1 in
    52           let cost2 = aux pc2 in
    53           if cost1 <> cost2 then
    54             warning
    55               (Printf.sprintf
    56                  "Warning: branching to %s has cost %d; continuing has cost %d.\n"
    57                  "*fixme*"(*pc2*) cost2 cost1) ;
    58         max cost1 cost2
    59       | _ -> aux pc
    60     in
    61      cost + inst_cost
    62   in
    63   aux
     38let treat mem costs pc =
     39  let (inst, next_pc, inst_cost) = ASMInterpret.fetch mem pc in
     40  let next_pcs = match inst_nature pc inst with
     41    | Return -> []
     42    | Goto pc -> [pc]
     43    | Branch pc2 ->
     44      let pc1 =
     45        snd (BitVectors.half_add pc (BitVectors.vect_of_int 1 `Sixteen)) in
     46      [pc1 ; pc2]
     47    | _ -> [next_pc] in
     48  (inst_cost, next_pcs)
     49
     50let compare pc1 cost1 pc2 cost2 =
     51  if cost1 <> cost2 then
     52    warning
     53      (Printf.sprintf
     54         "Warning: branching to %s has cost %d, branching to %s has cost %d"
     55         "*fixme*"(* pc1 *) cost1 "*fixme*" (* pc2 *) cost2) ;
     56  max cost1 cost2
     57
     58let block_cost mem costs pc =
     59  let treat = treat mem costs in
     60  let (init_cost, next_pcs) = treat pc in
     61  let rec aux = function
     62    | [] -> 0
     63    | [pc] when BitVectors.WordMap.mem pc costs -> 0
     64    | [pc] -> full_cost pc
     65    | [pc1 ; pc2] when BitVectors.WordMap.mem pc1 costs &&
     66                       BitVectors.WordMap.mem pc2 costs -> 0
     67    | [pc1 ; pc2] when BitVectors.WordMap.mem pc1 costs ->
     68      let cost2 = full_cost pc2 in
     69      compare pc1 0 pc2 cost2
     70    | [pc1 ; pc2] when BitVectors.WordMap.mem pc2 costs ->
     71      let cost1 = full_cost pc1 in
     72      compare pc1 cost1 pc2 0
     73    | [pc1 ; pc2] ->
     74      let cost1 = full_cost pc1 in
     75      let cost2 = full_cost pc2 in
     76      compare pc1 cost1 pc2 cost2
     77    | _ -> assert false (* should be impossible: only 0, 1 or 2 following pcs *)
     78  and full_cost pc =
     79    let (cost, next_pcs) = treat pc in
     80    cost + (aux next_pcs) in
     81  init_cost + (aux next_pcs)
    6482
    6583
     
    103121  let (lbl, cost) = first_cost_label mem costs in
    104122  let old_cost =
    105     assert (CostLabel.Map.mem lbl costs_mapping) ;
    106     CostLabel.Map.find lbl costs_mapping in
     123    if CostLabel.Map.mem lbl costs_mapping then
     124      CostLabel.Map.find lbl costs_mapping
     125    else 0 in
    107126  let new_cost = old_cost + cost in
    108127  CostLabel.Map.add lbl new_cost costs_mapping
Note: See TracChangeset for help on using the changeset viewer.