Ignore:
Timestamp:
Nov 23, 2011, 5:43:24 PM (9 years ago)
Author:
tranquil
Message:

merge of indexed labels branch

File:
1 edited

Legend:

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

    r1490 r1542  
    131131
    132132  exit_addr   : BitVectors.word;
    133   cost_labels : string BitVectors.WordMap.t
     133       
     134(*
     135  ind_0s      : int BitVectors.WordMap.t;
     136  ind_incs    : int BitVectors.WordMap.t;
     137  cost_labels : CostLabel.t BitVectors.WordMap.t
     138*)     
    134139}
    135140
     
    284289
    285290  exit_addr = BitVectors.zero `Sixteen;
     291(*
     292  ind_0s = BitVectors.WordMap.empty;
     293  ind_incs = BitVectors.WordMap.empty;
    286294  cost_labels = BitVectors.WordMap.empty
     295*)
    287296}
    288297
     
    950959   ) (StringTools.Map.empty,0) p.ASM.ppreamble
    951960 in
    952  let pc,exit_addr,labels,costs =
     961 let pc,exit_addr,labels,inds,incs,costs =
    953962  List.fold_left
    954    (fun (pc,exit_addr,labels,costs) i ->
     963   (fun (pc,exit_addr,labels,inds,incs,costs) i ->
    955964     match i with
    956         `Label s when s = p.ASM.pexit_label -> pc, pc, StringTools.Map.add s pc labels, costs
    957       | `Label s -> pc, exit_addr, StringTools.Map.add s pc labels, costs
    958       | `Cost s -> pc, exit_addr, labels, BitVectors.WordMap.add pc s costs
    959       | `Mov (_,_) -> (snd (half_add pc (vect_of_int 3 `Sixteen))), exit_addr, labels, costs
     965        `Label s when s = p.ASM.pexit_label -> pc, pc, StringTools.Map.add s pc labels, inds, incs, costs
     966      | `Label s -> pc, exit_addr, StringTools.Map.add s pc labels, inds, incs, costs
     967      | `Cost s -> pc, exit_addr, labels, inds, incs, BitVectors.WordMap.add pc s costs
     968      | `Index i -> pc, exit_addr, labels, BitVectors.WordMap.add pc i inds, incs, costs
     969      | `Inc i -> pc, exit_addr, labels, inds, BitVectors.WordMap.add pc i incs, costs
     970      | `Mov (_,_) -> (snd (half_add pc (vect_of_int 3 `Sixteen))), exit_addr, labels, inds, incs, costs
     971
    960972      | `Jmp _
    961       | `Call _ -> (snd (half_add pc (BitVectors.vect_of_int 3 `Sixteen))), exit_addr, labels, costs
     973      | `Call _ -> (snd (half_add pc (BitVectors.vect_of_int 3 `Sixteen))), exit_addr, labels, inds, incs, costs
    962974      (*CSC: very stupid: always expand to worst opcode *)
    963975      | `WithLabel i ->
     
    967979        assert (fake_jump = i');
    968980        let pc' = snd (half_add pc' (vect_of_int 5 `Sixteen)) in
    969           (snd (half_add pc pc'), exit_addr, labels, costs)
     981          (snd (half_add pc pc'), exit_addr, labels, inds, incs, costs)
    970982      | #instruction as i ->
    971983        let i',pc',_ = fetch (load_code_memory (assembly1 i)) (vect_of_int 0 `Sixteen) in
    972984         assert (i = i');
    973          (snd (half_add pc pc'),exit_addr,labels, costs)
     985         (snd (half_add pc pc'),exit_addr,labels, inds, incs, costs)
    974986   )
    975987    (BitVectors.zero `Sixteen,BitVectors.zero `Sixteen,
    976      StringTools.Map.empty, BitVectors.WordMap.empty) p.ASM.pcode
     988     StringTools.Map.empty, BitVectors.WordMap.empty, BitVectors.WordMap.empty,
     989     BitVectors.WordMap.empty) p.ASM.pcode
    977990 in
    978991 let code =
     
    980993     (function
    981994        `Label _
    982       | `Cost _ -> []
     995      | `Cost _
     996      | `Index _
     997      | `Inc _ -> []
    983998      | `WithLabel i ->
    984999         (* We need to expand a conditional jump to a label to a machine language
     
    10321047                 address, reconstructed
    10331048         in
    1034            let sjmp, jmp = `SJMP (`REL (vect_of_int 3 `Eight)), `LJMP (`ADDR16 jmp_address) in
     1049           let sjmp = `SJMP (`REL (vect_of_int 3 `Eight)) in
     1050           let jmp = `LJMP (`ADDR16 jmp_address) in
    10351051           let translation = [ translated_jump; sjmp; jmp ] in
    10361052             List.flatten (List.map assembly1 translation)
     
    10481064            assembly1 (`LCALL (`ADDR16 pc_offset ))
    10491065      | #instruction as i -> assembly1 i) p.ASM.pcode) in
    1050  { ASM.code = code ; ASM.cost_labels = costs ;
     1066 { ASM.code = code ;
     1067   ASM.inds = inds; ASM.incs = incs; ASM.cost_labels = costs ;
    10511068   ASM.labels = StringTools.Map.empty ;
    10521069   ASM.exit_addr = exit_addr ; ASM.has_main = p.ASM.phas_main }
     
    19751992let load_program p =
    19761993  let st = load p.ASM.code initialize in
    1977   { st with exit_addr = p.ASM.exit_addr ; cost_labels = p.ASM.cost_labels }
    1978 
    1979 let observe_trace trace_ref st =
    1980   let cost_label =
    1981     if BitVectors.WordMap.mem st.pc st.cost_labels then
    1982       [BitVectors.WordMap.find st.pc st.cost_labels]
    1983     else [] in
    1984   trace_ref := cost_label @ !trace_ref ;
     1994  { st with exit_addr = p.ASM.exit_addr (* ; cost_labels = p.ASM.cost_labels *)}
     1995
     1996type cost_trace = {
     1997  mutable ct_labels : CostLabel.t list;
     1998  mutable ct_inds   : CostLabel.const_indexing list;
     1999}
     2000
     2001(* FIXME: supposing only one index reset or increment per instruction *)
     2002let update_indexes trace p st =
     2003  try
     2004    let i = BitVectors.WordMap.find st.pc p.ASM.inds in
     2005    CostLabel.enter_loop trace.ct_inds i
     2006  with Not_found -> ();
     2007    try
     2008      let i = BitVectors.WordMap.find st.pc p.ASM.incs in
     2009      CostLabel.continue_loop trace.ct_inds i
     2010    with Not_found -> ();
     2011      let instr,_,_ = fetch st.code_memory st.pc in
     2012      match instr with
     2013        | `ACALL _ | `LCALL _ ->
     2014          trace.ct_inds <- CostLabel.new_const_ind trace.ct_inds
     2015        | `RET ->
     2016          trace.ct_inds <- CostLabel.forget_const_ind trace.ct_inds
     2017        | _ -> ()
     2018
     2019let update_labels trace p st =
     2020  try
     2021    let cost_label = BitVectors.WordMap.find st.pc p.cost_labels in
     2022    let ind = CostLabel.curr_const_ind trace.ct_inds in
     2023    let cost_label = CostLabel.ev_indexing ind cost_label in
     2024    trace.ct_labels <- cost_label :: trace.ct_labels
     2025  with Not_found -> ()
     2026
     2027
     2028
     2029let update_trace trace p st =
     2030  update_labels trace p st;
     2031  update_indexes trace p st;
    19852032  if st.pc = st.exit_addr (* <=> end of program *) then raise Halt else st
    19862033
     
    20002047  if p.ASM.has_main then
    20012048    let st = load_program p in
    2002     let trace = ref [] in
    2003     let callback = observe_trace trace in
     2049    let trace = {ct_labels = []; ct_inds = []} in
     2050    let callback = update_trace trace p in
    20042051    let st = execute callback st in
    20052052    let res = result st in
    20062053    if debug then
    20072054      Printf.printf "Result = %s\n%!" (IntValue.Int32.to_string res) ;
    2008     (res, List.rev !trace)
     2055    (res, List.rev trace.ct_labels)
    20092056  else (IntValue.Int32.zero, [])
    20102057
Note: See TracChangeset for help on using the changeset viewer.