-
Notifications
You must be signed in to change notification settings - Fork 10
/
test12.sml
131 lines (123 loc) · 2.6 KB
/
test12.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(* This test assumes that variable refs are already implemented.
The new feature in this code is the while loop. You can run
test12.py to see the generated code for a while loop. Be sure
to use python 3.2 when running the disassembler.
*)
let val zero = 0
fun fib n =
let val i = ref zero
val current = ref 0
val next = ref 1
val tmp = ref 0
in
while !i < n do (
tmp := !next + !current;
current := !next;
next := !tmp;
i := !i + 1
);
!current
end
val x = Int.fromString(input("Please enter an integer: "))
val r = fib(x)
in
print "Fib(";
print x;
print ") is ";
println r
end
(* Here is the correctly generated code for this program.
Function: main/0
Function: fib/1
Constants: None, 'Match Not Found', 0, 1
Locals: fib@Param, n@2
FreeVars: zero@0
CellVars: tmp@5, next@4, current@3, i@2
Globals: fprint, input, int, len, type, Exception
BEGIN
LOAD_FAST 0
STORE_FAST 1
LOAD_DEREF 4
STORE_DEREF 3
LOAD_CONST 2
STORE_DEREF 2
LOAD_CONST 3
STORE_DEREF 1
LOAD_CONST 2
STORE_DEREF 0
SETUP_LOOP L3
L1:
LOAD_DEREF 3
LOAD_FAST 1
COMPARE_OP 0
POP_JUMP_IF_FALSE L2
LOAD_DEREF 1
LOAD_DEREF 2
BINARY_ADD
DUP_TOP
STORE_DEREF 0
POP_TOP
LOAD_DEREF 1
DUP_TOP
STORE_DEREF 2
POP_TOP
LOAD_DEREF 0
DUP_TOP
STORE_DEREF 1
POP_TOP
LOAD_DEREF 3
LOAD_CONST 3
BINARY_ADD
DUP_TOP
STORE_DEREF 3
JUMP_ABSOLUTE L1
L2:
POP_BLOCK
L3:
POP_TOP
LOAD_DEREF 2
RETURN_VALUE
L0:
LOAD_GLOBAL 5
LOAD_CONST 1
CALL_FUNCTION 1
RAISE_VARARGS 1
END
Constants: None, 'Match Not Found', 0, code(fib), "Please enter an integer: ", "Fib(", ") is ", "\n"
Locals: r@3, x@2, fib
CellVars: zero@0
Globals: fprint, input, int, len, type, Exception
BEGIN
LOAD_CLOSURE 0
BUILD_TUPLE 1
LOAD_CONST 3
MAKE_CLOSURE 0
STORE_FAST 2
LOAD_CONST 2
STORE_DEREF 0
LOAD_GLOBAL 2
LOAD_GLOBAL 1
LOAD_CONST 4
CALL_FUNCTION 1
CALL_FUNCTION 1
STORE_FAST 1
LOAD_FAST 2
LOAD_FAST 1
CALL_FUNCTION 1
STORE_FAST 0
LOAD_GLOBAL 0
LOAD_CONST 5
CALL_FUNCTION 1
LOAD_FAST 1
CALL_FUNCTION 1
LOAD_CONST 6
CALL_FUNCTION 1
LOAD_FAST 0
CALL_FUNCTION 1
LOAD_CONST 7
CALL_FUNCTION 1
POP_TOP
LOAD_CONST 0
RETURN_VALUE
END
*)