Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Jan 18, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode.lua
More file actions
Latest commit
182 lines (141 loc) · 4.09 KB
/
Copy pathcode.lua
File metadata and controls
182 lines (141 loc) · 4.09 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
ifT==nilthen
(Messageorprint)('\a\n >>> testC not active: skipping opcode tests <<<\n\a')
return
end
print"testing code generation and optimizations"
-- this code gave an error for the code checker
do
localfunctionf (a)
fork,v,winadoend
end
end
functioncheck (f, ...)
localarg= {...}
localc=T.listcode(f)
fori=1, #argdo
-- print(arg[i], c[i])
assert(string.find(c[i], '- '..arg[i]..' *%d'))
end
assert(c[#arg+2] ==nil)
end
functioncheckequal (a, b)
a=T.listcode(a)
b=T.listcode(b)
fori=1, #ado
a[i] =string.gsub(a[i], '%b()', '') -- remove line number
b[i] =string.gsub(b[i], '%b()', '') -- remove line number
assert(a[i] ==b[i])
end
end
-- some basic instructions
check(function ()
(function () end){f()}
end, 'CLOSURE', 'NEWTABLE', 'GETTABUP', 'CALL', 'SETLIST', 'CALL', 'RETURN')
-- sequence of LOADNILs
check(function ()
locala,b,c
locald; locale;
localf,g,h;
d=nil; d=nil; b=nil; a=nil; c=nil;
end, 'LOADNIL', 'RETURN')
check(function ()
locala,b,c,d=1,1,1,1
d=nil;c=nil;b=nil;a=nil
end, 'LOADK', 'LOADK', 'LOADK', 'LOADK', 'LOADNIL', 'RETURN')
do
locala,b,c,d=1,1,1,1
d=nil;c=nil;b=nil;a=nil
assert(a==nilandb==nilandc==nilandd==nil)
end
-- single return
check (function (a,b,c) returnaend, 'RETURN')
-- infinite loops
check(function () whiletruedolocala=-1endend,
'LOADK', 'JMP', 'RETURN')
check(function () while1dolocala=-1endend,
'LOADK', 'JMP', 'RETURN')
check(function () repeatlocalx=1untiltrueend,
'LOADK', 'RETURN')
-- concat optimization
check(function (a,b,c,d) returna..b..c..dend,
'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN')
-- not
check(function () returnnotnotnilend, 'LOADBOOL', 'RETURN')
check(function () returnnotnotfalseend, 'LOADBOOL', 'RETURN')
check(function () returnnotnottrueend, 'LOADBOOL', 'RETURN')
check(function () returnnotnot1end, 'LOADBOOL', 'RETURN')
-- direct access to locals
check(function ()
locala,b,c,d
a=b*2
c[4], a[b] =-((a+d/-20.5-a[b]) ^a.x), b
end,
'LOADNIL',
'MUL',
'DIV', 'ADD', 'GETTABLE', 'SUB', 'GETTABLE', 'POW',
'UNM', 'SETTABLE', 'SETTABLE', 'RETURN')
-- direct access to constants
check(function ()
locala,b
a.x=0
a.x=b
a[b] ='y'
a=1-a
b=1/a
b=5+4
a[true] =false
end,
'LOADNIL',
'SETTABLE', 'SETTABLE', 'SETTABLE', 'SUB', 'DIV', 'LOADK',
'SETTABLE', 'RETURN')
-- constant folding
localfunctionf () return-((2^8+-(-1)) %8)/2*4-3end
check(f, 'LOADK', 'RETURN')
assert(f() ==-5)
-- bug in constant folding for 5.1
check(function () return-nilend,
'LOADNIL', 'UNM', 'RETURN')
check(function ()
locala,b,c
b[c], a=c, b
b[a], a=c, b
a, b=c, a
a=a
end,
'LOADNIL',
'MOVE', 'MOVE', 'SETTABLE',
'MOVE', 'MOVE', 'MOVE', 'SETTABLE',
'MOVE', 'MOVE', 'MOVE',
-- no code for a = a
'RETURN')
-- x == nil , x ~= nil
checkequal(function () if (a==nil) thena=1end; ifa~=nilthena=1endend,
function () if (a==9) thena=1end; ifa~=9thena=1endend)
check(function () ifa==nilthena=1endend,
'GETTABUP', 'EQ', 'JMP', 'SETTABUP', 'RETURN')
-- de morgan
checkequal(function () locala; ifnot (aorb) thenb=aendend,
function () locala; if (notaandnotb) thenb=aendend)
checkequal(function (l) locala; return0<=aanda<=lend,
function (l) locala; returnnot (not(a>=0) ornot(a<=l)) end)
-- if-goto optimizations
check(function (a)
ifa==1thengotol1
elseifa==2thengotol2
elseifa==3thengotol2
elseifa==4thengotol3
elsegotol3
end
end
::l1::::l2::::l3::::l4::
end, 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'EQ', 'JMP', 'JMP', 'RETURN')
checkequal(
function (a) whilea<10doa=a+1endend,
function (a) ::L2::ifnot(a<10) thengotoL1end; a=a+1;
gotoL2; ::L1::end
)
checkequal(
function (a) whilea<10doa=a+1endend,
function (a) whiletruedoifnot(a<10) thenbreakend; a=a+1; endend
)
print'OK'