|
1 | 1 | open Types |
2 | 2 |
|
| 3 | +module type S = sig |
| 4 | + type custom_float |
| 5 | + |
| 6 | + val get_var_space : |
| 7 | + custom_float Types.ctx -> M_ir.Com.var_space -> M_ir.Com.variable_space |
| 8 | + |
| 9 | + val get_var : |
| 10 | + custom_float Types.ctx -> |
| 11 | + M_ir.Com.var_space -> |
| 12 | + M_ir.Com.Var.t -> |
| 13 | + M_ir.Com.variable_space * M_ir.Com.Var.t * int |
| 14 | + |
| 15 | + val get_var_tab : |
| 16 | + custom_float Types.ctx -> M_ir.Com.Var.t -> int -> M_ir.Com.Var.t |
| 17 | + |
| 18 | + val get_var_value_org : |
| 19 | + custom_float Types.ctx -> |
| 20 | + M_ir.Com.variable_space -> |
| 21 | + M_ir.Com.Var.t -> |
| 22 | + int -> |
| 23 | + custom_float Types.value |
| 24 | + |
| 25 | + val get_var_value : |
| 26 | + custom_float Types.ctx -> |
| 27 | + M_ir.Com.var_space -> |
| 28 | + M_ir.Com.Var.t -> |
| 29 | + custom_float Types.value |
| 30 | + |
| 31 | + val get_var_value_tab : |
| 32 | + custom_float Types.ctx -> |
| 33 | + M_ir.Com.var_space -> |
| 34 | + M_ir.Com.Var.t -> |
| 35 | + int -> |
| 36 | + custom_float Types.value |
| 37 | + |
| 38 | + val set_var_ref : |
| 39 | + custom_float Types.ctx -> |
| 40 | + M_ir.Com.Var.t -> |
| 41 | + M_ir.Com.variable_space -> |
| 42 | + M_ir.Com.Var.t -> |
| 43 | + int -> |
| 44 | + unit |
| 45 | + |
| 46 | + val get_access_value : |
| 47 | + eval: |
| 48 | + (custom_float Types.ctx -> |
| 49 | + M_ir.Com.Var.t M_ir.Com.m_expression -> |
| 50 | + custom_float Types.value) -> |
| 51 | + custom_float Types.ctx -> |
| 52 | + M_ir.Com.Var.t M_ir.Com.access -> |
| 53 | + custom_float Types.value |
| 54 | + |
| 55 | + val get_access_var : |
| 56 | + eval: |
| 57 | + (custom_float Types.ctx -> |
| 58 | + M_ir.Com.Var.t M_ir.Com.m_expression -> |
| 59 | + custom_float Types.value) -> |
| 60 | + custom_float Types.ctx -> |
| 61 | + M_ir.Com.Var.t M_ir.Com.access -> |
| 62 | + (M_ir.Com.variable_space * M_ir.Com.Var.t * int) option |
| 63 | + |
| 64 | + val set_var_value_org : |
| 65 | + custom_float Types.ctx -> |
| 66 | + M_ir.Com.variable_space -> |
| 67 | + M_ir.Com.Var.t -> |
| 68 | + int -> |
| 69 | + custom_float Types.value -> |
| 70 | + unit |
| 71 | + |
| 72 | + val set_var_value : |
| 73 | + custom_float Types.ctx -> |
| 74 | + M_ir.Com.var_space -> |
| 75 | + M_ir.Com.Var.t -> |
| 76 | + custom_float Types.value -> |
| 77 | + unit |
| 78 | + |
| 79 | + val set_var_value_tab : |
| 80 | + custom_float Types.ctx -> |
| 81 | + M_ir.Com.var_space -> |
| 82 | + M_ir.Com.Var.t -> |
| 83 | + int -> |
| 84 | + custom_float Types.value -> |
| 85 | + unit |
| 86 | + |
| 87 | + val set_access : |
| 88 | + eval: |
| 89 | + (custom_float Types.ctx -> |
| 90 | + M_ir.Com.Var.t M_ir.Com.m_expression -> |
| 91 | + custom_float Types.value) -> |
| 92 | + custom_float Types.ctx -> |
| 93 | + M_ir.Com.Var.t M_ir.Com.access -> |
| 94 | + custom_float Types.value -> |
| 95 | + unit |
| 96 | +end |
| 97 | + |
3 | 98 | let update_ctx_with_inputs (ctx : 'a ctx) |
4 | 99 | (value_inputs : 'a value Com.Var.Map.t) : unit = |
5 | 100 | (* let value_inputs = *) |
@@ -136,3 +231,180 @@ let empty_ctx ?inputs ?events (p : M_ir.Mir.program) : 'a ctx = |
136 | 231 | Option.iter (update_ctx_with_inputs res) inputs; |
137 | 232 | Option.iter (update_ctx_with_events res) events; |
138 | 233 | res |
| 234 | + |
| 235 | +module Make (N : M_ir.Mir_number.NumberInterface) = struct |
| 236 | + let get_var_space (ctx : N.t ctx) (m_sp_opt : Com.var_space) = |
| 237 | + let i_sp = |
| 238 | + match m_sp_opt with None -> ctx.ctx_var_space | Some (_, i_sp) -> i_sp |
| 239 | + in |
| 240 | + IntMap.find i_sp ctx.ctx_prog.program_var_spaces_idx |
| 241 | + |
| 242 | + let get_var (ctx : N.t ctx) (m_sp_opt : Com.var_space) (var : Com.Var.t) : |
| 243 | + Com.variable_space * Com.Var.t * int = |
| 244 | + match var.scope with |
| 245 | + | Com.Var.Tgv _ -> (get_var_space ctx m_sp_opt, var, 0) |
| 246 | + | Com.Var.Temp _ -> (get_var_space ctx None, var, ctx.ctx_tmps_org) |
| 247 | + | Com.Var.Ref -> |
| 248 | + let rv = ctx.ctx_ref.(ctx.ctx_ref_org + Com.Var.loc_idx var) in |
| 249 | + let vsd = |
| 250 | + match m_sp_opt with |
| 251 | + | None -> rv.var_space |
| 252 | + | _ -> get_var_space ctx m_sp_opt |
| 253 | + in |
| 254 | + (vsd, rv.ref_var, rv.org) |
| 255 | + |
| 256 | + let get_var_tab (ctx : N.t ctx) (var : Com.Var.t) (i : int) : Com.Var.t = |
| 257 | + match Com.Var.get_table var with |
| 258 | + | Some _ -> ctx.ctx_tab_map.(Com.Var.loc_tab_idx var + 1 + i) |
| 259 | + | None -> assert false |
| 260 | + |
| 261 | + let get_var_value_org (ctx : N.t ctx) (vsd : Com.variable_space) |
| 262 | + (var : Com.Var.t) (vorg : int) : N.t value = |
| 263 | + let vi = Com.Var.loc_idx var in |
| 264 | + match var.scope with |
| 265 | + | Com.Var.Tgv _ -> |
| 266 | + let var_space = ctx.ctx_var_spaces.(vsd.vs_id) in |
| 267 | + let var_tab = |
| 268 | + match Com.Var.cat_var_loc var with |
| 269 | + | LocInput -> var_space.input |
| 270 | + | LocComputed -> var_space.computed |
| 271 | + | LocBase -> var_space.base |
| 272 | + in |
| 273 | + if Array.length var_tab > 0 then var_tab.(vi) else Undefined |
| 274 | + | Com.Var.Temp _ -> ctx.ctx_tmps.(vorg + vi).value |
| 275 | + | Com.Var.Ref -> assert false |
| 276 | + |
| 277 | + let get_var_value (ctx : N.t ctx) (m_sp_opt : Com.var_space) (v : Com.Var.t) : |
| 278 | + N.t value = |
| 279 | + let vsd, var, vorg = get_var ctx m_sp_opt v in |
| 280 | + let var = if Com.Var.is_table var then get_var_tab ctx var 0 else var in |
| 281 | + get_var_value_org ctx vsd var vorg |
| 282 | + |
| 283 | + let get_var_value_tab (ctx : N.t ctx) (m_sp_opt : Com.var_space) |
| 284 | + (v : Com.Var.t) (i : int) : N.t value = |
| 285 | + let vsd, var, vorg = get_var ctx m_sp_opt v in |
| 286 | + if i < 0 then Number (N.zero ()) |
| 287 | + else if Com.Var.size var <= i then Undefined |
| 288 | + else if Com.Var.is_table var then |
| 289 | + let var_i = get_var_tab ctx var i in |
| 290 | + get_var_value_org ctx vsd var_i vorg |
| 291 | + else get_var_value_org ctx vsd var vorg |
| 292 | + |
| 293 | + let set_var_ref (ctx : N.t ctx) (var : Com.Var.t) |
| 294 | + (var_space : Com.variable_space) (ref_var : Com.Var.t) (org : int) : unit |
| 295 | + = |
| 296 | + match var.loc with |
| 297 | + | LocRef (_, i) -> |
| 298 | + ctx.ctx_ref.(ctx.ctx_ref_org + i).var <- var; |
| 299 | + ctx.ctx_ref.(ctx.ctx_ref_org + i).var_space <- var_space; |
| 300 | + ctx.ctx_ref.(ctx.ctx_ref_org + i).ref_var <- ref_var; |
| 301 | + ctx.ctx_ref.(ctx.ctx_ref_org + i).org <- org |
| 302 | + | _ -> assert false |
| 303 | + |
| 304 | + let rec get_access_value ~eval ctx access = |
| 305 | + match access with |
| 306 | + | Com.VarAccess (m_sp_opt, v) -> get_var_value ctx m_sp_opt v |
| 307 | + | Com.TabAccess ((m_sp_opt, v), m_idx) -> ( |
| 308 | + match eval ctx m_idx with |
| 309 | + | Number z -> |
| 310 | + let i = Int64.to_int @@ N.to_int z in |
| 311 | + get_var_value_tab ctx m_sp_opt v i |
| 312 | + | Undefined -> Undefined) |
| 313 | + | Com.FieldAccess (m_sp_opt, e, _, j) -> ( |
| 314 | + match eval ctx e with |
| 315 | + | Number z -> |
| 316 | + let i = Int64.to_int @@ N.to_int z in |
| 317 | + let events = List.hd ctx.ctx_events in |
| 318 | + if 0 <= i && i < Array.length events then |
| 319 | + match events.(i).(j) with |
| 320 | + | Com.Numeric n -> n |
| 321 | + | Com.RefVar v -> get_var_value ctx m_sp_opt v |
| 322 | + else Undefined |
| 323 | + | Undefined -> Undefined) |
| 324 | + |
| 325 | + and get_access_var ~eval ctx access = |
| 326 | + match access with |
| 327 | + | Com.VarAccess (m_sp_opt, v) -> |
| 328 | + let vsd, v, vorg = get_var ctx m_sp_opt v in |
| 329 | + Some (vsd, v, vorg) |
| 330 | + | Com.TabAccess ((m_sp_opt, m_v), m_i) -> ( |
| 331 | + match eval ctx m_i with |
| 332 | + | Number z -> |
| 333 | + let vsd, v, vorg = get_var ctx m_sp_opt m_v in |
| 334 | + let i = Int64.to_int @@ N.to_int z in |
| 335 | + if 0 <= i && i < Com.Var.size v then |
| 336 | + if Com.Var.is_table v then |
| 337 | + let v_i = get_var_tab ctx v i in |
| 338 | + Some (vsd, v_i, vorg) |
| 339 | + else Some (vsd, v, vorg) |
| 340 | + else None |
| 341 | + | Undefined -> None) |
| 342 | + | Com.FieldAccess (m_sp_opt, m_e, _, j) -> ( |
| 343 | + match eval ctx m_e with |
| 344 | + | Number z -> |
| 345 | + let i = Int64.to_int @@ N.to_int z in |
| 346 | + let events = List.hd ctx.ctx_events in |
| 347 | + if 0 <= i && i < Array.length events then |
| 348 | + match events.(i).(j) with |
| 349 | + | Com.RefVar v -> |
| 350 | + let vsd, var, vorg = get_var ctx m_sp_opt v in |
| 351 | + Some (vsd, var, vorg) |
| 352 | + | Com.Numeric _ -> None |
| 353 | + else None |
| 354 | + | _ -> None) |
| 355 | + |
| 356 | + and set_var_value_org (ctx : N.t ctx) (vsd : Com.variable_space) |
| 357 | + (var : Com.Var.t) (vorg : int) (value : N.t value) : unit = |
| 358 | + let vi = Com.Var.loc_idx var in |
| 359 | + match var.scope with |
| 360 | + | Com.Var.Tgv _ -> |
| 361 | + let var_space = ctx.ctx_var_spaces.(vsd.vs_id) in |
| 362 | + let var_tab = |
| 363 | + match Com.Var.cat_var_loc var with |
| 364 | + | LocInput -> var_space.input |
| 365 | + | LocComputed -> var_space.computed |
| 366 | + | LocBase -> var_space.base |
| 367 | + in |
| 368 | + if Array.length var_tab > 0 then var_tab.(vi) <- value |
| 369 | + | Com.Var.Temp _ -> ctx.ctx_tmps.(vorg + vi).value <- value |
| 370 | + | Com.Var.Ref -> assert false |
| 371 | + |
| 372 | + and set_var_value (ctx : N.t ctx) (m_sp_opt : Com.var_space) (var : Com.Var.t) |
| 373 | + (value : N.t value) : unit = |
| 374 | + let vsd, v, vorg = get_var ctx m_sp_opt var in |
| 375 | + if Com.Var.is_table v then |
| 376 | + for i = 0 to Com.Var.size v - 1 do |
| 377 | + let v_i = get_var_tab ctx v i in |
| 378 | + set_var_value_org ctx vsd v_i vorg value |
| 379 | + done |
| 380 | + else set_var_value_org ctx vsd v vorg value |
| 381 | + |
| 382 | + and set_var_value_tab (ctx : N.t ctx) (m_sp_opt : Com.var_space) |
| 383 | + (v : Com.Var.t) (i : int) (value : N.t value) : unit = |
| 384 | + let vsd, var, vorg = get_var ctx m_sp_opt v in |
| 385 | + if 0 <= i && i < Com.Var.size var then |
| 386 | + if Com.Var.is_table var then |
| 387 | + let var_i = get_var_tab ctx var i in |
| 388 | + set_var_value_org ctx vsd var_i vorg value |
| 389 | + else set_var_value_org ctx vsd var vorg value |
| 390 | + |
| 391 | + and set_access ~eval ctx access value = |
| 392 | + match access with |
| 393 | + | Com.VarAccess (m_sp_opt, v) -> set_var_value ctx m_sp_opt v value |
| 394 | + | Com.TabAccess ((m_sp_opt, v), m_idx) -> ( |
| 395 | + match eval ctx m_idx with |
| 396 | + | Number z -> |
| 397 | + let i = Int64.to_int @@ N.to_int z in |
| 398 | + set_var_value_tab ctx m_sp_opt v i value |
| 399 | + | Undefined -> ()) |
| 400 | + | Com.FieldAccess (m_sp_opt, e, _, j) -> ( |
| 401 | + match eval ctx e with |
| 402 | + | Number z -> ( |
| 403 | + let i = Int64.to_int @@ N.to_int z in |
| 404 | + let events = List.hd ctx.ctx_events in |
| 405 | + if 0 <= i && i < Array.length events then |
| 406 | + match events.(i).(j) with |
| 407 | + | Com.Numeric _ -> events.(i).(j) <- Com.Numeric value |
| 408 | + | Com.RefVar v -> set_var_value ctx m_sp_opt v value) |
| 409 | + | Undefined -> ()) |
| 410 | +end |
0 commit comments