[486] | 1 | |
---|
| 2 | (** This module provides an interpreter for the RTL language. *) |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | let error_prefix = "RTL interpret" |
---|
| 6 | let error s = Error.global_error error_prefix s |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | module Mem = Driver.RTLMemory |
---|
| 10 | module Val = Mem.Value |
---|
| 11 | let chunk = Driver.RTLMemory.int_size |
---|
| 12 | module Eval = I8051.Eval (Val) |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | type memory = RTL.function_def Mem.memory |
---|
| 16 | |
---|
| 17 | |
---|
| 18 | (* Local environments. They associate a value to the registers of the function |
---|
| 19 | being executed. *) |
---|
| 20 | |
---|
| 21 | type local_env = Val.t Register.Map.t |
---|
| 22 | |
---|
| 23 | (* Call frames. The execution state has a call stack, each element of the stack |
---|
| 24 | being composed of the return registers to store the result of the callee, the |
---|
| 25 | graph, the node, the local environment and the value of the carry to resume |
---|
| 26 | the execution of the caller. *) |
---|
| 27 | |
---|
| 28 | type stack_frame = |
---|
| 29 | { ret_regs : Register.t list ; |
---|
| 30 | graph : RTL.graph ; |
---|
| 31 | pc : Label.t ; |
---|
[740] | 32 | sp : Val.address ; |
---|
[486] | 33 | lenv : local_env ; |
---|
| 34 | carry : Val.t } |
---|
| 35 | |
---|
[1542] | 36 | type indexing = CostLabel.const_indexing |
---|
| 37 | |
---|
[486] | 38 | (* Execution states. There are three possible states : |
---|
| 39 | - The constructor [State] represents a state when executing a function |
---|
| 40 | - The constructor [CallState] represents a state when calling a function |
---|
| 41 | - The constructor [ReturnState] represents a state when leaving a function *) |
---|
| 42 | |
---|
| 43 | type state = |
---|
[740] | 44 | | State of stack_frame list * RTL.graph * Label.t * Val.address (* sp *) * |
---|
[1542] | 45 | local_env * Val.t (* carry *) * memory * indexing list * |
---|
| 46 | CostLabel.t list |
---|
[486] | 47 | | CallState of stack_frame list * RTL.function_def * |
---|
[1542] | 48 | Val.t list (* args *) * memory * indexing list * CostLabel.t list |
---|
[486] | 49 | | ReturnState of stack_frame list * Val.t list (* return values *) * |
---|
[1542] | 50 | memory * indexing list * CostLabel.t list |
---|
[486] | 51 | |
---|
[740] | 52 | let string_of_local_env lenv = |
---|
| 53 | let f x v s = |
---|
| 54 | s ^ |
---|
| 55 | (if Val.eq v Val.undef then "" |
---|
| 56 | else (Register.print x) ^ " = " ^ (Val.to_string v) ^ " ") in |
---|
| 57 | Register.Map.fold f lenv "" |
---|
[486] | 58 | |
---|
[740] | 59 | let string_of_args args = |
---|
| 60 | let f s v = s ^ " " ^ (Val.to_string v) in |
---|
| 61 | List.fold_left f "" args |
---|
| 62 | |
---|
| 63 | let print_state = function |
---|
[1542] | 64 | | State (_, _, lbl, sp, lenv, carry, mem, ind, _) -> |
---|
| 65 | Printf.printf "Stack pointer: %s\n\nCarry: %s\n\nLocal environment:\n%s\n\nMemory:%s\nIndexing:" |
---|
[740] | 66 | (Val.string_of_address sp) |
---|
| 67 | (Val.to_string carry) |
---|
| 68 | (string_of_local_env lenv) |
---|
[1542] | 69 | (Mem.to_string mem); |
---|
| 70 | let i = CostLabel.curr_const_ind ind in |
---|
| 71 | CostLabel.const_ind_iter (fun a -> Printf.printf "%d, " a) i; |
---|
| 72 | Printf.printf "Regular state: %s\n\n%!" |
---|
[740] | 73 | lbl |
---|
[1542] | 74 | | CallState (_, _, args, mem, _, _) -> |
---|
[740] | 75 | Printf.printf "Memory:%s\nCall state: %s\n\n%!" |
---|
| 76 | (Mem.to_string mem) |
---|
| 77 | (string_of_args args) |
---|
[1542] | 78 | | ReturnState (_, vs, mem, _, _) -> |
---|
[740] | 79 | Printf.printf "Memory:%s\nReturn state: %s\n\n%!" |
---|
| 80 | (Mem.to_string mem) |
---|
| 81 | (string_of_args vs) |
---|
| 82 | |
---|
| 83 | |
---|
[486] | 84 | let find_function mem f = |
---|
[740] | 85 | let addr = Mem.find_global mem f in |
---|
| 86 | Mem.find_fun_def mem addr |
---|
[486] | 87 | |
---|
| 88 | let get_local_value (lenv : local_env) (r : Register.t) : Val.t = |
---|
| 89 | if Register.Map.mem r lenv then Register.Map.find r lenv |
---|
| 90 | else error ("Unknown local register \"" ^ (Register.print r) ^ "\".") |
---|
| 91 | let get_arg_values lenv args = List.map (get_local_value lenv) args |
---|
| 92 | |
---|
| 93 | let get_local_addr lenv f1 f2 = |
---|
[740] | 94 | List.map (get_local_value lenv) [f1 ; f2] |
---|
[486] | 95 | |
---|
| 96 | |
---|
[740] | 97 | let adds rs vs lenv = |
---|
| 98 | let f lenv r v = Register.Map.add r v lenv in |
---|
| 99 | List.fold_left2 f lenv rs vs |
---|
[486] | 100 | |
---|
| 101 | |
---|
| 102 | (* Assign a value to some destinations registers. *) |
---|
| 103 | |
---|
[1542] | 104 | let assign_state sfrs graph lbl sp lenv carry mem inds trace destrs vs = |
---|
[740] | 105 | let lenv = adds destrs vs lenv in |
---|
[1542] | 106 | State (sfrs, graph, lbl, sp, lenv, carry, mem, inds, trace) |
---|
[486] | 107 | |
---|
| 108 | (* Branch on a value. *) |
---|
| 109 | |
---|
[1542] | 110 | let branch_state sfrs graph lbl_true lbl_false sp lenv carry mem inds trace v = |
---|
[486] | 111 | let next_lbl = |
---|
| 112 | if Val.is_true v then lbl_true |
---|
| 113 | else |
---|
| 114 | if Val.is_false v then lbl_false |
---|
| 115 | else error "Undefined conditional value." in |
---|
[1542] | 116 | State (sfrs, graph, next_lbl, sp, lenv, carry, mem, inds, trace) |
---|
[486] | 117 | |
---|
[1542] | 118 | let curr_ind = CostLabel.curr_const_ind |
---|
[486] | 119 | |
---|
[1542] | 120 | let forget_ind = CostLabel.forget_const_ind |
---|
| 121 | |
---|
| 122 | let new_ind = CostLabel.new_const_ind |
---|
| 123 | |
---|
| 124 | |
---|
[486] | 125 | (* Interpret statements. *) |
---|
| 126 | |
---|
| 127 | let interpret_statement |
---|
[740] | 128 | (sfrs : stack_frame list) |
---|
| 129 | (graph : RTL.graph) |
---|
| 130 | (sp : Val.address) |
---|
[486] | 131 | (lenv : local_env) |
---|
| 132 | (carry : Val.t) |
---|
| 133 | (mem : memory) |
---|
[1542] | 134 | (inds : indexing list) |
---|
[486] | 135 | (stmt : RTL.statement) |
---|
[740] | 136 | (trace : CostLabel.t list) : |
---|
[486] | 137 | state = match stmt with |
---|
| 138 | |
---|
| 139 | | RTL.St_skip lbl -> |
---|
[1542] | 140 | State (sfrs, graph, lbl, sp, lenv, carry, mem, inds, trace) |
---|
[486] | 141 | |
---|
| 142 | | RTL.St_cost (cost_lbl, lbl) -> |
---|
[1542] | 143 | let cost_lbl = CostLabel.ev_indexing (curr_ind inds) cost_lbl in |
---|
| 144 | State (sfrs, graph, lbl, sp, lenv, carry, mem, inds, cost_lbl :: trace) |
---|
[486] | 145 | |
---|
[1542] | 146 | | RTL.St_ind_0 (i, lbl) -> |
---|
| 147 | CostLabel.enter_loop inds i; |
---|
| 148 | State (sfrs, graph, lbl, sp, lenv, carry, mem, inds, trace) |
---|
| 149 | |
---|
| 150 | | RTL.St_ind_inc (i, lbl) -> |
---|
| 151 | CostLabel.continue_loop inds i; |
---|
| 152 | State (sfrs, graph, lbl, sp, lenv, carry, mem, inds, trace) |
---|
| 153 | |
---|
[486] | 154 | | RTL.St_addr (r1, r2, x, lbl) -> |
---|
[1542] | 155 | assign_state sfrs graph lbl sp lenv carry mem inds trace [r1 ; r2] |
---|
[486] | 156 | (Mem.find_global mem x) |
---|
| 157 | |
---|
| 158 | | RTL.St_stackaddr (r1, r2, lbl) -> |
---|
[1542] | 159 | assign_state sfrs graph lbl sp lenv carry mem inds trace [r1 ; r2] sp |
---|
[486] | 160 | |
---|
| 161 | | RTL.St_int (r, i, lbl) -> |
---|
[1542] | 162 | assign_state sfrs graph lbl sp lenv carry mem inds trace [r] [Val.of_int i] |
---|
[486] | 163 | |
---|
| 164 | | RTL.St_move (destr, srcr, lbl) -> |
---|
[1542] | 165 | assign_state sfrs graph lbl sp lenv carry mem inds trace [destr] |
---|
[740] | 166 | [get_local_value lenv srcr] |
---|
[486] | 167 | |
---|
[818] | 168 | | RTL.St_opaccs (opaccs, destr1, destr2, srcr1, srcr2, lbl) -> |
---|
| 169 | let (v1, v2) = |
---|
[486] | 170 | Eval.opaccs opaccs |
---|
| 171 | (get_local_value lenv srcr1) |
---|
| 172 | (get_local_value lenv srcr2) in |
---|
[1542] | 173 | assign_state sfrs graph lbl sp lenv carry mem inds trace |
---|
[818] | 174 | [destr1 ; destr2] [v1 ; v2] |
---|
[486] | 175 | |
---|
| 176 | | RTL.St_op1 (op1, destr, srcr, lbl) -> |
---|
| 177 | let v = Eval.op1 op1 (get_local_value lenv srcr) in |
---|
[1542] | 178 | assign_state sfrs graph lbl sp lenv carry mem inds trace [destr] [v] |
---|
[486] | 179 | |
---|
| 180 | | RTL.St_op2 (op2, destr, srcr1, srcr2, lbl) -> |
---|
| 181 | let (v, carry) = |
---|
| 182 | Eval.op2 carry op2 |
---|
| 183 | (get_local_value lenv srcr1) |
---|
| 184 | (get_local_value lenv srcr2) in |
---|
[1542] | 185 | assign_state sfrs graph lbl sp lenv carry mem inds trace [destr] [v] |
---|
[486] | 186 | |
---|
| 187 | | RTL.St_clear_carry lbl -> |
---|
[1542] | 188 | State (sfrs, graph, lbl, sp, lenv, Val.zero, mem, inds, trace) |
---|
[486] | 189 | |
---|
[818] | 190 | | RTL.St_set_carry lbl -> |
---|
[1542] | 191 | State (sfrs, graph, lbl, sp, lenv, Val.of_int 1, mem, inds, trace) |
---|
[818] | 192 | |
---|
[486] | 193 | | RTL.St_load (destr, addr1, addr2, lbl) -> |
---|
[740] | 194 | let addr = get_local_addr lenv addr1 addr2 in |
---|
| 195 | let v = Mem.load mem chunk addr in |
---|
[1542] | 196 | assign_state sfrs graph lbl sp lenv carry mem inds trace [destr] [v] |
---|
[486] | 197 | |
---|
| 198 | | RTL.St_store (addr1, addr2, srcr, lbl) -> |
---|
[740] | 199 | let addr = get_local_addr lenv addr1 addr2 in |
---|
| 200 | let mem = Mem.store mem chunk addr (get_local_value lenv srcr) in |
---|
[1542] | 201 | State (sfrs, graph, lbl, sp, lenv, carry, mem, inds, trace) |
---|
[486] | 202 | |
---|
| 203 | | RTL.St_call_id (f, args, ret_regs, lbl) -> |
---|
| 204 | let f_def = find_function mem f in |
---|
| 205 | let args = get_arg_values lenv args in |
---|
| 206 | let sf = |
---|
[740] | 207 | { ret_regs = ret_regs ; graph = graph ; pc = lbl ; |
---|
[486] | 208 | sp = sp ; lenv = lenv ; carry = carry } |
---|
| 209 | in |
---|
[1542] | 210 | CallState (sf :: sfrs, f_def, args, mem, inds, trace) |
---|
[486] | 211 | |
---|
| 212 | | RTL.St_call_ptr (f1, f2, args, ret_regs, lbl) -> |
---|
| 213 | let addr = get_local_addr lenv f1 f2 in |
---|
| 214 | let f_def = Mem.find_fun_def mem addr in |
---|
| 215 | let args = get_arg_values lenv args in |
---|
[740] | 216 | let sf = { ret_regs = ret_regs ; graph = graph ; pc = lbl ; |
---|
[486] | 217 | sp = sp ; lenv = lenv ; carry = carry } in |
---|
[1542] | 218 | CallState (sf :: sfrs, f_def, args, mem, inds, trace) |
---|
[486] | 219 | |
---|
| 220 | | RTL.St_tailcall_id (f, args) -> |
---|
| 221 | let f_def = find_function mem f in |
---|
| 222 | let args = get_arg_values lenv args in |
---|
| 223 | let mem = Mem.free mem sp in |
---|
[1542] | 224 | CallState (sfrs, f_def, args, mem, inds, trace) |
---|
[486] | 225 | |
---|
| 226 | | RTL.St_tailcall_ptr (f1, f2, args) -> |
---|
| 227 | let addr = get_local_addr lenv f1 f2 in |
---|
| 228 | let f_def = Mem.find_fun_def mem addr in |
---|
| 229 | let args = get_arg_values lenv args in |
---|
| 230 | let mem = Mem.free mem sp in |
---|
[1542] | 231 | CallState (sfrs, f_def, args, mem, inds, trace) |
---|
[486] | 232 | |
---|
[818] | 233 | | RTL.St_cond (srcr, lbl_true, lbl_false) -> |
---|
[486] | 234 | let v = get_local_value lenv srcr in |
---|
[1542] | 235 | branch_state sfrs graph lbl_true lbl_false sp lenv carry mem inds trace v |
---|
[486] | 236 | |
---|
| 237 | | RTL.St_return rl -> |
---|
| 238 | let vl = List.map (get_local_value lenv) rl in |
---|
| 239 | let mem = Mem.free mem sp in |
---|
[1542] | 240 | ReturnState (sfrs, vl, mem, inds, trace) |
---|
[486] | 241 | |
---|
| 242 | |
---|
[740] | 243 | module InterpretExternal = Primitive.Interpret (Mem) |
---|
[486] | 244 | |
---|
[740] | 245 | let interpret_external mem f args = match InterpretExternal.t mem f args with |
---|
[818] | 246 | | (mem', InterpretExternal.V vs) -> (mem', vs) |
---|
[740] | 247 | | (mem', InterpretExternal.A addr) -> (mem', addr) |
---|
[486] | 248 | |
---|
| 249 | let init_locals |
---|
| 250 | (locals : Register.Set.t) |
---|
| 251 | (params : Register.t list) |
---|
| 252 | (args : Val.t list) : |
---|
| 253 | local_env = |
---|
| 254 | let f r lenv = Register.Map.add r Val.undef lenv in |
---|
| 255 | let lenv = Register.Set.fold f locals Register.Map.empty in |
---|
| 256 | let f lenv r v = Register.Map.add r v lenv in |
---|
| 257 | List.fold_left2 f lenv params args |
---|
| 258 | |
---|
| 259 | let state_after_call |
---|
[740] | 260 | (sfrs : stack_frame list) |
---|
[486] | 261 | (f_def : RTL.function_def) |
---|
| 262 | (args : Val.t list) |
---|
| 263 | (mem : memory) |
---|
[1542] | 264 | (inds : indexing list) |
---|
[740] | 265 | (trace : CostLabel.t list) : |
---|
[486] | 266 | state = |
---|
| 267 | match f_def with |
---|
| 268 | | RTL.F_int def -> |
---|
[1542] | 269 | let inds = new_ind inds in |
---|
[486] | 270 | let (mem', sp) = Mem.alloc mem def.RTL.f_stacksize in |
---|
[740] | 271 | State (sfrs, def.RTL.f_graph, def.RTL.f_entry, sp, |
---|
[486] | 272 | init_locals def.RTL.f_locals def.RTL.f_params args, |
---|
[1542] | 273 | Val.undef, mem', inds, trace) |
---|
[486] | 274 | | RTL.F_ext def -> |
---|
[740] | 275 | let (mem', vs) = interpret_external mem def.AST.ef_tag args in |
---|
[1542] | 276 | ReturnState (sfrs, vs, mem', inds, trace) |
---|
[486] | 277 | |
---|
| 278 | let state_after_return |
---|
| 279 | (sf : stack_frame) |
---|
[740] | 280 | (sfrs : stack_frame list) |
---|
[486] | 281 | (ret_vals : Val.t list) |
---|
| 282 | (mem : memory) |
---|
[1542] | 283 | (inds : indexing list) |
---|
[740] | 284 | (trace : CostLabel.t list) : |
---|
[486] | 285 | state = |
---|
| 286 | let f i lenv r = Register.Map.add r (List.nth ret_vals i) lenv in |
---|
| 287 | let lenv = MiscPottier.foldi f sf.lenv sf.ret_regs in |
---|
[1542] | 288 | let inds = forget_ind inds in |
---|
| 289 | State (sfrs, sf.graph, sf.pc, sf.sp, lenv, sf.carry, mem, inds, trace) |
---|
[486] | 290 | |
---|
| 291 | |
---|
| 292 | let small_step (st : state) : state = match st with |
---|
[1542] | 293 | | State (sfrs, graph, pc, sp, lenv, carry, mem, inds, trace) -> |
---|
[486] | 294 | let stmt = Label.Map.find pc graph in |
---|
[1542] | 295 | interpret_statement sfrs graph sp lenv carry mem inds stmt trace |
---|
| 296 | | CallState (sfrs, f_def, args, mem, inds, trace) -> |
---|
| 297 | state_after_call sfrs f_def args mem inds trace |
---|
| 298 | | ReturnState ([], ret_vals, mem, inds, trace) -> |
---|
[486] | 299 | assert false (* End of execution; handled in iter_small_step. *) |
---|
[1542] | 300 | | ReturnState (sf :: sfrs, ret_vals, mem, inds, trace) -> |
---|
| 301 | state_after_return sf sfrs ret_vals mem inds trace |
---|
[486] | 302 | |
---|
| 303 | |
---|
[619] | 304 | let compute_result vs = |
---|
[818] | 305 | let f res v = res && (Val.is_int v) in |
---|
| 306 | let is_int vs = (List.length vs > 0) && (List.fold_left f true vs) in |
---|
| 307 | if is_int vs then |
---|
| 308 | let chunks = |
---|
| 309 | List.map (fun v -> IntValue.Int32.cast (Val.to_int_repr v)) vs in |
---|
| 310 | IntValue.Int32.merge chunks |
---|
| 311 | else IntValue.Int32.zero |
---|
[619] | 312 | |
---|
[740] | 313 | let rec iter_small_step debug st = |
---|
[818] | 314 | let print_and_return_result (res, cost_labels) = |
---|
| 315 | if debug then Printf.printf "Result = %s\n%!" |
---|
| 316 | (IntValue.Int32.to_string res) ; |
---|
| 317 | (res, cost_labels) in |
---|
[740] | 318 | if debug then print_state st ; |
---|
| 319 | match small_step st with |
---|
[1542] | 320 | | ReturnState ([], vs, mem, _, trace) -> |
---|
[818] | 321 | print_and_return_result (compute_result vs, List.rev trace) |
---|
[740] | 322 | | st' -> iter_small_step debug st' |
---|
[486] | 323 | |
---|
| 324 | |
---|
| 325 | let add_global_vars = |
---|
| 326 | List.fold_left |
---|
[818] | 327 | (fun mem (id, size) -> Mem.add_var mem id (AST.SQ (AST.QInt size)) None) |
---|
[486] | 328 | |
---|
| 329 | let add_fun_defs = |
---|
| 330 | List.fold_left (fun mem (f_id, f_def) -> Mem.add_fun_def mem f_id f_def) |
---|
| 331 | |
---|
| 332 | |
---|
[740] | 333 | (* The memory is initialized by loading the code into it, and by reserving space |
---|
[486] | 334 | for the global variables. *) |
---|
| 335 | |
---|
| 336 | let init_mem (p : RTL.program) : memory = |
---|
| 337 | add_global_vars (add_fun_defs Mem.empty p.RTL.functs) p.RTL.vars |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | (* Interpret the program only if it has a main. *) |
---|
| 341 | |
---|
[740] | 342 | let interpret debug p = |
---|
[818] | 343 | Printf.printf "*** RTL interpret ***\n%!" ; |
---|
[740] | 344 | match p.RTL.main with |
---|
[818] | 345 | | None -> (IntValue.Int32.zero, []) |
---|
[740] | 346 | | Some main -> |
---|
| 347 | let mem = init_mem p in |
---|
| 348 | let main_def = find_function mem main in |
---|
[1542] | 349 | let st = CallState ([], main_def, [], mem, [], []) in |
---|
[740] | 350 | iter_small_step debug st |
---|