interplane.Get Omega
Read in your language

lua

[Ported from github.com/lua/lua] Cast in 'rl_readline_name'

Brought by drakestapleton

branch master Branches Tags History

lua / testes / code.lua

505 lines · 14774 bytes Raw Blame History
1-- $Id: testes/code.lua $
2-- See Copyright Notice in file lua.h
3
4global <const> *
5
6if T==nil then
7 (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n')
8 return
9end
10print "testing code generation and optimizations"
11
12-- to test constant propagation
13local k0aux <const> = 0
14local k0 <const> = k0aux
15local k1 <const> = 1
16local k3 <const> = 3
17local k6 <const> = k3 + (k3 << k0)
18local kFF0 <const> = 0xFF0
19local k3_78 <const> = 3.78
20local x, k3_78_4 <const> = 10, k3_78 / 4
21assert(x == 10)
22
23local kx <const> = "x"
24
25local kTrue <const> = true
26local kFalse <const> = false
27
28local kNil <const> = nil
29
30-- this code gave an error for the code checker
31do
32 local function f (a)
33 for k,v,w in a do end
34 end
35end
36
37
38-- testing reuse in constant table
39local function checkKlist (func, list)
40 local k = T.listk(func)
41 assert(#k == #list)
42 for i = 1, #k do
43 assert(k[i] == list[i] and math.type(k[i]) == math.type(list[i]))
44 end
45end
46
47local function foo ()
48 local a
49 a = k3;
50 a = 0; a = 0.0; a = -7 + 7
51 a = k3_78/4; a = k3_78_4
52 a = -k3_78/4; a = k3_78/4; a = -3.78/4
53 a = -3.79/4; a = 0.0; a = -0;
54 a = k3; a = 3.0; a = 3; a = 3.0
55end
56
57checkKlist(foo, {3.78/4, -3.78/4, -3.79/4})
58
59
60foo = function (f, a)
61 f(100 * 1000)
62 f(100.0 * 1000)
63 f(-100 * 1000)
64 f(-100 * 1000.0)
65 f(100000)
66 f(100000.0)
67 f(-100000)
68 f(-100000.0)
69 end
70
71checkKlist(foo, {100000, 100000.0, -100000, -100000.0})
72
73
74-- floats x integers
75foo = function (t, a)
76 t[a] = 1; t[a] = 1.0
77 t[a] = 1; t[a] = 1.0
78 t[a] = 2; t[a] = 2.0
79 t[a] = 0; t[a] = 0.0
80 t[a] = 1; t[a] = 1.0
81 t[a] = 2; t[a] = 2.0
82 t[a] = 0; t[a] = 0.0
83end
84
85checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0})
86
87
88-- testing opcodes
89
90-- check that 'f' opcodes match '...'
91local function check (f, ...)
92 local arg = {...}
93 local c = T.listcode(f)
94 for i=1, #arg do
95 local opcode = string.match(c[i], "%u%w+")
96 -- print(arg[i], opcode)
97 assert(arg[i] == opcode)
98 end
99 assert(c[#arg+2] == undef)
100end
101
102
103-- check that 'f' opcodes match '...' and that 'f(p) == r'.
104local function checkR (f, p, r, ...)
105 local r1 = f(p)
106 assert(r == r1 and math.type(r) == math.type(r1))
107 check(f, ...)
108end
109
110
111-- check that 'a' and 'b' has the same opcodes
112local function checkequal (a, b)
113 a = T.listcode(a)
114 b = T.listcode(b)
115 assert(#a == #b)
116 for i = 1, #a do
117 a[i] = string.gsub(a[i], '%b()', '') -- remove line number
118 b[i] = string.gsub(b[i], '%b()', '') -- remove line number
119 assert(a[i] == b[i])
120 end
121end
122
123
124-- some basic instructions
125check(function () -- function does not create upvalues
126 (function () end){f()}
127end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
128 'SETLIST', 'CALL', 'RETURN0')
129
130check(function (x) -- function creates upvalues
131 (function () return x end){f()}
132end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL',
133 'SETLIST', 'CALL', 'RETURN')
134
135
136-- sequence of LOADNILs
137check(function ()
138 local kNil <const> = nil
139 local a,b,c
140 local d; local e;
141 local f,g,h;
142 d = nil; d=nil; b=nil; a=kNil; c=nil;
143end, 'LOADNIL', 'RETURN0')
144
145check(function ()
146 local a,b,c,d = 1,1,1,1
147 d=nil;c=nil;b=nil;a=nil
148end, 'LOADI', 'LOADI', 'LOADI', 'LOADI', 'LOADNIL', 'RETURN0')
149
150do
151 local a,b,c,d = 1,1,1,1
152 d=nil;c=nil;b=nil;a=nil
153 assert(a == nil and b == nil and c == nil and d == nil)
154end
155
156
157-- single return
158check (function (a,b,c) return a end, 'RETURN1')
159
160
161-- infinite loops
162check(function () while kTrue do local a = -1 end end,
163'LOADI', 'JMP', 'RETURN0')
164
165check(function () while 1 do local a = -1 end end,
166'LOADI', 'JMP', 'RETURN0')
167
168check(function () repeat local x = 1 until true end,
169'LOADI', 'RETURN0')
170
171
172-- concat optimization
173check(function (a,b,c,d) return a..b..c..d end,
174 'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1')
175
176-- not
177check(function () return not not nil end, 'LOADFALSE', 'RETURN1')
178check(function () return not not kFalse end, 'LOADFALSE', 'RETURN1')
179check(function () return not not true end, 'LOADTRUE', 'RETURN1')
180check(function () return not not k3 end, 'LOADTRUE', 'RETURN1')
181
182-- direct access to locals
183check(function ()
184 local a,b,c,d
185 a = b*a
186 c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b
187end,
188 'LOADNIL',
189 'MUL', 'MMBIN',
190 'DIV', 'MMBIN', 'ADD', 'MMBIN', 'GETTABLE', 'SUB', 'MMBIN',
191 'GETFIELD', 'POW', 'MMBIN', 'UNM', 'SETTABLE', 'SETFIELD', 'RETURN0')
192
193
194-- direct access to constants
195check(function ()
196 local a,b
197 local c = kNil
198 a[kx] = 3.2
199 a.x = b
200 a[b] = 'x'
201end,
202 'LOADNIL', 'SETFIELD', 'SETFIELD', 'SETTABLE', 'RETURN0')
203
204-- "get/set table" with numeric indices
205check(function (a)
206 local k255 <const> = 255
207 a[1] = a[100]
208 a[k255] = a[256]
209 a[256] = 5
210end,
211 'GETI', 'SETI',
212 'LOADI', 'GETTABLE', 'SETI',
213 'LOADI', 'SETTABLE', 'RETURN0')
214
215check(function ()
216 local a,b
217 a = a - a
218 b = a/a
219 b = 5-4
220end,
221 'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0')
222
223check(function ()
224 local a,b
225 a[kTrue] = false
226end,
227 'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0')
228
229
230-- equalities
231checkR(function (a) if a == 1 then return 2 end end, 1, 2,
232 'EQI', 'JMP', 'LOADI', 'RETURN1')
233
234checkR(function (a) if -4.0 == a then return 2 end end, -4, 2,
235 'EQI', 'JMP', 'LOADI', 'RETURN1')
236
237checkR(function (a) if a == "hi" then return 2 end end, 10, nil,
238 'EQK', 'JMP', 'LOADI', 'RETURN1')
239
240checkR(function (a) if a == 10000 then return 2 end end, 1, nil,
241 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large
242
243checkR(function (a) if -10000 == a then return 2 end end, -10000, 2,
244 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large
245
246-- comparisons
247
248checkR(function (a) if -10 <= a then return 2 end end, -10, 2,
249 'GEI', 'JMP', 'LOADI', 'RETURN1')
250
251checkR(function (a) if 128.0 > a then return 2 end end, 129, nil,
252 'LTI', 'JMP', 'LOADI', 'RETURN1')
253
254checkR(function (a) if -127.0 < a then return 2 end end, -127, nil,
255 'GTI', 'JMP', 'LOADI', 'RETURN1')
256
257checkR(function (a) if 10 < a then return 2 end end, 11, 2,
258 'GTI', 'JMP', 'LOADI', 'RETURN1')
259
260checkR(function (a) if 129 < a then return 2 end end, 130, 2,
261 'LOADI', 'LT', 'JMP', 'LOADI', 'RETURN1')
262
263checkR(function (a) if a >= 23.0 then return 2 end end, 25, 2,
264 'GEI', 'JMP', 'LOADI', 'RETURN1')
265
266checkR(function (a) if a >= 23.1 then return 2 end end, 0, nil,
267 'LOADK', 'LE', 'JMP', 'LOADI', 'RETURN1')
268
269checkR(function (a) if a > 2300.0 then return 2 end end, 0, nil,
270 'LOADF', 'LT', 'JMP', 'LOADI', 'RETURN1')
271
272
273-- constant folding
274local function checkK (func, val)
275 check(func, 'LOADK', 'RETURN1')
276 checkKlist(func, {val})
277 assert(func() == val)
278end
279
280local function checkI (func, val)
281 check(func, 'LOADI', 'RETURN1')
282 checkKlist(func, {})
283 assert(func() == val)
284end
285
286local function checkF (func, val)
287 check(func, 'LOADF', 'RETURN1')
288 checkKlist(func, {})
289 assert(func() == val)
290end
291
292checkF(function () return 0.0 end, 0.0)
293checkI(function () return k0 end, 0)
294checkI(function () return -k0//1 end, 0)
295checkK(function () return 3^-1 end, 1/3)
296checkK(function () return (1 + 1)^(50 + 50) end, 2^100)
297checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0)
298checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0)
299checkI(function () return -k3 % 5 end, 2)
300checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0)
301checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0)
302checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4)
303checkI(function () return ~(~kFF0 | kFF0) end, 0)
304checkI(function () return ~~-1024.0 end, -1024)
305checkI(function () return ((100 << k6) << -4) >> 2 end, 100)
306checkK(function () return ~(-1 >> 1) end, math.mininteger)
307
308-- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535)
309local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding
310local border <const> = 65535
311checkI(function () return border end, sbx)
312checkI(function () return -border end, -sbx)
313checkI(function () return border + 1 end, sbx + 1)
314checkK(function () return border + 2 end, sbx + 2)
315checkK(function () return -(border + 1) end, -(sbx + 1))
316
317local border <const> = 65535.0
318checkF(function () return border end, sbx + 0.0)
319checkF(function () return -border end, -sbx + 0.0)
320checkF(function () return border + 1 end, (sbx + 1.0))
321checkK(function () return border + 2 end, (sbx + 2.0))
322checkK(function () return -(border + 1) end, -(sbx + 1.0))
323
324
325-- immediate operands
326checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1')
327checkR(function (x) return x - 127 end, 10, -117, 'ADDI', 'MMBINI', 'RETURN1')
328checkR(function (x) return 128 + x end, 0.0, 128.0,
329 'ADDI', 'MMBINI', 'RETURN1')
330checkR(function (x) return x * -127 end, -1.0, 127.0,
331 'MULK', 'MMBINK', 'RETURN1')
332checkR(function (x) return 20 * x end, 2, 40, 'MULK', 'MMBINK', 'RETURN1')
333checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWK', 'MMBINK', 'RETURN1')
334checkR(function (x) return x / 40 end, 40, 1.0, 'DIVK', 'MMBINK', 'RETURN1')
335checkR(function (x) return x // 1 end, 10.0, 10.0,
336 'IDIVK', 'MMBINK', 'RETURN1')
337checkR(function (x) return x % (100 - 10) end, 91, 1,
338 'MODK', 'MMBINK', 'RETURN1')
339checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1')
340checkR(function (x) return x << 127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
341checkR(function (x) return x << -127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1')
342checkR(function (x) return x >> 128 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
343checkR(function (x) return x >> -127 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1')
344checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1')
345checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1')
346checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1')
347
348-- K operands in arithmetic operations
349checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1')
350-- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1')
351checkR(function (x) return x * -10000 end, 2, -20000,
352 'MULK', 'MMBINK', 'RETURN1')
353-- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1')
354checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1')
355checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1')
356checkR(function (x) return x // 10000 end, 10000, 1,
357 'IDIVK', 'MMBINK', 'RETURN1')
358checkR(function (x) return x % (100.0 - 10) end, 91, 1.0,
359 'MODK', 'MMBINK', 'RETURN1')
360
361-- no foldings (and immediate operands)
362check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1')
363check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1')
364check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1')
365check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1')
366check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1')
367check(function (x) return x << 128 end, 'LOADI', 'SHL', 'MMBIN', 'RETURN1')
368check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1')
369
370-- basic 'for' loops
371check(function () for i = -10, 10.5 do end end,
372'LOADI', 'LOADK', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0')
373check(function () for i = 0xfffffff, 10.0, 1 do end end,
374'LOADK', 'LOADF', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0')
375
376-- bug in constant folding for 5.1
377check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN1')
378
379
380check(function ()
381 local a,b,c
382 b[c], a = c, b
383 b[a], a = c, b
384 a, b = c, a
385 a = a
386end,
387 'LOADNIL',
388 'MOVE', 'MOVE', 'SETTABLE',
389 'MOVE', 'MOVE', 'MOVE', 'SETTABLE',
390 'MOVE', 'MOVE', 'MOVE',
391 -- no code for a = a
392 'RETURN0')
393
394
395-- x == nil , x ~= nil
396-- checkequal(function (b) if (a==nil) then a=1 end; if a~=nil then a=1 end end,
397-- function () if (a==9) then a=1 end; if a~=9 then a=1 end end)
398
399-- check(function () if a==nil then a='a' end end,
400-- 'GETTABUP', 'EQ', 'JMP', 'SETTABUP', 'RETURN')
401
402do -- tests for table access in upvalues
403 local t
404 check(function () t[kx] = t.y end, 'GETTABUP', 'SETTABUP')
405 check(function (a) t[a()] = t[a()] end,
406 'MOVE', 'CALL', 'GETUPVAL', 'MOVE', 'CALL',
407 'GETUPVAL', 'GETTABLE', 'SETTABLE')
408end
409
410-- de morgan
411checkequal(function () local a, b; if not (a or b) then b=a end end,
412 function () local a, b; if (not a and not b) then b=a end end)
413
414checkequal(function (l) local a; return 0 <= a and a <= l end,
415 function (l) local a; return not (not(a >= 0) or not(a <= l)) end)
416
417
418check(function (a, b)
419 while a do
420 if b then break else a = a + 1 end
421 end
422 end,
423'TEST', 'JMP', 'TEST', 'JMP', 'JMP', 'CLOSE', 'JMP', 'ADDI', 'MMBINI', 'JMP', 'RETURN0')
424
425check(function ()
426 do
427 goto exit -- don't need to close
428 local x <close> = nil
429 goto exit -- must close
430 end
431 ::exit::
432 end, 'JMP', 'CLOSE', 'LOADNIL', 'TBC',
433 'CLOSE', 'JMP', 'CLOSE', 'RETURN')
434
435checkequal(function () return 6 or true or nil end,
436 function () return k6 or kTrue or kNil end)
437
438checkequal(function () return 6 and true or nil end,
439 function () return k6 and kTrue or kNil end)
440
441
442do -- string constants
443 local k0 <const> = "00000000000000000000000000000000000000000000000000"
444 local function f1 ()
445 local k <const> = k0
446 return function ()
447 return function () return k end
448 end
449 end
450
451 local f2 = f1()
452 local f3 = f2()
453 assert(f3() == k0)
454 checkK(f3, k0)
455 -- string is not needed by other functions
456 assert(T.listk(f1)[1] == nil)
457 assert(T.listk(f2)[1] == nil)
458end
459
460
461do -- check number of available registers
462 -- 1 register for local + 1 for function + 252 arguments
463 local source = "local a; return a(" .. string.rep("a, ", 252) .. "a)"
464 local prog = T.listcode(assert(load(source)))
465 -- maximum valid register is 254
466 for i = 1, 254 do
467 assert(string.find(prog[2 + i], "MOVE%s*" .. i))
468 end
469 -- one more argument would need register #255 (but that is reserved)
470 source = "local a; return a(" .. string.rep("a, ", 253) .. "a)"
471 local _, msg = load(source)
472 assert(string.find(msg, "too many registers"))
473end
474
475
476do -- basic check for SETLIST
477 -- create a list constructor with 50 elements
478 local source = "local a; return {" .. string.rep("a, ", 50) .. "}"
479 local func = assert(load(source))
480 local code = table.concat(T.listcode(func), "\n")
481 local _, count = string.gsub(code, "SETLIST", "")
482 -- code uses only 1 SETLIST for the constructor
483 assert(count == 1)
484end
485
486
487do print("testing code for integer limits")
488 local function checkints (n)
489 local source = string.format(
490 "local a = {[true] = 0X%x}; return a[true]", n)
491 local f = assert(load(source))
492 checkKlist(f, {n})
493 assert(f() == n)
494 f = load(string.dump(f))
495 assert(f() == n)
496 end
497
498 checkints(math.maxinteger)
499 checkints(math.mininteger)
500 checkints(-1)
501
502end
503
504print 'OK'
505