-
Notifications
You must be signed in to change notification settings - Fork 1
/
assem.sml
34 lines (31 loc) · 1.42 KB
/
assem.sml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
structure Assem =
struct
datatype instr = OPER of {assem: string,
dst: Temp.temp list,
src: Temp.temp list,
jump: Temp.label list option}
| LABEL of {assem: string, lab: Temp.label}
| MOVE of {assem: string,
dst: Temp.temp,
src: Temp.temp}
fun format saytemp = let
fun speak (assem, dst, src, jump) = let
val saylab = Symbol.name
fun f (#"`":: #"s":: i::rest) =
explode(saytemp(List.nth(src, ord i - ord #"0"))) @ f rest
| f ( #"`":: #"d":: i:: rest) =
explode(saytemp(List.nth(dst, ord i - ord #"0"))) @ f rest
| f ( #"`":: #"j":: i:: rest) =
explode(saylab(List.nth(jump, ord i - ord #"0"))) @ f rest
| f ( #"`":: #"`":: rest) = #"`" :: f rest
| f ( #"`":: _ :: rest) = ErrorMsg.impossible "bad Assem format"
| f (c :: rest) = (c :: f rest)
| f [] = []
in implode(f(explode assem))
end
in fn OPER{assem,dst,src,jump=NONE} => speak(assem, dst, src, nil)
| OPER{assem,dst,src,jump=SOME j} => speak(assem, dst, src, j)
| LABEL{assem,...} => assem
| MOVE{assem,dst,src} => speak(assem, [dst], [src], nil)
end
end