-
Notifications
You must be signed in to change notification settings - Fork 0
/
assem.sml
55 lines (43 loc) · 1.48 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
structure Assem = struct
type reg = string
type temp = Temp.temp
type label = Temp.label
datatype instr = OPER of {assem: string,
dst: temp list,
src: temp list,
jump: label list option}
| LABEL of {assem: string, lab: Temp.label}
| MOVE of {assem: string,
dst: temp,
src: 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 nil = nil
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
fun getAssem (OPER{assem,...}) = assem
| getAssem (LABEL{assem,...}) = assem
| getAssem( MOVE{assem,...}) = assem
fun isNormalJump instr =
let
val assem = getAssem instr
in
(String.isPrefix "j " assem) orelse (String.isPrefix "jr " assem)
end
end