Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/lua/blob/master/testes/code.lua # lua [Ported from github.com/lua/lua] Cast in 'rl_readline_name' Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers 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 4 global * 5 6 if T==nil then 7 (Message or print)('\n >>> testC not active: skipping opcode tests <<<\n') 8 return 9 end 10 print "testing code generation and optimizations" 11 12 -- to test constant propagation 13 local k0aux = 0 14 local k0 = k0aux 15 local k1 = 1 16 local k3 = 3 17 local k6 = k3 + (k3 << k0) 18 local kFF0 = 0xFF0 19 local k3_78 = 3.78 20 local x, k3_78_4 = 10, k3_78 / 4 21 assert(x == 10) 22 23 local kx = "x" 24 25 local kTrue = true 26 local kFalse = false 27 28 local kNil = nil 29 30 -- this code gave an error for the code checker 31 do 32 local function f (a) 33 for k,v,w in a do end 34 end 35 end 36 37 38 -- testing reuse in constant table 39 local 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 45 end 46 47 local 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 55 end 56 57 checkKlist(foo, {3.78/4, -3.78/4, -3.79/4}) 58 59 60 foo = 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 71 checkKlist(foo, {100000, 100000.0, -100000, -100000.0}) 72 73 74 -- floats x integers 75 foo = 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 83 end 84 85 checkKlist(foo, {1, 1.0, 2, 2.0, 0, 0.0}) 86 87 88 -- testing opcodes 89 90 -- check that 'f' opcodes match '...' 91 local 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) 100 end 101 102 103 -- check that 'f' opcodes match '...' and that 'f(p) == r'. 104 local function checkR (f, p, r, ...) 105 local r1 = f(p) 106 assert(r == r1 and math.type(r) == math.type(r1)) 107 check(f, ...) 108 end 109 110 111 -- check that 'a' and 'b' has the same opcodes 112 local 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 121 end 122 123 124 -- some basic instructions 125 check(function () -- function does not create upvalues 126 (function () end){f()} 127 end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', 128 'SETLIST', 'CALL', 'RETURN0') 129 130 check(function (x) -- function creates upvalues 131 (function () return x end){f()} 132 end, 'CLOSURE', 'NEWTABLE', 'EXTRAARG', 'GETTABUP', 'CALL', 133 'SETLIST', 'CALL', 'RETURN') 134 135 136 -- sequence of LOADNILs 137 check(function () 138 local kNil = 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; 143 end, 'LOADNIL', 'RETURN0') 144 145 check(function () 146 local a,b,c,d = 1,1,1,1 147 d=nil;c=nil;b=nil;a=nil 148 end, 'LOADI', 'LOADI', 'LOADI', 'LOADI', 'LOADNIL', 'RETURN0') 149 150 do 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) 154 end 155 156 157 -- single return 158 check (function (a,b,c) return a end, 'RETURN1') 159 160 161 -- infinite loops 162 check(function () while kTrue do local a = -1 end end, 163 'LOADI', 'JMP', 'RETURN0') 164 165 check(function () while 1 do local a = -1 end end, 166 'LOADI', 'JMP', 'RETURN0') 167 168 check(function () repeat local x = 1 until true end, 169 'LOADI', 'RETURN0') 170 171 172 -- concat optimization 173 check(function (a,b,c,d) return a..b..c..d end, 174 'MOVE', 'MOVE', 'MOVE', 'MOVE', 'CONCAT', 'RETURN1') 175 176 -- not 177 check(function () return not not nil end, 'LOADFALSE', 'RETURN1') 178 check(function () return not not kFalse end, 'LOADFALSE', 'RETURN1') 179 check(function () return not not true end, 'LOADTRUE', 'RETURN1') 180 check(function () return not not k3 end, 'LOADTRUE', 'RETURN1') 181 182 -- direct access to locals 183 check(function () 184 local a,b,c,d 185 a = b*a 186 c.x, a[b] = -((a + d/b - a[b]) ^ a.x), b 187 end, 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 195 check(function () 196 local a,b 197 local c = kNil 198 a[kx] = 3.2 199 a.x = b 200 a[b] = 'x' 201 end, 202 'LOADNIL', 'SETFIELD', 'SETFIELD', 'SETTABLE', 'RETURN0') 203 204 -- "get/set table" with numeric indices 205 check(function (a) 206 local k255 = 255 207 a[1] = a[100] 208 a[k255] = a[256] 209 a[256] = 5 210 end, 211 'GETI', 'SETI', 212 'LOADI', 'GETTABLE', 'SETI', 213 'LOADI', 'SETTABLE', 'RETURN0') 214 215 check(function () 216 local a,b 217 a = a - a 218 b = a/a 219 b = 5-4 220 end, 221 'LOADNIL', 'SUB', 'MMBIN', 'DIV', 'MMBIN', 'LOADI', 'RETURN0') 222 223 check(function () 224 local a,b 225 a[kTrue] = false 226 end, 227 'LOADNIL', 'LOADTRUE', 'SETTABLE', 'RETURN0') 228 229 230 -- equalities 231 checkR(function (a) if a == 1 then return 2 end end, 1, 2, 232 'EQI', 'JMP', 'LOADI', 'RETURN1') 233 234 checkR(function (a) if -4.0 == a then return 2 end end, -4, 2, 235 'EQI', 'JMP', 'LOADI', 'RETURN1') 236 237 checkR(function (a) if a == "hi" then return 2 end end, 10, nil, 238 'EQK', 'JMP', 'LOADI', 'RETURN1') 239 240 checkR(function (a) if a == 10000 then return 2 end end, 1, nil, 241 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large 242 243 checkR(function (a) if -10000 == a then return 2 end end, -10000, 2, 244 'EQK', 'JMP', 'LOADI', 'RETURN1') -- number too large 245 246 -- comparisons 247 248 checkR(function (a) if -10 <= a then return 2 end end, -10, 2, 249 'GEI', 'JMP', 'LOADI', 'RETURN1') 250 251 checkR(function (a) if 128.0 > a then return 2 end end, 129, nil, 252 'LTI', 'JMP', 'LOADI', 'RETURN1') 253 254 checkR(function (a) if -127.0 < a then return 2 end end, -127, nil, 255 'GTI', 'JMP', 'LOADI', 'RETURN1') 256 257 checkR(function (a) if 10 < a then return 2 end end, 11, 2, 258 'GTI', 'JMP', 'LOADI', 'RETURN1') 259 260 checkR(function (a) if 129 < a then return 2 end end, 130, 2, 261 'LOADI', 'LT', 'JMP', 'LOADI', 'RETURN1') 262 263 checkR(function (a) if a >= 23.0 then return 2 end end, 25, 2, 264 'GEI', 'JMP', 'LOADI', 'RETURN1') 265 266 checkR(function (a) if a >= 23.1 then return 2 end end, 0, nil, 267 'LOADK', 'LE', 'JMP', 'LOADI', 'RETURN1') 268 269 checkR(function (a) if a > 2300.0 then return 2 end end, 0, nil, 270 'LOADF', 'LT', 'JMP', 'LOADI', 'RETURN1') 271 272 273 -- constant folding 274 local function checkK (func, val) 275 check(func, 'LOADK', 'RETURN1') 276 checkKlist(func, {val}) 277 assert(func() == val) 278 end 279 280 local function checkI (func, val) 281 check(func, 'LOADI', 'RETURN1') 282 checkKlist(func, {}) 283 assert(func() == val) 284 end 285 286 local function checkF (func, val) 287 check(func, 'LOADF', 'RETURN1') 288 checkKlist(func, {}) 289 assert(func() == val) 290 end 291 292 checkF(function () return 0.0 end, 0.0) 293 checkI(function () return k0 end, 0) 294 checkI(function () return -k0//1 end, 0) 295 checkK(function () return 3^-1 end, 1/3) 296 checkK(function () return (1 + 1)^(50 + 50) end, 2^100) 297 checkK(function () return (-2)^(31 - 2) end, -0x20000000 + 0.0) 298 checkF(function () return (-k3^0 + 5) // 3.0 end, 1.0) 299 checkI(function () return -k3 % 5 end, 2) 300 checkF(function () return -((2.0^8 + -(-1)) % 8)/2 * 4 - 3 end, -5.0) 301 checkF(function () return -((2^8 + -(-1)) % 8)//2 * 4 - 3 end, -7.0) 302 checkI(function () return 0xF0.0 | 0xCC.0 ~ 0xAA & 0xFD end, 0xF4) 303 checkI(function () return ~(~kFF0 | kFF0) end, 0) 304 checkI(function () return ~~-1024.0 end, -1024) 305 checkI(function () return ((100 << k6) << -4) >> 2 end, 100) 306 checkK(function () return ~(-1 >> 1) end, math.mininteger) 307 308 -- borders around MAXARG_sBx ((((1 << 17) - 1) >> 1) == 65535) 309 local a = 17; local sbx = ((1 << a) - 1) >> 1 -- avoid folding 310 local border = 65535 311 checkI(function () return border end, sbx) 312 checkI(function () return -border end, -sbx) 313 checkI(function () return border + 1 end, sbx + 1) 314 checkK(function () return border + 2 end, sbx + 2) 315 checkK(function () return -(border + 1) end, -(sbx + 1)) 316 317 local border = 65535.0 318 checkF(function () return border end, sbx + 0.0) 319 checkF(function () return -border end, -sbx + 0.0) 320 checkF(function () return border + 1 end, (sbx + 1.0)) 321 checkK(function () return border + 2 end, (sbx + 2.0)) 322 checkK(function () return -(border + 1) end, -(sbx + 1.0)) 323 324 325 -- immediate operands 326 checkR(function (x) return x + k1 end, 10, 11, 'ADDI', 'MMBINI', 'RETURN1') 327 checkR(function (x) return x - 127 end, 10, -117, 'ADDI', 'MMBINI', 'RETURN1') 328 checkR(function (x) return 128 + x end, 0.0, 128.0, 329 'ADDI', 'MMBINI', 'RETURN1') 330 checkR(function (x) return x * -127 end, -1.0, 127.0, 331 'MULK', 'MMBINK', 'RETURN1') 332 checkR(function (x) return 20 * x end, 2, 40, 'MULK', 'MMBINK', 'RETURN1') 333 checkR(function (x) return x ^ -2 end, 2, 0.25, 'POWK', 'MMBINK', 'RETURN1') 334 checkR(function (x) return x / 40 end, 40, 1.0, 'DIVK', 'MMBINK', 'RETURN1') 335 checkR(function (x) return x // 1 end, 10.0, 10.0, 336 'IDIVK', 'MMBINK', 'RETURN1') 337 checkR(function (x) return x % (100 - 10) end, 91, 1, 338 'MODK', 'MMBINK', 'RETURN1') 339 checkR(function (x) return k1 << x end, 3, 8, 'SHLI', 'MMBINI', 'RETURN1') 340 checkR(function (x) return x << 127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1') 341 checkR(function (x) return x << -127 end, 10, 0, 'SHRI', 'MMBINI', 'RETURN1') 342 checkR(function (x) return x >> 128 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1') 343 checkR(function (x) return x >> -127 end, 8, 0, 'SHRI', 'MMBINI', 'RETURN1') 344 checkR(function (x) return x & 1 end, 9, 1, 'BANDK', 'MMBINK', 'RETURN1') 345 checkR(function (x) return 10 | x end, 1, 11, 'BORK', 'MMBINK', 'RETURN1') 346 checkR(function (x) return -10 ~ x end, -1, 9, 'BXORK', 'MMBINK', 'RETURN1') 347 348 -- K operands in arithmetic operations 349 checkR(function (x) return x + 0.0 end, 1, 1.0, 'ADDK', 'MMBINK', 'RETURN1') 350 -- check(function (x) return 128 + x end, 'ADDK', 'MMBINK', 'RETURN1') 351 checkR(function (x) return x * -10000 end, 2, -20000, 352 'MULK', 'MMBINK', 'RETURN1') 353 -- check(function (x) return 20 * x end, 'MULK', 'MMBINK', 'RETURN1') 354 checkR(function (x) return x ^ 0.5 end, 4, 2.0, 'POWK', 'MMBINK', 'RETURN1') 355 checkR(function (x) return x / 2.0 end, 4, 2.0, 'DIVK', 'MMBINK', 'RETURN1') 356 checkR(function (x) return x // 10000 end, 10000, 1, 357 'IDIVK', 'MMBINK', 'RETURN1') 358 checkR(function (x) return x % (100.0 - 10) end, 91, 1.0, 359 'MODK', 'MMBINK', 'RETURN1') 360 361 -- no foldings (and immediate operands) 362 check(function () return -0.0 end, 'LOADF', 'UNM', 'RETURN1') 363 check(function () return k3/0 end, 'LOADI', 'DIVK', 'MMBINK', 'RETURN1') 364 check(function () return 0%0 end, 'LOADI', 'MODK', 'MMBINK', 'RETURN1') 365 check(function () return -4//0 end, 'LOADI', 'IDIVK', 'MMBINK', 'RETURN1') 366 check(function (x) return x >> 2.0 end, 'LOADF', 'SHR', 'MMBIN', 'RETURN1') 367 check(function (x) return x << 128 end, 'LOADI', 'SHL', 'MMBIN', 'RETURN1') 368 check(function (x) return x & 2.0 end, 'LOADF', 'BAND', 'MMBIN', 'RETURN1') 369 370 -- basic 'for' loops 371 check(function () for i = -10, 10.5 do end end, 372 'LOADI', 'LOADK', 'LOADI', 'FORPREP', 'FORLOOP', 'RETURN0') 373 check(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 377 check(function () return -nil end, 'LOADNIL', 'UNM', 'RETURN1') 378 379 380 check(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 386 end, 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 402 do -- 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') 408 end 409 410 -- de morgan 411 checkequal(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 414 checkequal(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 418 check(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 425 check(function () 426 do 427 goto exit -- don't need to close 428 local x = nil 429 goto exit -- must close 430 end 431 ::exit:: 432 end, 'JMP', 'CLOSE', 'LOADNIL', 'TBC', 433 'CLOSE', 'JMP', 'CLOSE', 'RETURN') 434 435 checkequal(function () return 6 or true or nil end, 436 function () return k6 or kTrue or kNil end) 437 438 checkequal(function () return 6 and true or nil end, 439 function () return k6 and kTrue or kNil end) 440 441 442 do -- string constants 443 local k0 = "`00000000000000000000000000000000000000000000000000`" 444 local function f1 () 445 local k = 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) 458 end 459 460 461 do -- 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")) 473 end 474 475 476 do -- 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) 484 end 485 486 487 do 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 502 end 503 504 print 'OK' 505