| 1 |
(* |
| 2 |
* This just provide a very simple pretty printing function. |
| 3 |
* It is used for visualization. |
| 4 |
* |
| 5 |
* -- Allen |
| 6 |
* |
| 7 |
*) |
| 8 |
|
| 9 |
signature FORMAT_INSTRUCTION = |
| 10 |
sig |
| 11 |
structure I : INSTRUCTIONS |
| 12 |
|
| 13 |
val toString : Annotations.annotations -> I.instruction -> string |
| 14 |
|
| 15 |
end |
| 16 |
|
| 17 |
functor FormatInstruction(Asm : INSTRUCTION_EMITTER) : FORMAT_INSTRUCTION = |
| 18 |
struct |
| 19 |
structure I = Asm.I |
| 20 |
|
| 21 |
fun toString an insn = |
| 22 |
let val buffer = StringOutStream.mkStreamBuf() |
| 23 |
val S = StringOutStream.openStringOut buffer |
| 24 |
val () = AsmStream.withStream S |
| 25 |
(fn insn => |
| 26 |
let val Asm.S.STREAM{emit,...} = Asm.makeStream an |
| 27 |
in emit insn |
| 28 |
end) insn |
| 29 |
val text = StringOutStream.getString buffer |
| 30 |
fun isSpace #" " = true |
| 31 |
| isSpace #"\t" = true |
| 32 |
| isSpace _ = false |
| 33 |
val text = foldr (fn (x,"") => x | (x,y) => x^" "^y) "" |
| 34 |
(String.tokens isSpace text) |
| 35 |
fun stripNL "" = "" |
| 36 |
| stripNL s = |
| 37 |
let fun f(0) = "" |
| 38 |
| f(i) = |
| 39 |
case String.sub(s,i) of |
| 40 |
#"\n" => f(i-1) |
| 41 |
| #" " => f(i-1) |
| 42 |
| _ => String.extract(s,0,SOME(i+1)) |
| 43 |
in f(size s - 1) end |
| 44 |
in stripNL text end |
| 45 |
|
| 46 |
end |
| 47 |
|