x86codegen bug (2/2)

Stephen Weeks MLton@sourcelight.com
Thu, 31 May 2001 16:18:13 -0700


hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

	movl MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)],MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
remove: MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: USEDEF MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
future: DEF MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

	incl MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USEDEF MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

# directive: Cache: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi (reserved)
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

# directive: Cache: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi (reserved)
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi

# directive: Flush
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

# directive: Commit: 
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: DEF MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

	jo L_84
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: DEF MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

# directive: Unreserve: %edi
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: DEF MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

# directive: Unreserve: %esi
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: DEF MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

	movl MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)],MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
dead: MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
remove: MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: DEF MEM<F>(l){Stack}[MEM<>(l){Runtime}[(gcState+40)+(0*4)]+(184*1)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<T>(l){Runtime}[overflowCheckTemp+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

# directive: Cache: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi (reserved)
remove: MEM<>(l){Runtime}[(gcState+40)+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+40)+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi
hint: MEM<>(l){Runtime}[(gcState+40)+(0*4)] -> %edi

# directive: Cache: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi (reserved)
remove: MEM<>(l){Runtime}[(gcState+12)+(0*4)]
future: USE MEM<>(l){Runtime}[(gcState+12)+(0*4)]
hint: MEM<>(l){Runtime}[(gcState+12)+(0*4)] -> %esi

# directive: Flush

# directive: Commit: 

	jmp statementLimitCheckLoop_0

mlton: fltupdate

--------------------------------------------------------------------------------
Here is moments.sml
--------------------------------------------------------------------------------
fun for (start, stop, f) =
   let
      fun loop i =
	 if i > stop
	    then ()
	 else (f i; loop (i + 1))
   in
      loop start
   end
val sub = Array.sub
val update = Array.update
fun incr r = r := !r + 1

structure Quicksort:
   sig
      val quicksort: 'a array * ('a * 'a -> bool) -> unit
   end =
   struct
      fun assert (s, f: unit -> bool) =
	 if true orelse f ()
	    then ()
	 else raise Fail (concat ["assert: ", s])

      fun forall (low, high, f) =
	 let
	    fun loop i = i > high orelse (f i andalso loop (i + 1))
	 in
	    loop low
	 end

      fun fold (l, u, state, f) =
	 let
	    fun loop (i, state) =
	       if i > u
		  then state
	       else loop (i + 1, f (i, state))
	 in
	    loop (l, state)
	 end

      (* Check if entries in a[lo ... hi] are sorted. *)
      fun 'a isSorted (a: 'a array,
		       lo: int,
		       hi: int,
		       op <= : 'a * 'a -> bool) =
	 let
	    fun loop (i, x) =
	       i > hi
	       orelse let
			 val y = sub (a, i)
		      in
			 x <= y andalso loop (i + 1, y)
		      end
	 in
	    lo >= hi orelse loop (lo + 1, sub (a, lo))
	 end

      (* From page 284 of Numerical Recipes in C. *)
      local
	 open Word
	 val seed = ref 0w13
      in
	 fun rand () =
	    let
	       val res = 0w1664525 * !seed + 0w1013904223
	       val _ = seed := res
	    in
	       toIntX res
	    end
      end

      fun randInt (lo, hi) = lo + Int.mod (rand(), hi - lo + 1)

      (* Based on page 108 of Programming Pearls, by Bentley. *)
      fun insertionSort (a: 'a array, op <= : 'a * 'a -> bool): unit =
	 let
	    fun x i = sub (a, i)
	 in
	    for (1, Array.length a - 1, fn i =>
		 let
		    val _ =
		       assert ("insertionSort1", fn () =>
			       isSorted (a, 0, i - 1, op <=))
		    val t = x i
		    fun sift (j: int) =
		       (assert ("insertionSort2", fn () =>
				isSorted (a, 0, j - 1, op <=)
				andalso isSorted (a, j + 1, i, op <=)
				andalso forall (j + 1, i, fn k => t <= x k))
			; if j > 0
			     then
				let
				   val j' = j - 1
				   val z = x j'
				in if t <= z
				      then (update (a, j, z);
					    sift j')
				   else j
				end
			  else j)
		    val _ = update (a, sift i, t)
		 in ()
		 end)
	 end

      (* Based on page 112 of Programming Pearls, by Bentley. *)
      fun 'a quicksort (a: 'a array, op <= : 'a * 'a -> bool): unit =
	 let
	    fun x i = Array.sub (a, i)
	    fun swap (i, j) =
	       let
		  val t = x i
		  val _ = update (a, i, x j)
		  val _ = update (a, j, t)
	       in ()
	       end
	    val cutoff = 20
	    fun qsort (l: int, u: int): unit =
	       if u - l > cutoff
		  then
		     let
			(* Need to do random swap. *)
			val _ = swap (l, randInt (l, u))
			val t = x l
			val m =
			   fold
			   (l + 1, u, l, fn (i, m) =>
			    (assert
			     ("qsort", fn () =>
			      forall (l + 1, m, fn k => x k <= t)
			      andalso forall (m + 1, i - 1, fn k => not (x k <= t)))
			     ; if x i <= t
				  then (swap (m + 1, i)
					; m + 1)
			       else m))
			val _ = swap (l, m)
			val _ = qsort (l, m - 1)
			val _ = qsort (m + 1, u)
		     in ()
		     end
	       else ()
	    val max = Array.length a - 1
	    val _ = qsort (0, max)
	    val _ = insertionSort (a, op <=)  
	 in
	    ()
	 end
   end
   
val n = ref 0
val num = ref 0.0
val sum = ref 0.0
val mean = ref 0.0
val average_deviation = ref 0.0
val standard_deviation = ref 0.0
val variance = ref 0.0
val skew = ref 0.0
val kurtosis = ref 0.0
val deviation = ref 0.0
val size = ref 4096
val nums_in = ref (Array.array (!size, 0.0))

fun loop () =
   case TextIO.inputLine TextIO.stdIn of
      "" => ()
    | l =>
	 case Real.fromString l of
	    NONE => raise Fail "invalid input"
	  | SOME x => 
	       (num := x;
		update (!nums_in, !n, !num);
		sum := !sum + !num;
		incr n;
		if !n = !size
		   then
		      let
			 val newSize = 2 * !size
			 val _ = size := newSize
			 val a = Array.array (newSize, 0.0)
			 val _ = Array.copy {src = !nums_in, si = 0,
					     len = NONE,
					     dst = a, di = 0}
			 val _ = nums_in := a
		      in ()
		      end
		else ();
		loop())

val nums = Array.array (!n, 0.0)
val _ = Array.copy {src = !nums_in, si = 0, len = SOME (!n),
		    dst = nums, di = 0}

val n_float = real (!n)
val _ = mean := !sum / n_float

val _ =
   for (0, !n - 1, fn i =>
	let
	   val _ = deviation := sub (nums, i) - !mean;
	   val _ = average_deviation := !average_deviation + abs (!deviation);
	   val dev2 = !deviation * !deviation
	   val _ = variance := !variance + dev2
	   val dev3 = dev2 * !deviation
	   val _ = skew := !skew + dev3
	   val dev4 = dev3 * !deviation
	   val _ = kurtosis := !kurtosis + dev4
	in ()
	end)


val _ = (average_deviation := !average_deviation / n_float;
	 variance := !variance /  real (!n - 1);
	 standard_deviation := Real.Math.sqrt (!variance))

val _ =
   if !variance > 0.0
      then (skew := !skew / n_float / !variance / !standard_deviation;
	    kurtosis := !kurtosis / n_float / !variance / !variance - 3.0)
   else ()

val _ =  Quicksort.quicksort (nums, Real.<=)

val mid = Int.quot (!n, 2)

val median =
   if Int.rem (!n, 2) = 1
      then sub (nums, mid)
   else (sub (nums, mid) + sub (nums, mid - 1)) / 2.0

fun printl [] = print "\n" | printl(h::t) = ( print h ; printl t )

val _ =
  printl ["n:                  ", Int.toString (!n), "\n",
	  "median:             ", Real.toString median, "\n",
	  "mean:               ", Real.toString (!mean), "\n",
	  "average_deviation:  ", Real.toString (!average_deviation), "\n",
	  "standard_deviation: ", Real.toString (!standard_deviation), "\n",
	  "variance:           ", Real.toString (!variance), "\n",
	  "skew:               ", Real.toString (!skew), "\n",
	  "kurtosis:           ", Real.toString (!kurtosis), "\n"]