epatrizio / miniml2wasm

MiniML to WebAssembly compiler
The Unlicense
1 stars 0 forks source link

Static analysis #1

Open epatrizio opened 1 month ago

epatrizio commented 1 month ago

Set up a static analysis pass before compilation. Example: unused variables. Experimental.

epatrizio commented 3 days ago

I've tried to implement this by adding a usage counter to the variables in the environment :

type ('a, 'b) t =
  {
  (* ... *)
  ; globals : (string * int) SMap.t
  ; locals : (string * int) SMap.t
  (* ... *)
  }

let add_global n env =
  (* ... *)
  let globals = SMap.add n (fresh_name, 0) env.globals in
  (* ... *)

let add_local n env =
  (* ... *)
  let locals = SMap.add n (fresh_name, 0) env.locals in
  (* ... *)

let get_name n env =
  (* ... *)
  | Some (name, nb) ->
    let locals = SMap.add n (name, nb + 1) env.locals in
    (* ... *)
  | None -> (
      (* ... *)
      | Some (name, nb) ->
      let globals = SMap.add n (name, nb + 1) env.globals in
      (* ... *)

Then, during the scope analysis, detecting null counters is very tricky for example with local scopes and nested blocks :

(* .mll file *)
let z = false;
let y = 0 in
  let x = 42 in
    if z then 0 else (x+y)
Warning: unused local variable y
Warning: unused global variable z